Search in sources :

Example 91 with Position

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

the class ManagedCursorImpl method findNewestMatching.

@Override
public Position findNewestMatching(Predicate<Entry> condition) throws InterruptedException, ManagedLedgerException {
    final CountDownLatch counter = new CountDownLatch(1);
    class Result {

        ManagedLedgerException exception = null;

        Position position = null;
    }
    final Result result = new Result();
    asyncFindNewestMatching(FindPositionConstraint.SearchActiveEntries, condition, new FindEntryCallback() {

        @Override
        public void findEntryComplete(Position position, Object ctx) {
            result.position = position;
            counter.countDown();
        }

        @Override
        public void findEntryFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            counter.countDown();
        }
    }, null);
    counter.await();
    if (result.exception != null) {
        throw result.exception;
    }
    return result.position;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ManagedLedgerImpl.createManagedLedgerException(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) FindEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.FindEntryCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 92 with Position

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

the class OpFindNewest method readEntryComplete.

@Override
public void readEntryComplete(Entry entry, Object ctx) {
    final Position position = entry.getPosition();
    switch(state) {
        case checkFirst:
            if (!condition.apply(entry)) {
                callback.findEntryComplete(null, OpFindNewest.this.ctx);
                return;
            } else {
                lastMatchedPosition = position;
                // check last entry
                state = State.checkLast;
                searchPosition = cursor.ledger.getPositionAfterN(searchPosition, max, PositionBound.startExcluded);
                find();
            }
            break;
        case checkLast:
            if (condition.apply(entry)) {
                callback.findEntryComplete(position, OpFindNewest.this.ctx);
                return;
            } else {
                // start binary search
                state = State.searching;
                searchPosition = cursor.ledger.getPositionAfterN(startPosition, mid(), PositionBound.startExcluded);
                find();
            }
            break;
        case searching:
            if (condition.apply(entry)) {
                // mid - last
                lastMatchedPosition = position;
                min = mid();
            } else {
                // start - mid
                max = mid() - 1;
            }
            if (max <= min) {
                callback.findEntryComplete(lastMatchedPosition, OpFindNewest.this.ctx);
                return;
            }
            searchPosition = cursor.ledger.getPositionAfterN(startPosition, mid(), PositionBound.startExcluded);
            find();
    }
}
Also used : Position(org.apache.bookkeeper.mledger.Position)

Example 93 with Position

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

the class OpReadEntry method readEntriesComplete.

@Override
public void readEntriesComplete(List<Entry> returnedEntries, Object ctx) {
    // Filter the returned entries for individual deleted messages
    int entriesSize = returnedEntries.size();
    final PositionImpl lastPosition = (PositionImpl) returnedEntries.get(entriesSize - 1).getPosition();
    if (log.isDebugEnabled()) {
        log.debug("[{}][{}] Read entries succeeded batch_size={} cumulative_size={} requested_count={}", cursor.ledger.getName(), cursor.getName(), returnedEntries.size(), entries.size(), count);
    }
    List<Entry> filteredEntries = cursor.filterReadEntries(returnedEntries);
    entries.addAll(filteredEntries);
    // if entries have been filtered out then try to skip reading of already deletedMessages in that range
    final Position nexReadPosition = entriesSize != filteredEntries.size() ? cursor.getNextAvailablePosition(lastPosition) : lastPosition.getNext();
    updateReadPosition(nexReadPosition);
    checkReadCompletion();
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) Position(org.apache.bookkeeper.mledger.Position)

Example 94 with Position

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

the class PersistentMessageFinderTest method findMessage.

