Search in sources :

Example 66 with ManagedLedgerConfig

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

the class ManagedLedgerTest method testAsyncAddEntryAndSyncClose.

@Test(timeOut = 20000)
public void testAsyncAddEntryAndSyncClose() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(10);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    ledger.openCursor("c1");
    assertEquals(ledger.getNumberOfEntries(), 0);
    final CountDownLatch counter = new CountDownLatch(100);
    for (int i = 0; i < 100; i++) {
        String content = "entry-" + i;
        ledger.asyncAddEntry(content.getBytes(Encoding), new AddEntryCallback() {

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

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
                fail(exception.getMessage());
            }
        }, null);
    }
    counter.await();
    assertEquals(ledger.getNumberOfEntries(), 100);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) 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 67 with ManagedLedgerConfig

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

the class ManagedLedgerTest method testMinimumRolloverTime.

@Test
public void testMinimumRolloverTime() throws Exception {
    ManagedLedgerConfig conf = new ManagedLedgerConfig();
    conf.setMaxEntriesPerLedger(1);
    conf.setMinimumRolloverTime(1, TimeUnit.SECONDS);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", conf);
    ledger.openCursor("c1");
    ledger.addEntry("data".getBytes());
    ledger.addEntry("data".getBytes());
    assertEquals(ledger.getLedgersInfoAsList().size(), 1);
    Thread.sleep(1000);
    ledger.addEntry("data".getBytes());
    ledger.addEntry("data".getBytes());
    assertEquals(ledger.getLedgersInfoAsList().size(), 2);
}
Also used : ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 68 with ManagedLedgerConfig

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

the class ManagedLedgerTest method previousPosition.

@Test
public void previousPosition() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    ManagedCursor cursor = ledger.openCursor("my_cursor");
    Position p0 = cursor.getMarkDeletedPosition();
    // This is expected because p0 is already an "invalid" position (since no entry has been mark-deleted yet)
    assertEquals(ledger.getPreviousPosition((PositionImpl) p0), p0);
    // Force to close an empty ledger
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    // again
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    PositionImpl pBeforeWriting = ledger.getLastPosition();
    PositionImpl p1 = (PositionImpl) ledger.addEntry("entry".getBytes());
    ledger.close();
    ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    Position p2 = ledger.addEntry("entry".getBytes());
    Position p3 = ledger.addEntry("entry".getBytes());
    Position p4 = ledger.addEntry("entry".getBytes());
    assertEquals(ledger.getPreviousPosition(p1), pBeforeWriting);
    assertEquals(ledger.getPreviousPosition((PositionImpl) p2), p1);
    assertEquals(ledger.getPreviousPosition((PositionImpl) p3), p2);
    assertEquals(ledger.getPreviousPosition((PositionImpl) p4), p3);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 69 with ManagedLedgerConfig

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

the class ManagedLedgerTest method forceCloseLedgers.

@Test
public void forceCloseLedgers() throws Exception {
    ManagedLedger ledger1 = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
    ledger1.openCursor("c1");
    ManagedCursor c2 = ledger1.openCursor("c2");
    ledger1.addEntry("entry-1".getBytes(Encoding));
    ledger1.addEntry("entry-2".getBytes(Encoding));
    ledger1.addEntry("entry-3".getBytes(Encoding));
    c2.readEntries(1).forEach(e -> e.release());
    c2.readEntries(1).forEach(e -> e.release());
    c2.readEntries(1).forEach(e -> e.release());
    ledger1.close();
    try {
        ledger1.addEntry("entry-3".getBytes(Encoding));
        fail("should not have reached this point");
    } catch (ManagedLedgerException e) {
    // ok
    }
    try {
        ledger1.openCursor("new-cursor");
        fail("should not have reached this point");
    } catch (ManagedLedgerException e) {
    // ok
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 70 with ManagedLedgerConfig

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

the class ManagedLedgerTest method testTimestampOnWorkingLedger.

@Test
public void testTimestampOnWorkingLedger() throws Exception {
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig conf = new ManagedLedgerConfig();
    conf.setMaxEntriesPerLedger(1);
    conf.setRetentionSizeInMB(10);
    conf.setRetentionTime(1, TimeUnit.HOURS);
    ManagedLedgerImpl ml = (ManagedLedgerImpl) factory.open("my_test_ledger", conf);
    ml.openCursor("c1");
    ml.addEntry("msg1".getBytes());
    Iterator<LedgerInfo> iter = ml.getLedgersInfoAsList().iterator();
    long ts = -1;
    while (iter.hasNext()) {
        LedgerInfo i = iter.next();
        if (iter.hasNext()) {
            assertTrue(ts <= i.getTimestamp(), i.toString());
            ts = i.getTimestamp();
        } else {
            // the last timestamp can be
            // 0 if it is still opened
            // >0 if it is closed after the addEntry see OpAddEntry#addComplete()
            assertTrue(i.getTimestamp() == 0 || ts <= i.getTimestamp(), i.toString());
        }
    }
    ml.addEntry("msg02".getBytes());
    ml.close();
    // Thread.sleep(1000);
    iter = ml.getLedgersInfoAsList().iterator();
    ts = -1;
    while (iter.hasNext()) {
        LedgerInfo i = iter.next();
        if (iter.hasNext()) {
            assertTrue(ts <= i.getTimestamp(), i.toString());
            ts = i.getTimestamp();
        } else {
            assertTrue(i.getTimestamp() > 0, "well closed LedgerInfo should set a timestamp > 0");
        }
    }
}
Also used : ManagedLedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo) LedgerInfo(org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Aggregations

ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)194 Test (org.testng.annotations.Test)182 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)159 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)147 Position (org.apache.bookkeeper.mledger.Position)85 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)77 Entry (org.apache.bookkeeper.mledger.Entry)63 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)54 CountDownLatch (java.util.concurrent.CountDownLatch)52 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)33 CyclicBarrier (java.util.concurrent.CyclicBarrier)20 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)19 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)18 List (java.util.List)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)16 TimeUnit (java.util.concurrent.TimeUnit)15 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)15 BKException (org.apache.bookkeeper.client.BKException)14 Lists (com.google.common.collect.Lists)13