Class GracefulShutdownCoordinator

java.lang.Object
com.loomcache.server.lifecycle.GracefulShutdownCoordinator
All Implemented Interfaces:
AutoCloseable

public class GracefulShutdownCoordinator extends Object implements AutoCloseable
Coordinates orderly shutdown of all LoomCache components.

The shutdown process follows a well-defined sequence to ensure data consistency and clean resource cleanup:

  1. Stop accepting new connections
  2. Drain in-flight requests (configurable timeout, default 30s)
  3. Flush write-behind caches
  4. Take final state snapshot
  5. Close all connections
  6. Stop background threads
  7. 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:
  • 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 metrics
      drainConnectionsTimeout - timeout for draining connections
      flushDataTimeout - timeout for flushing data
      snapshotTimeout - timeout for taking snapshot
      stopServicesTimeout - timeout for stopping services
      hardShutdownTimeout - 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 component
      component - the component to shut down
      Returns:
      this coordinator for method chaining
    • deregisterComponent

      public GracefulShutdownCoordinator deregisterComponent(String componentName)
      Deregisters a previously registered component.
      Parameters:
      componentName - the name of the component to deregister
      Returns:
      this coordinator for method chaining
    • addShutdownListener

      Adds a listener to receive shutdown progress notifications.
      Parameters:
      listener - the listener to add
      Returns:
      this coordinator for method chaining
    • removeShutdownListener

      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

      public boolean awaitShutdownCompletion(Duration timeout) throws InterruptedException
      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:
      close in interface AutoCloseable