Search in sources :

Example 11 with ManagedLedgerFactory

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

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

the class ManagedLedgerBinaryFormatConversion method textToBinary.

@Test(timeOut = 20000, dataProvider = "gracefulClose")
void textToBinary(boolean gracefulClose) throws Exception {
    ManagedLedgerFactoryConfig textConf = new ManagedLedgerFactoryConfig();
    textConf.setUseProtobufBinaryFormatInZK(false);
    ManagedLedgerFactory textFactory = new ManagedLedgerFactoryImpl(bkc, zkc, textConf);
    ManagedLedger ledger = textFactory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("test-0".getBytes(Encoding));
    Position p1 = ledger.addEntry("test-1".getBytes(Encoding));
    ledger.addEntry("test-2".getBytes(Encoding));
    c1.delete(p1);
    if (gracefulClose) {
        ledger.close();
    }
    // Reopen with binary format
    ManagedLedgerFactoryConfig binaryConf = new ManagedLedgerFactoryConfig();
    binaryConf.setUseProtobufBinaryFormatInZK(true);
    ManagedLedgerFactory binaryFactory = new ManagedLedgerFactoryImpl(bkc, zkc, binaryConf);
    ledger = binaryFactory.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    // The 'p1' entry was already deleted
    assertEquals(c1.getNumberOfEntriesInBacklog(), 2);
    textFactory.shutdown();
    binaryFactory.shutdown();
}
Also used : ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Position(org.apache.bookkeeper.mledger.Position) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 13 with ManagedLedgerFactory

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

the class ManagedLedgerBinaryFormatConversion method binaryToText.

@Test(timeOut = 20000, dataProvider = "gracefulClose")
void binaryToText(boolean gracefulClose) throws Exception {
    ManagedLedgerFactoryConfig binaryConf = new ManagedLedgerFactoryConfig();
    binaryConf.setUseProtobufBinaryFormatInZK(true);
    ManagedLedgerFactory binaryFactory = new ManagedLedgerFactoryImpl(bkc, zkc, binaryConf);
    ManagedLedger ledger = binaryFactory.open("my_test_ledger");
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("test-0".getBytes(Encoding));
    Position p1 = ledger.addEntry("test-1".getBytes(Encoding));
    ledger.addEntry("test-2".getBytes(Encoding));
    c1.delete(p1);
    if (gracefulClose) {
        ledger.close();
    }
    // Reopen with binary format
    ManagedLedgerFactoryConfig textConf = new ManagedLedgerFactoryConfig();
    textConf.setUseProtobufBinaryFormatInZK(false);
    ManagedLedgerFactory textFactory = new ManagedLedgerFactoryImpl(bkc, zkc, textConf);
    ledger = textFactory.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    // The 'p1' entry was already deleted
    assertEquals(c1.getNumberOfEntriesInBacklog(), 2);
    textFactory.shutdown();
    binaryFactory.shutdown();
}
Also used : ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Position(org.apache.bookkeeper.mledger.Position) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 14 with ManagedLedgerFactory

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

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 15 with ManagedLedgerFactory

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

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)

Aggregations

ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)27 Test (org.testng.annotations.Test)26 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)23 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)23 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)19 Position (org.apache.bookkeeper.mledger.Position)14 CountDownLatch (java.util.concurrent.CountDownLatch)5 Entry (org.apache.bookkeeper.mledger.Entry)5 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)5 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 Future (java.util.concurrent.Future)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)2 ManagedLedgerInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo)2 LedgerInfo (org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo)2 Charsets (com.google.common.base.Charsets)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1