CompletableFuture<Void> findMessage(final Result result, final ManagedCursor c1, final long timestamp) {
    PersistentMessageFinder messageFinder = new PersistentMessageFinder("topicname", c1);
    final CompletableFuture<Void> future = new CompletableFuture<>();
    messageFinder.findMessages(timestamp, new AsyncCallbacks.FindEntryCallback() {

        @Override
        public void findEntryComplete(Position position, Object ctx) {
            result.position = position;
            future.complete(null);
        }

        @Override
        public void findEntryFailed(ManagedLedgerException exception, Object ctx) {
            result.exception = exception;
            future.completeExceptionally(exception);
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) Position(org.apache.bookkeeper.mledger.Position) PersistentMessageFinder(org.apache.pulsar.broker.service.persistent.PersistentMessageFinder) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks)

Example 95 with Position

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

the class PersistentMessageFinderTest method testPersistentMessageFinder.

@Test
void testPersistentMessageFinder() throws Exception {
    final String ledgerAndCursorName = "testPersistentMessageFinder";
    int entriesPerLedger = 2;
    long beginTimestamp = System.currentTimeMillis();
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setRetentionSizeInMB(10);
    config.setMaxEntriesPerLedger(entriesPerLedger);
    config.setRetentionTime(1, TimeUnit.HOURS);
    ManagedLedger ledger = factory.open(ledgerAndCursorName, config);
    ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor(ledgerAndCursorName);
    ledger.addEntry(createMessageWrittenToLedger("retained1"));
    // space apart message publish times
    Thread.sleep(100);
    ledger.addEntry(createMessageWrittenToLedger("retained2"));
    Thread.sleep(100);
    Position newPosition = ledger.addEntry(createMessageWrittenToLedger("retained3"));
    Thread.sleep(100);
    long timestamp = System.currentTimeMillis();
    Thread.sleep(10);
    ledger.addEntry(createMessageWrittenToLedger("afterresetposition"));
    Position lastPosition = ledger.addEntry(createMessageWrittenToLedger("not-read"));
    List<Entry> entries = c1.readEntries(3);
    c1.markDelete(entries.get(2).getPosition());
    c1.close();
    ledger.close();
    entries.forEach(e -> e.release());
    // give timed ledger trimming a chance to run
    Thread.sleep(1000);
    ledger = factory.open(ledgerAndCursorName, config);
    c1 = (ManagedCursorImpl) ledger.openCursor(ledgerAndCursorName);
    long endTimestamp = System.currentTimeMillis();
    Result result = new Result();
    CompletableFuture<Void> future = findMessage(result, c1, timestamp);
    future.get();
    assertEquals(result.exception, null);
    assertTrue(result.position != null);
    assertEquals(result.position, newPosition);
    result.reset();
    future = findMessage(result, c1, beginTimestamp);
    future.get();
    assertEquals(result.exception, null);
    assertEquals(result.position, null);
    result.reset();
    future = findMessage(result, c1, endTimestamp);
    future.get();
    assertEquals(result.exception, null);
    assertNotEquals(result.position, null);
    assertEquals(result.position, lastPosition);
    PersistentMessageFinder messageFinder = new PersistentMessageFinder("topicname", c1);
    final AtomicBoolean ex = new AtomicBoolean(false);
    messageFinder.findEntryFailed(new ManagedLedgerException("failed"), new AsyncCallbacks.FindEntryCallback() {

        @Override
        public void findEntryComplete(Position position, Object ctx) {
        }

        @Override
        public void findEntryFailed(ManagedLedgerException exception, Object ctx) {
            ex.set(true);
        }
    });
    assertTrue(ex.get());
    PersistentMessageExpiryMonitor monitor = new PersistentMessageExpiryMonitor("topicname", c1.getName(), c1);
    monitor.findEntryFailed(new ManagedLedgerException.ConcurrentFindCursorPositionException("failed"), null);
    Field field = monitor.getClass().getDeclaredField("expirationCheckInProgress");
    field.setAccessible(true);
    assertEquals(0, field.get(monitor));
    result.reset();
    c1.close();
    ledger.close();
    factory.shutdown();
}
Also used : ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) Position(org.apache.bookkeeper.mledger.Position) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) PersistentMessageExpiryMonitor(org.apache.pulsar.broker.service.persistent.PersistentMessageExpiryMonitor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Field(java.lang.reflect.Field) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PersistentMessageFinder(org.apache.pulsar.broker.service.persistent.PersistentMessageFinder) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) 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