Search in sources :

Example 1 with LongProperty

use of org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty in project incubator-pulsar by apache.

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
        long lastEntryInLedger = lh.getLastAddConfirmed();
        lh.asyncReadEntries(lastEntryInLedger, lastEntryInLedger, (rc1, lh1, seq, ctx1) -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}} readComplete rc={} entryId={}", ledger.getName(), 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(createManagedLedgerException(rc1));
                return;
            }
            LedgerEntry entry = seq.nextElement();
            PositionInfo positionInfo;
            try {
                positionInfo = PositionInfo.parseFrom(entry.getEntry());
            } catch (InvalidProtocolBufferException e) {
                callback.operationFailed(new ManagedLedgerException(e));
                return;
            }
            Map<String, Long> recoveredProperties = Collections.emptyMap();
            if (positionInfo.getPropertiesCount() > 0) {
                // Recover properties map
                recoveredProperties = Maps.newHashMap();
                for (int i = 0; i < positionInfo.getPropertiesCount(); i++) {
                    LongProperty property = positionInfo.getProperties(i);
                    recoveredProperties.put(property.getName(), property.getValue());
                }
            }
            PositionImpl position = new PositionImpl(positionInfo);
            if (positionInfo.getIndividualDeletedMessagesCount() > 0) {
                recoverIndividualDeletedMessages(positionInfo.getIndividualDeletedMessagesList());
            }
            recoveredCursor(position, recoveredProperties, lh);
            callback.operationComplete();
        }, null);
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ManagedLedgerImpl.createManagedLedgerException(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException) LongProperty(org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PositionInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.PositionInfo)

Example 2 with LongProperty

use of org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty in project incubator-pulsar by apache.

the class ManagedCursorImpl method buildPropertiesMap.

private List<LongProperty> buildPropertiesMap(Map<String, Long> properties) {
    if (properties.isEmpty()) {
        return Collections.emptyList();
    }
    List<LongProperty> longProperties = Lists.newArrayList();
    properties.forEach((name, value) -> {
        LongProperty lp = LongProperty.newBuilder().setName(name).setValue(value).build();
        longProperties.add(lp);
    });
    return longProperties;
}
Also used : LongProperty(org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty)

Example 3 with LongProperty

use of org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty in project incubator-pulsar by apache.

the class ManagedLedgerFactoryImpl method asyncGetManagedLedgerInfo.

@Override
public void asyncGetManagedLedgerInfo(String name, ManagedLedgerInfoCallback callback, Object ctx) {
    store.getManagedLedgerInfo(name, new MetaStoreCallback<MLDataFormats.ManagedLedgerInfo>() {

        @Override
        public void operationComplete(MLDataFormats.ManagedLedgerInfo pbInfo, Stat stat) {
            ManagedLedgerInfo info = new ManagedLedgerInfo();
            info.version = stat.getVersion();
            info.creationDate = DateFormatter.format(stat.getCreationTimestamp());
            info.modificationDate = DateFormatter.format(stat.getModificationTimestamp());
            info.ledgers = new ArrayList<>(pbInfo.getLedgerInfoCount());
            if (pbInfo.hasTerminatedPosition()) {
                info.terminatedPosition = new PositionInfo();
                info.terminatedPosition.ledgerId = pbInfo.getTerminatedPosition().getLedgerId();
                info.terminatedPosition.entryId = pbInfo.getTerminatedPosition().getEntryId();
            }
            for (int i = 0; i < pbInfo.getLedgerInfoCount(); i++) {
                MLDataFormats.ManagedLedgerInfo.LedgerInfo pbLedgerInfo = pbInfo.getLedgerInfo(i);
                LedgerInfo ledgerInfo = new LedgerInfo();
                ledgerInfo.ledgerId = pbLedgerInfo.getLedgerId();
                ledgerInfo.entries = pbLedgerInfo.hasEntries() ? pbLedgerInfo.getEntries() : null;
                ledgerInfo.size = pbLedgerInfo.hasSize() ? pbLedgerInfo.getSize() : null;
                info.ledgers.add(ledgerInfo);
            }
            store.getCursors(name, new MetaStoreCallback<List<String>>() {

                @Override
                public void operationComplete(List<String> cursorsList, Stat stat) {
                    // Get the info for each cursor
                    info.cursors = new ConcurrentSkipListMap<>();
                    List<CompletableFuture<Void>> cursorsFutures = new ArrayList<>();
                    for (String cursorName : cursorsList) {
                        CompletableFuture<Void> cursorFuture = new CompletableFuture<>();
                        cursorsFutures.add(cursorFuture);
                        store.asyncGetCursorInfo(name, cursorName, new MetaStoreCallback<MLDataFormats.ManagedCursorInfo>() {

                            @Override
                            public void operationComplete(ManagedCursorInfo pbCursorInfo, Stat stat) {
                                CursorInfo cursorInfo = new CursorInfo();
                                cursorInfo.version = stat.getVersion();
                                cursorInfo.creationDate = DateFormatter.format(stat.getCreationTimestamp());
                                cursorInfo.modificationDate = DateFormatter.format(stat.getModificationTimestamp());
                                cursorInfo.cursorsLedgerId = pbCursorInfo.getCursorsLedgerId();
                                if (pbCursorInfo.hasMarkDeleteLedgerId()) {
                                    cursorInfo.markDelete = new PositionInfo();
                                    cursorInfo.markDelete.ledgerId = pbCursorInfo.getMarkDeleteLedgerId();
                                    cursorInfo.markDelete.entryId = pbCursorInfo.getMarkDeleteEntryId();
                                }
                                if (pbCursorInfo.getPropertiesCount() > 0) {
                                    cursorInfo.properties = Maps.newTreeMap();
                                    for (int i = 0; i < pbCursorInfo.getPropertiesCount(); i++) {
                                        LongProperty property = pbCursorInfo.getProperties(i);
                                        cursorInfo.properties.put(property.getName(), property.getValue());
                                    }
                                }
                                if (pbCursorInfo.getIndividualDeletedMessagesCount() > 0) {
                                    cursorInfo.individualDeletedMessages = new ArrayList<>();
                                    for (int i = 0; i < pbCursorInfo.getIndividualDeletedMessagesCount(); i++) {
                                        MessageRange range = pbCursorInfo.getIndividualDeletedMessages(i);
                                        MessageRangeInfo rangeInfo = new MessageRangeInfo();
                                        rangeInfo.from.ledgerId = range.getLowerEndpoint().getLedgerId();
                                        rangeInfo.from.entryId = range.getLowerEndpoint().getEntryId();
                                        rangeInfo.to.ledgerId = range.getUpperEndpoint().getLedgerId();
                                        rangeInfo.to.entryId = range.getUpperEndpoint().getEntryId();
                                        cursorInfo.individualDeletedMessages.add(rangeInfo);
                                    }
                                }
                                info.cursors.put(cursorName, cursorInfo);
                                cursorFuture.complete(null);
                            }

                            @Override
                            public void operationFailed(MetaStoreException e) {
                                cursorFuture.completeExceptionally(e);
                            }
                        });
                    }
                    Futures.waitForAll(cursorsFutures).thenRun(() -> {
                        // Completed all the cursors info
                        callback.getInfoComplete(info, ctx);
                    }).exceptionally((ex) -> {
                        callback.getInfoFailed(getManagedLedgerException(ex.getCause()), ctx);
                        return null;
                    });
                }

                @Override
                public void operationFailed(MetaStoreException e) {
                    callback.getInfoFailed(e, ctx);
                }
            });
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            callback.getInfoFailed(e, ctx);
        }
    });
}
Also used : ManagedCursorInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo) CursorInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.CursorInfo) ArrayList(java.util.ArrayList) MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) ManagedLedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.LedgerInfo) ManagedLedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) ManagedCursorInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo) MessageRange(org.apache.bookkeeper.mledger.proto.MLDataFormats.MessageRange) MLDataFormats(org.apache.bookkeeper.mledger.proto.MLDataFormats) LongProperty(org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty) PositionInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.PositionInfo) List(java.util.List) ArrayList(java.util.ArrayList) MessageRangeInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo.MessageRangeInfo)

Aggregations

LongProperty (org.apache.bookkeeper.mledger.proto.MLDataFormats.LongProperty)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 MetaStoreException (org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException)1 ManagedLedgerException.getManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException)1 ManagedLedgerInfo (org.apache.bookkeeper.mledger.ManagedLedgerInfo)1 CursorInfo (org.apache.bookkeeper.mledger.ManagedLedgerInfo.CursorInfo)1 LedgerInfo (org.apache.bookkeeper.mledger.ManagedLedgerInfo.LedgerInfo)1 MessageRangeInfo (org.apache.bookkeeper.mledger.ManagedLedgerInfo.MessageRangeInfo)1 PositionInfo (org.apache.bookkeeper.mledger.ManagedLedgerInfo.PositionInfo)1 ManagedLedgerImpl.createManagedLedgerException (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)1 MetaStoreCallback (org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback)1 Stat (org.apache.bookkeeper.mledger.impl.MetaStore.Stat)1 MLDataFormats (org.apache.bookkeeper.mledger.proto.MLDataFormats)1 ManagedCursorInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedCursorInfo)1 MessageRange (org.apache.bookkeeper.mledger.proto.MLDataFormats.MessageRange)1