Search in sources :

Example 91 with ManagedLedgerException

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

the class ManagedCursorImpl method asyncDelete.

@Override
public void asyncDelete(Position pos, final AsyncCallbacks.DeleteCallback callback, Object ctx) {
    checkArgument(pos instanceof PositionImpl);
    if (STATE_UPDATER.get(this) == State.Closed) {
        callback.deleteFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
        return;
    }
    PositionImpl position = (PositionImpl) pos;
    PositionImpl previousPosition = ledger.getPreviousPosition(position);
    PositionImpl newMarkDeletePosition = null;
    lock.writeLock().lock();
    try {
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] Deleting single message at {}. Current status: {} - md-position: {}  - previous-position: {}", ledger.getName(), name, pos, individualDeletedMessages, markDeletePosition, previousPosition);
        }
        if (individualDeletedMessages.contains(position) || position.compareTo(markDeletePosition) <= 0) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] Position was already deleted {}", ledger.getName(), name, position);
            }
            callback.deleteComplete(ctx);
            return;
        }
        if (previousPosition.compareTo(markDeletePosition) == 0 && individualDeletedMessages.isEmpty()) {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{}] Immediately mark-delete to position {}", ledger.getName(), name, position);
            }
            newMarkDeletePosition = position;
        } else {
            // Add a range (prev, pos] to the set. Adding the previous entry as an open limit to the range will make
            // the RangeSet recognize the "continuity" between adjacent Positions
            individualDeletedMessages.add(Range.openClosed(previousPosition, position));
            ++messagesConsumedCounter;
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] Individually deleted messages: {}", ledger.getName(), name, individualDeletedMessages);
            }
            // If the lower bound of the range set is the current mark delete position, then we can trigger a new
            // mark
            // delete to the upper bound of the first range segment
            Range<PositionImpl> range = individualDeletedMessages.asRanges().iterator().next();
            // If the lowerBound is ahead of MarkDelete, verify if there are any entries in-between
            if (range.lowerEndpoint().compareTo(markDeletePosition) <= 0 || ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, range.lowerEndpoint())) <= 0) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Found a position range to mark delete for cursor {}: {} ", ledger.getName(), name, range);
                }
                newMarkDeletePosition = range.upperEndpoint();
            }
        }
        if (newMarkDeletePosition != null) {
            newMarkDeletePosition = setAcknowledgedPosition(newMarkDeletePosition);
        } else {
            newMarkDeletePosition = markDeletePosition;
        }
    } catch (Exception e) {
        log.warn("[{}] [{}] Error while updating individualDeletedMessages [{}]", ledger.getName(), name, e.getMessage(), e);
        callback.deleteFailed(new ManagedLedgerException(e), ctx);
        return;
    } finally {
        lock.writeLock().unlock();
    }
    // Apply rate limiting to mark-delete operations
    if (markDeleteLimiter != null && !markDeleteLimiter.tryAcquire()) {
        callback.deleteComplete(ctx);
        return;
    }
    try {
        internalAsyncMarkDelete(newMarkDeletePosition, new MarkDeleteCallback() {

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

            @Override
            public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                callback.deleteFailed(exception, ctx);
            }
        }, ctx);
    } catch (Exception e) {
        log.warn("[{}] [{}] Error doing asyncDelete [{}]", ledger.getName(), name, e.getMessage(), e);
        if (log.isDebugEnabled()) {
            log.debug("[{}] Consumer {} cursor asyncDelete error, counters: consumed {} mdPos {} rdPos {}", ledger.getName(), name, messagesConsumedCounter, markDeletePosition, readPosition);
        }
        callback.deleteFailed(new ManagedLedgerException(e), ctx);
    }
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException)

Example 92 with ManagedLedgerException

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

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) 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 93 with ManagedLedgerException

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

the class EntryCacheImpl method asyncReadEntry.

@Override
public void asyncReadEntry(LedgerHandle lh, PositionImpl position, final ReadEntryCallback callback, final Object ctx) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] Reading entry ledger {}: {}", ml.getName(), lh.getId(), position.getEntryId());
    }
    EntryImpl entry = entries.get(position);
    if (entry != null) {
        EntryImpl cachedEntry = new EntryImpl(entry);
        entry.release();
        manager.mlFactoryMBean.recordCacheHit(cachedEntry.getLength());
        callback.readEntryComplete(cachedEntry, ctx);
    } else {
        ReadCallback readCallback = (rc, ledgerHandle, sequence, obj) -> {
            if (rc != BKException.Code.OK) {
                ml.invalidateLedgerHandle(ledgerHandle, rc);
                callback.readEntryFailed(new ManagedLedgerException(BKException.create(rc)), obj);
                return;
            }
            if (sequence.hasMoreElements()) {
                EntryImpl returnEntry = new EntryImpl(sequence.nextElement());
                manager.mlFactoryMBean.recordCacheMiss(1, returnEntry.getLength());
                ml.mbean.addReadEntriesSample(1, returnEntry.getLength());
                callback.readEntryComplete(returnEntry, obj);
            } else {
                // got an empty sequence
                callback.readEntryFailed(new ManagedLedgerException("Could not read given position"), obj);
            }
        };
        lh.asyncReadEntries(position.getEntryId(), position.getEntryId(), readCallback, ctx);
    }
}
Also used : Longs(com.google.common.primitives.Longs) Logger(org.slf4j.Logger) Weighter(org.apache.bookkeeper.mledger.util.RangeCache.Weighter) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Pair(org.apache.bookkeeper.mledger.util.Pair) BKException(org.apache.bookkeeper.client.BKException) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SafeRun.safeRun(org.apache.bookkeeper.mledger.util.SafeRun.safeRun) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) List(java.util.List) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) RangeCache(org.apache.bookkeeper.mledger.util.RangeCache) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback)

Example 94 with ManagedLedgerException

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

the class ManagedCursorImpl method asyncReadEntries.

@Override
public void asyncReadEntries(final int numberOfEntriesToRead, final ReadEntriesCallback callback, final Object ctx) {
    checkArgument(numberOfEntriesToRead > 0);
    if (STATE_UPDATER.get(this) == State.Closed) {
        callback.readEntriesFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
        return;
    }
    PENDING_READ_OPS_UPDATER.incrementAndGet(this);
    OpReadEntry op = OpReadEntry.create(this, PositionImpl.get(readPosition), numberOfEntriesToRead, callback, ctx);
    ledger.asyncReadEntries(op);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 95 with ManagedLedgerException

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

the class ManagedCursorImpl method readEntriesOrWait.

@Override
public List<Entry> readEntriesOrWait(int numberOfEntriesToRead) throws InterruptedException, ManagedLedgerException {
    checkArgument(numberOfEntriesToRead > 0);
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedLedgerException exception = null;

        List<Entry> entries = null;
    }
    final Result result = new Result();
    asyncReadEntriesOrWait(numberOfEntriesToRead, new ReadEntriesCallback() {

        @Override
        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            result.entries = entries;
            counter.countDown();
        }

        @Override
        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    counter.await();
    if (result.exception != null)
        throw result.exception;
    return result.entries;
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch)

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