Search in sources :

Example 1 with ManagedCursorImpl

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

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);
    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(com.yahoo.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(com.yahoo.pulsar.broker.service.persistent.PersistentMessageFinder) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 2 with ManagedCursorImpl

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

the class PersistentTopic method getInternalStats.

public PersistentTopicInternalStats getInternalStats() {
    PersistentTopicInternalStats stats = new PersistentTopicInternalStats();
    ManagedLedgerImpl ml = (ManagedLedgerImpl) ledger;
    stats.entriesAddedCounter = ml.getEntriesAddedCounter();
    stats.numberOfEntries = ml.getNumberOfEntries();
    stats.totalSize = ml.getTotalSize();
    stats.currentLedgerEntries = ml.getCurrentLedgerEntries();
    stats.currentLedgerSize = ml.getCurrentLedgerSize();
    stats.lastLedgerCreatedTimestamp = DATE_FORMAT.format(Instant.ofEpochMilli(ml.getLastLedgerCreatedTimestamp()));
    if (ml.getLastLedgerCreationFailureTimestamp() != 0) {
        stats.lastLedgerCreationFailureTimestamp = DATE_FORMAT.format(Instant.ofEpochMilli(ml.getLastLedgerCreationFailureTimestamp()));
    }
    stats.waitingCursorsCount = ml.getWaitingCursorsCount();
    stats.pendingAddEntriesCount = ml.getPendingAddEntriesCount();
    stats.lastConfirmedEntry = ml.getLastConfirmedEntry().toString();
    stats.state = ml.getState().toString();
    stats.ledgers = Lists.newArrayList();
    ml.getLedgersInfo().forEach((id, li) -> {
        LedgerInfo info = new LedgerInfo();
        info.ledgerId = li.getLedgerId();
        info.entries = li.getEntries();
        info.size = li.getSize();
        stats.ledgers.add(info);
    });
    stats.cursors = Maps.newTreeMap();
    ml.getCursors().forEach(c -> {
        ManagedCursorImpl cursor = (ManagedCursorImpl) c;
        CursorStats cs = new CursorStats();
        cs.markDeletePosition = cursor.getMarkDeletedPosition().toString();
        cs.readPosition = cursor.getReadPosition().toString();
        cs.waitingReadOp = cursor.hasPendingReadRequest();
        cs.pendingReadOps = cursor.getPendingReadOpsCount();
        cs.messagesConsumedCounter = cursor.getMessagesConsumedCounter();
        cs.cursorLedger = cursor.getCursorLedger();
        cs.cursorLedgerLastEntry = cursor.getCursorLedgerLastEntry();
        cs.individuallyDeletedMessages = cursor.getIndividuallyDeletedMessages();
        cs.lastLedgerSwitchTimestamp = DATE_FORMAT.format(Instant.ofEpochMilli(cursor.getLastLedgerSwitchTimestamp()));
        cs.state = cursor.getState();
        stats.cursors.put(cursor.getName(), cs);
    });
    return stats;
}
Also used : LedgerInfo(com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats.LedgerInfo) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) PersistentTopicInternalStats(com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats) CursorStats(com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats)

Aggregations

ManagedCursorImpl (org.apache.bookkeeper.mledger.impl.ManagedCursorImpl)2 PersistentMessageExpiryMonitor (com.yahoo.pulsar.broker.service.persistent.PersistentMessageExpiryMonitor)1 PersistentMessageFinder (com.yahoo.pulsar.broker.service.persistent.PersistentMessageFinder)1 PersistentTopicInternalStats (com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats)1 CursorStats (com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats)1 LedgerInfo (com.yahoo.pulsar.common.policies.data.PersistentTopicInternalStats.LedgerInfo)1 Field (java.lang.reflect.Field)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)1 Entry (org.apache.bookkeeper.mledger.Entry)1 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)1 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1 Position (org.apache.bookkeeper.mledger.Position)1 ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)1 Test (org.testng.annotations.Test)1