Search in sources :

Example 66 with Position

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

the class NonDurableCursorTest method unorderedMarkDelete.

@Test(timeOut = 20000)
void unorderedMarkDelete() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    final ManagedCursor c1 = ledger.openCursor("c1");
    Position p1 = ledger.addEntry("entry-1".getBytes(Encoding));
    Position p2 = ledger.addEntry("entry-2".getBytes(Encoding));
    c1.markDelete(p2);
    try {
        c1.markDelete(p1);
        fail("Should have thrown exception");
    } catch (ManagedLedgerException e) {
    // ok
    }
    assertEquals(c1.getMarkDeletedPosition(), p2);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 67 with Position

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

the class NonDurableCursorTest method subscribeToEarliestPositionWithDeferredDeletion.

// (timeOut = 20000)
@Test
void subscribeToEarliestPositionWithDeferredDeletion() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1).setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(1));
    Position p1 = ledger.addEntry("entry-1".getBytes());
    Position p2 = ledger.addEntry("entry-2".getBytes());
    /* Position p3 = */
    ledger.addEntry("entry-3".getBytes());
    /* Position p4 = */
    ledger.addEntry("entry-4".getBytes());
    /* Position p5 = */
    ledger.addEntry("entry-5".getBytes());
    /* Position p6 = */
    ledger.addEntry("entry-6".getBytes());
    ManagedCursor c1 = ledger.newNonDurableCursor(PositionImpl.earliest);
    assertEquals(c1.getReadPosition(), p1);
    assertEquals(c1.getMarkDeletedPosition(), new PositionImpl(3, -1));
    assertEquals(c1.getNumberOfEntries(), 6);
    assertEquals(c1.getNumberOfEntriesInBacklog(), 6);
    ManagedCursor c2 = ledger.newNonDurableCursor(p1);
    assertEquals(c2.getReadPosition(), p2);
    assertEquals(c2.getMarkDeletedPosition(), p1);
    assertEquals(c2.getNumberOfEntries(), 5);
    assertEquals(c2.getNumberOfEntriesInBacklog(), 5);
}
Also used : 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 68 with Position

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

the class ManagedCursorConcurrencyTest method testConcurrentIndividualDeletesWithGetNthEntry.

@Test(timeOut = 30000)
public void testConcurrentIndividualDeletesWithGetNthEntry() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(100).setThrottleMarkDelete(0.5));
    final ManagedCursor cursor = ledger.openCursor("c1");
    final int N = 1000;
    final List<Position> addedEntries = Lists.newArrayListWithExpectedSize(N);
    for (int i = 0; i < N; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
    final int deleteEntries = 100;
    final CountDownLatch counter = new CountDownLatch(deleteEntries);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    final AtomicInteger iteration = new AtomicInteger(0);
    for (int i = 0; i < deleteEntries; i++) {
        executor.submit(safeRun(() -> {
            try {
                cursor.asyncDelete(addedEntries.get(iteration.getAndIncrement()), new DeleteCallback() {

                    @Override
                    public void deleteComplete(Object ctx) {
                    // Ok
                    }

                    @Override
                    public void deleteFailed(ManagedLedgerException exception, Object ctx) {
                        exception.printStackTrace();
                        gotException.set(true);
                    }
                }, null);
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }));
    }
    counter.await();
    final int readEntries = N - deleteEntries;
    final CountDownLatch readCounter = new CountDownLatch(readEntries);
    final AtomicInteger successReadEntries = new AtomicInteger(0);
    for (int i = 1; i <= readEntries; i++) {
        try {
            cursor.asyncGetNthEntry(i, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {

                @Override
                public void readEntryComplete(Entry entry, Object ctx) {
                    successReadEntries.getAndIncrement();
                    entry.release();
                    readCounter.countDown();
                }

                @Override
                public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
                    exception.printStackTrace();
                    gotException.set(true);
                }
            }, null);
        } catch (Exception e) {
            e.printStackTrace();
            gotException.set(true);
        }
    }
    readCounter.await();
    assertFalse(gotException.get());
    assertEquals(successReadEntries.get(), readEntries);
}
Also used : ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Entry(org.apache.bookkeeper.mledger.Entry) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 69 with Position

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

the class ManagedCursorConcurrencyTest method testAckAndClose.

@Test(timeOut = 30000)
public void testAckAndClose() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger_test_ack_and_close", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    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);
    // Deleter thread
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
            for (Position position : addedEntries) {
                cursor.asyncDelete(position, deleteCallback, position);
            }
        } catch (Exception e) {
            e.printStackTrace();
            gotException.set(true);
        } finally {
            counter.countDown();
        }
    });
    // Reader thread
    cachedExecutor.execute(() -> {
        try {
            barrier.await();
            for (int i = 0; i < 1000; i++) {
                cursor.readEntries(1).forEach(e -> e.release());
            }
        } catch (Exception e) {
            e.printStackTrace();
            gotException.set(true);
        } finally {
            counter.countDown();
        }
    });
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 70 with Position

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

the class ManagedCursorConcurrencyTest method testMarkDeleteAndRead.

@Test
public void testMarkDeleteAndRead() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    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);
                }
            } 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());
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        }
    };
    deleter.start();
    reader.start();
    counter.await();
    assertEquals(gotException.get(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) 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