Search in sources :

Example 1 with ReadCallback

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;
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) Vector(java.util.Vector)

Example 2 with ReadCallback

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);
    }
}
Also used : Longs(com.google.common.primitives.Longs) Logger(org.slf4j.Logger) Weighter(org.apache.bookkeeper.mledger.util.RangeCache.Weighter) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) LoggerFactory(org.slf4j.LoggerFactory) ReadEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Pair(org.apache.bookkeeper.mledger.util.Pair) BKException(org.apache.bookkeeper.client.BKException) TooManyRequestsException(org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SafeRun.safeRun(org.apache.bookkeeper.mledger.util.SafeRun.safeRun) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) List(java.util.List) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) RangeCache(org.apache.bookkeeper.mledger.util.RangeCache) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback)

Example 3 with ReadCallback

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();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) CountDownLatch(java.util.concurrent.CountDownLatch) Entry(org.apache.bookkeeper.mledger.Entry) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) Test(org.testng.annotations.Test)

Aggregations

ReadCallback (org.apache.bookkeeper.client.AsyncCallback.ReadCallback)3 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)3 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)2 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 Longs (com.google.common.primitives.Longs)1 ByteBuf (io.netty.buffer.ByteBuf)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 Collection (java.util.Collection)1 List (java.util.List)1 Vector (java.util.Vector)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BKException (org.apache.bookkeeper.client.BKException)1 ReadEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntryCallback)1 Entry (org.apache.bookkeeper.mledger.Entry)1 TooManyRequestsException (org.apache.bookkeeper.mledger.ManagedLedgerException.TooManyRequestsException)1