szetszwo commented on code in PR #733:
URL: https://github.com/apache/ratis/pull/733#discussion_r969358600


##########
ratis-common/src/main/java/org/apache/ratis/util/Daemon.java:
##########
@@ -17,24 +17,59 @@
  */
 package org.apache.ratis.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
 public class Daemon extends Thread {
+  static final Logger LOG = LoggerFactory.getLogger(Daemon.class);
+  public static final Thread.UncaughtExceptionHandler LOG_EXCEPTION =
+      (t, e) -> LOG.error(t.getName() + " threw an uncaught exception", e);
+
   {
     setDaemon(true);
   }
 
-  /** Construct a daemon thread. */
-  public Daemon() {
-    super();
+  /** Construct a daemon thread with flexible arguments. */
+  protected Daemon(Builder builder) {
+    super(builder.runnable);
+    setName(builder.name);
+    if (builder.uncaughtExceptionHandler != null) {
+      setUncaughtExceptionHandler(builder.uncaughtExceptionHandler);
+    }
   }
 
-  /** Construct a daemon thread with the given runnable. */
-  public Daemon(Runnable runnable) {
-    this(runnable, runnable.toString());
+  /** @return a {@link Builder}. */
+  // TODO(jiacheng): For exceptions that should crash the server, apply a 
correct UncaughtExceptionHandler

Review Comment:
   What does this TODO mean?



##########
ratis-common/src/main/java/org/apache/ratis/util/Daemon.java:
##########
@@ -17,24 +17,59 @@
  */
 package org.apache.ratis.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
 public class Daemon extends Thread {
+  static final Logger LOG = LoggerFactory.getLogger(Daemon.class);
+  public static final Thread.UncaughtExceptionHandler LOG_EXCEPTION =
+      (t, e) -> LOG.error(t.getName() + " threw an uncaught exception", e);
+
   {
     setDaemon(true);
   }
 
-  /** Construct a daemon thread. */
-  public Daemon() {
-    super();
+  /** Construct a daemon thread with flexible arguments. */
+  protected Daemon(Builder builder) {
+    super(builder.runnable);
+    setName(builder.name);
+    if (builder.uncaughtExceptionHandler != null) {
+      setUncaughtExceptionHandler(builder.uncaughtExceptionHandler);
+    }
   }
 
-  /** Construct a daemon thread with the given runnable. */
-  public Daemon(Runnable runnable) {
-    this(runnable, runnable.toString());
+  /** @return a {@link Builder}. */
+  // TODO(jiacheng): For exceptions that should crash the server, apply a 
correct UncaughtExceptionHandler
+  public static Builder newBuilder() {
+    return new Builder();
   }
 
-  /** Construct a daemon thread with the given runnable. */
-  public Daemon(Runnable runnable, String name) {
-    super(runnable);
-    this.setName(name);
+  public static class Builder {
+    private String name;
+    private Runnable runnable;
+    // By default, uncaught exceptions are just logged without further actions
+    private UncaughtExceptionHandler uncaughtExceptionHandler = LOG_EXCEPTION;

Review Comment:
   Use null for default.



##########
ratis-server-api/src/main/java/org/apache/ratis/server/RaftServer.java:
##########
@@ -116,6 +116,8 @@ default RaftGroup getGroup() {
     /** @return the internal {@link RaftClient} of this division. */
     RaftClient getRaftClient();
 
+    Thread.UncaughtExceptionHandler getUncaughtExceptionHandler();

Review Comment:
   Remove it since this is an internal API but not a public API.



##########
ratis-common/src/main/java/org/apache/ratis/util/Daemon.java:
##########
@@ -17,24 +17,59 @@
  */
 package org.apache.ratis.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
 public class Daemon extends Thread {
+  static final Logger LOG = LoggerFactory.getLogger(Daemon.class);
+  public static final Thread.UncaughtExceptionHandler LOG_EXCEPTION =
+      (t, e) -> LOG.error(t.getName() + " threw an uncaught exception", e);
+
   {
     setDaemon(true);
   }
 
-  /** Construct a daemon thread. */
-  public Daemon() {
-    super();
+  /** Construct a daemon thread with flexible arguments. */
+  protected Daemon(Builder builder) {
+    super(builder.runnable);
+    setName(builder.name);
+    if (builder.uncaughtExceptionHandler != null) {
+      setUncaughtExceptionHandler(builder.uncaughtExceptionHandler);
+    }

Review Comment:
   Use Optional:
   ```java
       Optional.ofNullable(builder.uncaughtExceptionHandler 
).ifPresent(this::setUncaughtExceptionHandler);
   ```



##########
ratis-common/src/main/java/org/apache/ratis/util/Daemon.java:
##########
@@ -17,24 +17,59 @@
  */
 package org.apache.ratis.util;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
 public class Daemon extends Thread {
+  static final Logger LOG = LoggerFactory.getLogger(Daemon.class);
+  public static final Thread.UncaughtExceptionHandler LOG_EXCEPTION =
+      (t, e) -> LOG.error(t.getName() + " threw an uncaught exception", e);

Review Comment:
   Move them to MiniRaftCluster since they are only used there.



##########
ratis-server-api/src/main/java/org/apache/ratis/server/RaftServer.java:
##########
@@ -156,6 +158,8 @@ default RpcType getRpcType() {
 
   LifeCycle.State getLifeCycleState();
 
+  Thread.UncaughtExceptionHandler getUncaughtExceptionHandler();
+

Review Comment:
   Same as before, remove this method declaration.



##########
ratis-common/src/main/java/org/apache/ratis/util/JvmPauseMonitor.java:
##########
@@ -137,7 +137,8 @@ private void handle(TimeDuration extraSleep) {
 
   /** Start this monitor. */
   public void start() {
-    final MemoizedSupplier<Thread> supplier = JavaUtils.memoize(() -> new 
Daemon(this::run));
+    final MemoizedSupplier<Thread> supplier = JavaUtils.memoize(() ->
+        
Daemon.newBuilder().setName("JvmPauseMonitor").setRunnable(this::run).build());

Review Comment:
   Add an id to the name.



-- 
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]

Reply via email to