use of org.apache.bookkeeper.client.AsyncCallback.ReadCallback in project pulsar by yahoo.
the class EntryCacheTest method getLedgerHandle.
private static LedgerHandle getLedgerHandle() {
final LedgerHandle lh = mock(LedgerHandle.class);
final LedgerEntry ledgerEntry = mock(LedgerEntry.class, Mockito.CALLS_REAL_METHODS);
doReturn(new byte[10]).when(ledgerEntry).getEntry();
doReturn(Unpooled.wrappedBuffer(new byte[10])).when(ledgerEntry).getEntryBuffer();
doReturn((long) 10).when(ledgerEntry).getLength();
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
long firstEntry = (Long) args[0];
long lastEntry = (Long) args[1];
ReadCallback callback = (ReadCallback) args[2];
Object ctx = args[3];
Vector<LedgerEntry> entries = new Vector<LedgerEntry>();
for (int i = 0; i <= (lastEntry - firstEntry); i++) {
entries.add(ledgerEntry);
}
callback.readComplete(0, lh, entries.elements(), ctx);
return null;
}
}).when(lh).asyncReadEntries(anyLong(), anyLong(), any(ReadCallback.class), any());
return lh;
}
use of org.apache.bookkeeper.client.AsyncCallback.ReadCallback in project pulsar by yahoo.
the class EntryCacheImpl method asyncReadEntry.
@Override
public void asyncReadEntry(LedgerHandle lh, PositionImpl position, final ReadEntryCallback callback, final Object ctx) {
if (log.isDebugEnabled()) {
log.debug("[{}] Reading entry ledger {}: {}", ml.getName(), lh.getId(), position.getEntryId());
}
EntryImpl entry = entries.get(position);
if (entry != null) {
EntryImpl cachedEntry = new EntryImpl(entry);
entry.release();
manager.mlFactoryMBean.recordCacheHit(cachedEntry.getLength());
callback.readEntryComplete(cachedEntry, ctx);
} else {
ReadCallback readCallback = (rc, ledgerHandle, sequence, obj) -> {
if (rc != BKException.Code.OK) {
ml.invalidateLedgerHandle(ledgerHandle, rc);
callback.readEntryFailed(new ManagedLedgerException(BKException.create(rc)), obj);
return;
}
if (sequence.hasMoreElements()) {
EntryImpl returnEntry = new EntryImpl(sequence.nextElement());
manager.mlFactoryMBean.recordCacheMiss(1, returnEntry.getLength());
ml.mbean.addReadEntriesSample(1, returnEntry.getLength());
callback.readEntryComplete(returnEntry, obj);
} else {
// got an empty sequence
callback.readEntryFailed(new ManagedLedgerException("Could not read given position"), obj);
}
};
lh.asyncReadEntries(position.getEntryId(), position.getEntryId(), readCallback, ctx);
}
}
use of org.apache.bookkeeper.client.AsyncCallback.ReadCallback in project pulsar by yahoo.
the class EntryCacheTest method testReadWithError.
@Test(timeOut = 5000)
void testReadWithError() throws Exception {
final LedgerHandle lh = getLedgerHandle();
when(lh.getId()).thenReturn((long) 0);
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
ReadCallback callback = (ReadCallback) args[2];
Object ctx = args[3];
callback.readComplete(BKException.Code.NoSuchLedgerExistsException, lh, null, ctx);
return null;
}
}).when(lh).asyncReadEntries(anyLong(), anyLong(), any(ReadCallback.class), any());
EntryCacheManager cacheManager = factory.getEntryCacheManager();
EntryCache entryCache = cacheManager.getEntryCache(ml);
byte[] data = new byte[10];
entryCache.insert(new EntryImpl(0, 2, data));
final CountDownLatch counter = new CountDownLatch(1);
entryCache.asyncReadEntry(lh, 0, 9, false, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
Assert.fail("should not complete");
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
counter.countDown();
}
}, null);
counter.await();
}
Aggregations