private void recoverCursor(PositionImpl mdPosition) { Pair<PositionImpl, Long> lastEntryAndCounter = ledger.getLastPositionAndCounter(); this.readPosition = ledger.getNextValidPosition(mdPosition); markDeletePosition = mdPosition; // Initialize the counter such that the difference between the messages written on the ML and the // messagesConsumed is equal to the current backlog (negated). long initialBacklog = readPosition.compareTo(lastEntryAndCounter.getLeft()) < 0 ? ledger.getNumberOfEntries(Range.closed(readPosition, lastEntryAndCounter.getLeft())) : 0; messagesConsumedCounter = lastEntryAndCounter.getRight() - initialBacklog; }
@Override public void operationComplete() { log.info("[{}] Opened new cursor: {}", name, cursor); cursor.setActive(); // Update the ack position (ignoring entries that were written while the cursor was being created) cursor.initializeCursorPosition(initialPosition == InitialPosition.Latest ? getLastPositionAndCounter() : getFirstPositionAndCounter()); synchronized (this) { cursors.add(cursor); uninitializedCursors.remove(cursorName).complete(cursor); } callback.openCursorComplete(cursor, ctx); }
/** * Get the first position written in the managed ledger, alongside with the associated counter */ Pair<PositionImpl, Long> getFirstPositionAndCounter() { PositionImpl pos; long count; Pair<PositionImpl, Long> lastPositionAndCounter; do { pos = getFirstPosition(); lastPositionAndCounter = getLastPositionAndCounter(); count = lastPositionAndCounter.getRight() - getNumberOfEntries(Range.openClosed(pos, lastPositionAndCounter.getLeft())); } while (pos.compareTo(getFirstPosition()) != 0 || lastPositionAndCounter.getLeft().compareTo(getLastPosition()) != 0); return Pair.of(pos, count); }
NonDurableCursorImpl(BookKeeper bookkeeper, ManagedLedgerConfig config, ManagedLedgerImpl ledger, String cursorName, PositionImpl startCursorPosition) { super(bookkeeper, config, ledger, cursorName); // Compare with "latest" position marker by using only the ledger id. Since the C++ client is using 48bits to // store the entryId, it's not able to pass a Long.max() as entryId. In this case there's no point to require // both ledgerId and entryId to be Long.max() if (startCursorPosition == null || startCursorPosition.getLedgerId() == PositionImpl.latest.getLedgerId()) { // Start from last entry initializeCursorPosition(ledger.getLastPositionAndCounter()); } else if (startCursorPosition.equals(PositionImpl.earliest)) { // Start from invalid ledger to read from first available entry recoverCursor(ledger.getPreviousPosition(ledger.getFirstPosition())); } else { // Since the cursor is positioning on the mark-delete position, we need to take 1 step back from the desired // read-position recoverCursor(startCursorPosition); } log.info("[{}] Created non-durable cursor read-position={} mark-delete-position={}", ledger.getName(), readPosition, markDeletePosition); }
@Test public void testConsumerSubscriptionInitializePosition() throws Exception{ final int MAX_ENTRY_PER_LEDGER = 2; ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(MAX_ENTRY_PER_LEDGER); ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("lastest_earliest_ledger", config); final int totalInsertedEntries = 20; for (int i = 0; i < totalInsertedEntries; i++) { String content = "entry" + i; // 5 bytes ledger.addEntry(content.getBytes()); } // Open Cursor also adds cursor into activeCursor-container ManagedCursor latestCursor = ledger.openCursor("c1", InitialPosition.Latest); ManagedCursor earliestCursor = ledger.openCursor("c2", InitialPosition.Earliest); // Since getReadPosition returns the next position, we decrease the entryId by 1 PositionImpl p1 = (PositionImpl) latestCursor.getReadPosition(); PositionImpl p2 = (PositionImpl) earliestCursor.getReadPosition(); Pair<PositionImpl, Long> latestPositionAndCounter = ledger.getLastPositionAndCounter(); Pair<PositionImpl, Long> earliestPositionAndCounter = ledger.getFirstPositionAndCounter(); assertEquals(latestPositionAndCounter.getLeft().getNext(), p1); assertEquals(earliestPositionAndCounter.getLeft().getNext(), p2); assertEquals(latestPositionAndCounter.getRight().longValue(), totalInsertedEntries); assertEquals(earliestPositionAndCounter.getRight().longValue(), totalInsertedEntries - earliestCursor.getNumberOfEntriesInBacklog()); ledger.close(); }
private void recoverCursor(PositionImpl mdPosition) { Pair<PositionImpl, Long> lastEntryAndCounter = ledger.getLastPositionAndCounter(); this.readPosition = ledger.getNextValidPosition(mdPosition); markDeletePosition = mdPosition; // Initialize the counter such that the difference between the messages written on the ML and the // messagesConsumed is equal to the current backlog (negated). long initialBacklog = readPosition.compareTo(lastEntryAndCounter.getLeft()) < 0 ? ledger.getNumberOfEntries(Range.closed(readPosition, lastEntryAndCounter.getLeft())) : 0; messagesConsumedCounter = lastEntryAndCounter.getRight() - initialBacklog; }
@Override public void operationComplete() { log.info("[{}] Opened new cursor: {}", name, cursor); cursor.setActive(); // Update the ack position (ignoring entries that were written while the cursor was being created) cursor.initializeCursorPosition(getLastPositionAndCounter()); synchronized (this) { cursors.add(cursor); uninitializedCursors.remove(cursorName).complete(cursor); } callback.openCursorComplete(cursor, ctx); }
@Override public void operationComplete() { log.info("[{}] Opened new cursor: {}", name, cursor); cursor.setActive(); // Update the ack position (ignoring entries that were written while the cursor was being created) cursor.initializeCursorPosition(initialPosition == InitialPosition.Latest ? getLastPositionAndCounter() : getFirstPositionAndCounter()); synchronized (this) { cursors.add(cursor); uninitializedCursors.remove(cursorName).complete(cursor); } callback.openCursorComplete(cursor, ctx); }
private void recoverCursor(PositionImpl mdPosition) { Pair<PositionImpl, Long> lastEntryAndCounter = ledger.getLastPositionAndCounter(); this.readPosition = ledger.getNextValidPosition(mdPosition); markDeletePosition = mdPosition; // Initialize the counter such that the difference between the messages written on the ML and the // messagesConsumed is equal to the current backlog (negated). long initialBacklog = readPosition.compareTo(lastEntryAndCounter.first) < 0 ? ledger.getNumberOfEntries(Range.closed(readPosition, lastEntryAndCounter.first)) : 0; messagesConsumedCounter = lastEntryAndCounter.second - initialBacklog; }
/** * Get the first position written in the managed ledger, alongside with the associated counter */ Pair<PositionImpl, Long> getFirstPositionAndCounter() { PositionImpl pos; long count; Pair<PositionImpl, Long> lastPositionAndCounter; do { pos = getFirstPosition(); lastPositionAndCounter = getLastPositionAndCounter(); count = lastPositionAndCounter.getRight() - getNumberOfEntries(Range.openClosed(pos, lastPositionAndCounter.getLeft())); } while (pos.compareTo(getFirstPosition()) != 0 || lastPositionAndCounter.getLeft().compareTo(getLastPosition()) != 0); return Pair.of(pos, count); }
NonDurableCursorImpl(BookKeeper bookkeeper, ManagedLedgerConfig config, ManagedLedgerImpl ledger, String cursorName, PositionImpl startCursorPosition) { super(bookkeeper, config, ledger, cursorName); if (startCursorPosition == null || startCursorPosition.equals(PositionImpl.latest)) { // Start from last entry initializeCursorPosition(ledger.getLastPositionAndCounter()); } else if (startCursorPosition.equals(PositionImpl.earliest)) { // Start from invalid ledger to read from first available entry recoverCursor(ledger.getPreviousPosition(ledger.getFirstPosition())); } else { // Since the cursor is positioning on the mark-delete position, we need to take 1 step back from the desired // read-position recoverCursor(startCursorPosition); } log.info("[{}] Created non-durable cursor read-position={} mark-delete-position={}", ledger.getName(), readPosition, markDeletePosition); }
NonDurableCursorImpl(BookKeeper bookkeeper, ManagedLedgerConfig config, ManagedLedgerImpl ledger, String cursorName, PositionImpl startCursorPosition) { super(bookkeeper, config, ledger, cursorName); // Compare with "latest" position marker by using only the ledger id. Since the C++ client is using 48bits to // store the entryId, it's not able to pass a Long.max() as entryId. In this case there's no point to require // both ledgerId and entryId to be Long.max() if (startCursorPosition == null || startCursorPosition.getLedgerId() == PositionImpl.latest.getLedgerId()) { // Start from last entry initializeCursorPosition(ledger.getLastPositionAndCounter()); } else if (startCursorPosition.equals(PositionImpl.earliest)) { // Start from invalid ledger to read from first available entry recoverCursor(ledger.getPreviousPosition(ledger.getFirstPosition())); } else { // Since the cursor is positioning on the mark-delete position, we need to take 1 step back from the desired // read-position recoverCursor(startCursorPosition); } log.info("[{}] Created non-durable cursor read-position={} mark-delete-position={}", ledger.getName(), readPosition, markDeletePosition); }