Search in sources :

Example 1 with DeleteCallback

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

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 2 with DeleteCallback

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

the class ManagedLedgerBkTest method asyncMarkDeleteAndClose.

@Test
public void asyncMarkDeleteAndClose() throws Exception {
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc);
    ManagedLedgerConfig config = new ManagedLedgerConfig().setEnsembleSize(1).setWriteQuorumSize(1).setAckQuorumSize(1).setMetadataEnsembleSize(1).setMetadataWriteQuorumSize(1).setMetadataAckQuorumSize(1);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    ManagedCursor cursor = ledger.openCursor("c1");
    List<Position> positions = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        Position p = ledger.addEntry("entry".getBytes());
        positions.add(p);
    }
    final CountDownLatch counter = new CountDownLatch(positions.size());
    final AtomicBoolean gotException = new AtomicBoolean(false);
    for (Position p : positions) {
        cursor.asyncDelete(p, new DeleteCallback() {

            @Override
            public void deleteComplete(Object ctx) {
                // Ok
                counter.countDown();
            }

            @Override
            public void deleteFailed(ManagedLedgerException exception, Object ctx) {
                exception.printStackTrace();
                gotException.set(true);
                counter.countDown();
            }
        }, null);
    }
    cursor.close();
    ledger.close();
    counter.await();
    assertFalse(gotException.get());
    factory.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback)2 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)2 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)2 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 Position (org.apache.bookkeeper.mledger.Position)2 Test (org.testng.annotations.Test)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReadEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback)1 Entry (org.apache.bookkeeper.mledger.Entry)1 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)1