Search in sources :

Example 6 with ManagedLedgerFencedException

use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException in project incubator-pulsar by apache.

the class ManagedLedgerImpl method asyncClose.

@Override
public synchronized void asyncClose(final CloseCallback callback, final Object ctx) {
    State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        factory.close(this);
        callback.closeFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Closed) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to close a closed managed ledger", name);
        }
        callback.closeComplete(ctx);
        return;
    }
    log.info("[{}] Closing managed ledger", name);
    factory.close(this);
    STATE_UPDATER.set(this, State.Closed);
    LedgerHandle lh = currentLedger;
    if (lh == null) {
        // No ledger to close, proceed with next step
        closeAllCursors(callback, ctx);
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
    }
    mbean.startDataLedgerCloseOp();
    lh.asyncClose((rc, lh1, ctx1) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
        }
        mbean.endDataLedgerCloseOp();
        if (rc != BKException.Code.OK) {
            callback.closeFailed(createManagedLedgerException(rc), ctx);
            return;
        }
        closeAllCursors(callback, ctx);
    }, null);
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)

Example 7 with ManagedLedgerFencedException

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

the class ManagedLedgerImpl method asyncClose.

@Override
public synchronized void asyncClose(final CloseCallback callback, final Object ctx) {
    State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        factory.close(this);
        callback.closeFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Closed) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to close a closed managed ledger", name);
        }
        callback.closeComplete(ctx);
        return;
    }
    log.info("[{}] Closing managed ledger", name);
    factory.close(this);
    STATE_UPDATER.set(this, State.Closed);
    LedgerHandle lh = currentLedger;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
    }
    mbean.startDataLedgerCloseOp();
    lh.asyncClose((rc, lh1, ctx1) -> {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
        }
        mbean.endDataLedgerCloseOp();
        if (rc != BKException.Code.OK) {
            callback.closeFailed(new ManagedLedgerException(BKException.getMessage(rc)), ctx);
            return;
        }
        // Close all cursors in parallel
        List<CompletableFuture<Void>> futures = Lists.newArrayList();
        for (ManagedCursor cursor : cursors) {
            Futures.CloseFuture closeFuture = new Futures.CloseFuture();
            cursor.asyncClose(closeFuture, null);
            futures.add(closeFuture);
        }
        Futures.waitForAll(futures).thenRun(() -> {
            callback.closeComplete(ctx);
        }).exceptionally(exception -> {
            callback.closeFailed(new ManagedLedgerException(exception), ctx);
            return null;
        });
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CompletableFuture(java.util.concurrent.CompletableFuture) Futures(org.apache.bookkeeper.mledger.util.Futures) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 8 with ManagedLedgerFencedException

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

the class ManagedLedgerImpl method asyncReadEntries.

void asyncReadEntries(OpReadEntry opReadEntry) {
    final State state = STATE_UPDATER.get(this);
    if (state == State.Fenced || state == State.Closed) {
        opReadEntry.readEntriesFailed(new ManagedLedgerFencedException(), opReadEntry.ctx);
        return;
    }
    long ledgerId = opReadEntry.readPosition.getLedgerId();
    LedgerHandle currentLedger = this.currentLedger;
    if (ledgerId == currentLedger.getId()) {
        // Current writing ledger is not in the cache (since we don't want
        // it to be automatically evicted), and we cannot use 2 different
        // ledger handles (read & write)for the same ledger.
        internalReadFromLedger(currentLedger, opReadEntry);
    } else {
        LedgerInfo ledgerInfo = ledgers.get(ledgerId);
        if (ledgerInfo == null || ledgerInfo.getEntries() == 0) {
            // Cursor is pointing to a empty ledger, there's no need to try opening it. Skip this ledger and
            // move to the next one
            opReadEntry.updateReadPosition(new PositionImpl(opReadEntry.readPosition.getLedgerId() + 1, 0));
            opReadEntry.checkReadCompletion();
            return;
        }
        // Get a ledger handle to read from
        getLedgerHandle(ledgerId).thenAccept(ledger -> {
            internalReadFromLedger(ledger, opReadEntry);
        }).exceptionally(ex -> {
            log.error("[{}] Error opening ledger for reading at position {} - {}", name, opReadEntry.readPosition, ex.getMessage());
            opReadEntry.readEntriesFailed(new ManagedLedgerException(ex), opReadEntry.ctx);
            return null;
        });
    }
}
Also used : ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) 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) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) 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) Commands(com.yahoo.pulsar.common.api.Commands) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) 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) BoundType(com.google.common.collect.BoundType) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) ConcurrentLongHashMap(com.yahoo.pulsar.common.util.collections.ConcurrentLongHashMap) 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) RateLimiter(com.google.common.util.concurrent.RateLimiter) 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) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) java.util.concurrent.atomic(java.util.concurrent.atomic) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) MessageMetadata(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CallbackMutex(org.apache.bookkeeper.mledger.util.CallbackMutex) VoidCallback(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.VoidCallback) UnboundArrayBlockingQueue(org.apache.bookkeeper.util.UnboundArrayBlockingQueue) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)

Example 9 with ManagedLedgerFencedException

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

the class ManagedLedgerErrorsTest method recoverAfterZnodeVersionError.

@Test
public void recoverAfterZnodeVersionError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    zkc.failNow(Code.BADVERSION);
    // First write will succeed
    ledger.addEntry("test".getBytes());
    try {
        // This write will try to create a ledger and it will fail at it
        ledger.addEntry("entry".getBytes());
        fail("should fail");
    } catch (BadVersionException e) {
    // ok
    }
    try {
        // At this point the ledger should be fenced for good
        ledger.addEntry("entry".getBytes());
        fail("should fail");
    } catch (ManagedLedgerFencedException e) {
    // ok
    }
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) BadVersionException(org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 10 with ManagedLedgerFencedException

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

the class PersistentTopic method addFailed.

@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
    PublishCallback callback = (PublishCallback) ctx;
    log.error("[{}] Failed to persist msg in store: {}", topic, exception.getMessage());
    callback.completed(new PersistenceException(exception), -1, -1);
    if (exception instanceof ManagedLedgerFencedException) {
        // If the managed ledger has been fenced, we cannot continue using it. We need to close and reopen
        close();
    }
}
Also used : PersistenceException(com.yahoo.pulsar.broker.service.BrokerServiceException.PersistenceException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)

Aggregations

ManagedLedgerFencedException (org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)12 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)6 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)6 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)5 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)5 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)4 BadVersionException (org.apache.bookkeeper.mledger.ManagedLedgerException.BadVersionException)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 Futures (org.apache.bookkeeper.mledger.util.Futures)3 Test (org.testng.annotations.Test)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 BoundType (com.google.common.collect.BoundType)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2 Queues (com.google.common.collect.Queues)2 Range (com.google.common.collect.Range)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)2 ByteBuf (io.netty.buffer.ByteBuf)2 Unpooled (io.netty.buffer.Unpooled)2 Math.min (java.lang.Math.min)2