Search in sources :

Example 1 with MarkDeleteCallback

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

the class ManagedCursorImpl method markDelete.

@Override
public void markDelete(Position position) throws InterruptedException, ManagedLedgerException {
    checkNotNull(position);
    checkArgument(position instanceof PositionImpl);
    class Result {

        ManagedLedgerException exception = null;
    }
    final Result result = new Result();
    final CountDownLatch counter = new CountDownLatch(1);
    asyncMarkDelete(position, new MarkDeleteCallback() {

        @Override
        public void markDeleteComplete(Object ctx) {
            counter.countDown();
        }

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    if (!counter.await(ManagedLedgerImpl.AsyncOperationTimeoutSeconds, TimeUnit.SECONDS)) {
        throw new ManagedLedgerException("Timeout during mark-delete operation");
    }
    if (result.exception != null) {
        throw result.exception;
    }
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with MarkDeleteCallback

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

the class ManagedCursorImpl method asyncSkipEntries.

@Override
public void asyncSkipEntries(int numEntriesToSkip, IndividualDeletedEntries deletedEntries, final SkipEntriesCallback callback, Object ctx) {
    log.info("[{}] Skipping {} entries on cursor {}", ledger.getName(), numEntriesToSkip, name);
    long numDeletedMessages = 0;
    if (deletedEntries == IndividualDeletedEntries.Exclude) {
        numDeletedMessages = getNumIndividualDeletedEntriesToSkip(numEntriesToSkip);
    }
    asyncMarkDelete(ledger.getPositionAfterN(markDeletePosition, numEntriesToSkip + numDeletedMessages, PositionBound.startExcluded), new MarkDeleteCallback() {

        @Override
        public void markDeleteComplete(Object ctx) {
            callback.skipEntriesComplete(ctx);
        }

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            if (exception.getCause() instanceof IllegalArgumentException) {
                // There could be a race condition between calling clear backlog and other mark delete
                // operations.
                // If we get an exception it means the backlog was already cleared in the meantime.
                callback.skipEntriesComplete(ctx);
            } else {
                log.error("[{}] Skip {} entries failed for cursor {}", ledger.getName(), numEntriesToSkip, name, exception);
                callback.skipEntriesFailed(exception, ctx);
            }
        }
    }, ctx);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException)

Example 3 with MarkDeleteCallback

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

the class ManagedCursorTest method unorderedAsyncMarkDelete.

@Test(timeOut = 20000)
void unorderedAsyncMarkDelete() 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));
    final CountDownLatch latch = new CountDownLatch(2);
    c1.asyncMarkDelete(p2, new MarkDeleteCallback() {

        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            fail();
        }

        public void markDeleteComplete(Object ctx) {
            latch.countDown();
        }
    }, null);
    c1.asyncMarkDelete(p1, new MarkDeleteCallback() {

        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
        }

        public void markDeleteComplete(Object ctx) {
            fail();
        }
    }, null);
    latch.await();
    assertEquals(c1.getMarkDeletedPosition(), p2);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 4 with MarkDeleteCallback

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

the class ManagedCursorTest method unorderedAsyncMarkDelete.

@Test(timeOut = 20000)
void unorderedAsyncMarkDelete() 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));
    final CountDownLatch latch = new CountDownLatch(2);
    c1.asyncMarkDelete(p2, new MarkDeleteCallback() {

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

        @Override
        public void markDeleteComplete(Object ctx) {
            latch.countDown();
        }
    }, null);
    c1.asyncMarkDelete(p1, new MarkDeleteCallback() {

        @Override
        public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
        }

        @Override
        public void markDeleteComplete(Object ctx) {
            fail();
        }
    }, null);
    latch.await();
    assertEquals(c1.getMarkDeletedPosition(), p2);
}
Also used : MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 5 with MarkDeleteCallback

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

the class ManagedCursorTest method asyncMarkDeleteBlocking.

@Test(timeOut = 20000)
public void asyncMarkDeleteBlocking() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setMaxEntriesPerLedger(10);
    config.setMetadataMaxEntriesPerLedger(5);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    final ManagedCursor c1 = ledger.openCursor("c1");
    final AtomicReference<Position> lastPosition = new AtomicReference<Position>();
    final int N = 100;
    final CountDownLatch latch = new CountDownLatch(N);
    for (int i = 0; i < N; i++) {
        ledger.asyncAddEntry("entry".getBytes(Encoding), new AddEntryCallback() {

            @Override
            public void addFailed(ManagedLedgerException exception, Object ctx) {
            }

            @Override
            public void addComplete(Position position, Object ctx) {
                lastPosition.set(position);
                c1.asyncMarkDelete(position, new MarkDeleteCallback() {

                    @Override
                    public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
                    }

                    @Override
                    public void markDeleteComplete(Object ctx) {
                        latch.countDown();
                    }
                }, null);
            }
        }, null);
    }
    latch.await();
    assertEquals(c1.getNumberOfEntries(), 0);
    // Reopen
    ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ledger = factory2.open("my_test_ledger");
    ManagedCursor c2 = ledger.openCursor("c1");
    assertEquals(c2.getMarkDeletedPosition(), lastPosition.get());
    factory2.shutdown();
}
Also used : Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) Test(org.testng.annotations.Test)

Aggregations

MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)19 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)19 CountDownLatch (java.util.concurrent.CountDownLatch)11 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)10 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)10 Position (org.apache.bookkeeper.mledger.Position)10 Test (org.testng.annotations.Test)9 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)8 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)6 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)5 ManagedLedgerException.getManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException)4 ManagedLedgerImpl.createManagedLedgerException (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)3 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 BKException (org.apache.bookkeeper.client.BKException)2