@@ -216,15 +216,22 @@ public synchronized void transitionState(NodeState state)
216216 }
217217 }
218218 case SHUTTING_DOWN -> {
219- if (currState .state () == DRAINED && nodeState .compareAndSet (currState , currState .toShuttingDown ())) {
219+ if (isCoordinator ) {
220+ throw new UnsupportedOperationException ("Cannot shutdown coordinator" );
221+ }
222+ VersionedState shuttingDown = currState .toShuttingDown ();
223+ if (currState .state () == DRAINED && nodeState .compareAndSet (currState , shuttingDown )) {
220224 requestTerminate ();
221225 return ;
222226 }
227+ nodeState .set (shuttingDown );
223228 requestGracefulShutdown ();
224- nodeState .set (currState .toShuttingDown ());
225229 return ;
226230 }
227231 case DRAINING -> {
232+ if (isCoordinator ) {
233+ throw new UnsupportedOperationException ("Cannot drain coordinator" );
234+ }
228235 if (currState .state () == ACTIVE && nodeState .compareAndSet (currState , currState .toDraining ())) {
229236 requestDrain ();
230237 return ;
@@ -246,9 +253,6 @@ private long nextStateVersion()
246253 private synchronized void requestDrain ()
247254 {
248255 log .debug ("Drain requested, NodeState: %s" , getServerState ());
249- if (isCoordinator ) {
250- throw new UnsupportedOperationException ("Cannot drain coordinator" );
251- }
252256
253257 // wait for a grace period (so that draining state is observed by the coordinator) before starting draining
254258 // when coordinator observes draining no new tasks are assigned to this worker
@@ -259,22 +263,16 @@ private synchronized void requestDrain()
259263 private void requestTerminate ()
260264 {
261265 log .info ("Immediate Shutdown requested" );
262- if (isCoordinator ) {
263- throw new UnsupportedOperationException ("Cannot shutdown coordinator" );
264- }
265266
266267 shutdownHandler .schedule (this ::terminate , 0 , MILLISECONDS );
267268 }
268269
269270 private void requestGracefulShutdown ()
270271 {
271272 log .info ("Shutdown requested" );
272- if (isCoordinator ) {
273- throw new UnsupportedOperationException ("Cannot shutdown coordinator" );
274- }
275273
276- // wait for a grace period (so that shutting down state is observed by the coordinator) to start the shutdown sequence
277274 VersionedState expectedState = nodeState .get ();
275+ // wait for a grace period (so that shutting down state is observed by the coordinator) to start the shutdown sequence
278276 shutdownHandler .schedule (() -> shutdown (expectedState ), gracePeriod .toMillis (), MILLISECONDS );
279277 }
280278
0 commit comments