Search in sources :

Example 6 with ManagedLedger

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

the class ManagedCursorConcurrencyTest method testCloseAndRead.

@Test
public void testCloseAndRead() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger_test_close_and_read", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final CompletableFuture<String> closeFuture = new CompletableFuture<>();
    final String CLOSED = "closed";
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final CountDownLatch counter = new CountDownLatch(2);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    Thread deleter = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (Position position : addedEntries) {
                    cursor.markDelete(position);
                    Thread.sleep(1);
                }
            } catch (ManagedLedgerException e) {
                if (!e.getMessage().equals("Cursor was already closed")) {
                    gotException.set(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    Thread reader = new Thread() {

        public void run() {
            try {
                barrier.await();
                for (int i = 0; i < 1000; i++) {
                    cursor.readEntries(1).forEach(e -> e.release());
                    // Thread.sleep(2,200);
                    Thread.sleep(2, 195);
                }
                cursor.asyncClose(new AsyncCallbacks.CloseCallback() {

                    @Override
                    public void closeComplete(Object ctx) {
                        log.info("Successfully closed cursor ledger");
                        closeFuture.complete(CLOSED);
                    }

                    @Override
                    public void closeFailed(ManagedLedgerException exception, Object ctx) {
                        log.error("Error closing cursor: ", exception);
                        closeFuture.completeExceptionally(new Exception(exception));
                    }
                }, null);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    reader.start();
    counter.await();
    assertEquals(gotException.get(), false);
    assertEquals(closeFuture.get(), CLOSED);
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 7 with ManagedLedger

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

the class ManagedLedgerFactoryImpl method open.

@Override
public ManagedLedger open(String name, ManagedLedgerConfig config) throws InterruptedException, ManagedLedgerException {
    class Result {

        ManagedLedger l = null;

        ManagedLedgerException e = null;
    }
    final Result r = new Result();
    final CountDownLatch latch = new CountDownLatch(1);
    asyncOpen(name, config, new OpenLedgerCallback() {

        @Override
        public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
            r.l = ledger;
            latch.countDown();
        }

        @Override
        public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
            r.e = exception;
            latch.countDown();
        }
    }, null);
    latch.await();
    if (r.e != null) {
        throw r.e;
    }
    return r.l;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)

Example 8 with ManagedLedger

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

the class ManagedCursorTest method testNumberOfEntries.

@Test(timeOut = 20000)
void testNumberOfEntries() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    ManagedCursor c1 = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ManagedCursor c2 = ledger.openCursor("c2");
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    ManagedCursor c3 = ledger.openCursor("c3");
    ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    ManagedCursor c4 = ledger.openCursor("c4");
    ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    ManagedCursor c5 = ledger.openCursor("c5");
    assertEquals(c1.getNumberOfEntries(), 4);
    assertEquals(c1.hasMoreEntries(), true);
    assertEquals(c2.getNumberOfEntries(), 3);
    assertEquals(c2.hasMoreEntries(), true);
    assertEquals(c3.getNumberOfEntries(), 2);
    assertEquals(c3.hasMoreEntries(), true);
    assertEquals(c4.getNumberOfEntries(), 1);
    assertEquals(c4.hasMoreEntries(), true);
    assertEquals(c5.getNumberOfEntries(), 0);
    assertEquals(c5.hasMoreEntries(), false);
    List<Entry> entries = c1.readEntries(2);
    assertEquals(entries.size(), 2);
    c1.markDelete(entries.get(1).getPosition());
    assertEquals(c1.getNumberOfEntries(), 2);
    entries.forEach(e -> e.release());
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 9 with ManagedLedger

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

the class ManagedCursorTest method testOutOfOrderDeletePersistenceAfterCrash.

@Test(timeOut = 20000)
public void testOutOfOrderDeletePersistenceAfterCrash() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig());
    ManagedCursor c1 = ledger.openCursor("c1");
    List<Position> addedPositions = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Position p = ledger.addEntry(("dummy-entry-" + i).getBytes(Encoding));
        addedPositions.add(p);
    }
    // Acknowledge few messages leaving holes
    c1.delete(addedPositions.get(2));
    c1.delete(addedPositions.get(5));
    c1.delete(addedPositions.get(7));
    c1.delete(addedPositions.get(8));
    c1.delete(addedPositions.get(9));
    assertEquals(c1.getNumberOfEntriesInBacklog(), 20 - 5);
    // Re-Open
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory2.open("my_test_ledger", new ManagedLedgerConfig());
    c1 = ledger.openCursor("c1");
    assertEquals(c1.getNumberOfEntriesInBacklog(), 20 - 5);
    List<Entry> entries = c1.readEntries(20);
    assertEquals(entries.size(), 20 - 5);
    List<String> entriesStr = entries.stream().map(e -> new String(e.getDataAndRelease(), Encoding)).collect(Collectors.toList());
    assertEquals(entriesStr.get(0), "dummy-entry-0");
    assertEquals(entriesStr.get(1), "dummy-entry-1");
    // Entry-2 was deleted
    assertEquals(entriesStr.get(2), "dummy-entry-3");
    assertEquals(entriesStr.get(3), "dummy-entry-4");
    // Entry-6 was deleted
    assertEquals(entriesStr.get(4), "dummy-entry-6");
    assertFalse(c1.hasMoreEntries());
    factory2.shutdown();
}
Also used : ZNodeProtobufFormat(org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.ZNodeProtobufFormat) Arrays(java.util.Arrays) Assert.assertNull(org.testng.Assert.assertNull) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) MockedBookKeeperTestCase(org.apache.bookkeeper.test.MockedBookKeeperTestCase) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertEquals(org.testng.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Assert.assertFalse(org.testng.Assert.assertFalse) ExecutorService(java.util.concurrent.ExecutorService) Charsets(com.google.common.base.Charsets) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) CyclicBarrier(java.util.concurrent.CyclicBarrier) Logger(org.slf4j.Logger) Factory(org.testng.annotations.Factory) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) Sets(com.google.common.collect.Sets) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ArrayList(java.util.ArrayList) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 10 with ManagedLedger

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

the class ManagedCursorTest method errorRecoveringCursor3.

@Test(timeOut = 20000)
void errorRecoveringCursor3() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    Position p1 = ledger.addEntry("entry".getBytes());
    ledger.addEntry("entry".getBytes());
    ManagedCursor c1 = ledger.openCursor("c1");
    Position p3 = ledger.addEntry("entry".getBytes());
    assertEquals(c1.getReadPosition(), p3);
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    bkc.failAfter(4, BKException.Code.ReadException);
    ledger = factory2.open("my_test_ledger");
    c1 = ledger.openCursor("c1");
    // Verify the ManagedCursor was rewind back to the snapshotted position
    assertEquals(c1.getReadPosition(), p3);
    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)

Aggregations

ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)166 Test (org.testng.annotations.Test)162 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)118 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)89 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)86 Position (org.apache.bookkeeper.mledger.Position)75 Entry (org.apache.bookkeeper.mledger.Entry)68 CountDownLatch (java.util.concurrent.CountDownLatch)57 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)48 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35 CyclicBarrier (java.util.concurrent.CyclicBarrier)33 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)33 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)29 ArrayList (java.util.ArrayList)28 Future (java.util.concurrent.Future)28 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)28 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)28 ExecutorService (java.util.concurrent.ExecutorService)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)27 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)27