Class ManagedCursorImpl

    • Field Detail

      • bookkeeper

        protected final org.apache.bookkeeper.client.BookKeeper bookkeeper
      • markDeletePosition

        protected volatile PositionImpl markDeletePosition
      • persistentMarkDeletePosition

        protected volatile PositionImpl persistentMarkDeletePosition
      • READ_POSITION_UPDATER

        protected static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ManagedCursorImpl,​PositionImpl> READ_POSITION_UPDATER
      • readPosition

        protected volatile PositionImpl readPosition
      • statsLastReadPosition

        protected volatile PositionImpl statsLastReadPosition
      • LAST_MARK_DELETE_ENTRY_UPDATER

        protected static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ManagedCursorImpl,​org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.MarkDeleteEntry> LAST_MARK_DELETE_ENTRY_UPDATER
      • lastMarkDeleteEntry

        protected volatile org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.MarkDeleteEntry lastMarkDeleteEntry
      • WAITING_READ_OP_UPDATER

        protected static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<ManagedCursorImpl,​org.apache.bookkeeper.mledger.impl.OpReadEntry> WAITING_READ_OP_UPDATER
      • messagesConsumedCounter

        protected volatile long messagesConsumedCounter
      • pendingMarkDeleteOps

        protected final java.util.ArrayDeque<org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.MarkDeleteEntry> pendingMarkDeleteOps
      • state

        protected volatile org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.State state
    • Method Detail

      • getProperties

        public java.util.Map<java.lang.String,​java.lang.Long> getProperties()
        Description copied from interface: ManagedCursor
        Return any properties that were associated with the last stored position.
        Specified by:
        getProperties in interface ManagedCursor
      • readEntries

        public java.util.List<Entry> readEntries​(int numberOfEntriesToRead)
                                          throws java.lang.InterruptedException,
                                                 ManagedLedgerException
        Description copied from interface: ManagedCursor
        Read entries from the ManagedLedger, up to the specified number. The returned list can be smaller.
        Specified by:
        readEntries in interface ManagedCursor
        Parameters:
        numberOfEntriesToRead - maximum number of entries to return
        Returns:
        the list of entries
        Throws:
        ManagedLedgerException
        java.lang.InterruptedException
      • asyncReadEntries

        public void asyncReadEntries​(int numberOfEntriesToRead,
                                     long maxSizeBytes,
                                     AsyncCallbacks.ReadEntriesCallback callback,
                                     java.lang.Object ctx,
                                     PositionImpl maxPosition)
        Description copied from interface: ManagedCursor
        Asynchronously read entries from the ManagedLedger.
        Specified by:
        asyncReadEntries in interface ManagedCursor
        Parameters:
        numberOfEntriesToRead - maximum number of entries to return
        maxSizeBytes - max size in bytes of the entries to return
        callback - callback object
        ctx - opaque context
        maxPosition - max position can read
      • readEntriesOrWait

        public java.util.List<Entry> readEntriesOrWait​(int numberOfEntriesToRead)
                                                throws java.lang.InterruptedException,
                                                       ManagedLedgerException
        Description copied from interface: ManagedCursor
        Read entries from the ManagedLedger, up to the specified number. The returned list can be smaller.

        If no entries are available, the method will block until at least a new message will be persisted.

        Specified by:
        readEntriesOrWait in interface ManagedCursor
        Parameters:
        numberOfEntriesToRead - maximum number of entries to return
        Returns:
        the list of entries
        Throws:
        ManagedLedgerException
        java.lang.InterruptedException
      • readEntriesOrWait

        public java.util.List<Entry> readEntriesOrWait​(int numberOfEntriesToRead,
                                                       long maxSizeBytes)
                                                throws java.lang.InterruptedException,
                                                       ManagedLedgerException
        Description copied from interface: ManagedCursor
        Read entries from the ManagedLedger, up to the specified number and size.

        If no entries are available, the method will block until at least a new message will be persisted.

        Specified by:
        readEntriesOrWait in interface ManagedCursor
        Parameters:
        numberOfEntriesToRead - maximum number of entries to return
        maxSizeBytes - max size in bytes of the entries to return
        Returns:
        the list of entries
        Throws:
        ManagedLedgerException
        java.lang.InterruptedException
      • asyncReadEntriesOrWait

        public void asyncReadEntriesOrWait​(int numberOfEntriesToRead,
                                           AsyncCallbacks.ReadEntriesCallback callback,
                                           java.lang.Object ctx,
                                           PositionImpl maxPosition)
        Description copied from interface: ManagedCursor
        Asynchronously read entries from the ManagedLedger.

        If no entries are available, the callback will not be triggered. Instead it will be registered to wait until a new message will be persisted into the managed ledger

        Specified by:
        asyncReadEntriesOrWait in interface ManagedCursor
        Parameters:
        numberOfEntriesToRead - maximum number of entries to return
        callback - callback object
        ctx - opaque context
        maxPosition - max position can read
        See Also:
        ManagedCursor.readEntriesOrWait(int)
      • asyncReadEntriesOrWait

        public void asyncReadEntriesOrWait​(int maxEntries,
                                           long maxSizeBytes,
                                           AsyncCallbacks.ReadEntriesCallback callback,
                                           java.lang.Object ctx,
                                           PositionImpl maxPosition)
        Description copied from interface: ManagedCursor
        Asynchronously read entries from the ManagedLedger, up to the specified number and size.

        If no entries are available, the callback will not be triggered. Instead it will be registered to wait until a new message will be persisted into the managed ledger

        Specified by:
        asyncReadEntriesOrWait in interface ManagedCursor
        Parameters:
        maxEntries - maximum number of entries to return
        maxSizeBytes - max size in bytes of the entries to return
        callback - callback object
        ctx - opaque context
        maxPosition - max position can read
        See Also:
        ManagedCursor.readEntriesOrWait(int, long)
      • isClosed

        public boolean isClosed()
      • hasPendingReadRequest

        public boolean hasPendingReadRequest()
      • hasMoreEntries

        public boolean hasMoreEntries()
        Description copied from interface: ManagedCursor
        Tells whether this cursor has already consumed all the available entries.

        This method is not blocking.

        Specified by:
        hasMoreEntries in interface ManagedCursor
        Returns:
        true if there are pending entries to read, false otherwise
      • getNumberOfEntries

        public long getNumberOfEntries()
        Description copied from interface: ManagedCursor
        Return the number of messages that this cursor still has to read.

        This method has linear time complexity on the number of ledgers included in the managed ledger.

        Specified by:
        getNumberOfEntries in interface ManagedCursor
        Returns:
        the number of entries
      • getNumberOfEntriesSinceFirstNotAckedMessage

        public long getNumberOfEntriesSinceFirstNotAckedMessage()
        Description copied from interface: ManagedCursor
        Returns total number of entries from the first not-acked message to current dispatching position.
        Specified by:
        getNumberOfEntriesSinceFirstNotAckedMessage in interface ManagedCursor
        Returns:
      • getEstimatedSizeSinceMarkDeletePosition

        public long getEstimatedSizeSinceMarkDeletePosition()
        Description copied from interface: ManagedCursor
        Returns the estimated size of the unacknowledged backlog for this cursor
        Specified by:
        getEstimatedSizeSinceMarkDeletePosition in interface ManagedCursor
        Returns:
        the estimated size from the mark delete position of the cursor
      • getNumberOfEntriesInBacklog

        public long getNumberOfEntriesInBacklog​(boolean isPrecise)
        Description copied from interface: ManagedCursor
        Return the number of non-deleted messages on this cursor.

        This will also include messages that have already been read from the cursor but not deleted or mark-deleted yet.

        This method has linear time complexity on the number of ledgers included in the managed ledger.

        Specified by:
        getNumberOfEntriesInBacklog in interface ManagedCursor
        Parameters:
        isPrecise - set to true to get precise backlog count
        Returns:
        the number of entries
      • getNumberOfEntriesInStorage

        public long getNumberOfEntriesInStorage()
      • findNewestMatching

        public Position findNewestMatching​(com.google.common.base.Predicate<Entry> condition)
                                    throws java.lang.InterruptedException,
                                           ManagedLedgerException
        Description copied from interface: ManagedCursor
        Find the newest entry that matches the given predicate. Will only search among active entries
        Specified by:
        findNewestMatching in interface ManagedCursor
        Parameters:
        condition - predicate that reads an entry an applies a condition
        Returns:
        Position of the newest entry that matches the given predicate
        Throws:
        java.lang.InterruptedException
        ManagedLedgerException
      • asyncFindNewestMatching

        public void asyncFindNewestMatching​(ManagedCursor.FindPositionConstraint constraint,
                                            com.google.common.base.Predicate<Entry> condition,
                                            AsyncCallbacks.FindEntryCallback callback,
                                            java.lang.Object ctx)
        Description copied from interface: ManagedCursor
        Find the newest entry that matches the given predicate.
        Specified by:
        asyncFindNewestMatching in interface ManagedCursor
        Parameters:
        constraint - search only active entries or all entries
        condition - predicate that reads an entry an applies a condition
        callback - callback object returning the resultant position
        ctx - opaque context
      • setActive

        public void setActive()
        Description copied from interface: ManagedCursor
        Activate cursor: EntryCacheManager caches entries only for activated-cursors.
        Specified by:
        setActive in interface ManagedCursor
      • isActive

        public boolean isActive()
        Description copied from interface: ManagedCursor
        Checks if cursor is active or not.
        Specified by:
        isActive in interface ManagedCursor
        Returns:
      • setAlwaysInactive

        public void setAlwaysInactive()
        Description copied from interface: ManagedCursor
        A cursor that is set as always-inactive will never trigger the caching of entries.
        Specified by:
        setAlwaysInactive in interface ManagedCursor
      • asyncReplayEntries

        public java.util.Set<? extends Position> asyncReplayEntries​(java.util.Set<? extends Position> positions,
                                                                    AsyncCallbacks.ReadEntriesCallback callback,
                                                                    java.lang.Object ctx)
        Async replays given positions: a. before reading it filters out already-acked messages b. reads remaining entries async and gives it to given ReadEntriesCallback c. returns all already-acked messages which are not replayed so, those messages can be removed by caller(Dispatcher)'s replay-list and it won't try to replay it again
        Specified by:
        asyncReplayEntries in interface ManagedCursor
        Parameters:
        positions - set of positions to read
        callback - callback object returning the list of entries
        ctx - opaque context
        Returns:
        skipped positions set of positions which are already deleted/acknowledged and skipped while replaying them
      • asyncReplayEntries

        public java.util.Set<? extends Position> asyncReplayEntries​(java.util.Set<? extends Position> positions,
                                                                    AsyncCallbacks.ReadEntriesCallback callback,
                                                                    java.lang.Object ctx,
                                                                    boolean sortEntries)
        Description copied from interface: ManagedCursor
        Read the specified set of positions from ManagedLedger.
        Specified by:
        asyncReplayEntries in interface ManagedCursor
        Parameters:
        positions - set of positions to read
        callback - callback object returning the list of entries
        ctx - opaque context
        sortEntries - callback with sorted entry list.
        Returns:
        skipped positions set of positions which are already deleted/acknowledged and skipped while replaying them
      • getNumberOfEntries

        protected long getNumberOfEntries​(com.google.common.collect.Range<PositionImpl> range)
      • markDelete

        public void markDelete​(Position position)
                        throws java.lang.InterruptedException,
                               ManagedLedgerException
        Description copied from interface: ManagedCursor
        This signals that the reader is done with all the entries up to "position" (included). This can potentially trigger a ledger deletion, if all the other cursors are done too with the underlying ledger.
        Specified by:
        markDelete in interface ManagedCursor
        Parameters:
        position - the last position that have been successfully consumed
        Throws:
        ManagedLedgerException
        java.lang.InterruptedException
      • markDelete

        public void markDelete​(Position position,
                               java.util.Map<java.lang.String,​java.lang.Long> properties)
                        throws java.lang.InterruptedException,
                               ManagedLedgerException
        Description copied from interface: ManagedCursor
        This signals that the reader is done with all the entries up to "position" (included). This can potentially trigger a ledger deletion, if all the other cursors are done too with the underlying ledger.
        Specified by:
        markDelete in interface ManagedCursor
        Parameters:
        position - the last position that have been successfully consumed
        properties - additional user-defined properties that can be associated with a particular cursor position
        Throws:
        ManagedLedgerException
        java.lang.InterruptedException
      • asyncMarkDelete

        public void asyncMarkDelete​(Position position,
                                    java.util.Map<java.lang.String,​java.lang.Long> properties,
                                    AsyncCallbacks.MarkDeleteCallback callback,
                                    java.lang.Object ctx)
        Description copied from interface: ManagedCursor
        Asynchronous mark delete.
        Specified by:
        asyncMarkDelete in interface ManagedCursor
        Parameters:
        position - the last position that have been successfully consumed
        properties - additional user-defined properties that can be associated with a particular cursor position
        callback - callback object
        ctx - opaque context
        See Also:
        ManagedCursor.markDelete(Position)
      • internalAsyncMarkDelete

        protected void internalAsyncMarkDelete​(PositionImpl newPosition,
                                               java.util.Map<java.lang.String,​java.lang.Long> properties,
                                               AsyncCallbacks.MarkDeleteCallback callback,
                                               java.lang.Object ctx)
      • delete

        public void delete​(Position position)
                    throws java.lang.InterruptedException,
                           ManagedLedgerException
        Description copied from interface: ManagedCursor
        Delete a single message.

        Mark a single message for deletion. When all the previous messages are all deleted, then markDelete() will be called internally to advance the persistent acknowledged position.

        The deletion of the message is not persisted into the durable storage and cannot be recovered upon the reopening of the ManagedLedger

        Specified by:
        delete in interface ManagedCursor
        Parameters:
        position - the position of the message to be deleted
        Throws:
        java.lang.InterruptedException
        ManagedLedgerException
      • asyncDelete

        public void asyncDelete​(Position pos,
                                AsyncCallbacks.DeleteCallback callback,
                                java.lang.Object ctx)
        Description copied from interface: ManagedCursor
        Delete a single message asynchronously

        Mark a single message for deletion. When all the previous messages are all deleted, then markDelete() will be called internally to advance the persistent acknowledged position.

        The deletion of the message is not persisted into the durable storage and cannot be recovered upon the reopening of the ManagedLedger

        Specified by:
        asyncDelete in interface ManagedCursor
        Parameters:
        pos - the position of the message to be deleted
        callback - callback object
        ctx - opaque context
      • delete

        public void delete​(java.lang.Iterable<Position> positions)
                    throws java.lang.InterruptedException,
                           ManagedLedgerException
        Description copied from interface: ManagedCursor
        Delete a group of entries.

        Mark multiple single messages for deletion. When all the previous messages are all deleted, then markDelete() will be called internally to advance the persistent acknowledged position.

        The deletion of the message is not persisted into the durable storage and cannot be recovered upon the reopening of the ManagedLedger

        Specified by:
        delete in interface ManagedCursor
        Parameters:
        positions - positions of the messages to be deleted
        Throws:
        java.lang.InterruptedException
        ManagedLedgerException
      • asyncDelete

        public void asyncDelete​(java.lang.Iterable<Position> positions,
                                AsyncCallbacks.DeleteCallback callback,
                                java.lang.Object ctx)
        Description copied from interface: ManagedCursor
        Delete a group of messages asynchronously

        Mark a group of messages for deletion. When all the previous messages are all deleted, then markDelete() will be called internally to advance the persistent acknowledged position.

        The deletion of the messages is not persisted into the durable storage and cannot be recovered upon the reopening of the ManagedLedger

        Specified by:
        asyncDelete in interface ManagedCursor
        Parameters:
        positions - the positions of the messages to be deleted
        callback - callback object
        ctx - opaque context
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getName

        public java.lang.String getName()
        Description copied from interface: ManagedCursor
        Get the unique cursor name.
        Specified by:
        getName in interface ManagedCursor
        Returns:
        the cursor name
      • getLastActive

        public long getLastActive()
        Description copied from interface: ManagedCursor
        Get the last active time of the cursor.
        Specified by:
        getLastActive in interface ManagedCursor
        Returns:
        the last active time of the cursor
      • isDurable

        public boolean isDurable()
        Description copied from interface: ManagedCursor
        Tells whether the cursor is durable or just kept in memory.
        Specified by:
        isDurable in interface ManagedCursor
      • getReadPosition

        public Position getReadPosition()
        Description copied from interface: ManagedCursor
        Get the read position. This points to the next message to be read from the cursor.
        Specified by:
        getReadPosition in interface ManagedCursor
        Returns:
        the read position
      • rewind

        public void rewind()
        Description copied from interface: ManagedCursor
        Rewind the cursor to the mark deleted position to replay all the already read but not yet mark deleted messages.

        The next message to be read is the one after the current mark deleted message.

        Specified by:
        rewind in interface ManagedCursor
      • seek

        public void seek​(Position newReadPositionInt)
        Description copied from interface: ManagedCursor
        Move the cursor to a different read position.

        If the new position happens to be before the already mark deleted position, it will be set to the mark deleted position instead.

        Specified by:
        seek in interface ManagedCursor
        Parameters:
        newReadPositionInt - the position where to move the cursor
      • asyncClose

        public void asyncClose​(AsyncCallbacks.CloseCallback callback,
                               java.lang.Object ctx)
        Description copied from interface: ManagedCursor
        Close the cursor asynchronously and release the associated resources.
        Specified by:
        asyncClose in interface ManagedCursor
        Parameters:
        callback - callback object
        ctx - opaque context
      • isBkErrorNotRecoverable

        public static boolean isBkErrorNotRecoverable​(int rc)
        return BK error codes that are considered not likely to be recoverable.
      • getPendingReadOpsCount

        public int getPendingReadOpsCount()
      • getMessagesConsumedCounter

        public long getMessagesConsumedCounter()
      • getCursorLedger

        public long getCursorLedger()
      • getCursorLedgerLastEntry

        public long getCursorLedgerLastEntry()
      • getIndividuallyDeletedMessages

        public java.lang.String getIndividuallyDeletedMessages()
      • getIndividuallyDeletedMessagesSet

        public org.apache.pulsar.common.util.collections.LongPairRangeSet<PositionImpl> getIndividuallyDeletedMessagesSet()
      • isMessageDeleted

        public boolean isMessageDeleted​(Position position)
      • getBatchPositionAckSet

        public long[] getBatchPositionAckSet​(Position position)
      • getNextAvailablePosition

        public PositionImpl getNextAvailablePosition​(PositionImpl position)
        Checks given position is part of deleted-range and returns next position of upper-end as all the messages are deleted up to that point.
        Parameters:
        position -
        Returns:
        next available position
      • getNextLedgerPosition

        public Position getNextLedgerPosition​(long currentLedgerId)
      • isIndividuallyDeletedEntriesEmpty

        public boolean isIndividuallyDeletedEntriesEmpty()
      • getLastLedgerSwitchTimestamp

        public long getLastLedgerSwitchTimestamp()
      • getState

        public java.lang.String getState()
      • setThrottleMarkDelete

        public void setThrottleMarkDelete​(double throttleMarkDelete)
        Description copied from interface: ManagedCursor
        Update throttle mark delete rate.
        Specified by:
        setThrottleMarkDelete in interface ManagedCursor
      • checkAndUpdateReadPositionChanged

        public boolean checkAndUpdateReadPositionChanged()
        Description copied from interface: ManagedCursor
        Checks if read position changed since this method was called last time.
        Specified by:
        checkAndUpdateReadPositionChanged in interface ManagedCursor
        Returns:
        if read position changed