Search in sources :

Example 11 with ReadEntriesCallback

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

Example 12 with ReadEntriesCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback 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)

Example 13 with ReadEntriesCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.

the class EntryCacheTest method testRead.

@Test(timeOut = 5000)
void testRead() 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];
    for (int i = 0; i < 10; i++) {
        entryCache.insert(new EntryImpl(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()).asyncReadEntries(anyLong(), anyLong(), any(ReadCallback.class), any());
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) MockedBookKeeperTestCase(org.apache.bookkeeper.test.MockedBookKeeperTestCase) Entry(org.apache.bookkeeper.mledger.Entry) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Unpooled(io.netty.buffer.Unpooled) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Vector(java.util.Vector) Assert(org.testng.Assert) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Matchers.anyLong(org.mockito.Matchers.anyLong) Mockito.doReturn(org.mockito.Mockito.doReturn) Method(java.lang.reflect.Method) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BeforeMethod(org.testng.annotations.BeforeMethod) Mockito.when(org.mockito.Mockito.when) BKException(org.apache.bookkeeper.client.BKException) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) List(java.util.List) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) Mockito.mock(org.mockito.Mockito.mock) 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) ReadCallback(org.apache.bookkeeper.client.AsyncCallback.ReadCallback) Test(org.testng.annotations.Test)

Example 14 with ReadEntriesCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.

the class ReplicatorTest method testCloseReplicatorStartProducer.

/**
     * It verifies that PersistentReplicator considers CursorAlreadyClosedException as non-retriable-read exception and
     * it should closed the producer as cursor is already closed because replicator is already deleted.
     * 
     * @throws Exception
     */
@Test(timeOut = 5000)
public void testCloseReplicatorStartProducer() throws Exception {
    DestinationName dest = DestinationName.get("persistent://pulsar/global/ns1/closeCursor");
    // Producer on r1
    MessageProducer producer1 = new MessageProducer(url1, dest);
    // Consumer on r1
    MessageConsumer consumer1 = new MessageConsumer(url1, dest);
    // Consumer on r2
    MessageConsumer consumer2 = new MessageConsumer(url2, dest);
    // Replicator for r1 -> r2
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(dest.toString());
    PersistentReplicator replicator = topic.getPersistentReplicator("r2");
    // close the cursor
    Field cursorField = PersistentReplicator.class.getDeclaredField("cursor");
    cursorField.setAccessible(true);
    ManagedCursor cursor = (ManagedCursor) cursorField.get(replicator);
    cursor.close();
    // try to read entries
    CountDownLatch latch = new CountDownLatch(1);
    producer1.produce(10);
    cursor.asyncReadEntriesOrWait(10, new ReadEntriesCallback() {

        @Override
        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            latch.countDown();
            fail("it should have been failed");
        }

        @Override
        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            latch.countDown();
            assertTrue(exception instanceof CursorAlreadyClosedException);
        }
    }, null);
    // replicator-readException: cursorAlreadyClosed
    replicator.readEntriesFailed(new CursorAlreadyClosedException("Cursor already closed exception"), null);
    // wait replicator producer to be closed
    Thread.sleep(1000);
    // Replicator producer must be closed
    Field producerField = PersistentReplicator.class.getDeclaredField("producer");
    producerField.setAccessible(true);
    ProducerImpl replicatorProducer = (ProducerImpl) producerField.get(replicator);
    assertEquals(replicatorProducer, null);
    producer1.close();
    consumer1.close();
    consumer2.close();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) PersistentReplicator(com.yahoo.pulsar.broker.service.persistent.PersistentReplicator) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Field(java.lang.reflect.Field) ProducerImpl(com.yahoo.pulsar.client.impl.ProducerImpl) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException) Test(org.testng.annotations.Test)

Example 15 with ReadEntriesCallback

use of org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback in project pulsar by yahoo.

the class ManagedCursorTest method asyncReadWithoutErrors.

@Test(timeOut = 20000)
void asyncReadWithoutErrors() throws Exception {
    ManagedLedger ledger = factory.open("my_test_ledger");
    ManagedCursor cursor = ledger.openCursor("c1");
    ledger.addEntry("dummy-entry-1".getBytes(Encoding));
    final CountDownLatch counter = new CountDownLatch(1);
    cursor.asyncReadEntries(100, new ReadEntriesCallback() {

        public void readEntriesComplete(List<Entry> entries, Object ctx) {
            assertNull(ctx);
            assertEquals(entries.size(), 1);
            entries.forEach(e -> e.release());
            counter.countDown();
        }

        public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
            fail(exception.getMessage());
        }
    }, null);
    counter.await();
}
Also used : ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) ZNodeProtobufFormat(org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.ZNodeProtobufFormat) Arrays(java.util.Arrays) Assert.assertNull(org.testng.Assert.assertNull) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) MockedBookKeeperTestCase(org.apache.bookkeeper.test.MockedBookKeeperTestCase) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertEquals(org.testng.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Assert.assertFalse(org.testng.Assert.assertFalse) ExecutorService(java.util.concurrent.ExecutorService) Charsets(com.google.common.base.Charsets) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) CyclicBarrier(java.util.concurrent.CyclicBarrier) Logger(org.slf4j.Logger) Factory(org.testng.annotations.Factory) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) Position(org.apache.bookkeeper.mledger.Position) IndividualDeletedEntries(org.apache.bookkeeper.mledger.ManagedCursor.IndividualDeletedEntries) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) Sets(com.google.common.collect.Sets) AddEntryCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MarkDeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ReadEntriesCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)17 ReadEntriesCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.ReadEntriesCallback)17 Entry (org.apache.bookkeeper.mledger.Entry)17 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)17 Test (org.testng.annotations.Test)13 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)10 List (java.util.List)9 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)8 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)7 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)6 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)6 BKException (org.apache.bookkeeper.client.BKException)5 MarkDeleteCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback)5 Position (org.apache.bookkeeper.mledger.Position)5 Lists (com.google.common.collect.Lists)4 Sets (com.google.common.collect.Sets)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Set (java.util.Set)4 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)4