Class GracefulShutdownCoordinator
java.lang.Object
com.loomcache.server.lifecycle.GracefulShutdownCoordinator
- All Implemented Interfaces:
AutoCloseable
Coordinates orderly shutdown of all LoomCache components.
The shutdown process follows a well-defined sequence to ensure data consistency and clean resource cleanup:
- Stop accepting new connections
- Drain in-flight requests (configurable timeout, default 30s)
- Flush write-behind caches
- Take final state snapshot
- Close all connections
- Stop background threads
- Close metrics exporters
This class is thread-safe and implements AutoCloseable for use with
try-with-resources. Multiple shutdown requests are idempotent — only the first
invocation triggers the shutdown sequence.
Shutdown progress can be monitored via GracefulShutdownCoordinator.ShutdownListener callbacks,
which are invoked as each phase progresses.
Usage:
var coordinator = new GracefulShutdownCoordinator(meterRegistry);
coordinator.registerComponent("tcpServer", tcpServer);
coordinator.registerComponent("raftNode", raftNode);
// Register for progress updates
coordinator.addShutdownListener(progress -> {
log.info("Shutdown progress: {} - {}/{} steps",
progress.phase(), progress.completedSteps(), progress.totalSteps());
});
// Initiate graceful shutdown
coordinator.shutdown();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classPhase: Shutdown is complete.static final classPhase: Stop accepting new connections and mark servers as shutting down.static final classPhase: Flush write-behind caches and pending data structures.static interfaceInterface for components that can be gracefully shut down.static interfaceFunctional interface for receiving shutdown progress notifications.static interfaceSealed interface defining the phases of graceful shutdown.static final recordRecord representing the current progress of shutdown.static final classPhase: Take a final snapshot of system state for recovery.static final classPhase: Stop background threads and shut down services. -
Constructor Summary
ConstructorsConstructorDescriptionGracefulShutdownCoordinator(@Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a GracefulShutdownCoordinator with default timeouts.GracefulShutdownCoordinator(@Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, Duration drainConnectionsTimeout, Duration flushDataTimeout, Duration snapshotTimeout, Duration stopServicesTimeout, Duration hardShutdownTimeout) Creates a GracefulShutdownCoordinator with custom timeouts. -
Method Summary
Modifier and TypeMethodDescriptionAdds a listener to receive shutdown progress notifications.booleanawaitShutdownCompletion(Duration timeout) Waits for shutdown to complete with a timeout.voidclose()Closes this coordinator and initiates shutdown.deregisterComponent(String componentName) Deregisters a previously registered component.intReturns the number of registered components.booleanReturns whether shutdown is complete.booleanReturns whether shutdown is in progress.registerComponent(String componentName, GracefulShutdownCoordinator.ShutdownComponent component) Registers a component to be shut down.Removes a previously added shutdown listener.booleanshutdown()Initiates the graceful shutdown sequence.
-
Constructor Details
-
GracefulShutdownCoordinator
public GracefulShutdownCoordinator(@Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a GracefulShutdownCoordinator with default timeouts.- Parameters:
meterRegistry- the metrics registry, or null to disable metrics
-
GracefulShutdownCoordinator
public GracefulShutdownCoordinator(@Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, Duration drainConnectionsTimeout, Duration flushDataTimeout, Duration snapshotTimeout, Duration stopServicesTimeout, Duration hardShutdownTimeout) Creates a GracefulShutdownCoordinator with custom timeouts.- Parameters:
meterRegistry- the metrics registry, or null to disable metricsdrainConnectionsTimeout- timeout for draining connectionsflushDataTimeout- timeout for flushing datasnapshotTimeout- timeout for taking snapshotstopServicesTimeout- timeout for stopping serviceshardShutdownTimeout- hard deadline for entire shutdown
-
-
Method Details
-
registerComponent
public GracefulShutdownCoordinator registerComponent(String componentName, GracefulShutdownCoordinator.ShutdownComponent component) Registers a component to be shut down.- Parameters:
componentName- the name of the componentcomponent- the component to shut down- Returns:
- this coordinator for method chaining
-
deregisterComponent
Deregisters a previously registered component.- Parameters:
componentName- the name of the component to deregister- Returns:
- this coordinator for method chaining
-
addShutdownListener
public GracefulShutdownCoordinator addShutdownListener(GracefulShutdownCoordinator.ShutdownListener listener) Adds a listener to receive shutdown progress notifications.- Parameters:
listener- the listener to add- Returns:
- this coordinator for method chaining
-
removeShutdownListener
public GracefulShutdownCoordinator removeShutdownListener(GracefulShutdownCoordinator.ShutdownListener listener) Removes a previously added shutdown listener.- Parameters:
listener- the listener to remove- Returns:
- this coordinator for method chaining
-
shutdown
public boolean shutdown()Initiates the graceful shutdown sequence. This method is idempotent and thread-safe. Multiple concurrent calls will all return, but only the first will execute the shutdown.- Returns:
- true if shutdown was initiated, false if already shutting down
-
isShuttingDown
public boolean isShuttingDown()Returns whether shutdown is in progress.- Returns:
- true if shutdown is in progress or complete
-
isShutdownComplete
public boolean isShutdownComplete()Returns whether shutdown is complete.- Returns:
- true if shutdown is complete
-
awaitShutdownCompletion
Waits for shutdown to complete with a timeout.- Parameters:
timeout- the maximum time to wait- Returns:
- true if shutdown completed within the timeout, false if timeout occurred
- Throws:
InterruptedException- if interrupted while waiting
-
getComponentCount
public int getComponentCount()Returns the number of registered components.- Returns:
- the number of components
-
close
public void close()Closes this coordinator and initiates shutdown.- Specified by:
closein interfaceAutoCloseable
-