Search in sources :

Example 91 with ManagedLedger

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

the class ManagedLedgerBkTest method testBookieFailure.

@Test
public void testBookieFailure() throws Exception {
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    ManagedLedger ledger = factory.open("my-ledger", config);
    ManagedCursor cursor = ledger.openCursor("my-cursor");
    ledger.addEntry("entry-0".getBytes());
    killBookie(1);
    // Now we want to simulate that:
    // 1. The write operation fails because we only have 1 bookie available
    // 2. The bk client cannot properly close the ledger (finalizing the number of entries) because ZK is also
    // not available
    // 3. When we re-establish the service one, the ledger recovery will be triggered and the half-committed entry
    // is restored
    // Force to close the ZK client object so that BK will fail to close the ledger
    bkc.getZkHandle().close();
    try {
        ledger.addEntry("entry-1".getBytes());
        fail("should fail");
    } catch (ManagedLedgerException e) {
    // ok
    }
    bkc = new BookKeeperTestClient(baseClientConf);
    startNewBookie();
    // Reconnect a new bk client
    factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    ledger = factory.open("my-ledger", config);
    cursor = ledger.openCursor("my-cursor");
    // Next add should succeed
    ledger.addEntry("entry-2".getBytes());
    assertEquals(3, cursor.getNumberOfEntriesInBacklog());
    List<Entry> entries = cursor.readEntries(1);
    assertEquals(1, entries.size());
    assertEquals("entry-0", new String(entries.get(0).getData()));
    entries.forEach(e -> e.release());
    // entry-1 which was half-committed will get fully committed during the recovery phase
    entries = cursor.readEntries(1);
    assertEquals(1, entries.size());
    assertEquals("entry-1", new String(entries.get(0).getData()));
    entries.forEach(e -> e.release());
    entries = cursor.readEntries(1);
    assertEquals(1, entries.size());
    assertEquals("entry-2", new String(entries.get(0).getData()));
    entries.forEach(e -> e.release());
    factory.shutdown();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) BookKeeperTestClient(org.apache.bookkeeper.client.BookKeeperTestClient) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 92 with ManagedLedger

use of org.apache.bookkeeper.mledger.ManagedLedger 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 93 with ManagedLedger

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

the class ManagedLedgerBkTest method testSimple.

@Test
public void testSimple() throws Exception {
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    ManagedLedgerConfig mlConfig = new ManagedLedgerConfig();
    mlConfig.setEnsembleSize(1).setAckQuorumSize(1).setMetadataEnsembleSize(1);
    // set the data ledger size
    mlConfig.setMaxEntriesPerLedger(100);
    // set the metadata ledger size to 1 to kick off many ledger switching cases
    mlConfig.setMetadataMaxEntriesPerLedger(2);
    ManagedLedger ledger = factory.open("ml-simple-ledger", mlConfig);
    ledger.addEntry("test".getBytes());
    factory.shutdown();
}
Also used : ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 94 with ManagedLedger

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

the class ManagedLedgerErrorsTest method closingManagedLedger.

@Test
public void closingManagedLedger() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("c1");
    ledger.addEntry("entry".getBytes());
    bkc.failNow(BKException.Code.NoSuchLedgerExistsException);
    try {
        ledger.close();
        fail("should fail");
    } catch (ManagedLedgerException e) {
    // ok
    }
    // ML should be closed even if it failed before
    try {
        ledger.addEntry("entry".getBytes());
        fail("managed ledger was closed");
    } catch (ManagedLedgerException e) {
    // ok
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Test(org.testng.annotations.Test)

Example 95 with ManagedLedger

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

the class ManagedLedgerErrorsTest method errorInRecovering6.

@Test
public void errorInRecovering6() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("c1");
    ledger.addEntry("entry".getBytes());
    ledger.close();
    factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    zkc.failAfter(3, Code.CONNECTIONLOSS);
    try {
        ledger = factory.open("my_test_ledger");
        fail("should fail");
    } catch (ManagedLedgerException e) {
    // ok
    }
    // It should be fine now
    ledger = factory.open("my_test_ledger");
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Test(org.testng.annotations.Test)

Aggregations

ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)363 Test (org.testng.annotations.Test)354 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)262 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)198 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)180 Position (org.apache.bookkeeper.mledger.Position)167 Entry (org.apache.bookkeeper.mledger.Entry)151 CountDownLatch (java.util.concurrent.CountDownLatch)117 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)98 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)74 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)68 CyclicBarrier (java.util.concurrent.CyclicBarrier)66 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)59 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)58 ArrayList (java.util.ArrayList)57 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)57 Future (java.util.concurrent.Future)56 AtomicReference (java.util.concurrent.atomic.AtomicReference)55 ExecutorService (java.util.concurrent.ExecutorService)54 BKException (org.apache.bookkeeper.client.BKException)54