Search in sources :

Example 16 with ManagedLedgerException

use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.

the class ManagedCursorImpl method persistPosition.

void persistPosition(final LedgerHandle lh, final PositionImpl position, final VoidCallback callback) {
    PositionInfo pi = PositionInfo.newBuilder().setLedgerId(position.getLedgerId()).setEntryId(position.getEntryId()).addAllIndividualDeletedMessages(buildIndividualDeletedMessageRanges()).build();
    if (log.isDebugEnabled()) {
        log.debug("[{}] Cursor {} Appending to ledger={} position={}", ledger.getName(), name, lh.getId(), position);
    }
    checkNotNull(lh);
    lh.asyncAddEntry(pi.toByteArray(), (rc, lh1, entryId, ctx) -> {
        if (rc == BKException.Code.OK) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Updated cursor {} position {} in meta-ledger {}", ledger.getName(), name, position, lh1.getId());
            }
            if (shouldCloseLedger(lh1)) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Need to create new metadata ledger for consumer {}", ledger.getName(), name);
                }
                startCreatingNewMetadataLedger();
            }
            callback.operationComplete();
        } else {
            log.warn("[{}] Error updating cursor {} position {} in meta-ledger {}: {}", ledger.getName(), name, position, lh1.getId(), BKException.getMessage(rc));
            // If we've had a write error, the ledger will be automatically closed, we need to create a new one,
            // in the meantime the mark-delete will be queued.
            STATE_UPDATER.compareAndSet(ManagedCursorImpl.this, State.Open, State.NoLedger);
            callback.operationFailed(new ManagedLedgerException(BKException.getMessage(rc)));
        }
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PositionInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo)

Example 17 with ManagedLedgerException

use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.

the class ManagedCursorImpl method recoverFromLedger.

protected void recoverFromLedger(final ManagedCursorInfo info, final VoidCallback callback) {
    // Read the acknowledged position from the metadata ledger, then create
    // a new ledger and write the position into it
    ledger.mbean.startCursorLedgerOpenOp();
    long ledgerId = info.getCursorsLedgerId();
    bookkeeper.asyncOpenLedger(ledgerId, config.getDigestType(), config.getPassword(), (rc, lh, ctx) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Opened ledger {} for consumer {}. rc={}", ledger.getName(), ledgerId, name, rc);
        }
        if (isBkErrorNotRecoverable(rc)) {
            log.error("[{}] Error opening metadata ledger {} for consumer {}: {}", ledger.getName(), ledgerId, name, BKException.getMessage(rc));
            // Rewind to oldest entry available
            initialize(getRollbackPosition(info), callback);
            return;
        } else if (rc != BKException.Code.OK) {
            log.warn("[{}] Error opening metadata ledger {} for consumer {}: {}", ledger.getName(), ledgerId, name, BKException.getMessage(rc));
            callback.operationFailed(new ManagedLedgerException(BKException.getMessage(rc)));
            return;
        }
        // Read the last entry in the ledger
        cursorLedger = lh;
        lh.asyncReadLastEntry((rc1, lh1, seq, ctx1) -> {
            if (log.isDebugEnabled()) {
                log.debug("readComplete rc={} entryId={}", rc1, lh1.getLastAddConfirmed());
            }
            if (isBkErrorNotRecoverable(rc1)) {
                log.error("[{}] Error reading from metadata ledger {} for consumer {}: {}", ledger.getName(), ledgerId, name, BKException.getMessage(rc1));
                // Rewind to oldest entry available
                initialize(getRollbackPosition(info), callback);
                return;
            } else if (rc1 != BKException.Code.OK) {
                log.warn("[{}] Error reading from metadata ledger {} for consumer {}: {}", ledger.getName(), ledgerId, name, BKException.getMessage(rc1));
                callback.operationFailed(new ManagedLedgerException(BKException.getMessage(rc1)));
                return;
            }
            LedgerEntry entry = seq.nextElement();
            PositionInfo positionInfo;
            try {
                positionInfo = PositionInfo.parseFrom(entry.getEntry());
            } catch (InvalidProtocolBufferException e) {
                callback.operationFailed(new ManagedLedgerException(e));
                return;
            }
            PositionImpl position = new PositionImpl(positionInfo);
            if (positionInfo.getIndividualDeletedMessagesCount() > 0) {
                recoverIndividualDeletedMessages(positionInfo.getIndividualDeletedMessagesList());
            }
            recoveredCursor(position);
            callback.operationComplete();
        }, null);
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PositionInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo)

