Search in sources :

Example 86 with ManagedLedgerException

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

the class ManagedCursorImpl method asyncReadEntriesOrWait.

@Override
public void asyncReadEntriesOrWait(int numberOfEntriesToRead, ReadEntriesCallback callback, Object ctx) {
    checkArgument(numberOfEntriesToRead > 0);
    if (STATE_UPDATER.get(this) == State.Closed) {
        callback.readEntriesFailed(new CursorAlreadyClosedException("Cursor was already closed"), ctx);
        return;
    }
    if (hasMoreEntries()) {
        // If we have available entries, we can read them immediately
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] Read entries immediately", ledger.getName(), name);
        }
        asyncReadEntries(numberOfEntriesToRead, callback, ctx);
    } else {
        OpReadEntry op = OpReadEntry.create(this, PositionImpl.get(readPosition), numberOfEntriesToRead, callback, ctx);
        if (!WAITING_READ_OP_UPDATER.compareAndSet(this, null, op)) {
            callback.readEntriesFailed(new ManagedLedgerException("We can only have a single waiting callback"), ctx);
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] Deferring retry of read at position {}", ledger.getName(), name, op.readPosition);
        }
        // Check again for new entries again in 10ms, then if still no entries are available register to be notified
        ledger.getScheduledExecutor().schedule(safeRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] Re-trying the read at position {}", ledger.getName(), name, op.readPosition);
            }
            if (!hasMoreEntries()) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] [{}] Still no entries available. Register for notification", ledger.getName(), name);
                }
                // Let the managed ledger know we want to be notified whenever a new entry is published
                ledger.waitingCursors.add(this);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] [{}] Skip notification registering since we do have entries available", ledger.getName(), name);
                }
            }
            // checked and the time we've asked to be notified by managed ledger
            if (hasMoreEntries()) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] [{}] Found more entries", ledger.getName(), name);
                }
                // Try to cancel the notification request
                if (WAITING_READ_OP_UPDATER.compareAndSet(this, op, null)) {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] [{}] Cancelled notification and scheduled read at {}", ledger.getName(), name, op.readPosition);
                    }
                    PENDING_READ_OPS_UPDATER.incrementAndGet(this);
                    ledger.asyncReadEntries(op);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] [{}] notification was already cancelled", ledger.getName(), name);
                    }
                }
            }
        }), 10, TimeUnit.MILLISECONDS);
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException)

Example 87 with ManagedLedgerException

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

the class ManagedCursorImpl method asyncGetNthEntry.

@Override
public void asyncGetNthEntry(int N, IndividualDeletedEntries deletedEntries, ReadEntryCallback callback, Object ctx) {
    checkArgument(N > 0);
    if (STATE_UPDATER.get(this) == State.Closed) {
        callback.readEntryFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
        return;
    }
    PositionImpl startPosition = ledger.getNextValidPosition(markDeletePosition);
    PositionImpl endPosition = ledger.getLastPosition();
    if (startPosition.compareTo(endPosition) <= 0) {
        long numOfEntries = getNumberOfEntries(Range.closed(startPosition, endPosition));
        if (numOfEntries >= N) {
            long deletedMessages = 0;
            if (deletedEntries == IndividualDeletedEntries.Exclude) {
                deletedMessages = getNumIndividualDeletedEntriesToSkip(N);
            }
            PositionImpl positionAfterN = ledger.getPositionAfterN(markDeletePosition, N + deletedMessages, PositionBound.startExcluded);
            ledger.asyncReadEntry(positionAfterN, callback, ctx);
        } else {
            callback.readEntryComplete(null, ctx);
        }
    } else {
        callback.readEntryComplete(null, ctx);
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 88 with ManagedLedgerException

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

the class ManagedCursorImpl method internalAsyncMarkDelete.

private void internalAsyncMarkDelete(final PositionImpl newPosition, final MarkDeleteCallback callback, final Object ctx) {
    ledger.mbean.addMarkDeleteOp();
    PendingMarkDeleteEntry mdEntry = new PendingMarkDeleteEntry(newPosition, callback, ctx);
    // We cannot write to the ledger during the switch, need to wait until the new metadata ledger is available
    synchronized (pendingMarkDeleteOps) {
        // The state might have changed while we were waiting on the queue mutex
        switch(STATE_UPDATER.get(this)) {
            case Closed:
                callback.markDeleteFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
                return;
            case NoLedger:
                // We need to create a new ledger to write into
                startCreatingNewMetadataLedger();
            // fall through
            case SwitchingLedger:
                pendingMarkDeleteOps.add(mdEntry);
                break;
            case Open:
                if (PENDING_READ_OPS_UPDATER.get(this) > 0) {
                    // Wait until no read operation are pending
                    pendingMarkDeleteOps.add(mdEntry);
                } else {
                    // Execute the mark delete immediately
                    internalMarkDelete(mdEntry);
                }
                break;
            default:
                log.error("[{}][{}] Invalid cursor state: {}", ledger.getName(), name, state);
                callback.markDeleteFailed(new ManagedLedgerException("Cursor was in invalid state: " + state), ctx);
                break;
        }
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 89 with ManagedLedgerException

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

the class ManagedCursorImpl method readEntries.

@Override
public List<Entry> readEntries(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();
    asyncReadEntries(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)

Example 90 with ManagedLedgerException

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

the class ManagedCursorImpl method resetCursor.

@Override
public void resetCursor(Position newPos) throws ManagedLedgerException, InterruptedException {
    class Result {

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    final CountDownLatch counter = new CountDownLatch(1);
    asyncResetCursor(newPos, new AsyncCallbacks.ResetCursorCallback() {

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

        @Override
        public void resetFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    });
    if (!counter.await(ManagedLedgerImpl.AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        if (result.exception != null) {
            log.warn("[{}] Reset cursor to {} on cursor {} timed out with exception {}", ledger.getName(), newPos, name, result.exception);
        }
        throw new ManagedLedgerException("Timeout during reset cursor");
    }
    if (result.exception != null) {
        throw result.exception;
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) 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