Class LongZkLedgerIdGenerator

java.lang.Object
org.apache.bookkeeper.meta.LongZkLedgerIdGenerator
All Implemented Interfaces:
Closeable, AutoCloseable, LedgerIdGenerator

public class LongZkLedgerIdGenerator extends 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 Details

    • LongZkLedgerIdGenerator

      public LongZkLedgerIdGenerator(org.apache.zookeeper.ZooKeeper zk, String ledgersPath, String idGenZnodeName, ZkLedgerIdGenerator shortIdGen, List<org.apache.zookeeper.data.ACL> zkAcls)
  • Method Details

    • invalidateLedgerIdGenPathStatus

      public void invalidateLedgerIdGenPathStatus()
    • ledgerIdGenPathPresent

      public boolean ledgerIdGenPathPresent(org.apache.zookeeper.ZooKeeper zk) throws org.apache.zookeeper.KeeperException, InterruptedException
      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.
      Parameters:
      zk -
      Returns:
      Does the long ledger id gen path exist?
      Throws:
      org.apache.zookeeper.KeeperException
      InterruptedException
      See Also:
      • createLongLedgerIdPathAndGenerateLongLedgerId(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback<java.lang.Long>,java.lang.String)
    • generateLedgerId

      public void generateLedgerId(BookkeeperInternalCallbacks.GenericCallback<Long> cb)
      Description copied from interface: LedgerIdGenerator
      Generate a global unique ledger id.
      Specified by:
      generateLedgerId in interface LedgerIdGenerator
      Parameters:
      cb - Callback when a new ledger id is generated, return code:
      • BKException.Code.OK if success
      • BKException.Code.ZKException when can't generate new ledger id
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException