Package me.ahoo.cosid.machine
Class MachineIdLease
java.lang.Object
me.ahoo.cosid.machine.MachineIdLease
The local view of how long this instance may keep using its machine id.
Other instances may take a machine id over once its owner has not renewed it for safeGuardDuration.
The lease is therefore valid until the start of the last successful renewal plus safeGuardDuration,
minus a margin for clock skew between hosts. Generators check it before every id, so an instance that cannot renew
its lease stops generating ids before another instance can reuse the machine id.
Thread-safe; isValid(long) is a pair of volatile reads.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MachineIdLeaseA lease that never expires, used when machine ids are never recycled.static final DurationUpper bound of the clock skew margin. -
Constructor Summary
ConstructorsConstructorDescriptionMachineIdLease(@Nullable String namespace, @Nullable InstanceId instanceId, Duration safeGuardDuration) -
Method Summary
Modifier and TypeMethodDescriptionvoidThrowMachineIdLostExceptionif the lease is lost or expired.longbooleanisLost()booleanbooleanisValid(long now) voidmarkLost()The remote store reported that the machine id now belongs to another instance.voidThe machine id is about to be reverted on shutdown.voidrenew(long renewedAt) Extend the lease after a successful distribute or guard.toString()
-
Field Details
-
MAX_CLOCK_SKEW_MARGIN
Upper bound of the clock skew margin. -
FOREVER
A lease that never expires, used when machine ids are never recycled.
-
-
Constructor Details
-
MachineIdLease
public MachineIdLease(@Nullable String namespace, @Nullable InstanceId instanceId, Duration safeGuardDuration)
-
-
Method Details
-
renew
public void renew(long renewedAt) Extend the lease after a successful distribute or guard.- Parameters:
renewedAt- the time, in milliseconds, at which the renewal request was started
-
markLost
public void markLost()The remote store reported that the machine id now belongs to another instance. The lease never becomes valid again. -
isLost
public boolean isLost() -
markReleased
public void markReleased()The machine id is about to be reverted on shutdown. The lease never becomes valid again, so no id is generated with a machine id that another instance may already have taken. -
isReleased
public boolean isReleased() -
getValidUntil
public long getValidUntil() -
isValid
public boolean isValid(long now) -
ensureValid
ThrowMachineIdLostExceptionif the lease is lost or expired.- Throws:
MachineIdLostException
-
toString
-