Search in sources :

Example 46 with ManagedLedger

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

the class ManagedLedgerTest method asyncAddEntryWithoutError.

@Test(timeOut = 20000)
public void asyncAddEntryWithoutError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test-cursor");
    final CountDownLatch counter = new CountDownLatch(1);
    ledger.asyncAddEntry("dummy-entry-1".getBytes(Encoding), new AddEntryCallback() {

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

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            fail(exception.getMessage());
        }
    }, null);
    counter.await();
    assertEquals(ledger.getNumberOfEntries(), 1);
    assertEquals(ledger.getTotalSize(), "dummy-entry-1".getBytes(Encoding).length);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 47 with ManagedLedger

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

the class ManagedLedgerTest method asyncCloseWithoutError.

@Test(timeOut = 20000)
public void asyncCloseWithoutError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test-cursor");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    final CountDownLatch counter = new CountDownLatch(1);
    ledger.asyncClose(new CloseCallback() {

        @Override
        public void closeComplete(Object ctx) {
            assertNull(ctx);
            counter.countDown();
        }

        @Override
        public void closeFailed(ManagedLedgerException exception, Object ctx) {
            fail(exception.getMessage());
        }
    }, null);
    counter.await();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CloseCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 48 with ManagedLedger

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

the class ManagedLedgerTest method closeLedgerWithError.

@Test
public void closeLedgerWithError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.addEntry("entry-1".getBytes(Encoding));
    stopZooKeeper();
    stopBookKeeper();
    try {
        ledger.close();
    // fail("should have thrown exception");
    } catch (ManagedLedgerException e) {
    // Ok
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Test(org.testng.annotations.Test)

Example 49 with ManagedLedger

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

the class ManagedLedgerTest method testProducerAndNoConsumer.

@Test(timeOut = 20000)
public void testProducerAndNoConsumer() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    assertEquals(ledger.getNumberOfEntries(), 0);
    ledger.addEntry("entry-1".getBytes(Encoding));
    assertEquals(ledger.getNumberOfEntries(), 1);
    // Since there are no consumers, older ledger will be deleted
    // in a short time (in a background thread)
    ledger.addEntry("entry-2".getBytes(Encoding));
    while (ledger.getNumberOfEntries() > 1) {
        log.debug("entries={}", ledger.getNumberOfEntries());
        Thread.sleep(100);
    }
    ledger.addEntry("entry-3".getBytes(Encoding));
    while (ledger.getNumberOfEntries() > 1) {
        log.debug("entries={}", ledger.getNumberOfEntries());
        Thread.sleep(100);
    }
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 50 with ManagedLedger

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

the class ManagedCursorTest method testReplayEntries.

@Test(timeOut = 20000)
void testReplayEntries() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1");
    PositionImpl p1 = (PositionImpl) ledger.addEntry("entry1".getBytes(Encoding));
    PositionImpl p2 = (PositionImpl) ledger.addEntry("entry2".getBytes(Encoding));
    PositionImpl p3 = (PositionImpl) ledger.addEntry("entry3".getBytes(Encoding));
    ledger.addEntry("entry4".getBytes(Encoding));
    // 1. Replay empty position set should return empty entry set
    Set<PositionImpl> positions = Sets.newHashSet();
    assertTrue(c1.replayEntries(positions).isEmpty());
    positions.add(p1);
    positions.add(p3);
    // 2. entries 1 and 3 should be returned, but they can be in any order
    List<Entry> entries = c1.replayEntries(positions);
    assertEquals(entries.size(), 2);
    assertTrue((Arrays.equals(entries.get(0).getData(), "entry1".getBytes(Encoding)) && Arrays.equals(entries.get(1).getData(), "entry3".getBytes(Encoding))) || (Arrays.equals(entries.get(0).getData(), "entry3".getBytes(Encoding)) && Arrays.equals(entries.get(1).getData(), "entry1".getBytes(Encoding))));
    entries.forEach(Entry::release);
    // 3. Fail on reading non-existing position
    PositionImpl invalidPosition = new PositionImpl(100, 100);
    positions.add(invalidPosition);
    try {
        c1.replayEntries(positions);
        fail("Should fail");
    } catch (ManagedLedgerException e) {
    // ok
    }
    positions.remove(invalidPosition);
    // 4. Fail to attempt to read mark-deleted position (p1)
    c1.markDelete(p2);
    try {
        // as mark-delete is at position: p2 it should read entry : p3
        assertEquals(1, c1.replayEntries(positions).size());
    } catch (ManagedLedgerException e) {
        fail("Should have not failed");
    }
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) 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