Search in sources :

Example 1 with PositionInfo

use of org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo 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 2 with PositionInfo

use of org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo 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)

Aggregations

ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 PositionInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)1