Java Code Examples for org.apache.bookkeeper.client.api.ReadHandle#getLastAddConfirmed()

The following examples show how to use org.apache.bookkeeper.client.api.ReadHandle#getLastAddConfirmed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ledgers.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Reliably retrieves the LastAddConfirmed for the Ledger with given LedgerId, by opening the Ledger in fencing mode
 * and getting the value. NOTE: this open-fences the Ledger which will effectively stop any writing action on it.
 *
 * @param ledgerId   The Id of the Ledger to query.
 * @param bookKeeper A references to the BookKeeper client to use.
 * @param config     Configuration to use.
 * @return The LastAddConfirmed for the given LedgerId.
 * @throws DurableDataLogException If an exception occurred. The causing exception is wrapped inside it.
 */
static long readLastAddConfirmed(long ledgerId, BookKeeper bookKeeper, BookKeeperConfig config) throws DurableDataLogException {
    ReadHandle h = null;
    try {
        // Here we open the Ledger WITH recovery, to force BookKeeper to reconcile any appends that may have been
        // interrupted and not properly acked. Otherwise there is no guarantee we can get an accurate value for
        // LastAddConfirmed.
        h = openFence(ledgerId, bookKeeper, config);
        return h.getLastAddConfirmed();
    } finally {
        if (h != null) {
            close(h);
        }
    }
}
 
Example 2
Source File: Ledgers.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Fences out a Log made up of the given ledgers.
 *
 * @param ledgers       An ordered list of LedgerMetadata objects representing all the Ledgers in the log.
 * @param bookKeeper    A reference to the BookKeeper client to use.
 * @param config        Configuration to use.
 * @param traceObjectId Used for logging.
 * @return A Map of LedgerId to LastAddConfirmed for those Ledgers that were fenced out and had a different
 * LastAddConfirmed than what their LedgerMetadata was indicating.
 * @throws DurableDataLogException If an exception occurred. The causing exception is wrapped inside it.
 */
static Map<Long, Long> fenceOut(List<LedgerMetadata> ledgers, BookKeeper bookKeeper, BookKeeperConfig config, String traceObjectId) throws DurableDataLogException {
    // Fence out the ledgers, in descending order. During the process, we need to determine whether the ledgers we
    // fenced out actually have any data in them, and update the LedgerMetadata accordingly.
    // We need to fence out at least MIN_FENCE_LEDGER_COUNT ledgers that are not empty to properly ensure we fenced
    // the log correctly and identify any empty ledgers (Since this algorithm is executed upon every recovery, any
    // empty ledgers should be towards the end of the Log).
    int nonEmptyCount = 0;
    val result = new HashMap<Long, Long>();
    val iterator = ledgers.listIterator(ledgers.size());
    while (iterator.hasPrevious() && (nonEmptyCount < MIN_FENCE_LEDGER_COUNT)) {
        LedgerMetadata ledgerMetadata = iterator.previous();
        ReadHandle handle = openFence(ledgerMetadata.getLedgerId(), bookKeeper, config);
        if (handle.getLastAddConfirmed() != NO_ENTRY_ID) {
            // Non-empty.
            nonEmptyCount++;
        }

        if (ledgerMetadata.getStatus() == LedgerMetadata.Status.Unknown) {
            // We did not know the status of this Ledger before, but now we do.
            result.put(ledgerMetadata.getLedgerId(), handle.getLastAddConfirmed());
        }

        close(handle);
        log.info("{}: Fenced out Ledger {}.", traceObjectId, ledgerMetadata);
    }

    return result;
}
 
Example 3
Source File: OffloadPrefixReadTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
MockOffloadReadHandle(ReadHandle toCopy) throws Exception {
    id = toCopy.getId();
    long lac = toCopy.getLastAddConfirmed();
    try (LedgerEntries entries = toCopy.read(0, lac)) {
        for (LedgerEntry e : entries) {
            this.entries.add(e.getEntryBuffer().retainedSlice());
        }
    }
    metadata = new MockMetadata(toCopy.getLedgerMetadata());
}
 
Example 4
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void internalReadFromLedger(ReadHandle ledger, OpReadEntry opReadEntry) {

        // Perform the read
        long firstEntry = opReadEntry.readPosition.getEntryId();
        long lastEntryInLedger;
        final ManagedCursorImpl cursor = opReadEntry.cursor;

        PositionImpl lastPosition = lastConfirmedEntry;

        if (ledger.getId() == lastPosition.getLedgerId()) {
            // For the current ledger, we only give read visibility to the last entry we have received a confirmation in
            // the managed ledger layer
            lastEntryInLedger = lastPosition.getEntryId();
        } else {
            // For other ledgers, already closed the BK lastAddConfirmed is appropriate
            lastEntryInLedger = ledger.getLastAddConfirmed();
        }

        if (firstEntry > lastEntryInLedger) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] No more messages to read from ledger={} lastEntry={} readEntry={}", name,
                        ledger.getId(), lastEntryInLedger, firstEntry);
            }

            if (currentLedger == null || ledger.getId() != currentLedger.getId()) {
                // Cursor was placed past the end of one ledger, move it to the
                // beginning of the next ledger
                Long nextLedgerId = ledgers.ceilingKey(ledger.getId() + 1);
                if (nextLedgerId != null) {
                    opReadEntry.updateReadPosition(new PositionImpl(nextLedgerId, 0));
                } else {
                    opReadEntry.updateReadPosition(new PositionImpl(ledger.getId() + 1, 0));
                }
            }

            opReadEntry.checkReadCompletion();
            return;
        }

        long lastEntry = min(firstEntry + opReadEntry.getNumberOfEntriesToRead() - 1, lastEntryInLedger);

        if (log.isDebugEnabled()) {
            log.debug("[{}] Reading entries from ledger {} - first={} last={}", name, ledger.getId(), firstEntry,
                    lastEntry);
        }
        asyncReadEntry(ledger, firstEntry, lastEntry, false, opReadEntry, opReadEntry.ctx);
    }