use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.
the class ManagedCursorTest method testOutOfOrderDeletePersistenceAfterCrash.
@Test(timeOut = 20000)
public void testOutOfOrderDeletePersistenceAfterCrash() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig());
ManagedCursor c1 = ledger.openCursor("c1");
List<Position> addedPositions = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Position p = ledger.addEntry(("dummy-entry-" + i).getBytes(Encoding));
addedPositions.add(p);
}
// Acknowledge few messages leaving holes
c1.delete(addedPositions.get(2));
c1.delete(addedPositions.get(5));
c1.delete(addedPositions.get(7));
c1.delete(addedPositions.get(8));
c1.delete(addedPositions.get(9));
assertEquals(c1.getNumberOfEntriesInBacklog(), 20 - 5);
// Re-Open
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ledger = factory2.open("my_test_ledger", new ManagedLedgerConfig());
c1 = ledger.openCursor("c1");
assertEquals(c1.getNumberOfEntriesInBacklog(), 20 - 5);
List<Entry> entries = c1.readEntries(20);
assertEquals(entries.size(), 20 - 5);
List<String> entriesStr = entries.stream().map(e -> new String(e.getDataAndRelease(), Encoding)).collect(Collectors.toList());
assertEquals(entriesStr.get(0), "dummy-entry-0");
assertEquals(entriesStr.get(1), "dummy-entry-1");
// Entry-2 was deleted
assertEquals(entriesStr.get(2), "dummy-entry-3");
assertEquals(entriesStr.get(3), "dummy-entry-4");
// Entry-6 was deleted
assertEquals(entriesStr.get(4), "dummy-entry-6");
assertFalse(c1.hasMoreEntries());
factory2.shutdown();
}
use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.
the class ManagedCursorTest method errorRecoveringCursor3.
@Test(timeOut = 20000)
void errorRecoveringCursor3() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
Position p1 = ledger.addEntry("entry".getBytes());
ledger.addEntry("entry".getBytes());
ManagedCursor c1 = ledger.openCursor("c1");
Position p3 = ledger.addEntry("entry".getBytes());
assertEquals(c1.getReadPosition(), p3);
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
bkc.failAfter(4, BKException.Code.ReadException);
ledger = factory2.open("my_test_ledger");
c1 = ledger.openCursor("c1");
// Verify the ManagedCursor was rewind back to the snapshotted position
assertEquals(c1.getReadPosition(), p3);
factory2.shutdown();
}
use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.
the class ManagedCursorTest method asyncReadWithErrors.
@Test(timeOut = 20000)
void asyncReadWithErrors() throws Exception {
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger");
ManagedCursor cursor = ledger.openCursor("c1");
ledger.addEntry("dummy-entry-1".getBytes(Encoding));
final CountDownLatch counter = new CountDownLatch(1);
stopBookKeeper();
cursor.asyncReadEntries(100, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
entries.forEach(e -> e.release());
counter.countDown();
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
fail("async-call should not have failed");
}
}, null);
counter.await();
cursor.rewind();
// Clear the cache to force reading from BK
ledger.entryCache.clear();
final CountDownLatch counter2 = new CountDownLatch(1);
cursor.asyncReadEntries(100, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
fail("async-call should have failed");
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
counter2.countDown();
}
}, null);
counter2.await();
}
use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.
the class ManagedCursorTest method readTwice.
@Test(timeOut = 20000)
void readTwice() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
ManagedCursor c1 = ledger.openCursor("c1");
ManagedCursor c2 = ledger.openCursor("c2");
ledger.addEntry("entry-1".getBytes(Encoding));
ledger.addEntry("entry-2".getBytes(Encoding));
List<Entry> entries = c1.readEntries(2);
assertEquals(entries.size(), 2);
entries.forEach(e -> e.release());
entries = c1.readEntries(2);
assertEquals(entries.size(), 0);
entries.forEach(e -> e.release());
entries = c2.readEntries(2);
assertEquals(entries.size(), 2);
entries.forEach(e -> e.release());
entries = c2.readEntries(2);
assertEquals(entries.size(), 0);
entries.forEach(e -> e.release());
}
use of org.apache.bookkeeper.mledger.ManagedCursor in project pulsar by yahoo.
the class ManagedCursorTest method deleteCursor.
@Test(timeOut = 20000)
void deleteCursor() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
ManagedCursor c1 = ledger.openCursor("c1");
ledger.addEntry("entry-1".getBytes(Encoding));
Position p2 = ledger.addEntry("entry-2".getBytes(Encoding));
assertEquals(c1.getNumberOfEntries(), 2);
// Remove and recreate the same cursor
ledger.deleteCursor("c1");
try {
c1.readEntries(10);
fail("must fail, the cursor should be closed");
} catch (ManagedLedgerException e) {
// ok
}
try {
c1.markDelete(p2);
fail("must fail, the cursor should be closed");
} catch (ManagedLedgerException e) {
// ok
}
c1 = ledger.openCursor("c1");
assertEquals(c1.getNumberOfEntries(), 0);
c1.close();
try {
c1.readEntries(10);
fail("must fail, the cursor should be closed");
} catch (ManagedLedgerException e) {
// ok
}
c1.close();
}
Aggregations