ivandika3 commented on code in PR #1622:
URL: https://github.com/apache/ratis/pull/1622#discussion_r4235933872


##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -534,52 +536,97 @@ void groupRemove(boolean deleteDirectory, boolean 
renameDirectory) {
 
   @Override
   public void close() {
-    lifeCycle.checkStateAndClose(() -> {
-      LOG.info("{}: shutdown", getMemberId());
-      try {
-        jmxAdapter.unregister();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to un-register RaftServer JMX bean", 
getMemberId(), e);
-      }
-      try {
-        role.shutdownFollowerState();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown FollowerState", getMemberId(), e);
-      }
-      try{
-        role.shutdownLeaderElection();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown LeaderElection", getMemberId(), e);
-      }
-      try{
-        role.shutdownLeaderState(true).join();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown LeaderState monitor", getMemberId(), 
e);
-      }
-      try{
-        state.close();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to close state", getMemberId(), e);
-      }
-      try {
-        leaderElectionMetrics.unregister();
-        raftServerMetrics.unregister();
-        RaftServerMetricsImpl.removeRaftServerMetrics(getMemberId());
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to unregister metric", getMemberId(), e);
-      }
-      try {
-        ConcurrentUtils.shutdownAndWait(clientExecutor);
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown clientExecutor", getMemberId(), e);
-      }
-      try {
-        ConcurrentUtils.shutdownAndWait(serverExecutor);
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown serverExecutor", getMemberId(), e);
+    try {
+      lifeCycle.checkStateAndClose(this::closeImpl);
+    } finally {
+      if (lifeCycle.getCurrentState() == State.CLOSED) {
+        closeFinishedLatch.countDown();
       }
-      closeFinishedLatch.countDown();
-    });
+    }
+  }
+
+  void closeOnFailure(Throwable cause) {

Review Comment:
   Thanks, it's cleaner. Updated.



##########
ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java:
##########
@@ -534,52 +536,97 @@ void groupRemove(boolean deleteDirectory, boolean 
renameDirectory) {
 
   @Override
   public void close() {
-    lifeCycle.checkStateAndClose(() -> {
-      LOG.info("{}: shutdown", getMemberId());
-      try {
-        jmxAdapter.unregister();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to un-register RaftServer JMX bean", 
getMemberId(), e);
-      }
-      try {
-        role.shutdownFollowerState();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown FollowerState", getMemberId(), e);
-      }
-      try{
-        role.shutdownLeaderElection();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown LeaderElection", getMemberId(), e);
-      }
-      try{
-        role.shutdownLeaderState(true).join();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown LeaderState monitor", getMemberId(), 
e);
-      }
-      try{
-        state.close();
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to close state", getMemberId(), e);
-      }
-      try {
-        leaderElectionMetrics.unregister();
-        raftServerMetrics.unregister();
-        RaftServerMetricsImpl.removeRaftServerMetrics(getMemberId());
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to unregister metric", getMemberId(), e);
-      }
-      try {
-        ConcurrentUtils.shutdownAndWait(clientExecutor);
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown clientExecutor", getMemberId(), e);
-      }
-      try {
-        ConcurrentUtils.shutdownAndWait(serverExecutor);
-      } catch (Exception e) {
-        LOG.warn("{}: Failed to shutdown serverExecutor", getMemberId(), e);
+    try {
+      lifeCycle.checkStateAndClose(this::closeImpl);
+    } finally {
+      if (lifeCycle.getCurrentState() == State.CLOSED) {
+        closeFinishedLatch.countDown();
       }
-      closeFinishedLatch.countDown();
-    });
+    }
+  }
+
+  void closeOnFailure(Throwable cause) {
+    LOG.error("{}: Closing this division due to a failure.", getMemberId(), 
cause);
+    final State closing = lifeCycle.transition(current -> 
current.isClosingOrClosed() ? current
+        : current == State.NEW ? State.CLOSED : State.CLOSING);
+    final boolean notify = shutdownOnFailure.compareAndSet(false, true);
+    if (closing == null && !notify) {
+      return;
+    }
+    // The caller may hold the server lock or run on an executor that 
closeImpl must stop.
+    Daemon.newBuilder().setName(getMemberId() + 
"-close").setThreadGroup(threadGroup)
+        .setRunnable(() -> {
+          try {
+            if (closing == State.CLOSING) {
+              closeImpl();
+            }
+          } finally {
+            if (closing == State.CLOSING) {
+              lifeCycle.transition(State.CLOSED);
+            }
+            if (closing != null) {
+              closeFinishedLatch.countDown();

Review Comment:
   Thanks, updated.



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