Example 18 with ManagedLedgerException

use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.

the class ManagedCursorImpl method asyncFindNewestMatching.

@Override
public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition, FindEntryCallback callback, Object ctx) {
    OpFindNewest op;
    PositionImpl startPosition = null;
    long max = 0;
    switch(constraint) {
        case SearchAllAvailableEntries:
            startPosition = (PositionImpl) getFirstPosition();
            max = ledger.getNumberOfEntries() - 1;
            break;
        case SearchActiveEntries:
            startPosition = ledger.getNextValidPosition(markDeletePosition);
            max = getNumberOfEntriesInStorage();
            break;
        default:
            callback.findEntryFailed(new ManagedLedgerException("Unknown position constraint"), ctx);
            return;
    }
    if (startPosition == null) {
        callback.findEntryFailed(new ManagedLedgerException("Couldn't find start position"), ctx);
        return;
    }
    op = new OpFindNewest(this, startPosition, condition, max, callback, ctx);
    op.find();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 19 with ManagedLedgerException

use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.

the class ManagedCursorImpl method clearBacklog.

@Override
public void clearBacklog() throws InterruptedException, ManagedLedgerException {
    class Result {

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    final CountDownLatch counter = new CountDownLatch(1);
    asyncClearBacklog(new ClearBacklogCallback() {

        @Override
        public void clearBacklogComplete(Object ctx) {
            counter.countDown();
        }

        @Override
        public void clearBacklogFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(ManagedLedgerImpl.AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during clear backlog operation");
    }
    if (result.exception != null) {
        throw result.exception;
    }
}
Also used : ClearBacklogCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ClearBacklogCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with ManagedLedgerException

use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.

the class ManagedCursorImpl method asyncSkipEntries.

@Override
public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries, final SkipEntriesCallback callback, Object ctx) {
    log.info("[{}] Skipping {} entries on cursor {}", ledger.getName(), numEntriesToSkip, name);
    long numDeletedMessages = 0;
    if (deletedEntries == IndividualDeletedEntries.Exclude) {
        numDeletedMessages = getNumIndividualDeletedEntriesToSkip(numEntriesToSkip);
    }
    asyncMarkDelete(ledger.getPositionAfterN(markDeletePosition, numEntriesToSkip + numDeletedMessages, PositionBound.startExcluded), new MarkDeleteCallback() {

        @Override
        public void markDeleteComplete(Object ctx) {
            callback.skipEntriesComplete(ctx);
        }

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            if (exception.getCause() instanceof IllegalArgumentException) {
                // There could be a race condition between calling clear backlog and other mark delete
                // operations.
                // If we get an exception it means the backlog was already cleared in the meantime.
                callback.skipEntriesComplete(ctx);
            } else {
                log.error("[{}] Skip {} entries failed for cursor {}", ledger.getName(), numEntriesToSkip, name, exception);
                callback.skipEntriesFailed(exception, ctx);
            }
        }
    }, ctx);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Aggregations

ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)133 Test (org.testng.annotations.Test)70 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)65 CountDownLatch (java.util.concurrent.CountDownLatch)63 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)46 Entry (org.apache.bookkeeper.mledger.Entry)36 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)33 Position (org.apache.bookkeeper.mledger.Position)33 CompletableFuture (java.util.concurrent.CompletableFuture)23 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 List (java.util.List)18 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)18 BKException (org.apache.bookkeeper.client.BKException)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)13 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)13 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13