Class ZookeeperMachineLeaseStore

java.lang.Object
me.ahoo.cosid.zookeeper.ZookeeperMachineLeaseStore
All Implemented Interfaces:
MachineLeaseStore

public class ZookeeperMachineLeaseStore extends Object implements MachineLeaseStore
MachineLeaseStore on ZooKeeper, with versioned writes and multi-operation transactions as the compare-and-set.

It uses the same nodes as the 3.4 machine id distributor, so instances running either one can share a namespace during an upgrade:

  • /cosid/{namespace}/__itc_idx/{instanceId}: machineId|lastTimestamp of the lease the instance holds
  • /cosid/{namespace}/__revert/{machineId}: machineId|lastTimestamp of a released machine id
  • /cosid/{namespace}/__counter: the number of machine ids ever handed out (a Curator atomic integer)
  • /cosid/{namespace}/__stable/{instanceId}: marks a stable instance, whose lease is never taken over

The nodes keep the 3.4 format, machineId|lastTimestamp, which 3.4 parses strictly. 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 node is written with a last timestamp that covers both. Do not add fields for them: 3.4 parses the node data strictly, and the last timestamp already covers both, since expiry and takeover are anchored on it.

ZooKeeper has no server clock, so MachineLeaseStore.serverTime() is this host's clock: keep the hosts' clocks in sync. Every call is bounded by the CallTimeout, since Curator has no per-operation timeout.

Since:
3.4.0
  • Constructor Details

    • ZookeeperMachineLeaseStore

      public ZookeeperMachineLeaseStore(org.apache.curator.framework.CuratorFramework curatorFramework, CallTimeout callTimeout)
  • Method Details

    • claimSelf

      public Optional<Lease> claimSelf(String namespace, InstanceId instanceId, long now)
      Description copied from interface: MachineLeaseStore
      Take back the lease this instance already holds, renewing it to now, whether or not it has expired. The watermark is raised to at least now and the stored renewed at.
      Specified by:
      claimSelf in interface MachineLeaseStore
      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: MachineLeaseStore
      Take a machine id that a non-stable instance released on shutdown.
      Specified by:
      claimReleased in interface MachineLeaseStore
      maxMachineId - 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 maxMachineId is left
    • claimNew

      public Optional<Lease> claimNew(String namespace, InstanceId instanceId, int maxMachineId, long now)
      Description copied from interface: MachineLeaseStore
      Take the next machine id that was never used in the namespace.
      Specified by:
      claimNew in interface MachineLeaseStore
      maxMachineId - the largest machine id allowed
      Returns:
      the new lease, or empty if every machine id up to maxMachineId is in use
    • claimExpired

      public Optional<Lease> claimExpired(String namespace, InstanceId instanceId, int maxMachineId, long expiredBefore, long now)
      Description copied from interface: MachineLeaseStore
      Take over a machine id whose owner has not renewed it since expiredBefore. Leases of stable instances are never taken over.
      Specified by:
      claimExpired in interface MachineLeaseStore
      maxMachineId - the largest machine id allowed: expired ids above it are never returned
      expiredBefore - leases renewed at or before this time are expired
      Returns:
      the new lease, or empty if no lease up to maxMachineId has expired
    • renew

      public Optional<Lease> renew(Lease lease, long now)
      Description copied from interface: MachineLeaseStore
      Renew a lease to now if it is still the current version, even when now is before the stored renewed at (the store clock moved back). The watermark is unchanged.
      Specified by:
      renew in interface MachineLeaseStore
      Returns:
      the renewed lease, or empty if the lease was taken over, released or changed since
    • release

      public boolean release(Lease lease, long watermark)
      Description copied from interface: MachineLeaseStore
      Release 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 to MachineLeaseStore.claimReleased(java.lang.String, me.ahoo.cosid.machine.InstanceId, int, long).
      Specified by:
      release in interface MachineLeaseStore
      watermark - the generator's last timestamp: the watermark becomes at least this, so that the next owner waits if its clock is behind
      Returns:
      false if the lease was taken over or changed since