@Override public CompletableFuture<LedgerEntries> readAsync(long firstEntry, long lastEntry) { return readHandle.readAsync(firstEntry, lastEntry); }
@Override public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, boolean isSlowestReader, final ReadEntriesCallback callback, Object ctx) { lh.readAsync(firstEntry, lastEntry).whenComplete( (ledgerEntries, exception) -> { if (exception != null) { callback.readEntriesFailed(createManagedLedgerException(exception), ctx); return; } List<Entry> entries = Lists.newArrayList(); long totalSize = 0; try { for (LedgerEntry e : ledgerEntries) { // Insert the entries at the end of the list (they will be unsorted for now) EntryImpl entry = EntryImpl.create(e); entries.add(entry); totalSize += entry.getLength(); } } finally { ledgerEntries.close(); } mlFactoryMBean.recordCacheMiss(entries.size(), totalSize); ml.mbean.addReadEntriesSample(entries.size(), totalSize); callback.readEntriesComplete(entries, null); }); }
private static ReadHandle getLedgerHandle() { final ReadHandle lh = mock(ReadHandle.class); final LedgerEntry ledgerEntry = mock(LedgerEntry.class, Mockito.CALLS_REAL_METHODS); doReturn(Unpooled.wrappedBuffer(new byte[10])).when(ledgerEntry).getEntryBuffer(); doReturn((long) 10).when(ledgerEntry).getLength(); doAnswer((invocation) -> { Object[] args = invocation.getArguments(); long firstEntry = (Long) args[0]; long lastEntry = (Long) args[1]; Vector<LedgerEntry> entries = new Vector<LedgerEntry>(); for (int i = 0; i <= (lastEntry - firstEntry); i++) { entries.add(ledgerEntry); } LedgerEntries ledgerEntries = mock(LedgerEntries.class); doAnswer((invocation2) -> entries.iterator()).when(ledgerEntries).iterator(); return CompletableFuture.completedFuture(ledgerEntries); }).when(lh).readAsync(anyLong(), anyLong()); return lh; }
@Test(timeOut = 5000) void testReadWithError() throws Exception { final ReadHandle lh = getLedgerHandle(); when(lh.getId()).thenReturn((long) 0); doAnswer((invocation) -> { CompletableFuture<LedgerEntries> future = new CompletableFuture<>(); future.completeExceptionally(new BKNoSuchLedgerExistsException()); return future; }).when(lh).readAsync(anyLong(), anyLong()); EntryCacheManager cacheManager = factory.getEntryCacheManager(); EntryCache entryCache = cacheManager.getEntryCache(ml); byte[] data = new byte[10]; entryCache.insert(EntryImpl.create(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(); }
lh.readAsync(firstEntry, lastEntry).whenCompleteAsync( (ledgerEntries, exception) -> { if (exception != null) {
@Test(timeOut = 5000) void testRead() throws Exception { ReadHandle lh = getLedgerHandle(); when(lh.getId()).thenReturn((long) 0); EntryCacheManager cacheManager = factory.getEntryCacheManager(); EntryCache entryCache = cacheManager.getEntryCache(ml); byte[] data = new byte[10]; for (int i = 0; i < 10; i++) { entryCache.insert(EntryImpl.create(0, i, 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); entries.forEach(e -> e.release()); counter.countDown(); } public void readEntriesFailed(ManagedLedgerException exception, Object ctx) { Assert.fail("should not have failed"); } }, null); counter.await(); // Verify no entries were read from bookkeeper verify(lh, never()).readAsync(anyLong(), anyLong()); }
doReturn(entriesFuture).when(ledgerHandle).readAsync(PositionImpl.earliest.getLedgerId(), PositionImpl.earliest.getEntryId());
callback.readEntryComplete(cachedEntry, ctx); } else { lh.readAsync(position.getEntryId(), position.getEntryId()).whenCompleteAsync( (ledgerEntries, exception) -> { if (exception != null) {
/** * Read a sequence of entries synchronously. * * @param firstEntry * id of first entry of sequence * @param lastEntry * id of last entry of sequence, inclusive * @return the result of the operation */ default LedgerEntries read(long firstEntry, long lastEntry) throws BKException, InterruptedException { return FutureUtils.<LedgerEntries, BKException>result(readAsync(firstEntry, lastEntry), BKException.HANDLER); }
@Override public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, boolean isSlowestReader, final ReadEntriesCallback callback, Object ctx) { lh.readAsync(firstEntry, lastEntry).whenComplete( (ledgerEntries, exception) -> { if (exception != null) { callback.readEntriesFailed(createManagedLedgerException(exception), ctx); return; } List<Entry> entries = Lists.newArrayList(); long totalSize = 0; try { for (LedgerEntry e : ledgerEntries) { // Insert the entries at the end of the list (they will be unsorted for now) EntryImpl entry = EntryImpl.create(e); entries.add(entry); totalSize += entry.getLength(); } } finally { ledgerEntries.close(); } mlFactoryMBean.recordCacheMiss(entries.size(), totalSize); ml.mbean.addReadEntriesSample(entries.size(), totalSize); callback.readEntriesComplete(entries, null); }); }
lh.readAsync(firstEntry, lastEntry).whenCompleteAsync( (ledgerEntries, exception) -> { if (exception != null) {
callback.readEntryComplete(cachedEntry, ctx); } else { lh.readAsync(position.getEntryId(), position.getEntryId()).whenCompleteAsync( (ledgerEntries, exception) -> { if (exception != null) {