Search in sources :

Example 61 with Position

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

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);
                                    final Position position = entry.getPosition();
                                    assertEquals(new String(entry.getDataAndRelease(), Encoding), "test");
                                    log.debug("Mark-Deleting to position {}", position);
                                    cursor.asyncMarkDelete(position, 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) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) 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) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair) Test(org.testng.annotations.Test)

Example 62 with Position

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

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

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

the class NonDurableCursorTest method testNumberOfEntriesInBacklog.

@Test(timeOut = 20000)
void testNumberOfEntriesInBacklog() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2).setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(1));
    ManagedCursor c1 = ledger.newNonDurableCursor(PositionImpl.latest);
    Position p1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    ManagedCursor c2 = ledger.newNonDurableCursor(PositionImpl.latest);
    ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    ManagedCursor c3 = ledger.newNonDurableCursor(PositionImpl.latest);
    Position p3 = ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    ManagedCursor c4 = ledger.newNonDurableCursor(PositionImpl.latest);
    Position p4 = ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    ManagedCursor c5 = ledger.newNonDurableCursor(PositionImpl.latest);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 4);
    assertEquals(c2.getNumberOfEntriesInBacklog(), 3);
    assertEquals(c3.getNumberOfEntriesInBacklog(), 2);
    assertEquals(c4.getNumberOfEntriesInBacklog(), 1);
    assertEquals(c5.getNumberOfEntriesInBacklog(), 0);
    List<Entry> entries = c1.readEntries(2);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    assertEquals(c1.getNumberOfEntries(), 2);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 4);
    c1.markDelete(p1);
    assertEquals(c1.getNumberOfEntries(), 2);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 3);
    c1.delete(p3);
    assertEquals(c1.getNumberOfEntries(), 1);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 2);
    c1.markDelete(p4);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 0);
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 65 with Position

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

the class NonDurableCursorTest method rewind.

@Test(timeOut = 20000)
void rewind() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2).setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(1));
    ManagedCursor c1 = ledger.newNonDurableCursor(PositionImpl.earliest);
    Position p1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    Position p2 = ledger.addEntry("dummy-entry-2".getBytes(Encoding));
    Position p3 = ledger.addEntry("dummy-entry-3".getBytes(Encoding));
    Position p4 = ledger.addEntry("dummy-entry-4".getBytes(Encoding));
    log.debug("p1: {}", p1);
    log.debug("p2: {}", p2);
    log.debug("p3: {}", p3);
    log.debug("p4: {}", p4);
    assertEquals(c1.getNumberOfEntries(), 4);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 4);
    c1.markDelete(p1);
    assertEquals(c1.getNumberOfEntries(), 3);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 3);
    List<Entry> entries = c1.readEntries(10);
    assertEquals(entries.size(), 3);
    entries.forEach(e -> e.release());
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 3);
    c1.rewind();
    assertEquals(c1.getNumberOfEntries(), 3);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 3);
    c1.markDelete(p2);
    assertEquals(c1.getNumberOfEntries(), 2);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 2);
    entries = c1.readEntries(10);
    assertEquals(entries.size(), 2);
    entries.forEach(e -> e.release());
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 2);
    c1.rewind();
    assertEquals(c1.getNumberOfEntries(), 2);
    c1.markDelete(p4);
    assertEquals(c1.getNumberOfEntries(), 0);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 0);
    c1.rewind();
    assertEquals(c1.getNumberOfEntries(), 0);
    ledger.addEntry("dummy-entry-5".getBytes(Encoding));
    assertEquals(c1.getNumberOfEntries(), 1);
    ledger.addEntry("dummy-entry-6".getBytes(Encoding));
    assertEquals(c1.getNumberOfEntries(), 2);
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

Position (org.apache.bookkeeper.mledger.Position)201 Test (org.testng.annotations.Test)169 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)168 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)167 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)115 Entry (org.apache.bookkeeper.mledger.Entry)104 CountDownLatch (java.util.concurrent.CountDownLatch)97 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)72 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)68 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 BKException (org.apache.bookkeeper.client.BKException)56 List (java.util.List)55 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)53 AtomicReference (java.util.concurrent.atomic.AtomicReference)52 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)52 Logger (org.slf4j.Logger)52 LoggerFactory (org.slf4j.LoggerFactory)52 Lists (com.google.common.collect.Lists)51 TimeUnit (java.util.concurrent.TimeUnit)51