Package me.ahoo.cosid.machine.lease
Class MachineLeaseManager
java.lang.Object
me.ahoo.cosid.machine.lease.MachineLeaseManager
The single machine id allocation algorithm, on top of any
MachineLeaseStore.
Order: take back this instance's own lease, then a released machine id, then a never-used one, and only then
an expired one. A stable instance never takes over another instance's expired lease, and with
MachineIdDistributor.FOREVER_SAFE_GUARD_DURATION no lease ever expires.
Every time comes from MachineLeaseStore.serverTime(), except the watermark written on
release, which is the generator's last timestamp from this host's clock. Hosts' clocks must therefore be
kept in sync: a host running ahead only delays the next owner of a released machine id (it waits until its clock
passes the watermark), which is safe but slow.
- Since:
- 3.4.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionacquire(String namespace, InstanceId instanceId, int machineBit, Duration safeGuardDuration) Take a machine id for the instance.getStore()reclaim(String namespace, InstanceId instanceId) Take back the lease this instance holds, for instance after a restart without a lease in memory.booleanRelease a lease on shutdown.Renew a lease.
-
Constructor Details
-
MachineLeaseManager
-
-
Method Details
-
getStore
-
acquire
public Lease acquire(String namespace, InstanceId instanceId, int machineBit, Duration safeGuardDuration) throws MachineIdOverflowException Take a machine id for the instance.- Throws:
MachineIdOverflowException- if every machine id is held by a live (or stable) owner
-
reclaim
Take back the lease this instance holds, for instance after a restart without a lease in memory.- Returns:
- empty if the instance holds none: it was released or taken over
-
renew
Renew a lease.- Returns:
- the renewed lease, or empty if it was taken over in the meantime
-
release
Release a lease on shutdown.- Parameters:
watermark- the generator's last timestamp (host clock), the next owner's clock-backwards watermark- Returns:
falseif the lease was taken over in the meantime
-