@Override public PositionImpl getPosition() { return new PositionImpl(ledgerId, entryId); }
public static PositionImpl get(long ledgerId, long entryId) { return new PositionImpl(ledgerId, entryId); }
public static PositionImpl get(PositionImpl other) { return new PositionImpl(other); }
PositionImpl getFirstPosition() { Long ledgerId = ledgers.firstKey(); return ledgerId == null ? null : new PositionImpl(ledgerId, -1); }
private void recoverIndividualDeletedMessages(List<MLDataFormats.MessageRange> individualDeletedMessagesList) { lock.writeLock().lock(); try { individualDeletedMessages.clear(); individualDeletedMessagesList.forEach(messageRange -> individualDeletedMessages .add(Range.openClosed(new PositionImpl(messageRange.getLowerEndpoint()), new PositionImpl(messageRange.getUpperEndpoint())))); } finally { lock.writeLock().unlock(); } }
@Test(expectedExceptions = NullPointerException.class) public void nullParam() { new PositionImpl((PositionInfo) null); }
@Override public Position getFirstPosition() { Long firstLedgerId = ledger.getLedgersInfo().firstKey(); return firstLedgerId == null ? null : new PositionImpl(firstLedgerId, 0); }
PositionImpl startReadOperationOnLedger(PositionImpl position) { long ledgerId = ledgers.ceilingKey(position.getLedgerId()); if (ledgerId != position.getLedgerId()) { // The ledger pointed by this position does not exist anymore. It was deleted because it was empty. We need // to skip on the next available ledger position = new PositionImpl(ledgerId, 0); } return position; }
@Test public void simpleTest() { PositionImpl pos = new PositionImpl(1, 2); assertEquals(pos.getLedgerId(), 1); assertEquals(pos.getEntryId(), 2); assertEquals(pos, new PositionImpl(1, 2)); assertFalse(pos.equals(new PositionImpl(1, 3))); assertFalse(pos.equals(new PositionImpl(3, 2))); assertFalse(pos.equals("1:2")); }
@Test void updatingCursorOutsideContainer() throws Exception { ManagedCursorContainer container = new ManagedCursorContainer(); ManagedCursor cursor1 = new MockManagedCursor(container, "test1", new PositionImpl(5, 5)); container.add(cursor1); assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5)); MockManagedCursor cursor2 = new MockManagedCursor(container, "test2", new PositionImpl(2, 2)); container.add(cursor2); assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2)); cursor2.position = new PositionImpl(8, 8); // Until we don't update the container, the ordering will not change assertEquals(container.getSlowestReaderPosition(), new PositionImpl(2, 2)); container.cursorUpdated(cursor2, cursor2.position); assertEquals(container.getSlowestReaderPosition(), new PositionImpl(5, 5)); }
private ReadOnlyCursor createReadOnlyCursor(PositionImpl startPosition) { if (ledgers.isEmpty()) { lastConfirmedEntry = PositionImpl.earliest; } else if (ledgers.lastEntry().getValue().getEntries() > 0) { // Last ledger has some of the entries lastConfirmedEntry = new PositionImpl(ledgers.lastKey(), ledgers.lastEntry().getValue().getEntries() - 1); } else { // Last ledger is empty. If there is a previous ledger, position on the last entry of that ledger if (ledgers.size() > 1) { long lastLedgerId = ledgers.lastKey(); LedgerInfo li = ledgers.headMap(lastLedgerId, false).lastEntry().getValue(); lastConfirmedEntry = new PositionImpl(li.getLedgerId(), li.getEntries() - 1); } else { lastConfirmedEntry = PositionImpl.earliest; } } ReadOnlyCursorImpl cursor = new ReadOnlyCursorImpl(bookKeeper, config, this, startPosition, "read-only-cursor"); return cursor; }
@Test public void hashes() throws Exception { PositionImpl p1 = new PositionImpl(5, 15); PositionImpl p2 = new PositionImpl(PositionInfo.parseFrom(p1.getPositionInfo().toByteArray())); assertEquals(p2.getLedgerId(), 5); assertEquals(p2.getEntryId(), 15); assertEquals(new PositionImpl(5, 15).hashCode(), p2.hashCode()); } }
/** * If we fail to recover the cursor ledger, we want to still open the ML and rollback. * * @param info */ private PositionImpl getRollbackPosition(ManagedCursorInfo info) { PositionImpl firstPosition = ledger.getFirstPosition(); PositionImpl snapshottedPosition = new PositionImpl(info.getMarkDeleteLedgerId(), info.getMarkDeleteEntryId()); if (firstPosition == null) { // There are no ledgers in the ML, any position is good return snapshottedPosition; } else if (snapshottedPosition.compareTo(firstPosition) < 0) { // The snapshotted position might be pointing to a ledger that was already deleted return firstPosition; } else { return snapshottedPosition; } }
@Test(timeOut = 20000) void subscribeToEarliestPositionWithImmediateDeletion() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1)); /* Position p1 = */ ledger.addEntry("entry-1".getBytes()); /* Position p2 = */ ledger.addEntry("entry-2".getBytes()); Position p3 = ledger.addEntry("entry-3".getBytes()); Thread.sleep(300); ManagedCursor c1 = ledger.newNonDurableCursor(PositionImpl.earliest); assertEquals(c1.getReadPosition(), p3); assertEquals(c1.getMarkDeletedPosition(), new PositionImpl(5, -1)); }
@Test(timeOut = 20000) void seekPosition2() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2)); ManagedCursor cursor = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); ledger.addEntry("dummy-entry-2".getBytes(Encoding)); PositionImpl seekPosition = (PositionImpl) ledger.addEntry("dummy-entry-3".getBytes(Encoding)); ledger.addEntry("dummy-entry-4".getBytes(Encoding)); ledger.addEntry("dummy-entry-5".getBytes(Encoding)); ledger.addEntry("dummy-entry-6".getBytes(Encoding)); cursor.seek(new PositionImpl(seekPosition.getLedgerId(), seekPosition.getEntryId())); }
@Test(timeOut = 20000) void testResetCursor() throws Exception { ManagedLedger ledger = factory.open("my_test_move_cursor_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(10)); ManagedCursor cursor = ledger.newNonDurableCursor(PositionImpl.latest); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); ledger.addEntry("dummy-entry-2".getBytes(Encoding)); ledger.addEntry("dummy-entry-3".getBytes(Encoding)); PositionImpl lastPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding)); final AtomicBoolean moveStatus = new AtomicBoolean(false); PositionImpl resetPosition = new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() - 2); try { cursor.resetCursor(resetPosition); moveStatus.set(true); } catch (Exception e) { log.warn("error in reset cursor", e.getCause()); } assertTrue(moveStatus.get()); assertTrue(cursor.getReadPosition().equals(resetPosition)); cursor.close(); ledger.close(); }
@Test(timeOut = 20000) void seekPosition() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(10)); ManagedCursor cursor = ledger.openCursor("c1"); ledger.addEntry("dummy-entry-1".getBytes(Encoding)); ledger.addEntry("dummy-entry-2".getBytes(Encoding)); ledger.addEntry("dummy-entry-3".getBytes(Encoding)); PositionImpl lastPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding)); cursor.seek(new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() - 1)); }
@Test(timeOut = 20000) void markDeleteSkippingMessage() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(10)); ManagedCursor cursor = ledger.openCursor("c1"); Position p1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding)); Position p2 = ledger.addEntry("dummy-entry-2".getBytes(Encoding)); ledger.addEntry("dummy-entry-3".getBytes(Encoding)); PositionImpl p4 = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding)); assertEquals(cursor.getNumberOfEntries(), 4); cursor.markDelete(p1); assertEquals(cursor.hasMoreEntries(), true); assertEquals(cursor.getNumberOfEntries(), 3); assertEquals(cursor.getReadPosition(), p2); List<Entry> entries = cursor.readEntries(1); assertEquals(entries.size(), 1); assertEquals(new String(entries.get(0).getData(), Encoding), "dummy-entry-2"); entries.forEach(e -> e.release()); cursor.markDelete(p4); assertEquals(cursor.hasMoreEntries(), false); assertEquals(cursor.getNumberOfEntries(), 0); assertEquals(cursor.getReadPosition(), new PositionImpl(p4.getLedgerId(), p4.getEntryId() + 1)); }
@Test public void totalSizeTest() throws Exception { ManagedLedgerConfig conf = new ManagedLedgerConfig(); conf.setMaxEntriesPerLedger(1); ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", conf); ManagedCursor c1 = ledger.openCursor("c1"); ledger.addEntry(new byte[10], 1, 8); assertEquals(ledger.getTotalSize(), 8); PositionImpl p2 = (PositionImpl) ledger.addEntry(new byte[12], 2, 5); assertEquals(ledger.getTotalSize(), 13); c1.markDelete(new PositionImpl(p2.getLedgerId(), -1)); // Wait for background trimming Thread.sleep(400); assertEquals(ledger.getTotalSize(), 5); }
@Test // (timeOut = 20000) void subscribeToEarliestPositionWithDeferredDeletion() throws Exception { ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1) .setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(1)); Position p1 = ledger.addEntry("entry-1".getBytes()); Position p2 = ledger.addEntry("entry-2".getBytes()); /* Position p3 = */ ledger.addEntry("entry-3".getBytes()); /* Position p4 = */ ledger.addEntry("entry-4".getBytes()); /* Position p5 = */ ledger.addEntry("entry-5".getBytes()); /* Position p6 = */ ledger.addEntry("entry-6".getBytes()); ManagedCursor c1 = ledger.newNonDurableCursor(PositionImpl.earliest); assertEquals(c1.getReadPosition(), p1); assertEquals(c1.getMarkDeletedPosition(), new PositionImpl(3, -1)); assertEquals(c1.getNumberOfEntries(), 6); assertEquals(c1.getNumberOfEntriesInBacklog(), 6); ManagedCursor c2 = ledger.newNonDurableCursor(p1); assertEquals(c2.getReadPosition(), p2); assertEquals(c2.getMarkDeletedPosition(), p1); assertEquals(c2.getNumberOfEntries(), 5); assertEquals(c2.getNumberOfEntriesInBacklog(), 5); }