Package me.ahoo.cosid.spring.redis
Class SpringRedisMachineLeaseStore
java.lang.Object
me.ahoo.cosid.spring.redis.SpringRedisMachineLeaseStore
- All Implemented Interfaces:
MachineLeaseStore
MachineLeaseStore on Redis: one atomic Lua script per primitive (machine_lease.lua).
It uses the same keys as the 3.4 machine id distributor (cosid:{namespace}:itc_idx, revert,
adder, stable), so instances running either one, and 3.4 instances, can share a namespace during an
upgrade. The watermark, renewal time and version of each machine id are kept in cosid:{namespace}:lease; the
3.4 keys hold a timestamp that is at least all of them, which 3.4 reads as its last timestamp. A machine id last
written by a 3.4 instance is read with that timestamp as its watermark, renewal time and version. The server clock is
Redis' TIME. Every command is bounded by the RedisCommandTimeout.
- Since:
- 3.4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.springframework.data.redis.core.script.RedisScript<List> -
Constructor Summary
ConstructorsConstructorDescriptionSpringRedisMachineLeaseStore(org.springframework.data.redis.core.StringRedisTemplate redisTemplate, RedisCommandTimeout commandTimeout) Create a Redis 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).longRedis'TIME, in milliseconds.withCommandTimeout(RedisCommandTimeout commandTimeout) The same store with another command timeout, for example a longer one for the distribution at startup, where the first command also pays for connecting.
-
Field Details
-
MACHINE_LEASE
-
-
Constructor Details
-
SpringRedisMachineLeaseStore
public SpringRedisMachineLeaseStore(org.springframework.data.redis.core.StringRedisTemplate redisTemplate, RedisCommandTimeout commandTimeout) Create a Redis lease store.- Parameters:
commandTimeout- bounds every command, which may run under the lease distributor's lock: must be enabled
-
-
Method Details
-
withCommandTimeout
The same store with another command timeout, for example a longer one for the distribution at startup, where the first command also pays for connecting. -
getCommandTimeout
-
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
-
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
-
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
-
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()Redis'TIME, in milliseconds.- Specified by:
serverTimein interfaceMachineLeaseStore
-