Search in sources :

Example 11 with AddEntryCallback

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

the class ManagedLedgerTest method asyncAPI.

@Test(timeOut = 20000)
public void asyncAPI() throws Throwable {
    final CountDownLatch counter = new CountDownLatch(1);
    factory.asyncOpen("my_test_ledger", new ManagedLedgerConfig(), new OpenLedgerCallback() {

        @Override
        public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
            ledger.asyncOpenCursor("test-cursor", new OpenCursorCallback() {

                @Override
                public void openCursorComplete(ManagedCursor cursor, Object ctx) {
                    ManagedLedger ledger = (ManagedLedger) ctx;
                    ledger.asyncAddEntry("test".getBytes(Encoding), new AddEntryCallback() {

                        @Override
                        public void addComplete(Position position, Object ctx) {
                            @SuppressWarnings("unchecked") Pair<ManagedLedger, ManagedCursor> pair = (Pair<ManagedLedger, ManagedCursor>) ctx;
                            ManagedLedger ledger = pair.first;
                            ManagedCursor cursor = pair.second;
                            assertEquals(ledger.getNumberOfEntries(), 1);
                            assertEquals(ledger.getTotalSize(), "test".getBytes(Encoding).length);
                            cursor.asyncReadEntries(2, new ReadEntriesCallback() {

                                @Override
                                public void readEntriesComplete(List<Entry> entries, Object ctx) {
                                    ManagedCursor cursor = (ManagedCursor) ctx;
                                    assertEquals(entries.size(), 1);
                                    Entry entry = entries.get(0);
                                    assertEquals(new String(entry.getDataAndRelease(), Encoding), "test");
                                    log.debug("Mark-Deleting to position {}", entry.getPosition());
                                    cursor.asyncMarkDelete(entry.getPosition(), new MarkDeleteCallback() {

                                        @Override
                                        public void markDeleteComplete(Object ctx) {
                                            log.debug("Mark delete complete");
                                            ManagedCursor cursor = (ManagedCursor) ctx;
                                            assertEquals(cursor.hasMoreEntries(), false);
                                            counter.countDown();
                                        }

                                        @Override
                                        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                                            fail(exception.getMessage());
                                        }
                                    }, cursor);
                                }

                                @Override
                                public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
                                    fail(exception.getMessage());
                                }
                            }, cursor);
                        }

                        @Override
                        public void addFailed(ManagedLedgerException exception, Object ctx) {
                            fail(exception.getMessage());
                        }
                    }, new Pair<ManagedLedger, ManagedCursor>(ledger, cursor));
                }

                @Override
                public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
                    fail(exception.getMessage());
                }
            }, ledger);
        }

        @Override
        public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
            fail(exception.getMessage());
        }
    }, null);
    counter.await();
    log.info("Test completed");
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) OpenCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) List(java.util.List) ArrayList(java.util.ArrayList) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) OpenLedgerCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Pair(org.apache.bookkeeper.mledger.util.Pair) Test(org.testng.annotations.Test)

Example 12 with AddEntryCallback

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

the class ManagedLedgerTest method doubleAsyncAddEntryWithoutError.

@Test(timeOut = 20000)
public void doubleAsyncAddEntryWithoutError() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test-cursor");
    final CountDownLatch done = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final String content = "dummy-entry-" + i;
        ledger.asyncAddEntry(content.getBytes(Encoding), new AddEntryCallback() {

            @Override
            public void addComplete(Position position, Object ctx) {
                assertNotNull(ctx);
                log.info("Successfully added {}", content);
                done.countDown();
            }

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
                fail(exception.getMessage());
            }
        }, this);
    }
    done.await();
    assertEquals(ledger.getNumberOfEntries(), 10);
}
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 13 with AddEntryCallback

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

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) 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 14 with AddEntryCallback

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

the class ManagedLedgerTest method testBacklogCursor.

@Test
public void testBacklogCursor() throws Exception {
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("cache_backlog_ledger");
    final long maxMessageCacheRetentionTimeMillis = 100;
    Field field = ManagedLedgerImpl.class.getDeclaredField("maxMessageCacheRetentionTimeMillis");
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(ledger, maxMessageCacheRetentionTimeMillis);
    Field backlogThresholdField = ManagedLedgerImpl.class.getDeclaredField("maxActiveCursorBacklogEntries");
    backlogThresholdField.setAccessible(true);
    final long maxActiveCursorBacklogEntries = (long) backlogThresholdField.get(ledger);
    // Open Cursor also adds cursor into activeCursor-container
    ManagedCursor cursor1 = ledger.openCursor("c1");
    ManagedCursor cursor2 = ledger.openCursor("c2");
    final int totalBacklogSizeEntries = (int) maxActiveCursorBacklogEntries;
    CountDownLatch latch = new CountDownLatch(totalBacklogSizeEntries);
    for (int i = 0; i < totalBacklogSizeEntries + 1; i++) {
        // 5 bytes
        String content = "entry";
        ByteBuf entry = getMessageWithMetadata(content.getBytes());
        ledger.asyncAddEntry(entry, new AddEntryCallback() {

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

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
                latch.countDown();
                entry.release();
            }
        }, null);
    }
    latch.await();
    // Verify: cursors are active as :haven't started deactivateBacklogCursor scan
    assertTrue(cursor1.isActive());
    assertTrue(cursor2.isActive());
    // it allows message to be older enough to be considered in backlog
    Thread.sleep(maxMessageCacheRetentionTimeMillis * 2);
    // deactivate backlog cursors
    ledger.checkBackloggedCursors();
    Thread.sleep(100);
    // both cursors have to be inactive
    assertFalse(cursor1.isActive());
    assertFalse(cursor2.isActive());
    // read entries so, cursor1 reaches maxBacklog threshold again to be active again
    List<Entry> entries1 = cursor1.readEntries(50);
    for (Entry entry : entries1) {
        log.info("Read entry. Position={} Content='{}'", entry.getPosition(), new String(entry.getData()));
        entry.release();
    }
    // activate cursors which caught up maxbacklog threshold
    ledger.checkBackloggedCursors();
    // verify: cursor1 has consumed messages so, under maxBacklog threshold => active
    assertTrue(cursor1.isActive());
    // verify: cursor2 has not consumed messages so, above maxBacklog threshold => inactive
    assertFalse(cursor2.isActive());
    ledger.close();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) CountDownLatch(java.util.concurrent.CountDownLatch) DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf) ByteBuf(io.netty.buffer.ByteBuf) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Field(java.lang.reflect.Field) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Example 15 with AddEntryCallback

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

the class ManagedLedgerTest method asyncAddEntryWithError.

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

        @Override
        public void addComplete(Position position, Object ctx) {
            fail("Should have failed");
        }

        @Override
        public void addFailed(ManagedLedgerException exception, Object ctx) {
            counter.countDown();
        }
    }, null);
    counter.await();
}
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)

Aggregations

AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)16 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 Position (org.apache.bookkeeper.mledger.Position)10 Test (org.testng.annotations.Test)10 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)9 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)6 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)5 ByteBuf (io.netty.buffer.ByteBuf)4 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)3 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)3 Entry (org.apache.bookkeeper.mledger.Entry)3 Matchers.anyObject (org.mockito.Matchers.anyObject)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)2 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)2 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)2 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)1