szetszwo commented on code in PR #733:
URL: https://github.com/apache/ratis/pull/733#discussion_r962703943
##########
ratis-common/src/main/java/org/apache/ratis/util/TimeoutScheduler.java:
##########
@@ -84,7 +84,10 @@ ScheduledFuture<?> schedule(Runnable task, Supplier<String>
name, TimeDuration t
private static ScheduledThreadPoolExecutor newExecutor() {
LOG.debug("new ScheduledThreadPoolExecutor");
- final ScheduledThreadPoolExecutor e = new ScheduledThreadPoolExecutor(1,
(ThreadFactory) Daemon::new);
+ AtomicInteger count = new AtomicInteger(0);
Review Comment:
It should be private static final:
```java
private static final AtomicInteger COUNT = new AtomicInteger();
```
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -97,14 +98,31 @@
class RaftServerImpl implements RaftServer.Division,
RaftServerProtocol, RaftServerAsynchronousProtocol,
- RaftClientProtocol, RaftClientAsynchronousProtocol{
+ RaftClientProtocol, RaftClientAsynchronousProtocol, ErrorRecorded {
private static final String CLASS_NAME =
JavaUtils.getClassSimpleName(RaftServerImpl.class);
static final String REQUEST_VOTE = CLASS_NAME + ".requestVote";
static final String APPEND_ENTRIES = CLASS_NAME + ".appendEntries";
static final String INSTALL_SNAPSHOT = CLASS_NAME + ".installSnapshot";
static final String LOG_SYNC = APPEND_ENTRIES + ".logComplete";
static final String START_LEADER_ELECTION = CLASS_NAME +
".startLeaderElection";
+ private Throwable throwable;
+
+ @Override
+ public void setError(Throwable t) {
+ throwable = t;
+ LOG.error("Server transitioning to EXCEPTION state due to", t);
+ // TODO(jiacheng): will the server keep serving or just die itself?
Review Comment:
We should let the application decide. Therefore, we should pass an
`UncaughtExceptionHandler` from the application.
##########
ratis-common/src/main/java/org/apache/ratis/util/ErrorRecorded.java:
##########
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.util;
+
+import javax.annotation.Nullable;
+
+/**
+ * An internal error will be recorded so it can be later retrieved for report
or recovery.
+ */
+public interface ErrorRecorded {
Review Comment:
Pass a `UncaughtExceptionHandler` instead of adding this interface.
##########
ratis-common/src/main/java/org/apache/ratis/util/Daemon.java:
##########
@@ -17,24 +17,76 @@
*/
package org.apache.ratis.util;
+import javax.annotation.Nullable;
+import java.util.concurrent.atomic.AtomicReference;
+
public class Daemon extends Thread {
{
setDaemon(true);
}
- /** Construct a daemon thread. */
+ /** If the thread meets an uncaught exception, this field will be set. */
+ private final AtomicReference<Throwable> throwable = new
AtomicReference<>(null);
+ private ErrorRecorded statedServer;
+
+ /** Construct a daemon thread with no arguments, left only for extension. */
public Daemon() {
super();
+ setUncaughtExceptionHandler((thread, t) -> {
+ onError(t);
+ });
+ }
+
+ /** Construct a daemon thread with flexible arguments. */
+ public Daemon(Builder builder) {
+ super(builder.runnable);
+ setName(builder.name);
+ this.statedServer = builder.statedServer;
+ setUncaughtExceptionHandler((thread, t) -> {
+ onError(t);
+ });
+ }
+
+ /**
+ * This will be invoked on uncaught exceptions.
+ * Necessary bookkeeping or graceful exit logics should be put here.
+ *
+ * @param t the crashing error
+ */
+ public void onError(Throwable t) {
+ throwable.set(t);
+ if (statedServer != null) {
+ // Rely on the server to log
+ statedServer.setError(t);
+ }
}
- /** Construct a daemon thread with the given runnable. */
- public Daemon(Runnable runnable) {
- this(runnable, runnable.toString());
+ @Nullable
+ public Throwable getError() {
+ return throwable.get();
}
- /** Construct a daemon thread with the given runnable. */
- public Daemon(Runnable runnable, String name) {
- super(runnable);
- this.setName(name);
+ public static class Builder {
+ private final String name;
+ private Runnable runnable;
+ private ErrorRecorded statedServer;
Review Comment:
Let's pass an `UncaughtExceptionHandler` instead of adding ErrorRecorded.
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java:
##########
@@ -407,6 +426,7 @@ public void close() {
try {
getServerRpc().close();
} catch(IOException ignored) {
+ // TODO(jiacheng): transition state to EXCEPTION here?
Review Comment:
When it is in CLOSING state, it must transit to CLOSED but won't transit to
EXCEPTION. CLOSING must handle the exceptions, if there are any.
##########
ratis-common/src/main/java/org/apache/ratis/util/Daemon.java:
##########
@@ -17,24 +17,76 @@
*/
package org.apache.ratis.util;
+import javax.annotation.Nullable;
+import java.util.concurrent.atomic.AtomicReference;
+
public class Daemon extends Thread {
{
setDaemon(true);
}
- /** Construct a daemon thread. */
+ /** If the thread meets an uncaught exception, this field will be set. */
+ private final AtomicReference<Throwable> throwable = new
AtomicReference<>(null);
+ private ErrorRecorded statedServer;
+
+ /** Construct a daemon thread with no arguments, left only for extension. */
public Daemon() {
super();
+ setUncaughtExceptionHandler((thread, t) -> {
+ onError(t);
+ });
+ }
+
+ /** Construct a daemon thread with flexible arguments. */
+ public Daemon(Builder builder) {
+ super(builder.runnable);
+ setName(builder.name);
+ this.statedServer = builder.statedServer;
+ setUncaughtExceptionHandler((thread, t) -> {
+ onError(t);
+ });
+ }
+
+ /**
+ * This will be invoked on uncaught exceptions.
+ * Necessary bookkeeping or graceful exit logics should be put here.
+ *
+ * @param t the crashing error
+ */
+ public void onError(Throwable t) {
+ throwable.set(t);
+ if (statedServer != null) {
+ // Rely on the server to log
+ statedServer.setError(t);
+ }
}
- /** Construct a daemon thread with the given runnable. */
- public Daemon(Runnable runnable) {
- this(runnable, runnable.toString());
+ @Nullable
+ public Throwable getError() {
+ return throwable.get();
}
- /** Construct a daemon thread with the given runnable. */
- public Daemon(Runnable runnable, String name) {
- super(runnable);
- this.setName(name);
+ public static class Builder {
+ private final String name;
+ private Runnable runnable;
+ private ErrorRecorded statedServer;
+
+ public Builder(String name) {
Review Comment:
Instead of adding a constructor, please add
```java
/** @return a {@link Builder}. */
static Builder newBuilder() {
return new Builder();
}
```
##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -97,14 +98,31 @@
class RaftServerImpl implements RaftServer.Division,
RaftServerProtocol, RaftServerAsynchronousProtocol,
- RaftClientProtocol, RaftClientAsynchronousProtocol{
+ RaftClientProtocol, RaftClientAsynchronousProtocol, ErrorRecorded {
private static final String CLASS_NAME =
JavaUtils.getClassSimpleName(RaftServerImpl.class);
static final String REQUEST_VOTE = CLASS_NAME + ".requestVote";
static final String APPEND_ENTRIES = CLASS_NAME + ".appendEntries";
static final String INSTALL_SNAPSHOT = CLASS_NAME + ".installSnapshot";
static final String LOG_SYNC = APPEND_ENTRIES + ".logComplete";
static final String START_LEADER_ELECTION = CLASS_NAME +
".startLeaderElection";
+ private Throwable throwable;
+
+ @Override
+ public void setError(Throwable t) {
+ throwable = t;
+ LOG.error("Server transitioning to EXCEPTION state due to", t);
+ // TODO(jiacheng): will the server keep serving or just die itself?
+ // What is the cleanup I should consider?
+ lifeCycle.transition(LifeCycle.State.EXCEPTION);
Review Comment:
Pass an `UncaughtExceptionHandler`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]