use of org.apache.bookkeeper.client.LedgerHandle in project pulsar by yahoo.
the class ManagedLedgerImpl method asyncReadEntries.
void asyncReadEntries(OpReadEntry opReadEntry) {
final State state = STATE_UPDATER.get(this);
if (state == State.Fenced || state == State.Closed) {
opReadEntry.readEntriesFailed(new ManagedLedgerFencedException(), opReadEntry.ctx);
return;
}
long ledgerId = opReadEntry.readPosition.getLedgerId();
LedgerHandle currentLedger = this.currentLedger;
if (ledgerId == currentLedger.getId()) {
// Current writing ledger is not in the cache (since we don't want
// it to be automatically evicted), and we cannot use 2 different
// ledger handles (read & write)for the same ledger.
internalReadFromLedger(currentLedger, opReadEntry);
} else {
LedgerInfo ledgerInfo = ledgers.get(ledgerId);
if (ledgerInfo == null || ledgerInfo.getEntries() == 0) {
// Cursor is pointing to a empty ledger, there's no need to try opening it. Skip this ledger and
// move to the next one
opReadEntry.updateReadPosition(new PositionImpl(opReadEntry.readPosition.getLedgerId() + 1, 0));
opReadEntry.checkReadCompletion();
return;
}
// Get a ledger handle to read from
getLedgerHandle(ledgerId).thenAccept(ledger -> {
internalReadFromLedger(ledger, opReadEntry);
}).exceptionally(ex -> {
log.error("[{}] Error opening ledger for reading at position {} - {}", name, opReadEntry.readPosition, ex.getMessage());
opReadEntry.readEntriesFailed(new ManagedLedgerException(ex), opReadEntry.ctx);
return null;
});
}
}
use of org.apache.bookkeeper.client.LedgerHandle 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.LedgerHandle in project pulsar by yahoo.
the class SimpleBookKeeperTest method simpleTest.
@Test
public void simpleTest() throws Exception {
LedgerHandle ledger = bkc.createLedger(DigestType.MAC, SECRET.getBytes());
long ledgerId = ledger.getId();
log.info("Writing to ledger: {}", ledgerId);
for (int i = 0; i < 10; i++) {
String content = "entry-" + i;
ledger.addEntry(content.getBytes(Encoding));
}
ledger.close();
ledger = bkc.openLedger(ledgerId, DigestType.MAC, SECRET.getBytes());
Enumeration<LedgerEntry> entries = ledger.readEntries(0, 9);
while (entries.hasMoreElements()) {
LedgerEntry entry = entries.nextElement();
String content = new String(entry.getEntry(), Encoding);
log.info("Entry {} lenght={} content='{}'", entry.getEntryId(), entry.getLength(), content);
}
ledger.close();
}
use of org.apache.bookkeeper.client.LedgerHandle in project pulsar by yahoo.
the class EntryCacheTest method testReadMissingMultiple.
@Test(timeOut = 5000)
void testReadMissingMultiple() throws Exception {
LedgerHandle lh = getLedgerHandle();
when(lh.getId()).thenReturn((long) 0);
EntryCacheManager cacheManager = factory.getEntryCacheManager();
EntryCache entryCache = cacheManager.getEntryCache(ml);
byte[] data = new byte[10];
entryCache.insert(new EntryImpl(0, 0, data));
entryCache.insert(new EntryImpl(0, 2, data));
entryCache.insert(new EntryImpl(0, 5, data));
entryCache.insert(new EntryImpl(0, 8, data));
final CountDownLatch counter = new CountDownLatch(1);
entryCache.asyncReadEntry(lh, 0, 9, false, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
assertEquals(entries.size(), 10);
counter.countDown();
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
Assert.fail("should not have failed");
}
}, null);
counter.await();
}
use of org.apache.bookkeeper.client.LedgerHandle 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