Class LongZkLedgerIdGenerator
- java.lang.Object
-
- org.apache.bookkeeper.meta.LongZkLedgerIdGenerator
-
- All Implemented Interfaces:
java.io.Closeable
,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 Summary
Constructors Constructor Description LongZkLedgerIdGenerator(org.apache.zookeeper.ZooKeeper zk, java.lang.String ledgersPath, java.lang.String idGenZnodeName, ZkLedgerIdGenerator shortIdGen, java.util.List<org.apache.zookeeper.data.ACL> zkAcls)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
generateLedgerId(BookkeeperInternalCallbacks.GenericCallback<java.lang.Long> cb)
Generate a global unique ledger id.void
invalidateLedgerIdGenPathStatus()
boolean
ledgerIdGenPathPresent(org.apache.zookeeper.ZooKeeper zk)
Checks the existence of the long ledger id gen path.
-
-
-
Constructor Detail
-
LongZkLedgerIdGenerator
public LongZkLedgerIdGenerator(org.apache.zookeeper.ZooKeeper zk, java.lang.String ledgersPath, java.lang.String idGenZnodeName, ZkLedgerIdGenerator shortIdGen, java.util.List<org.apache.zookeeper.data.ACL> zkAcls)
-
-
Method Detail
-
invalidateLedgerIdGenPathStatus
public void invalidateLedgerIdGenPathStatus()
-
ledgerIdGenPathPresent
public boolean ledgerIdGenPathPresent(org.apache.zookeeper.ZooKeeper zk) throws org.apache.zookeeper.KeeperException, java.lang.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
java.lang.InterruptedException
- See Also:
createLongLedgerIdPathAndGenerateLongLedgerId(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback<java.lang.Long>,java.lang.String)
-
generateLedgerId
public void generateLedgerId(BookkeeperInternalCallbacks.GenericCallback<java.lang.Long> cb)
Description copied from interface:LedgerIdGenerator
Generate a global unique ledger id.- Specified by:
generateLedgerId
in interfaceLedgerIdGenerator
- Parameters:
cb
- Callback when a new ledger id is generated, return code:BKException.Code.OK
if successBKException.Code.ZKException
when can't generate new ledger id
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
-