Search in sources :

Example 1 with ManagedCursor

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

the class ManagedLedgerImpl method asyncDelete.

@Override
public void asyncDelete(final DeleteLedgerCallback callback, final Object ctx) {
    // Delete the managed ledger without closing, since we are not interested in gracefully closing cursors and
    // ledgers
    STATE_UPDATER.set(this, State.Fenced);
    List<ManagedCursor> cursors = Lists.newArrayList(this.cursors);
    if (cursors.isEmpty()) {
        // No cursors to delete, proceed with next step
        deleteAllLedgers(callback, ctx);
        return;
    }
    AtomicReference<ManagedLedgerException> cursorDeleteException = new AtomicReference<>();
    AtomicInteger cursorsToDelete = new AtomicInteger(cursors.size());
    for (ManagedCursor cursor : cursors) {
        asyncDeleteCursor(cursor.getName(), new DeleteCursorCallback() {

            @Override
            public void deleteCursorComplete(Object ctx) {
                if (cursorsToDelete.decrementAndGet() == 0) {
                    if (cursorDeleteException.get() != null) {
                        // Some cursor failed to delete
                        callback.deleteLedgerFailed(cursorDeleteException.get(), ctx);
                        return;
                    }
                    // All cursors deleted, continue with deleting all ledgers
                    deleteAllLedgers(callback, ctx);
                }
            }

            @Override
            public void deleteCursorFailed(ManagedLedgerException exception, Object ctx) {
                log.warn("[{}] Failed to delete cursor {}", name, cursor, exception);
                cursorDeleteException.compareAndSet(null, exception);
                if (cursorsToDelete.decrementAndGet() == 0) {
                    // Trigger callback only once
                    callback.deleteLedgerFailed(exception, ctx);
                }
            }
        }, null);
    }
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) DeleteCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor)

Example 2 with ManagedCursor

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

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 3 with ManagedCursor

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

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)

Example 4 with ManagedCursor

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

the class ManagedCursorConcurrencyTest method testConcurrentReadOfSameEntry.

@Test(timeOut = 30000)
public void testConcurrentReadOfSameEntry() throws Exception {
    ManagedLedger ledger = factory.open("testConcurrentReadOfSameEntry", new ManagedLedgerConfig());
    final int numCursors = 5;
    final List<ManagedCursor> cursors = Lists.newArrayList();
    for (int i = 0; i < numCursors; i++) {
        final ManagedCursor cursor = ledger.openCursor("c" + i);
        cursors.add(cursor);
    }
    final int N = 100;
    for (int i = 0; i < N; i++) {
        ledger.addEntry(("entry" + i).getBytes());
    }
    long currentLedger = ((PositionImpl) cursors.get(0).getMarkDeletedPosition()).getLedgerId();
    // empty the cache
    ((ManagedLedgerImpl) ledger).entryCache.invalidateAllEntries(currentLedger);
    final CyclicBarrier barrier = new CyclicBarrier(numCursors);
    final CountDownLatch counter = new CountDownLatch(numCursors);
    AtomicReference<String> result = new AtomicReference<String>();
    for (int i = 0; i < numCursors; i++) {
        final int cursorIndex = i;
        final ManagedCursor cursor = cursors.get(cursorIndex);
        cachedExecutor.execute(() -> {
            try {
                barrier.await();
                for (int j = 0; j < N; j++) {
                    String expected = "entry" + j;
                    String data = new String(cursor.readEntries(1).get(0).getDataAndRelease());
                    if ((!expected.equals(data)) && result.get() == null) {
                        result.set("Mismatched entry in cursor " + (cursorIndex + 1) + " at position " + (j + 1) + "--- Expected: " + expected + ", Actual: " + data);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                counter.countDown();
            }
        });
    }
    counter.await();
    assertNull(result.get());
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 5 with ManagedCursor

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

the class ManagedCursorConcurrencyTest method testConcurrentIndividualDeletes.

@Test(timeOut = 30000)
public void testConcurrentIndividualDeletes() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(100));
    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 Threads = 10;
    final CyclicBarrier barrier = new CyclicBarrier(Threads);
    final CountDownLatch counter = new CountDownLatch(Threads);
    final AtomicBoolean gotException = new AtomicBoolean(false);
    for (int thread = 0; thread < Threads; thread++) {
        final int myThread = thread;
        cachedExecutor.execute(() -> {
            try {
                barrier.await();
                for (int i = 0; i < N; i++) {
                    int threadId = i % Threads;
                    if (threadId == myThread) {
                        cursor.delete(addedEntries.get(i));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                gotException.set(true);
            } finally {
                counter.countDown();
            }
        });
    }
    counter.await();
    assertEquals(gotException.get(), false);
    assertEquals(cursor.getMarkDeletedPosition(), addedEntries.get(addedEntries.size() - 1));
}
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

ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)302 Test (org.testng.annotations.Test)281 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)228 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)153 Position (org.apache.bookkeeper.mledger.Position)131 Entry (org.apache.bookkeeper.mledger.Entry)127 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)111 CountDownLatch (java.util.concurrent.CountDownLatch)66 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 CyclicBarrier (java.util.concurrent.CyclicBarrier)30 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)26 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)24 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 BKException (org.apache.bookkeeper.client.BKException)21 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)19 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)19 List (java.util.List)18 Future (java.util.concurrent.Future)18