Search in sources :

Example 1 with ManagedLedgerFencedException

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

the class ManagedLedgerImpl method asyncAddEntry.

@Override
public synchronized void asyncAddEntry(ByteBuf buffer, AddEntryCallback callback, Object ctx) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] asyncAddEntry size={} state={}", name, buffer.readableBytes(), state);
    }
    final State state = STATE_UPDATER.get(this);
    if (state == State.Fenced) {
        callback.addFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Closed) {
        callback.addFailed(new ManagedLedgerException("Managed ledger was already closed"), ctx);
        return;
    }
    OpAddEntry addOperation = OpAddEntry.create(this, buffer, callback, ctx);
    pendingAddEntries.add(addOperation);
    if (state == State.ClosingLedger || state == State.CreatingLedger) {
        // We are waiting for a new ledger to be created
        if (log.isDebugEnabled()) {
            log.debug("[{}] Queue addEntry request", name);
        }
    } else if (state == State.ClosedLedger) {
        long now = System.currentTimeMillis();
        if (now < lastLedgerCreationFailureTimestamp + WaitTimeAfterLedgerCreationFailureMs) {
            // Deny the write request, since we haven't waited enough time since last attempt to create a new ledger
            pendingAddEntries.remove(addOperation);
            callback.addFailed(new ManagedLedgerException("Waiting for new ledger creation to complete"), ctx);
            return;
        }
        // No ledger and no pending operations. Create a new ledger
        if (log.isDebugEnabled()) {
            log.debug("[{}] Creating a new ledger", name);
        }
        if (STATE_UPDATER.compareAndSet(this, State.ClosedLedger, State.CreatingLedger)) {
            this.lastLedgerCreationInitiationTimestamp = System.nanoTime();
            mbean.startDataLedgerCreateOp();
            bookKeeper.asyncCreateLedger(config.getEnsembleSize(), config.getWriteQuorumSize(), config.getAckQuorumSize(), config.getDigestType(), config.getPassword(), this, ctx);
        }
    } else {
        checkArgument(state == State.LedgerOpened);
        // Write into lastLedger
        addOperation.setLedger(currentLedger);
        ++currentLedgerEntries;
        currentLedgerSize += buffer.readableBytes();
        if (log.isDebugEnabled()) {
            log.debug("[{}] Write into current ledger lh={} entries={}", name, currentLedger.getId(), currentLedgerEntries);
        }
        if (currentLedgerIsFull()) {
            if (log.isDebugEnabled()) {
                log.debug("[{}] Closing current ledger lh={}", name, currentLedger.getId());
            }
            // This entry will be the last added to current ledger
            addOperation.setCloseWhenDone(true);
            STATE_UPDATER.set(this, State.ClosingLedger);
        }
        addOperation.initiate();
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException)

Example 2 with ManagedLedgerFencedException

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

the class ManagedLedgerTest method fenceManagedLedger.

@Test(enabled = false)
public void fenceManagedLedger() throws Exception {
    ManagedLedgerFactory factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedger ledger1 = factory1.open("my_test_ledger");
    ManagedCursor cursor1 = ledger1.openCursor("c1");
    ledger1.addEntry("entry-1".getBytes(Encoding));
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedger ledger2 = factory2.open("my_test_ledger");
    ManagedCursor cursor2 = ledger2.openCursor("c1");
    // At this point ledger1 must have been fenced
    try {
        ledger1.addEntry("entry-1".getBytes(Encoding));
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    try {
        ledger1.addEntry("entry-2".getBytes(Encoding));
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    try {
        cursor1.readEntries(10);
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    try {
        ledger1.openCursor("new cursor");
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    ledger2.addEntry("entry-2".getBytes(Encoding));
    assertEquals(cursor2.getNumberOfEntries(), 2);
    factory1.shutdown();
    factory2.shutdown();
}
Also used : ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 3 with ManagedLedgerFencedException

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

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 4 with ManagedLedgerFencedException

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

the class ManagedLedgerTest method fenceManagedLedger.

@Test(enabled = false)
public void fenceManagedLedger() throws Exception {
    ManagedLedgerFactory factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedger ledger1 = factory1.open("my_test_ledger");
    ManagedCursor cursor1 = ledger1.openCursor("c1");
    ledger1.addEntry("entry-1".getBytes(Encoding));
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedger ledger2 = factory2.open("my_test_ledger");
    ManagedCursor cursor2 = ledger2.openCursor("c1");
    // At this point ledger1 must have been fenced
    try {
        ledger1.addEntry("entry-1".getBytes(Encoding));
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    try {
        ledger1.addEntry("entry-2".getBytes(Encoding));
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    try {
        cursor1.readEntries(10);
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    try {
        ledger1.openCursor("new cursor");
        fail("Expecting exception");
    } catch (ManagedLedgerFencedException e) {
    }
    ledger2.addEntry("entry-2".getBytes(Encoding));
    assertEquals(cursor2.getNumberOfEntries(), 2);
    factory1.shutdown();
    factory2.shutdown();
}
Also used : ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFencedException(org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 5 with ManagedLedgerFencedException

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

the class ManagedLedgerImpl method asyncTerminate.

@Override
public synchronized void asyncTerminate(TerminateCallback callback, Object ctx) {
    if (state == State.Fenced) {
        callback.terminateFailed(new ManagedLedgerFencedException(), ctx);
        return;
    } else if (state == State.Terminated) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Ignoring request to terminate an already terminated managed ledger", name);
        }
        callback.terminateComplete(lastConfirmedEntry, ctx);
        return;
    }
    log.info("[{}] Terminating managed ledger", name);
    state = State.Terminated;
    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.terminateFailed(createManagedLedgerException(rc), ctx);
        } else {
            lastConfirmedEntry = new PositionImpl(lh.getId(), lh.getLastAddConfirmed());
            // Store the new state in metadata
            store.asyncUpdateLedgerIds(name, getManagedLedgerInfo(), ledgersStat, new MetaStoreCallback<Void>() {

                @Override
                public void operationComplete(Void result, Stat stat) {
                    ledgersStat = stat;
                    log.info("[{}] Terminated managed ledger at {}", name, lastConfirmedEntry);
                    callback.terminateComplete(lastConfirmedEntry, ctx);
                }

                @Override
                public void operationFailed(MetaStoreException e) {
                    log.error("[{}] Failed to terminate managed ledger: {}", name, e.getMessage());
                    callback.terminateFailed(new ManagedLedgerException(e), ctx);
                }
            });
        }
    }, null);
}
Also used : MetaStoreException(org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Stat(org.apache.bookkeeper.mledger.impl.MetaStore.Stat) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) 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