Skip to content

RATIS-2721. Notify the StateMachine when a server shutdown starts - #1647

Open
amaliujia wants to merge 3 commits into
apache:masterfrom
amaliujia:robus_server_and_ratis
Open

amaliujia wants to merge 3 commits into
apache:masterfrom
amaliujia:robus_server_and_ratis

Conversation

@amaliujia

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Notify the StateMachine when a server shutdown starts

What is the link to the Apache JIRA

https://issues.apache.org/jira/browse/RATIS-2721

Please replace this section with the link to the Apache JIRA)

How was this patch tested?

Unit test

@amaliujia
amaliujia force-pushed the robus_server_and_ratis branch from 0f1bd8b to b55f83b Compare October 17, 2026 14:10
Comment on lines 245 to +272
default void notifyServerShutdown(RoleInfoProto roleInfo, boolean allServer) {}

/**
* Notify the {@link StateMachine} that the shutdown of the server for this division has started.
* @deprecated please use/override {@link #notifyServerShutdownStarted(RoleInfoProto, boolean)} instead
*/
@Deprecated
default void notifyServerShutdownStarted(RoleInfoProto roleInfo) {
notifyServerShutdownStarted(roleInfo, false);
}

/**
* Notify the {@link StateMachine} that the shutdown of either the server for this division
* or all the servers has started.
* <p>
* In contrast to {@link #notifyServerShutdown(RoleInfoProto, boolean)}, which is invoked once the shutdown
* has completed, this method is invoked before the shutdown has closed any resources.
* In particular, it is invoked before waiting for the committed log entries to be applied to this
* {@link StateMachine}, which is unbounded in time. An application embedding a {@link RaftServer} can use
* this notification to react immediately, e.g. to reject incoming requests or to terminate itself,
* instead of waiting for the shutdown to complete.
* <p>
* This method is invoked from the shutdown path, so an implementation must return promptly.
*
* @param roleInfo roleInfo this server
* @param allServer whether all raft servers will be shutdown at this time
*/
default void notifyServerShutdownStarted(RoleInfoProto roleInfo, boolean allServer) {}

@szetszwo szetszwo Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amaliujia , thanks for working on this!

Please deprecate and override the existing notifyServerShutdown method:
(revision: renamed the new enum to EventStatus)

diff --git a/ratis-server-api/src/main/java/org/apache/ratis/statemachine/StateMachine.java b/ratis-server-api/src/main/java/org/apache/ratis/statemachine/StateMachine.java
index bd009b0e9..967debb04 100644
--- a/ratis-server-api/src/main/java/org/apache/ratis/statemachine/StateMachine.java
+++ b/ratis-server-api/src/main/java/org/apache/ratis/statemachine/StateMachine.java
@@ -58,6 +58,11 @@ import java.util.function.Function;
 public interface StateMachine extends Closeable {
   Logger LOG = LoggerFactory.getLogger(StateMachine.class);
 
+  enum EventStatus {
+    INITIATED,
+    COMPLETED
+  }
+
   /** A registry to support different state machines in multi-raft environment. */
   interface Registry extends Function<RaftGroupId, StateMachine> {
   }
@@ -229,20 +234,33 @@ public interface StateMachine extends Closeable {
     default void notifySnapshotInstalled(InstallSnapshotResult result, long snapshotIndex,  RaftPeer peer) {}
 
     /**
-     * Notify the {@link StateMachine} that the server for this division has been shut down.
-     * @deprecated please use/override {@link #notifyServerShutdown(RoleInfoProto, boolean)} instead
+     * The same as notifyServerShutdown(roleInfo, false, EventStatus.COMPLETED).
+     *
+     * @deprecated please use/override {@link #notifyServerShutdown(RoleInfoProto, boolean, EventStatus)} instead
      */
     @Deprecated
     default void notifyServerShutdown(RoleInfoProto roleInfo) {
-      notifyServerShutdown(roleInfo, false);
+      notifyServerShutdown(roleInfo, false, EventStatus.COMPLETED);
+    }
+
+    /**
+     * The same as notifyServerShutdown(roleInfo, allServer, EventStatus.COMPLETED).
+     *
+     * @deprecated please use/override {@link #notifyServerShutdown(RoleInfoProto, boolean, EventStatus)} instead
+     */
+    @Deprecated
+    default void notifyServerShutdown(RoleInfoProto roleInfo, boolean allServer) {
+      notifyServerShutdown(roleInfo, allServer, EventStatus.COMPLETED);
     }
 
     /**
-     * Notify the {@link StateMachine} that either the server for this division or all the servers have been shut down.
+     * Notify the {@link StateMachine} server shutdown.
+     *
      * @param roleInfo roleInfo this server
      * @param allServer whether all raft servers will be shutdown at this time
+     * @param eventStatus {@link EventStatus#INITIATED} or {@link EventStatus#COMPLETED}
      */
-    default void notifyServerShutdown(RoleInfoProto roleInfo, boolean allServer) {}
+    default void notifyServerShutdown(RoleInfoProto roleInfo, boolean allServer, EventStatus eventStatus) {}
   }
 
   /**

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants