Search in sources :

Example 6 with ReadEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.

the class ManagedLedgerImpl method asyncReadEntry.

void asyncReadEntry(PositionImpl position, ReadEntryCallback callback, Object ctx) {
    LedgerHandle currentLedger = this.currentLedger;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Reading entry ledger {}: {}", name, position.getLedgerId(), position.getEntryId());
    }
    if (position.getLedgerId() == currentLedger.getId()) {
        LedgerHandle ledger = currentLedger;
        entryCache.asyncReadEntry(ledger, position, callback, ctx);
    } else {
        getLedgerHandle(position.getLedgerId()).thenAccept(ledger -> {
            entryCache.asyncReadEntry(ledger, position, callback, ctx);
        }).exceptionally(ex -> {
            log.error("[{}] Error opening ledger for reading at position {} - {}", name, position, ex.getMessage());
            callback.readEntryFailed(ManagedLedgerException.getManagedLedgerException(ex.getCause()), ctx);
            return null;
        });
    }
}
Also used : OpenCallback(org.apache.bookkeeper.client.AsyncCallback.OpenCallback) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) MetaStoreCallback(org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback) Pair(org.apache.bookkeeper.mledger.util.Pair) Unpooled(io.netty.buffer.Unpooled) GrowableArrayBlockingQueue(org.apache.pulsar.common.util.collections.GrowableArrayBlockingQueue) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Map(java.util.Map) ManagedLedgerMXBean(org.apache.bookkeeper.mledger.ManagedLedgerMXBean) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) Commands(org.apache.pulsar.common.api.Commands) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) ManagedLedgerAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerAlreadyClosedException) NestedPositionInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.NestedPositionInfo) AtomicReferenceFieldUpdater(java.util.concurrent.atomic.AtomicReferenceFieldUpdater) Range(com.google.common.collect.Range) Futures(org.apache.bookkeeper.mledger.util.Futures) Math.min(java.lang.Math.min) Position(org.apache.bookkeeper.mledger.Position) NavigableMap(java.util.NavigableMap) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Queues(com.google.common.collect.Queues) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) NonRecoverableLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.NonRecoverableLedgerException) BoundType(com.google.common.collect.BoundType) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) ManagedLedgerTerminatedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerTerminatedException) Queue(java.util.Queue) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) CreateCallback(org.apache.bookkeeper.client.AsyncCallback.CreateCallback) DeleteLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteLedgerCallback) Entry(org.apache.bookkeeper.mledger.Entry) CompletableFuture(java.util.concurrent.CompletableFuture) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) RateLimiter(com.google.common.util.concurrent.RateLimiter) TerminateCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.TerminateCallback) SafeRun.safeRun(org.apache.bookkeeper.mledger.util.SafeRun.safeRun) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) Maps(com.google.common.collect.Maps) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) TimeUnit(java.util.concurrent.TimeUnit) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ConcurrentLongHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongHashMap) CallbackMutex(org.apache.bookkeeper.mledger.util.CallbackMutex) VoidCallback(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.VoidCallback) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) Collections(java.util.Collections) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle)

Example 7 with ReadEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.

the class ManagedLedgerImpl method checkBackloggedCursors.

@Override
public void checkBackloggedCursors() {
    // activate caught up cursors
    cursors.forEach(cursor -> {
        if (cursor.getNumberOfEntries() < maxActiveCursorBacklogEntries) {
            cursor.setActive();
        }
    });
    // deactivate backlog cursors
    Iterator<ManagedCursor> cursors = activeCursors.iterator();
    while (cursors.hasNext()) {
        ManagedCursor cursor = cursors.next();
        long backlogEntries = cursor.getNumberOfEntries();
        if (backlogEntries > maxActiveCursorBacklogEntries) {
            PositionImpl readPosition = (PositionImpl) cursor.getReadPosition();
            readPosition = isValidPosition(readPosition) ? readPosition : getNextValidPosition(readPosition);
            if (readPosition == null) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Couldn't find valid read position [{}] {}", name, cursor.getName(), cursor.getReadPosition());
                }
                continue;
            }
            try {
                asyncReadEntry(readPosition, new ReadEntryCallback() {

                    @Override
                    public void readEntryFailed(ManagedLedgerException e, Object ctx) {
                        log.warn("[{}] Failed while reading entries on [{}] {}", name, cursor.getName(), e.getMessage(), e);
                    }

                    @Override
                    public void readEntryComplete(Entry entry, Object ctx) {
                        MessageMetadata msgMetadata = null;
                        try {
                            msgMetadata = Commands.parseMessageMetadata(entry.getDataBuffer());
                            long msgTimeSincePublish = (System.currentTimeMillis() - msgMetadata.getPublishTime());
                            if (msgTimeSincePublish > maxMessageCacheRetentionTimeMillis) {
                                cursor.setInactive();
                            }
                        } finally {
                            if (msgMetadata != null) {
                                msgMetadata.recycle();
                            }
                            entry.release();
                        }
                    }
                }, null);
            } catch (Exception e) {
                log.warn("[{}] Failed while reading entries from cache on [{}] {}", name, cursor.getName(), e.getMessage(), e);
            }
        }
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) ManagedLedgerAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerAlreadyClosedException) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) NonRecoverableLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.NonRecoverableLedgerException) ManagedLedgerTerminatedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerTerminatedException) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 8 with ReadEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.

the class ManagedCursorImpl method getNthEntry.

@Override
public Entry getNthEntry(int n, IndividualDeletedEntries deletedEntries) throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedLedgerException exception = null;

        Entry entry = null;
    }
    final Result result = new Result();
    asyncGetNthEntry(n, deletedEntries, new ReadEntryCallback() {

        @Override
        public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }

        @Override
        public void readEntryComplete(Entry entry, Object ctx) {
            result.entry = entry;
            counter.countDown();
        }
    }, null);
    counter.await();
    if (result.exception != null) {
        throw result.exception;
    }
    return result.entry;
}
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) Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with ReadEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.

the class PersistentReplicator method peekNthMessage.

public CompletableFuture<Entry> peekNthMessage(int messagePosition) {
    CompletableFuture<Entry> future = new CompletableFuture<>();
    if (log.isDebugEnabled()) {
        log.debug("[{}][{} -> {}] Getting message at position {}", topicName, localCluster, remoteCluster, messagePosition);
    }
    cursor.asyncGetNthEntry(messagePosition, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {

        @Override
        public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
            future.completeExceptionally(exception);
        }

        @Override
        public void readEntryComplete(Entry entry, Object ctx) {
            future.complete(entry);
        }
    }, null);
    return future;
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback)

Example 10 with ReadEntryCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback in project incubator-pulsar by apache.

the class PersistentSubscription method peekNthMessage.

@Override
public CompletableFuture<Entry> peekNthMessage(int messagePosition) {
    CompletableFuture<Entry> future = new CompletableFuture<>();
    if (log.isDebugEnabled()) {
        log.debug("[{}][{}] Getting message at position {}", topicName, subName, messagePosition);
    }
    cursor.asyncGetNthEntry(messagePosition, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {

        @Override
        public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
            future.completeExceptionally(exception);
        }

        @Override
        public void readEntryComplete(Entry entry, Object ctx) {
            future.complete(entry);
        }
    }, null);
    return future;
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback)

Aggregations

ReadEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback)15 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)15 Entry (org.apache.bookkeeper.mledger.Entry)14 CountDownLatch (java.util.concurrent.CountDownLatch)8 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)8 BKException (org.apache.bookkeeper.client.BKException)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 MetaStoreException (org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)5 Lists (com.google.common.collect.Lists)5 List (java.util.List)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)5 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)5 Position (org.apache.bookkeeper.mledger.Position)5 Pair (org.apache.bookkeeper.mledger.util.Pair)5 SafeRun.safeRun (org.apache.bookkeeper.mledger.util.SafeRun.safeRun)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Range (com.google.common.collect.Range)4 RateLimiter (com.google.common.util.concurrent.RateLimiter)4