public long getNumberOfEntries(Range<PositionImpl> range) { return this.ledger.getNumberOfEntries(range); } }
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 long getNumberOfEntriesSinceFirstNotAckedMessage() { // sometimes for already caught up consumer: due to race condition markDeletePosition > readPosition. so, // validate it before preparing range PositionImpl markDeletePosition = this.markDeletePosition; PositionImpl readPosition = this.readPosition; return (markDeletePosition.compareTo(readPosition) < 0) ? ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, readPosition)) : 0; }
@Override public long getNumberOfActiveEntries() { long totalEntries = getNumberOfEntries(); PositionImpl pos = cursors.getSlowestReaderPosition(); if (pos == null) { // If there are no consumers, there are no active entries return 0; } else { // The slowest consumer will be in the first ledger in the list. We need to subtract the entries it has // already consumed in order to get the active entries count. return totalEntries - (pos.getEntryId() + 1); } }
protected long getNumberOfEntries(Range<PositionImpl> range) { long allEntries = ledger.getNumberOfEntries(range); if (log.isDebugEnabled()) { log.debug("[{}] getNumberOfEntries. {} allEntries: {}", ledger.getName(), range, allEntries); } long deletedEntries = 0; lock.readLock().lock(); try { for (Range<PositionImpl> r : individualDeletedMessages.asRanges()) { if (r.isConnected(range)) { Range<PositionImpl> commonEntries = r.intersection(range); long commonCount = ledger.getNumberOfEntries(commonEntries); if (log.isDebugEnabled()) { log.debug("[{}] [{}] Discounting {} entries for already deleted range {}", ledger.getName(), name, commonCount, commonEntries); } deletedEntries += commonCount; } } } finally { lock.readLock().unlock(); } if (log.isDebugEnabled()) { log.debug("[{}] Found {} entries - deleted: {}", ledger.getName(), allEntries - deletedEntries, deletedEntries); } return allEntries - deletedEntries; }
long getNumIndividualDeletedEntriesToSkip(long numEntries) { long totalEntriesToSkip = 0; long deletedMessages = 0; lock.readLock().lock(); try { PositionImpl startPosition = markDeletePosition; PositionImpl endPosition = null; for (Range<PositionImpl> r : individualDeletedMessages.asRanges()) { endPosition = r.lowerEndpoint(); if (startPosition.compareTo(endPosition) <= 0) { Range<PositionImpl> range = Range.openClosed(startPosition, endPosition); long entries = ledger.getNumberOfEntries(range); if (totalEntriesToSkip + entries >= numEntries) { break; } totalEntriesToSkip += entries; deletedMessages += ledger.getNumberOfEntries(r); startPosition = r.upperEndpoint(); } else { if (log.isDebugEnabled()) { log.debug("[{}] deletePosition {} moved ahead without clearing deleteMsgs {} for cursor {}", ledger.getName(), markDeletePosition, r.lowerEndpoint(), name); } } } } finally { lock.readLock().unlock(); } return deletedMessages; }
public long getNumberOfEntriesInStorage() { return ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, ledger.getLastPosition().getNext())); }
/** * 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); }
@Test public void discardEmptyLedgersOnError() throws Exception { ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger"); assertEquals(ledger.getLedgersInfoAsList().size(), 1); bkc.failNow(BKException.Code.NoBookieAvailableException); zkc.failNow(Code.CONNECTIONLOSS); try { ledger.addEntry("entry".getBytes()); fail("Should have received exception"); } catch (ManagedLedgerException e) { // Ok } assertEquals(ledger.getLedgersInfoAsList().size(), 0); // Next write should fail as well try { ledger.addEntry("entry".getBytes()); fail("Should have received exception"); } catch (ManagedLedgerException e) { // Ok } assertEquals(ledger.getLedgersInfoAsList().size(), 0); assertEquals(ledger.getNumberOfEntries(), 0); }
.getNumberOfEntries(Range.openClosed(markDeletePosition, range.lowerEndpoint())) <= 0) {
@Test // (timeOut = 20000) public void asyncOpenClosedLedger() throws Exception { ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my-closed-ledger"); ManagedCursor c1 = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); c1.close(); assertEquals(ledger.getNumberOfEntries(), 1); ledger.setFenced(); final CountDownLatch counter = new CountDownLatch(1); class Result { ManagedLedger instance1 = null; } final Result result = new Result(); factory.asyncOpen("my-closed-ledger", new OpenLedgerCallback() { @Override public void openLedgerComplete(ManagedLedger ledger, Object ctx) { result.instance1 = ledger; counter.countDown(); } @Override public void openLedgerFailed(ManagedLedgerException exception, Object ctx) { } }, null); counter.await(); assertNotNull(result.instance1); ManagedCursor c2 = result.instance1.openCursor("c1"); List<Entry> entries = c2.readEntries(1); assertEquals(entries.size(), 1); entries.forEach(e -> e.release()); }
@Override public void asyncFindNewestMatching(FindPositionConstraint constraint, Predicate<Entry> condition, FindEntryCallback callback, Object ctx) { OpFindNewest op; PositionImpl startPosition = null; long max = 0; switch (constraint) { case SearchAllAvailableEntries: startPosition = (PositionImpl) getFirstPosition(); max = ledger.getNumberOfEntries() - 1; break; case SearchActiveEntries: startPosition = ledger.getNextValidPosition(markDeletePosition); max = getNumberOfEntriesInStorage(); break; default: callback.findEntryFailed(new ManagedLedgerException("Unknown position constraint"), ctx); return; } if (startPosition == null) { callback.findEntryFailed(new ManagedLedgerException("Couldn't find start position"), ctx); return; } op = new OpFindNewest(this, startPosition, condition, max, callback, ctx); op.find(); }
public long getNumberOfEntries(Range<PositionImpl> range) { return this.ledger.getNumberOfEntries(range); } }
@Override public long getNumberOfEntriesSinceFirstNotAckedMessage() { return ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, readPosition)); }
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 long getNumberOfActiveEntries() { long totalEntries = getNumberOfEntries(); PositionImpl pos = cursors.getSlowestReaderPosition(); if (pos == null) { // If there are no consumers, there are no active entries return 0; } else { // The slowest consumer will be in the first ledger in the list. We need to subtract the entries it has // already consumed in order to get the active entries count. return totalEntries - (pos.getEntryId() + 1); } }
@Override public long getNumberOfActiveEntries() { long totalEntries = getNumberOfEntries(); PositionImpl pos = cursors.getSlowestReaderPosition(); if (pos == null) { // If there are no consumers, there are no active entries return 0; } else { // The slowest consumer will be in the first ledger in the list. We need to subtract the entries it has // already consumed in order to get the active entries count. return totalEntries - (pos.getEntryId() + 1); } }
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; }
public long getNumberOfEntriesInStorage() { return ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, ledger.getLastPosition().getNext())); }
public long getNumberOfEntriesInStorage() { return ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, ledger.getLastPosition().getNext())); }