Class DefaultMachineIdGuarder

java.lang.Object
me.ahoo.cosid.machine.DefaultMachineIdGuarder
All Implemented Interfaces:
MachineIdGuarder

public class DefaultMachineIdGuarder extends Object implements MachineIdGuarder
Default MachineId Guarder implementation.

This class provides a default implementation of the MachineIdGuarder interface. It uses a scheduled executor to periodically guard machine IDs for registered instances, ensuring they remain active and preventing conflicts in distributed systems.

The guarder maintains a set of registered instance IDs and periodically calls the MachineIdDistributor.guard(String, InstanceId, Duration) method for each registered instance. This helps in scenarios where machine IDs need to be kept alive or refreshed at regular intervals.

Example usage:


 MachineIdDistributor distributor = new SomeMachineIdDistributor();
 Duration safeGuardDuration = Duration.ofMinutes(5);
 DefaultMachineIdGuarder guarder = new DefaultMachineIdGuarder(distributor, safeGuardDuration);
 guarder.register("myNamespace", new InstanceId("instance1"));
 guarder.start();
 // ... application runs
 guarder.stop();
 
  • Field Details

    • DEFAULT_INITIAL_DELAY

      public static final Duration DEFAULT_INITIAL_DELAY
    • DEFAULT_DELAY

      public static final Duration DEFAULT_DELAY
  • Constructor Details

    • DefaultMachineIdGuarder

      public DefaultMachineIdGuarder(MachineIdDistributor machineIdDistributor, Duration safeGuardDuration)
      Constructs a DefaultMachineIdGuarder with default scheduling parameters.

      This constructor creates a guarder with a default scheduled executor service, initial delay of 1 minute, and delay of 1 minute between guard operations.

      Parameters:
      machineIdDistributor - the distributor used to guard machine IDs
      safeGuardDuration - the duration for which to guard each machine ID
    • DefaultMachineIdGuarder

      public DefaultMachineIdGuarder(MachineIdDistributor machineIdDistributor, ScheduledExecutorService executorService, Duration initialDelay, Duration delay, Duration safeGuardDuration)
      Constructs a DefaultMachineIdGuarder with custom scheduling parameters.

      This constructor allows full customization of the guarder's behavior, including the executor service and scheduling intervals.

      Parameters:
      machineIdDistributor - the distributor used to guard machine IDs
      executorService - the scheduled executor service for periodic guarding
      initialDelay - the initial delay before the first guard operation
      delay - the delay between subsequent guard operations
      safeGuardDuration - the duration for which to guard each machine ID
  • Method Details

    • executorService

      public static ScheduledExecutorService executorService()
      Creates a default scheduled executor service for the guarder.

      This method returns a single-threaded scheduled executor service with a daemon thread named "MachineIdGuarder". The executor is suitable for periodic guarding operations.

      Returns:
      a new ScheduledExecutorService configured for machine ID guarding
    • getGuardianStates

      public Map<NamespacedInstanceId,GuardianState> getGuardianStates()
      Gets the guardian state for all registered instances.

      This method returns a map of namespaced instance IDs to their current guardian state, including the timestamp of the last guarding operation and any errors that occurred.

      This implementation returns an immutable copy of the current guardian status map, showing the status of all registered instances.

      Specified by:
      getGuardianStates in interface MachineIdGuarder
      Returns:
      a map of namespaced instance IDs to their guardian state
    • register

      public void register(String namespace, InstanceId instanceId)
      Registers an instance ID within a specific namespace.

      This method associates the given instance ID with the provided namespace, allowing the guarder to track and manage machine IDs for conflict prevention.

      This implementation adds the instance to an internal set for periodic guarding. If the instance is already registered, it will not be added again.

      Specified by:
      register in interface MachineIdGuarder
      Parameters:
      namespace - the namespace to register the instance in, must not be null
      instanceId - the instance ID to register, must not be null
    • unregister

      public void unregister(String namespace, InstanceId instanceId)
      Unregisters an instance ID from a specific namespace.

      This method removes the association of the given instance ID with the provided namespace, releasing any resources or locks held for that instance.

      This implementation removes the instance from the set of registered instances, preventing further periodic guarding for this instance.

      Specified by:
      unregister in interface MachineIdGuarder
      Parameters:
      namespace - the namespace to unregister the instance from, must not be null
      instanceId - the instance ID to unregister, must not be null
    • start

      public void start()
      Starts the machine ID guarder.

      This method initializes the guarder and begins monitoring or guarding machine IDs. After calling start(), the guarder is considered running.

      This implementation schedules periodic guarding of registered instances using the configured executor service. The first guard operation occurs after the initial delay, and subsequent operations occur at the specified interval.

      Specified by:
      start in interface MachineIdGuarder
    • stop

      public void stop()
      Stops the machine ID guarder.

      This method shuts down the guarder and stops monitoring or guarding machine IDs. After calling stop(), the guarder is no longer running.

      This implementation cancels the scheduled guarding task and marks the guarder as stopped. The cancellation is forceful, interrupting any ongoing guard operation.

      Specified by:
      stop in interface MachineIdGuarder
    • isRunning

      public boolean isRunning()
      Checks if the machine ID guarder is currently running.

      This implementation returns the current running state, which is atomically managed.

      Specified by:
      isRunning in interface MachineIdGuarder
      Returns:
      true if the guarder is running, false otherwise