use of org.apache.bookkeeper.mledger.ManagedLedgerConfig in project incubator-pulsar by apache.
the class ManagedCursorConcurrencyTest method testConcurrentIndividualDeletesWithGetNthEntry.
@Test(timeOut = 30000)
public void testConcurrentIndividualDeletesWithGetNthEntry() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(100).setThrottleMarkDelete(0.5));
final ManagedCursor cursor = ledger.openCursor("c1");
final int N = 1000;
final List<Position> addedEntries = Lists.newArrayListWithExpectedSize(N);
for (int i = 0; i < N; i++) {
Position pos = ledger.addEntry("entry".getBytes());
addedEntries.add(pos);
}
final int deleteEntries = 100;
final CountDownLatch counter = new CountDownLatch(deleteEntries);
final AtomicBoolean gotException = new AtomicBoolean(false);
final AtomicInteger iteration = new AtomicInteger(0);
for (int i = 0; i < deleteEntries; i++) {
executor.submit(safeRun(() -> {
try {
cursor.asyncDelete(addedEntries.get(iteration.getAndIncrement()), new DeleteCallback() {
@Override
public void deleteComplete(Object ctx) {
// Ok
}
@Override
public void deleteFailed(ManagedLedgerException exception, Object ctx) {
exception.printStackTrace();
gotException.set(true);
}
}, null);
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}));
}
counter.await();
final int readEntries = N - deleteEntries;
final CountDownLatch readCounter = new CountDownLatch(readEntries);
final AtomicInteger successReadEntries = new AtomicInteger(0);
for (int i = 1; i <= readEntries; i++) {
try {
cursor.asyncGetNthEntry(i, IndividualDeletedEntries.Exclude, new ReadEntryCallback() {
@Override
public void readEntryComplete(Entry entry, Object ctx) {
successReadEntries.getAndIncrement();
entry.release();
readCounter.countDown();
}
@Override
public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
exception.printStackTrace();
gotException.set(true);
}
}, null);
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
}
}
readCounter.await();
assertFalse(gotException.get());
assertEquals(successReadEntries.get(), readEntries);
}
use of org.apache.bookkeeper.mledger.ManagedLedgerConfig in project incubator-pulsar by apache.
the class ManagedCursorConcurrencyTest method testConcurrentReadOfSameEntry.
@Test(timeOut = 30000)
public void testConcurrentReadOfSameEntry() throws Exception {
ManagedLedger ledger = factory.open("testConcurrentReadOfSameEntry", new ManagedLedgerConfig());
final int numCursors = 5;
final List<ManagedCursor> cursors = Lists.newArrayList();
for (int i = 0; i < numCursors; i++) {
final ManagedCursor cursor = ledger.openCursor("c" + i);
cursors.add(cursor);
}
final int N = 100;
for (int i = 0; i < N; i++) {
ledger.addEntry(("entry" + i).getBytes());
}
long currentLedger = ((PositionImpl) cursors.get(0).getMarkDeletedPosition()).getLedgerId();
// empty the cache
((ManagedLedgerImpl) ledger).entryCache.invalidateAllEntries(currentLedger);
final CyclicBarrier barrier = new CyclicBarrier(numCursors);
final CountDownLatch counter = new CountDownLatch(numCursors);
AtomicReference<String> result = new AtomicReference<String>();
for (int i = 0; i < numCursors; i++) {
final int cursorIndex = i;
final ManagedCursor cursor = cursors.get(cursorIndex);
cachedExecutor.execute(() -> {
try {
barrier.await();
for (int j = 0; j < N; j++) {
String expected = "entry" + j;
String data = new String(cursor.readEntries(1).get(0).getDataAndRelease());
if ((!expected.equals(data)) && result.get() == null) {
result.set("Mismatched entry in cursor " + (cursorIndex + 1) + " at position " + (j + 1) + "--- Expected: " + expected + ", Actual: " + data);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
counter.countDown();
}
});
}
counter.await();
assertNull(result.get());
}
use of org.apache.bookkeeper.mledger.ManagedLedgerConfig in project incubator-pulsar by apache.
the class ManagedCursorConcurrencyTest method testAckAndClose.
@Test(timeOut = 30000)
public void testAckAndClose() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger_test_ack_and_close", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
final ManagedCursor cursor = ledger.openCursor("c1");
final List<Position> addedEntries = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
Position pos = ledger.addEntry("entry".getBytes());
addedEntries.add(pos);
}
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
// Deleter thread
cachedExecutor.execute(() -> {
try {
barrier.await();
for (Position position : addedEntries) {
cursor.asyncDelete(position, deleteCallback, position);
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
});
// Reader thread
cachedExecutor.execute(() -> {
try {
barrier.await();
for (int i = 0; i < 1000; i++) {
cursor.readEntries(1).forEach(e -> e.release());
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
});
counter.await();
assertEquals(gotException.get(), false);
}
use of org.apache.bookkeeper.mledger.ManagedLedgerConfig in project incubator-pulsar by apache.
the class ManagedCursorConcurrencyTest method testMarkDeleteAndRead.
@Test
public void testMarkDeleteAndRead() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
final ManagedCursor cursor = ledger.openCursor("c1");
final List<Position> addedEntries = Lists.newArrayList();
for (int i = 0; i < 1000; i++) {
Position pos = ledger.addEntry("entry".getBytes());
addedEntries.add(pos);
}
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
Thread deleter = new Thread() {
public void run() {
try {
barrier.await();
for (Position position : addedEntries) {
cursor.markDelete(position);
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
Thread reader = new Thread() {
public void run() {
try {
barrier.await();
for (int i = 0; i < 1000; i++) {
cursor.readEntries(1).forEach(e -> e.release());
}
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
deleter.start();
reader.start();
counter.await();
assertEquals(gotException.get(), false);
}
use of org.apache.bookkeeper.mledger.ManagedLedgerConfig in project incubator-pulsar by apache.
the class ManagedCursorListAckTest method testMultiPositionDelete.
@Test(timeOut = 20000)
void testMultiPositionDelete() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
ManagedCursor c1 = ledger.openCursor("c1");
Position p0 = c1.getMarkDeletedPosition();
Position p1 = ledger.addEntry("dummy-entry-1".getBytes(Encoding));
Position p2 = ledger.addEntry("dummy-entry-2".getBytes(Encoding));
Position p3 = ledger.addEntry("dummy-entry-3".getBytes(Encoding));
Position p4 = ledger.addEntry("dummy-entry-4".getBytes(Encoding));
Position p5 = ledger.addEntry("dummy-entry-5".getBytes(Encoding));
Position p6 = ledger.addEntry("dummy-entry-6".getBytes(Encoding));
Position p7 = ledger.addEntry("dummy-entry-7".getBytes(Encoding));
assertEquals(c1.getNumberOfEntries(), 7);
assertEquals(c1.getNumberOfEntriesInBacklog(), 7);
c1.delete(Lists.newArrayList(p2, p3, p5, p7));
assertEquals(c1.getNumberOfEntries(), 3);
assertEquals(c1.getNumberOfEntriesInBacklog(), 3);
assertEquals(c1.getMarkDeletedPosition(), p0);
c1.delete(Lists.newArrayList(p1));
assertEquals(c1.getNumberOfEntries(), 2);
assertEquals(c1.getNumberOfEntriesInBacklog(), 2);
assertEquals(c1.getMarkDeletedPosition(), p3);
c1.delete(Lists.newArrayList(p4, p6, p7));
assertEquals(c1.getNumberOfEntries(), 0);
assertEquals(c1.getNumberOfEntriesInBacklog(), 0);
assertEquals(c1.getMarkDeletedPosition(), p7);
}
Aggregations