Search in sources :

Example 51 with Position

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

the class ManagedLedgerBkTest method testResetCursorAfterRecovery.

@Test(timeOut = 20000)
void testResetCursorAfterRecovery() throws Exception {
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    ManagedLedgerConfig conf = new ManagedLedgerConfig().setMaxEntriesPerLedger(10).setEnsembleSize(1).setWriteQuorumSize(1).setAckQuorumSize(1).setMetadataEnsembleSize(1).setMetadataWriteQuorumSize(1).setMetadataAckQuorumSize(1);
    ManagedLedger ledger = factory.open("my_test_move_cursor_ledger", conf);
    ManagedCursor cursor = ledger.openCursor("trc1");
    Position p1 = ledger.addEntry("dummy-entry-1".getBytes());
    Position p2 = ledger.addEntry("dummy-entry-2".getBytes());
    Position p3 = ledger.addEntry("dummy-entry-3".getBytes());
    Position p4 = ledger.addEntry("dummy-entry-4".getBytes());
    cursor.markDelete(p3);
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, zkc);
    ledger = factory2.open("my_test_move_cursor_ledger", conf);
    cursor = ledger.openCursor("trc1");
    assertEquals(cursor.getMarkDeletedPosition(), p3);
    assertEquals(cursor.getReadPosition(), p4);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 1);
    cursor.resetCursor(p2);
    assertEquals(cursor.getMarkDeletedPosition(), p1);
    assertEquals(cursor.getReadPosition(), p2);
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 3);
    factory2.shutdown();
    factory.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 52 with Position

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

the class ManagedLedgerErrorsTest method errorInUpdatingLedgersList.

@Test(timeOut = 20000, invocationCount = 1, skipFailedInvocations = true, enabled = false)
public void errorInUpdatingLedgersList() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    final CountDownLatch latch = new CountDownLatch(1);
    zkc.failAfter(0, Code.CONNECTIONLOSS);
    ledger.asyncAddEntry("entry".getBytes(), new AddEntryCallback() {

        public void addFailed(ManagedLedgerException exception, Object ctx) {
        // not-ok
        }

        public void addComplete(Position position, Object ctx) {
        // ok
        }
    }, null);
    ledger.asyncAddEntry("entry".getBytes(), new AddEntryCallback() {

        public void addFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
        }

        public void addComplete(Position position, Object ctx) {
            fail("should have failed");
        }
    }, null);
    latch.await();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 53 with Position

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

the class ManagedLedgerErrorsTest method recoverLongTimeAfterMultipleWriteErrors.

@Test
public void recoverLongTimeAfterMultipleWriteErrors() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("recoverLongTimeAfterMultipleWriteErrors");
    ManagedCursor cursor = ledger.openCursor("c1");
    bkc.failNow(BKException.Code.BookieHandleNotAvailableException, BKException.Code.BookieHandleNotAvailableException);
    CountDownLatch counter = new CountDownLatch(2);
    AtomicReference<ManagedLedgerException> ex = new AtomicReference<>();
    // Write 2 entries, both should fail the first time and get re-tried internally in a new ledger
    AddEntryCallback cb = new AddEntryCallback() {

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

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            log.warn("Error in write", exception);
            ex.set(exception);
            counter.countDown();
        }
    };
    ledger.asyncAddEntry("entry-1".getBytes(), cb, null);
    ledger.asyncAddEntry("entry-2".getBytes(), cb, null);
    counter.await();
    assertNull(ex.get());
    assertEquals(cursor.getNumberOfEntriesInBacklog(), 2);
    // Ensure that we are only creating one new ledger
    // even when there are multiple (here, 2) add entry failed ops
    assertEquals(ledger.getLedgersInfoAsList().size(), 1);
    ledger.addEntry("entry-3".getBytes());
    List<Entry> entries = cursor.readEntries(10);
    assertEquals(entries.size(), 3);
    assertEquals(new String(entries.get(0).getData()), "entry-1");
    assertEquals(new String(entries.get(1).getData()), "entry-2");
    assertEquals(new String(entries.get(2).getData()), "entry-3");
    entries.forEach(e -> e.release());
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 54 with Position

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

the class ManagedLedgerErrorsTest method handleCursorRecoveryFailure.

@Test
public void handleCursorRecoveryFailure() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("my-cursor");
    Position p0 = cursor.getMarkDeletedPosition();
    Position p1 = ledger.addEntry("entry-1".getBytes());
    cursor.markDelete(p1);
    // Re-open from a different factory
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, zkc);
    bkc.failAfter(3, BKException.Code.LedgerRecoveryException);
    ledger = factory2.open("my_test_ledger");
    cursor = ledger.openCursor("my-cursor");
    // Since the cursor was rewind, it will be back to p0
    assertEquals(cursor.getMarkDeletedPosition(), p0);
    factory2.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 55 with Position

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

the class ManagedLedgerMBeanTest method simple.

