Class ReadOnlyLedgerHandle

java.lang.Object
org.apache.bookkeeper.client.LedgerHandle
org.apache.bookkeeper.client.ReadOnlyLedgerHandle
All Implemented Interfaces:
AutoCloseable, ForceableHandle, Handle, ReadHandle, WriteHandle, BookkeeperInternalCallbacks.LedgerMetadataListener

class ReadOnlyLedgerHandle extends LedgerHandle implements BookkeeperInternalCallbacks.LedgerMetadataListener
Read only ledger handle. This ledger handle allows you to read from a ledger but not to write to it. It overrides all the public write operations from LedgerHandle. It should be returned for BookKeeper#openLedger operations.
  • Constructor Details

  • Method Details

    • registerLedgerMetadataListener

      void registerLedgerMetadataListener()
    • close

      public void close() throws InterruptedException, BKException
      Description copied from class: LedgerHandle
      Synchronous close the write handle, any adds in flight will return errors.

      Closing a ledger will ensure that all clients agree on what the last entry of the ledger is. Once the ledger has been closed, all reads from the ledger will return the same set of entries.

      The close operation can error if it finds conflicting metadata when it tries to write to the metadata store. On close, the metadata state is set to closed and lastEntry and length of the ledger are fixed in the metadata. A conflict occurs if the metadata in the metadata store has a different value for the lastEntry or length. If another process has updated the metadata, setting it to closed, but have fixed the lastEntry and length to the same values as this process is trying to write, the operation completes successfully.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Handle
      Specified by:
      close in interface WriteHandle
      Overrides:
      close in class LedgerHandle
      Throws:
      InterruptedException
      BKException
      See Also:
    • asyncClose

      public void asyncClose(AsyncCallback.CloseCallback cb, Object ctx)
      Description copied from class: LedgerHandle
      Asynchronous close, any adds in flight will return errors.

      Closing a ledger will ensure that all clients agree on what the last entry of the ledger is. This ensures that, once the ledger has been closed, all reads from the ledger will return the same set of entries.

      Overrides:
      asyncClose in class LedgerHandle
      Parameters:
      cb - callback implementation
      ctx - control object
    • addEntry

      public long addEntry(byte[] data) throws InterruptedException, BKException
      Description copied from class: LedgerHandle
      Add entry synchronously to an open ledger.
      Overrides:
      addEntry in class LedgerHandle
      Parameters:
      data - array of bytes to be written to the ledger do not reuse the buffer, bk-client will release it appropriately
      Returns:
      the entryId of the new inserted entry
      Throws:
      InterruptedException
      BKException
    • addEntry

      public long addEntry(byte[] data, int offset, int length) throws InterruptedException, BKException
      Description copied from class: LedgerHandle
      Add entry synchronously to an open ledger.
      Overrides:
      addEntry in class LedgerHandle
      Parameters:
      data - array of bytes to be written to the ledger do not reuse the buffer, bk-client will release it appropriately
      offset - offset from which to take bytes from data
      length - number of bytes to take from data
      Returns:
      the entryId of the new inserted entry
      Throws:
      InterruptedException
      BKException
    • asyncAddEntry

      public void asyncAddEntry(byte[] data, AsyncCallback.AddCallback cb, Object ctx)
      Description copied from class: LedgerHandle
      Add entry asynchronously to an open ledger.
      Overrides:
      asyncAddEntry in class LedgerHandle
      Parameters:
      data - array of bytes to be written do not reuse the buffer, bk-client will release it appropriately
      cb - object implementing callbackinterface
      ctx - some control object
    • asyncAddEntry

      public void asyncAddEntry(byte[] data, int offset, int length, AsyncCallback.AddCallback cb, Object ctx)
      Description copied from class: LedgerHandle
      Add entry asynchronously to an open ledger, using an offset and range.
      Overrides:
      asyncAddEntry in class LedgerHandle
      Parameters:
      data - array of bytes to be written do not reuse the buffer, bk-client will release it appropriately
      offset - offset from which to take bytes from data
      length - number of bytes to take from data
      cb - object implementing callbackinterface
      ctx - some control object
    • onChanged

      public void onChanged(long lid, Versioned<LedgerMetadata> newMetadata)
      Description copied from interface: BookkeeperInternalCallbacks.LedgerMetadataListener
      Triggered each time ledger metadata changed.
      Specified by:
      onChanged in interface BookkeeperInternalCallbacks.LedgerMetadataListener
      Parameters:
      lid - ledger id.
      newMetadata - new ledger metadata.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • initializeWriteHandleState

      protected void initializeWriteHandleState()
      Overrides:
      initializeWriteHandleState in class LedgerHandle
    • asyncReadLastEntry

      public void asyncReadLastEntry(AsyncCallback.ReadCallback cb, Object ctx)
      Overrides:
      asyncReadLastEntry in class LedgerHandle
    • handleBookieFailure

      void handleBookieFailure(Map<Integer,BookieId> failedBookies)
      For a read only ledger handle, this method will only ever be called during recovery, when we are reading forward from LAC and writing back those entries. As such, unlike with LedgerHandle, we do not want to persist changes to the metadata as they occur, but rather, we want to defer the persistence until recovery has completed, and do it all on the close.
      Overrides:
      handleBookieFailure in class LedgerHandle
    • handleUnrecoverableErrorDuringAdd

      void handleUnrecoverableErrorDuringAdd(int rc)
      Overrides:
      handleUnrecoverableErrorDuringAdd in class LedgerHandle
    • recover

    • recover

      void recover(BookkeeperInternalCallbacks.GenericCallback<Void> finalCb, BookkeeperInternalCallbacks.ReadEntryListener listener, boolean forceRecovery)
      Recover the ledger.
      Parameters:
      finalCb - callback after recovery is done.
      listener - read entry listener on recovery reads.
      forceRecovery - force the recovery procedure even the ledger metadata shows the ledger is closed.
    • closeRecovered

    • getCurrentEnsemble

      List<BookieId> getCurrentEnsemble()
      Description copied from class: LedgerHandle
      Get the current ensemble from the ensemble list. The current ensemble is the last ensemble in the list. The ledger handle uses this ensemble when triggering operations which work on the end of the ledger, such as adding new entries or reading the last add confirmed.

      This method is also used by ReadOnlyLedgerHandle during recovery, and when tailing a ledger.

      Generally, this method should only be called by LedgerHandle and not by the operations themselves, to avoid adding more dependencies between the classes. There are too many already.

      Overrides:
      getCurrentEnsemble in class LedgerHandle