Class DefaultMachineIdGuarder
- All Implemented Interfaces:
MachineIdGuarder
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();
-
Nested Class Summary
Nested classes/interfaces inherited from interface me.ahoo.cosid.machine.MachineIdGuarder
MachineIdGuarder.None -
Field Summary
FieldsFields inherited from interface me.ahoo.cosid.machine.MachineIdGuarder
NONE -
Constructor Summary
ConstructorsConstructorDescriptionDefaultMachineIdGuarder(MachineIdDistributor machineIdDistributor, Duration safeGuardDuration) Constructs a DefaultMachineIdGuarder with default scheduling parameters.DefaultMachineIdGuarder(MachineIdDistributor machineIdDistributor, ScheduledExecutorService executorService, Duration initialDelay, Duration delay, Duration safeGuardDuration) Constructs a DefaultMachineIdGuarder with custom scheduling parameters. -
Method Summary
Modifier and TypeMethodDescriptionstatic ScheduledExecutorServiceCreates a default scheduled executor service for the guarder.Gets the guardian state for all registered instances.booleanChecks if the machine ID guarder is currently running.voidregister(String namespace, InstanceId instanceId) Registers an instance ID within a specific namespace.voidstart()Starts the machine ID guarder.voidstop()Stops the machine ID guarder.voidunregister(String namespace, InstanceId instanceId) Unregisters an instance ID from a specific namespace.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface me.ahoo.cosid.machine.MachineIdGuarder
hasFailure
-
Field Details
-
DEFAULT_INITIAL_DELAY
-
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 IDssafeGuardDuration- 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 IDsexecutorService- the scheduled executor service for periodic guardinginitialDelay- the initial delay before the first guard operationdelay- the delay between subsequent guard operationssafeGuardDuration- the duration for which to guard each machine ID
-
-
Method Details
-
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
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:
getGuardianStatesin interfaceMachineIdGuarder- Returns:
- a map of namespaced instance IDs to their guardian state
-
register
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:
registerin interfaceMachineIdGuarder- Parameters:
namespace- the namespace to register the instance in, must not be nullinstanceId- the instance ID to register, must not be null
-
unregister
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:
unregisterin interfaceMachineIdGuarder- Parameters:
namespace- the namespace to unregister the instance from, must not be nullinstanceId- 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:
startin interfaceMachineIdGuarder
-
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:
stopin interfaceMachineIdGuarder
-
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:
isRunningin interfaceMachineIdGuarder- Returns:
- true if the guarder is running, false otherwise
-