use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback in project pulsar by yahoo.
the class ManagedLedgerTest method asyncAPI.
@Test(timeOut = 20000)
public void asyncAPI() throws Throwable {
final CountDownLatch counter = new CountDownLatch(1);
factory.asyncOpen("my_test_ledger", new ManagedLedgerConfig(), new OpenLedgerCallback() {
@Override
public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
ledger.asyncOpenCursor("test-cursor", new OpenCursorCallback() {
@Override
public void openCursorComplete(ManagedCursor cursor, Object ctx) {
ManagedLedger ledger = (ManagedLedger) ctx;
ledger.asyncAddEntry("test".getBytes(Encoding), new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
@SuppressWarnings("unchecked") Pair<ManagedLedger, ManagedCursor> pair = (Pair<ManagedLedger, ManagedCursor>) ctx;
ManagedLedger ledger = pair.first;
ManagedCursor cursor = pair.second;
assertEquals(ledger.getNumberOfEntries(), 1);
assertEquals(ledger.getTotalSize(), "test".getBytes(Encoding).length);
cursor.asyncReadEntries(2, new ReadEntriesCallback() {
@Override
public void readEntriesComplete(List<Entry> entries, Object ctx) {
ManagedCursor cursor = (ManagedCursor) ctx;
assertEquals(entries.size(), 1);
Entry entry = entries.get(0);
assertEquals(new String(entry.getDataAndRelease(), Encoding), "test");
log.debug("Mark-Deleting to position {}", entry.getPosition());
cursor.asyncMarkDelete(entry.getPosition(), new MarkDeleteCallback() {
@Override
public void markDeleteComplete(Object ctx) {
log.debug("Mark delete complete");
ManagedCursor cursor = (ManagedCursor) ctx;
assertEquals(cursor.hasMoreEntries(), false);
counter.countDown();
}
@Override
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, cursor);
}
@Override
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, cursor);
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, new Pair<ManagedLedger, ManagedCursor>(ledger, cursor));
}
@Override
public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, ledger);
}
@Override
public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, null);
counter.await();
log.info("Test completed");
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback in project pulsar by yahoo.
the class ManagedLedgerTest method doubleAsyncAddEntryWithoutError.
@Test(timeOut = 20000)
public void doubleAsyncAddEntryWithoutError() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
ledger.openCursor("test-cursor");
final CountDownLatch done = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
final String content = "dummy-entry-" + i;
ledger.asyncAddEntry(content.getBytes(Encoding), new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
assertNotNull(ctx);
log.info("Successfully added {}", content);
done.countDown();
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, this);
}
done.await();
assertEquals(ledger.getNumberOfEntries(), 10);
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback in project pulsar by yahoo.
the class ManagedLedgerTest method testAsyncAddEntryAndSyncClose.
@Test(timeOut = 20000)
public void testAsyncAddEntryAndSyncClose() throws Exception {
ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(10);
ManagedLedger ledger = factory.open("my_test_ledger", config);
ledger.openCursor("c1");
assertEquals(ledger.getNumberOfEntries(), 0);
final CountDownLatch counter = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
String content = "entry-" + i;
ledger.asyncAddEntry(content.getBytes(Encoding), new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
counter.countDown();
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, null);
}
counter.await();
assertEquals(ledger.getNumberOfEntries(), 100);
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback in project pulsar by yahoo.
the class ManagedLedgerTest method testBacklogCursor.
@Test
public void testBacklogCursor() throws Exception {
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("cache_backlog_ledger");
final long maxMessageCacheRetentionTimeMillis = 100;
Field field = ManagedLedgerImpl.class.getDeclaredField("maxMessageCacheRetentionTimeMillis");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(ledger, maxMessageCacheRetentionTimeMillis);
Field backlogThresholdField = ManagedLedgerImpl.class.getDeclaredField("maxActiveCursorBacklogEntries");
backlogThresholdField.setAccessible(true);
final long maxActiveCursorBacklogEntries = (long) backlogThresholdField.get(ledger);
// Open Cursor also adds cursor into activeCursor-container
ManagedCursor cursor1 = ledger.openCursor("c1");
ManagedCursor cursor2 = ledger.openCursor("c2");
final int totalBacklogSizeEntries = (int) maxActiveCursorBacklogEntries;
CountDownLatch latch = new CountDownLatch(totalBacklogSizeEntries);
for (int i = 0; i < totalBacklogSizeEntries + 1; i++) {
// 5 bytes
String content = "entry";
ByteBuf entry = getMessageWithMetadata(content.getBytes());
ledger.asyncAddEntry(entry, new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
latch.countDown();
entry.release();
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
latch.countDown();
entry.release();
}
}, null);
}
latch.await();
// Verify: cursors are active as :haven't started deactivateBacklogCursor scan
assertTrue(cursor1.isActive());
assertTrue(cursor2.isActive());
// it allows message to be older enough to be considered in backlog
Thread.sleep(maxMessageCacheRetentionTimeMillis * 2);
// deactivate backlog cursors
ledger.checkBackloggedCursors();
Thread.sleep(100);
// both cursors have to be inactive
assertFalse(cursor1.isActive());
assertFalse(cursor2.isActive());
// read entries so, cursor1 reaches maxBacklog threshold again to be active again
List<Entry> entries1 = cursor1.readEntries(50);
for (Entry entry : entries1) {
log.info("Read entry. Position={} Content='{}'", entry.getPosition(), new String(entry.getData()));
entry.release();
}
// activate cursors which caught up maxbacklog threshold
ledger.checkBackloggedCursors();
// verify: cursor1 has consumed messages so, under maxBacklog threshold => active
assertTrue(cursor1.isActive());
// verify: cursor2 has not consumed messages so, above maxBacklog threshold => inactive
assertFalse(cursor2.isActive());
ledger.close();
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback in project pulsar by yahoo.
the class ManagedLedgerTest method asyncAddEntryWithError.
@Test(timeOut = 20000)
public void asyncAddEntryWithError() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
ledger.openCursor("test-cursor");
final CountDownLatch counter = new CountDownLatch(1);
stopBookKeeper();
stopZooKeeper();
ledger.asyncAddEntry("dummy-entry-1".getBytes(Encoding), new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
fail("Should have failed");
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
counter.countDown();
}
}, null);
counter.await();
}
Aggregations