@Test
public void simple() throws Exception {
    ManagedLedgerFactoryConfig config = new ManagedLedgerFactoryConfig();
    config.setMaxCacheSize(0);
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, zkc, config);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ManagedLedgerMBeanImpl mbean = ledger.mbean;
    assertEquals(mbean.getName(), "my_test_ledger");
    assertEquals(mbean.getStoredMessagesSize(), 0);
    assertEquals(mbean.getNumberOfMessagesInBacklog(), 0);
    waitForRefresh(mbean);
    mbean.addAddEntryLatencySample(1, TimeUnit.MILLISECONDS);
    mbean.addAddEntryLatencySample(10, TimeUnit.MILLISECONDS);
    mbean.addAddEntryLatencySample(1, TimeUnit.SECONDS);
    mbean.addLedgerSwitchLatencySample(1, TimeUnit.MILLISECONDS);
    mbean.addLedgerSwitchLatencySample(10, TimeUnit.MILLISECONDS);
    mbean.addLedgerSwitchLatencySample(1, TimeUnit.SECONDS);
    // Simulate stats getting update from different thread
    factory.executor.submit(() -> {
        mbean.refreshStats(1, TimeUnit.SECONDS);
    }).get();
    assertEquals(mbean.getAddEntryBytesRate(), 0.0);
    assertEquals(mbean.getAddEntryMessagesRate(), 0.0);
    assertEquals(mbean.getAddEntrySucceed(), 0);
    assertEquals(mbean.getAddEntryErrors(), 0);
    assertEquals(mbean.getReadEntriesBytesRate(), 0.0);
    assertEquals(mbean.getReadEntriesRate(), 0.0);
    assertEquals(mbean.getReadEntriesSucceeded(), 0);
    assertEquals(mbean.getReadEntriesErrors(), 0);
    assertEquals(mbean.getMarkDeleteRate(), 0.0);
    assertEquals(mbean.getAddEntryLatencyBuckets(), new long[] { 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 });
    assertEquals(mbean.getAddEntryLatencyAverageUsec(), 337_000.0);
    assertEquals(mbean.getEntrySizeBuckets(), new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 });
    assertEquals(mbean.getLedgerSwitchLatencyBuckets(), new long[] { 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 });
    assertEquals(mbean.getLedgerSwitchLatencyAverageUsec(), 337_000.0);
    Position p1 = ledger.addEntry(new byte[200]);
    ledger.addEntry(new byte[600]);
    cursor.markDelete(p1);
    factory.executor.submit(() -> {
        mbean.refreshStats(1, TimeUnit.SECONDS);
    }).get();
    assertEquals(mbean.getAddEntryBytesRate(), 800.0);
    assertEquals(mbean.getAddEntryMessagesRate(), 2.0);
    assertEquals(mbean.getAddEntrySucceed(), 2);
    assertEquals(mbean.getAddEntryErrors(), 0);
    assertEquals(mbean.getReadEntriesBytesRate(), 0.0);
    assertEquals(mbean.getReadEntriesRate(), 0.0);
    assertEquals(mbean.getReadEntriesSucceeded(), 0);
    assertEquals(mbean.getReadEntriesErrors(), 0);
    assertTrue(mbean.getMarkDeleteRate() > 0.0);
    assertEquals(mbean.getEntrySizeBuckets(), new long[] { 0, 1, 1, 0, 0, 0, 0, 0, 0 });
    mbean.recordAddEntryError();
    mbean.recordReadEntriesError();
    factory.executor.submit(() -> {
        mbean.refreshStats(1, TimeUnit.SECONDS);
    }).get();
    assertEquals(mbean.getAddEntryErrors(), 1);
    assertEquals(mbean.getReadEntriesErrors(), 1);
    List<Entry> entries = cursor.readEntries(100);
    assertEquals(entries.size(), 1);
    factory.executor.submit(() -> {
        mbean.refreshStats(1, TimeUnit.SECONDS);
    }).get();
    assertEquals(mbean.getReadEntriesBytesRate(), 600.0);
    assertEquals(mbean.getReadEntriesRate(), 1.0);
    assertEquals(mbean.getReadEntriesSucceeded(), 1);
    assertEquals(mbean.getReadEntriesErrors(), 0);
    assertEquals(mbean.getNumberOfMessagesInBacklog(), 1);
    assertEquals(mbean.getMarkDeleteRate(), 0.0);
    factory.shutdown();
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Position(org.apache.bookkeeper.mledger.Position) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

Position (org.apache.bookkeeper.mledger.Position)201 Test (org.testng.annotations.Test)169 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)168 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)167 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)115 Entry (org.apache.bookkeeper.mledger.Entry)104 CountDownLatch (java.util.concurrent.CountDownLatch)97 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)72 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)68 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 BKException (org.apache.bookkeeper.client.BKException)56 List (java.util.List)55 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)53 AtomicReference (java.util.concurrent.atomic.AtomicReference)52 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)52 Logger (org.slf4j.Logger)52 LoggerFactory (org.slf4j.LoggerFactory)52 Lists (com.google.common.collect.Lists)51 TimeUnit (java.util.concurrent.TimeUnit)51