Search in sources :

Example 26 with ReadOnlyCursor

use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project incubator-pulsar by apache.

the class ManagedLedgerFactoryShutdownTest method openEncounteredShutdown.

@Test(timeOut = 5000)
public void openEncounteredShutdown() throws Exception {
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(metadataStore, bookKeeper);
    CountDownLatch callbackInvoked = new CountDownLatch(2);
    factory.asyncOpen(ledgerName, new AsyncCallbacks.OpenLedgerCallback() {

        @Override
        public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
            callbackInvoked.countDown();
        }

        @Override
        public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
            callbackInvoked.countDown();
        }
    }, null);
    factory.asyncOpenReadOnlyCursor(ledgerName, PositionImpl.EARLIEST, new ManagedLedgerConfig(), new AsyncCallbacks.OpenReadOnlyCursorCallback() {

        @Override
        public void openReadOnlyCursorComplete(ReadOnlyCursor cursor, Object ctx) {
            callbackInvoked.countDown();
        }

        @Override
        public void openReadOnlyCursorFailed(ManagedLedgerException exception, Object ctx) {
            log.info("openReadOnlyCursorFailed");
            callbackInvoked.countDown();
        }
    }, null);
    log.info("Shutdown factory...");
    factory.shutdownAsync().get();
    // make zk returned after factory shutdown
    slowZk.countDown();
    // 
    Assert.assertTrue(callbackInvoked.await(5, TimeUnit.SECONDS));
    // ManagedLedgerFactoryClosedException should be thrown after factory is shutdown
    Assert.assertThrows(ManagedLedgerException.ManagedLedgerFactoryClosedException.class, () -> factory.open(ledgerName));
    Assert.assertThrows(ManagedLedgerException.ManagedLedgerFactoryClosedException.class, () -> factory.openReadOnlyCursor(ledgerName, PositionImpl.EARLIEST, new ManagedLedgerConfig()));
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ReadOnlyCursor(org.apache.bookkeeper.mledger.ReadOnlyCursor) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 27 with ReadOnlyCursor

use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project incubator-pulsar by apache.

the class ReadOnlyCursorTest method skipMultiple.

@Test
void skipMultiple() throws Exception {
    ManagedLedger ledger = factory.open("skip", new ManagedLedgerConfig().setMaxEntriesPerLedger(7).setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(-1));
    int N = 30;
    for (int i = 0; i < N; i++) {
        ledger.addEntry(("entry-" + i).getBytes());
    }
    ReadOnlyCursor cursor = factory.openReadOnlyCursor("skip", PositionImpl.EARLIEST, new ManagedLedgerConfig());
    assertEquals(cursor.getNumberOfEntries(), N);
    assertTrue(cursor.hasMoreEntries());
    cursor.skipEntries(25);
    assertEquals(cursor.getNumberOfEntries(), 5);
    assertTrue(cursor.hasMoreEntries());
    cursor.skipEntries(5);
    assertEquals(cursor.getNumberOfEntries(), 0);
    assertFalse(cursor.hasMoreEntries());
    cursor.close();
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ReadOnlyCursor(org.apache.bookkeeper.mledger.ReadOnlyCursor) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 28 with ReadOnlyCursor

use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project incubator-pulsar by apache.

the class ReadOnlyCursorTest method skip.

@Test
void skip() throws Exception {
    ManagedLedger ledger = factory.open("skip", new ManagedLedgerConfig().setMaxEntriesPerLedger(2).setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(-1));
    int N = 10;
    for (int i = 0; i < N; i++) {
        ledger.addEntry(("entry-" + i).getBytes());
    }
    ReadOnlyCursor cursor = factory.openReadOnlyCursor("skip", PositionImpl.EARLIEST, new ManagedLedgerConfig());
    assertEquals(cursor.getNumberOfEntries(), N);
    assertTrue(cursor.hasMoreEntries());
    cursor.skipEntries(5);
    assertEquals(cursor.getNumberOfEntries(), N - 5);
    assertTrue(cursor.hasMoreEntries());
    cursor.close();
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ReadOnlyCursor(org.apache.bookkeeper.mledger.ReadOnlyCursor) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 29 with ReadOnlyCursor

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

the class ManagedLedgerFactoryImpl method openReadOnlyCursor.

@Override
public ReadOnlyCursor openReadOnlyCursor(String managedLedgerName, Position startPosition, ManagedLedgerConfig config) throws InterruptedException, ManagedLedgerException {
    class Result {

        ReadOnlyCursor c = null;

        ManagedLedgerException e = null;
    }
    final Result r = new Result();
    final CountDownLatch latch = new CountDownLatch(1);
    asyncOpenReadOnlyCursor(managedLedgerName, startPosition, config, new OpenReadOnlyCursorCallback() {

        @Override
        public void openReadOnlyCursorComplete(ReadOnlyCursor cursor, Object ctx) {
            r.c = cursor;
            latch.countDown();
        }

        @Override
        public void openReadOnlyCursorFailed(ManagedLedgerException exception, Object ctx) {
            r.e = exception;
            latch.countDown();
        }
    }, null);
    latch.await();
    if (r.e != null) {
        throw r.e;
    }
    return r.c;
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerException.getManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException) ReadOnlyCursor(org.apache.bookkeeper.mledger.ReadOnlyCursor) OpenReadOnlyCursorCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.OpenReadOnlyCursorCallback) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 30 with ReadOnlyCursor

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

the class ReadOnlyCursorTest method skipMultiple.

@Test
void skipMultiple() throws Exception {
    ManagedLedger ledger = factory.open("skip", new ManagedLedgerConfig().setMaxEntriesPerLedger(7).setRetentionTime(1, TimeUnit.HOURS).setRetentionSizeInMB(-1));
    int N = 30;
    for (int i = 0; i < N; i++) {
        ledger.addEntry(("entry-" + i).getBytes());
    }
    ReadOnlyCursor cursor = factory.openReadOnlyCursor("skip", PositionImpl.EARLIEST, new ManagedLedgerConfig());
    assertEquals(cursor.getNumberOfEntries(), N);
    assertTrue(cursor.hasMoreEntries());
    cursor.skipEntries(25);
    assertEquals(cursor.getNumberOfEntries(), 5);
    assertTrue(cursor.hasMoreEntries());
    cursor.skipEntries(5);
    assertEquals(cursor.getNumberOfEntries(), 0);
    assertFalse(cursor.hasMoreEntries());
    cursor.close();
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ReadOnlyCursor(org.apache.bookkeeper.mledger.ReadOnlyCursor) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Aggregations

ReadOnlyCursor (org.apache.bookkeeper.mledger.ReadOnlyCursor)42 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)36 Test (org.testng.annotations.Test)24 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)18 Entry (org.apache.bookkeeper.mledger.Entry)15 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)12 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)12 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)12 HashMap (java.util.HashMap)9 LinkedList (java.util.LinkedList)9 NullStatsProvider (org.apache.bookkeeper.stats.NullStatsProvider)9 MessageMetadata (org.apache.pulsar.common.api.proto.MessageMetadata)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ByteBuf (io.netty.buffer.ByteBuf)6 ArrayList (java.util.ArrayList)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)6 ReadOnlyCursorImpl (org.apache.bookkeeper.mledger.impl.ReadOnlyCursorImpl)6 TopicName (org.apache.pulsar.common.naming.TopicName)6