In release 4.5.0, Apache BookKeeper introduces a few advanced API for advanced usage. This sections covers these advanced APIs.

Before learn the advanced API, please read Ledger API first.

LedgerHandleAdv

LedgerHandleAdv is an advanced extension of LedgerHandle. It allows user passing in an entryId when adding an entry.

Creating advanced ledgers

Here’s an exmaple:

  1. byte[] passwd = "some-passwd".getBytes();
  2. LedgerHandleAdv handle = bkClient.createLedgerAdv(
  3. 3, 3, 2, // replica settings
  4. DigestType.CRC32,
  5. passwd);

You can also create advanced ledgers asynchronously.

  1. class LedgerCreationCallback implements AsyncCallback.CreateCallback {
  2. public void createComplete(int returnCode, LedgerHandle handle, Object ctx) {
  3. System.out.println("Ledger successfully created");
  4. }
  5. }
  6. client.asyncCreateLedgerAdv(
  7. 3, // ensemble size
  8. 3, // write quorum size
  9. 2, // ack quorum size
  10. BookKeeper.DigestType.CRC32,
  11. password,
  12. new LedgerCreationCallback(),
  13. "some context"
  14. );

Besides the APIs above, BookKeeper allows users providing ledger-id when creating advanced ledgers.

  1. long ledgerId = ...; // the ledger id is generated externally.
  2. byte[] passwd = "some-passwd".getBytes();
  3. LedgerHandleAdv handle = bkClient.createLedgerAdv(
  4. ledgerId, // ledger id generated externally
  5. 3, 3, 2, // replica settings
  6. DigestType.CRC32,
  7. passwd);

Please note, it is users’ responsibility to provide a unique ledger id when using the API above. If a ledger already exists when users try to create an advanced ledger with same ledger id, a LedgerExistsException is thrown by the bookkeeper client.

Creating advanced ledgers can be done throught a fluent API since 4.6.

  1. BookKeeper bk = ...;
  2. byte[] passwd = "some-passwd".getBytes();
  3. WriteHandle wh = bk.newCreateLedgerOp()
  4. .withDigestType(DigestType.CRC32)
  5. .withPassword(passwd)
  6. .withEnsembleSize(3)
  7. .withWriteQuorumSize(3)
  8. .withAckQuorumSize(2)
  9. .makeAdv() // convert the create ledger builder to create ledger adv builder
  10. .withLedgerId(1234L)
  11. .execute() // execute the creation op
  12. .get(); // wait for the execution to complete

Add Entries

The normal add entries api in advanced ledgers are disabled. Instead, when users want to add entries to advanced ledgers, an entry id is required to pass in along with the entry data when adding an entry.

  1. long entryId = ...; // entry id generated externally
  2. ledger.addEntry(entryId, "Some entry data".getBytes());

If you are using the new API, you can do as following:

  1. WriteHandle wh = ...;
  2. long entryId = ...; // entry id generated externally
  3. wh.write(entryId, "Some entry data".getBytes()).get();

A few notes when using this API:

  • The entry id has to be non-negative.
  • Clients are okay to add entries out of order.
  • However, the entries are only acknowledged in a monotonic order starting from 0.

Read Entries

The read entries api in advanced ledgers remain same as normal ledgers.