Class LongZkLedgerIdGenerator

  • All Implemented Interfaces:, java.lang.AutoCloseable, LedgerIdGenerator

    public class LongZkLedgerIdGenerator
    extends java.lang.Object
    implements LedgerIdGenerator
    ZooKeeper based ledger id generator class, which using EPHEMERAL_SEQUENTIAL with (ledgerIdGenPath)/HOB-[high-32-bits]/ID- prefix to generate ledger id. Note zookeeper sequential counter has a format of %10d -- that is 10 digits with 0 (zero) padding, i.e. "<path>0000000001", so ledger id space would be fundamentally limited to 9 billion. In practice, the id generated by zookeeper is only 31 bits (signed 32-bit integer), so the limit is much lower than 9 billion.

    In order to support the full range of the long ledgerId, once ledgerIds reach Integer.MAX_INT, a new system is employed. The 32 most significant bits of the ledger ID are taken and turned into a directory prefixed with HOB- under (ledgerIdGenPath)

    Under this HOB- directory, zookeeper is used to continue generating EPHEMERAL_SEQUENTIAL ids which constitute the lower 32-bits of the ledgerId (sign bit is always 0). Once the HOB- directory runs out of available ids, the process is repeated. The higher bits are incremented, a new HOB- directory is created, and zookeeper generates sequential ids underneath it.

    The reason for treating ids which are less than Integer.MAX_INT differently is to maintain backwards compatibility. This is a drop-in replacement for ZkLedgerIdGenerator.

    • Constructor Detail

      • LongZkLedgerIdGenerator

        public LongZkLedgerIdGenerator​(org.apache.zookeeper.ZooKeeper zk,
                                       java.lang.String ledgersPath,
                                       java.lang.String idGenZnodeName,
                                       ZkLedgerIdGenerator shortIdGen,
                                       java.util.List<> zkAcls)
    • Method Detail

      • invalidateLedgerIdGenPathStatus

        public void invalidateLedgerIdGenPathStatus()
      • ledgerIdGenPathPresent

        public boolean ledgerIdGenPathPresent​(org.apache.zookeeper.ZooKeeper zk)
                                       throws org.apache.zookeeper.KeeperException,
        Checks the existence of the long ledger id gen path. Existence indicates we have switched from the legacy algorithm to the new method of generating 63-bit ids. If the existence is UNKNOWN, it looks in zk to find out. If it previously checked in zk, it returns that value. This value changes when we run out of ids < Integer.MAX_VALUE, and try to create the long ledger id gen path.
        zk -
        Does the long ledger id gen path exist?
        See Also:
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface