Class MongoMachineLeaseStore
- All Implemented Interfaces:
MachineLeaseStore
MachineLeaseStore on the cosid_machine collection: every claim, renewal and release is one atomic
findOneAndUpdate or conditional update.
It uses the same documents as the 3.4 machine id distributor (a released machine id has an empty
instanceId), so instances running either one can share a namespace during an upgrade. The server clock is
the localTime of the hello command, and every operation has the driver's client-side timeout
(timeoutMS). Claims use pipeline updates, which need MongoDB 4.2 or later. On a sharded cluster the server clock is the
mongos one, so keep the mongos clocks in sync.
Documents keep the 3.4 fields. The last timestamp stays the expiry check and the version, as in 3.4; it never decreases and is at least every renewal and watermark, so a claim starts after it. A lease still carries its own watermark and renewal time: renewing leaves the watermark alone and reports the store time it renewed at, while the document is written with a last timestamp that covers both. Do not add fields for them: the last timestamp already covers both, since expiry and takeover are anchored on it, and the ZooKeeper store keeps the same model.
- Since:
- 3.4.0
-
Constructor Summary
ConstructorsConstructorDescriptionMongoMachineLeaseStore(com.mongodb.client.MongoDatabase database, Duration timeout) Create a MongoDB lease store. -
Method Summary
Modifier and TypeMethodDescriptionclaimExpired(String namespace, InstanceId instanceId, int maxMachineId, long expiredBefore, long now) Take over a machine id whose owner has not renewed it sinceexpiredBefore.claimNew(String namespace, InstanceId instanceId, int maxMachineId, long now) Take the next machine id that was never used in the namespace.claimReleased(String namespace, InstanceId instanceId, int maxMachineId, long now) Take a machine id that a non-stable instance released on shutdown.claimSelf(String namespace, InstanceId instanceId, long now) Take back the lease this instance already holds, renewing it tonow, whether or not it has expired.booleanRelease a lease on shutdown, if it is still the current version.Renew a lease tonowif it is still the currentversion, even whennowis before the stored renewed at (the store clock moved back).longThelocalTimeof the server'shellocommand, or ofisMasteron servers before 4.2.10, which have nohello.
-
Constructor Details
-
MongoMachineLeaseStore
Create a MongoDB lease store.- Parameters:
timeout- client-side timeout of each operation, must be positive: the lease adapter holds a lock during each call
-
-
Method Details
-
claimSelf
Description copied from interface:MachineLeaseStoreTake back the lease this instance already holds, renewing it tonow, whether or not it has expired. The watermark is raised to at leastnowand the stored renewed at.- Specified by:
claimSelfin interfaceMachineLeaseStore- Returns:
- the renewed lease, or empty if the instance holds none (it was released or recycled)
-
claimReleased
public Optional<Lease> claimReleased(String namespace, InstanceId instanceId, int maxMachineId, long now) Description copied from interface:MachineLeaseStoreTake a machine id that a non-stable instance released on shutdown.- Specified by:
claimReleasedin interfaceMachineLeaseStoremaxMachineId- the largest machine id allowed: released ids above it (left over from a larger machine bit) are never returned- Returns:
- the new lease, or empty if no released machine id up to
maxMachineIdis left
-
claimExpired
public Optional<Lease> claimExpired(String namespace, InstanceId instanceId, int maxMachineId, long expiredBefore, long now) Description copied from interface:MachineLeaseStoreTake over a machine id whose owner has not renewed it sinceexpiredBefore. Leases of stable instances are never taken over.- Specified by:
claimExpiredin interfaceMachineLeaseStoremaxMachineId- the largest machine id allowed: expired ids above it are never returnedexpiredBefore- leases renewed at or before this time are expired- Returns:
- the new lease, or empty if no lease up to
maxMachineIdhas expired
-
claimNew
public Optional<Lease> claimNew(String namespace, InstanceId instanceId, int maxMachineId, long now) Description copied from interface:MachineLeaseStoreTake the next machine id that was never used in the namespace.- Specified by:
claimNewin interfaceMachineLeaseStoremaxMachineId- the largest machine id allowed- Returns:
- the new lease, or empty if every machine id up to
maxMachineIdis in use
-
renew
Description copied from interface:MachineLeaseStoreRenew a lease tonowif it is still the currentversion, even whennowis before the stored renewed at (the store clock moved back). The watermark is unchanged.- Specified by:
renewin interfaceMachineLeaseStore- Returns:
- the renewed lease, or empty if the lease was taken over, released or changed since
-
release
Description copied from interface:MachineLeaseStoreRelease a lease on shutdown, if it is still the current version. A stable instance keeps its machine id (only the watermark is updated); otherwise the machine id becomes available toMachineLeaseStore.claimReleased(java.lang.String, me.ahoo.cosid.machine.InstanceId, int, long).- Specified by:
releasein interfaceMachineLeaseStorewatermark- the generator's last timestamp: the watermark becomes at least this, so that the next owner waits if its clock is behind- Returns:
falseif the lease was taken over or changed since
-
serverTime
public long serverTime()ThelocalTimeof the server'shellocommand, or ofisMasteron servers before 4.2.10, which have nohello.- Specified by:
serverTimein interfaceMachineLeaseStore
-