Search in sources :

Example 21 with ManagedLedgerException

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

the class ManagedCursorImpl method asyncMarkDelete.

@Override
public void asyncMarkDelete(final Position position, final MarkDeleteCallback callback, final Object ctx) {
    checkNotNull(position);
    checkArgument(position instanceof PositionImpl);
    if (STATE_UPDATER.get(this) == State.Closed) {
        callback.markDeleteFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
        return;
    }
    if (RESET_CURSOR_IN_PROGRESS_UPDATER.get(this) == TRUE) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] cursor reset in progress - ignoring mark delete on position [{}] for cursor [{}]", ledger.getName(), (PositionImpl) position, name);
        }
        callback.markDeleteFailed(new ManagedLedgerException("Reset cursor in progress - unable to mark delete position " + ((PositionImpl) position).toString()), ctx);
    }
    if (log.isDebugEnabled()) {
        log.debug("[{}] Mark delete cursor {} up to position: {}", ledger.getName(), name, position);
    }
    PositionImpl newPosition = (PositionImpl) position;
    lock.writeLock().lock();
    try {
        newPosition = setAcknowledgedPosition(newPosition);
    } catch (IllegalArgumentException e) {
        callback.markDeleteFailed(new ManagedLedgerException(e), ctx);
        return;
    } finally {
        lock.writeLock().unlock();
    }
    // Apply rate limiting to mark-delete operations
    if (markDeleteLimiter != null && !markDeleteLimiter.tryAcquire()) {
        callback.markDeleteComplete(ctx);
        return;
    }
    internalAsyncMarkDelete(newPosition, callback, ctx);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 22 with ManagedLedgerException

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

the class ManagedCursorImpl method findNewestMatching.

@Override
public Position findNewestMatching(Predicate<Entry> condition) throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedLedgerException exception = null;

        Position position = null;
    }
    final Result result = new Result();
    asyncFindNewestMatching(FindPositionConstraint.SearchActiveEntries, condition, new FindEntryCallback() {

        @Override
        public void findEntryComplete(Position position, Object ctx) {
            result.position = position;
            counter.countDown();
        }

        @Override
        public void findEntryFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    counter.await();
    if (result.exception != null)
        throw result.exception;
    return result.position;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) FindEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.FindEntryCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 23 with ManagedLedgerException

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

the class ManagedLedgerFactoryImpl method asyncOpen.

@Override
public void asyncOpen(final String name, final ManagedLedgerConfig config, final OpenLedgerCallback callback, final Object ctx) {
    // If the ledger state is bad, remove it from the map.
    CompletableFuture<ManagedLedgerImpl> existingFuture = ledgers.get(name);
    if (existingFuture != null && existingFuture.isDone()) {
        try {
            ManagedLedgerImpl l = existingFuture.get();
            if (l.getState().equals(State.Fenced.toString()) || l.getState().equals(State.Closed.toString())) {
                // Managed ledger is in unusable state. Recreate it.
                log.warn("[{}] Attempted to open ledger in {} state. Removing from the map to recreate it", name, l.getState());
                ledgers.remove(name, existingFuture);
            }
        } catch (Exception e) {
            // Unable to get the future
            log.warn("[{}] Got exception while trying to retrieve ledger", name, e);
        }
    }
    // Ensure only one managed ledger is created and initialized
    ledgers.computeIfAbsent(name, (mlName) -> {
        // Create the managed ledger
        CompletableFuture<ManagedLedgerImpl> future = new CompletableFuture<>();
        final ManagedLedgerImpl newledger = new ManagedLedgerImpl(this, bookKeeper, store, config, executor, orderedExecutor, name);
        newledger.initialize(new ManagedLedgerInitializeLedgerCallback() {

            @Override
            public void initializeComplete() {
                future.complete(newledger);
            }

            @Override
            public void initializeFailed(ManagedLedgerException e) {
                // Clean the map if initialization fails
                ledgers.remove(name, future);
                future.completeExceptionally(e);
            }
        }, null);
        return future;
    }).thenAccept(ml -> {
        callback.openLedgerComplete(ml, ctx);
    }).exceptionally(exception -> {
        callback.openLedgerFailed((ManagedLedgerException) exception.getCause(), ctx);
        return null;
    });
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerFactoryMXBean(org.apache.bookkeeper.mledger.ManagedLedgerFactoryMXBean) State(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.State) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) States(org.apache.zookeeper.ZooKeeper.States) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Predicates(com.google.common.base.Predicates) ZooKeeper(org.apache.zookeeper.ZooKeeper) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) Logger(org.slf4j.Logger) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Watcher(org.apache.zookeeper.Watcher) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BookKeeper(org.apache.bookkeeper.client.BookKeeper) Maps(com.google.common.collect.Maps) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerInitializeLedgerCallback(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.ManagedLedgerInitializeLedgerCallback) BKException(org.apache.bookkeeper.client.BKException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 24 with ManagedLedgerException

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

the class ManagedLedgerFactoryImpl method open.

@Override
public ManagedLedger open(String name, ManagedLedgerConfig config) throws InterruptedException, ManagedLedgerException {
    class Result {

        ManagedLedger l = null;

        ManagedLedgerException e = null;
    }
    final Result r = new Result();
    final CountDownLatch latch = new CountDownLatch(1);
    asyncOpen(name, config, new OpenLedgerCallback() {

        @Override
        public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
            r.l = ledger;
            latch.countDown();
        }

        @Override
        public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
            r.e = exception;
            latch.countDown();
        }
    }, null);
    latch.await();
    if (r.e != null) {
        throw r.e;
    }
    return r.l;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)

Example 25 with ManagedLedgerException

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

the class ManagedLedgerImpl method openCursor.

@Override
public ManagedCursor openCursor(String cursorName) throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedCursor cursor = null;

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    asyncOpenCursor(cursorName, new OpenCursorCallback() {

        @Override
        public void openCursorComplete(ManagedCursor cursor, Object ctx) {
            result.cursor = cursor;
            counter.countDown();
        }

        @Override
        public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during open-cursor operation");
    }
    if (result.exception != null) {
        log.error("Error adding entry", result.exception);
        throw result.exception;
    }
    return result.cursor;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

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