Search in sources :

Example 21 with ManagedLedgerConfig

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

the class ManagedLedgerBkTest method verifyConcurrentUsage.

@Test
public void verifyConcurrentUsage() throws Exception {
    ManagedLedgerFactoryConfig config = new ManagedLedgerFactoryConfig();
    config.setMaxCacheSize(100 * 1024 * 1024);
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle(), config);
    EntryCacheManager cacheManager = factory.getEntryCacheManager();
    ManagedLedgerConfig conf = new ManagedLedgerConfig();
    conf.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    final ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my-ledger", conf);
    int NumProducers = 1;
    int NumConsumers = 1;
    final AtomicBoolean done = new AtomicBoolean();
    final CyclicBarrier barrier = new CyclicBarrier(NumProducers + NumConsumers + 1);
    List<Future<?>> futures = Lists.newArrayList();
    for (int i = 0; i < NumProducers; i++) {
        futures.add(executor.submit(() -> {
            try {
                barrier.await();
                while (!done.get()) {
                    ledger.addEntry("entry".getBytes());
                    Thread.sleep(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }
    for (int i = 0; i < NumConsumers; i++) {
        final int idx = i;
        futures.add(executor.submit(() -> {
            try {
                barrier.await();
                ManagedCursor cursor = ledger.openCursor("my-cursor-" + idx);
                while (!done.get()) {
                    List<Entry> entries = cursor.readEntries(1);
                    if (!entries.isEmpty()) {
                        cursor.markDelete(entries.get(0).getPosition());
                    }
                    entries.forEach(e -> e.release());
                    Thread.sleep(2);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }
    barrier.await();
    Thread.sleep(1 * 1000);
    done.set(true);
    for (Future<?> future : futures) {
        future.get();
    }
    cacheManager.mlFactoryMBean.refreshStats(1, TimeUnit.SECONDS);
    assertTrue(cacheManager.mlFactoryMBean.getCacheHitsRate() > 0.0);
    assertEquals(cacheManager.mlFactoryMBean.getCacheMissesRate(), 0.0);
    assertTrue(cacheManager.mlFactoryMBean.getCacheHitsThroughput() > 0.0);
    assertEquals(cacheManager.mlFactoryMBean.getNumberOfCacheEvictions(), 0);
    factory.shutdown();
}
Also used : BookKeeperClusterTestCase(org.apache.bookkeeper.test.BookKeeperClusterTestCase) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) Entry(org.apache.bookkeeper.mledger.Entry) Assert.assertEquals(org.testng.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.testng.annotations.Test) DeleteCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCallback) Future(java.util.concurrent.Future) PersistentOfflineTopicStats(com.yahoo.pulsar.common.policies.data.PersistentOfflineTopicStats) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Lists(com.google.common.collect.Lists) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) Assert.assertFalse(org.testng.Assert.assertFalse) Charsets(com.google.common.base.Charsets) CyclicBarrier(java.util.concurrent.CyclicBarrier) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) Assert.fail(org.testng.Assert.fail) BookKeeperTestClient(org.apache.bookkeeper.client.BookKeeperTestClient) Assert.assertNotNull(org.testng.Assert.assertNotNull) Position(org.apache.bookkeeper.mledger.Position) TimeUnit(java.util.concurrent.TimeUnit) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertTrue(org.testng.Assert.assertTrue) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Future(java.util.concurrent.Future) List(java.util.List) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) Test(org.testng.annotations.Test)

Example 22 with ManagedLedgerConfig

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

the class ManagedLedgerBkTest method testOfflineTopicBacklog.

@Test
public void testOfflineTopicBacklog() throws Exception {
    ManagedLedgerFactoryConfig factoryConf = new ManagedLedgerFactoryConfig();
    factoryConf.setMaxCacheSize(0);
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, zkc, factoryConf);
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(1).setWriteQuorumSize(1).setAckQuorumSize(1).setMetadataEnsembleSize(1).setMetadataAckQuorumSize(1);
    ManagedLedger ledger = factory.open("property/cluster/namespace/my-ledger", config);
    ManagedCursor cursor = ledger.openCursor("c1");
    int N = 1;
    for (int i = 0; i < N; i++) {
        String entry = "entry-" + i;
        ledger.addEntry(entry.getBytes());
    }
    List<Entry> entries = cursor.readEntries(N);
    assertEquals(N, entries.size());
    entries.forEach(e -> e.release());
    ledger.close();
    ManagedLedgerOfflineBacklog offlineTopicBacklog = new ManagedLedgerOfflineBacklog(DigestType.CRC32, "".getBytes(Charsets.UTF_8), "", false);
    PersistentOfflineTopicStats offlineTopicStats = offlineTopicBacklog.getEstimatedUnloadedTopicBacklog((ManagedLedgerFactoryImpl) factory, "property/cluster/namespace/my-ledger");
    factory.shutdown();
    assertNotNull(offlineTopicStats);
}
Also used : Entry(org.apache.bookkeeper.mledger.Entry) ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) PersistentOfflineTopicStats(com.yahoo.pulsar.common.policies.data.PersistentOfflineTopicStats) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 23 with ManagedLedgerConfig

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

the class ManagedLedgerBkTest method ledgerFencedByAutoReplication.

/**
     * When auto-replication is triggered, if there were no writes on the ML during the grace period, auto-replication
     * will close the ledger an re-replicate it. After that, the next write will get a FencedException. We should
     * recover from this condition by creating a new ledger and retrying the write.
     */
@Test
public void ledgerFencedByAutoReplication() throws Exception {
    ManagedLedgerFactoryImpl factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("my_test_ledger", config);
    ManagedCursor c1 = ledger.openCursor("c1");
    PositionImpl p1 = (PositionImpl) ledger.addEntry("entry-1".getBytes());
    // Trigger the closure of the data ledger
    bkc.openLedger(p1.getLedgerId(), DigestType.MAC, new byte[] {});
    ledger.addEntry("entry-2".getBytes());
    assertEquals(2, c1.getNumberOfEntries());
    assertEquals(2, c1.getNumberOfEntriesInBacklog());
    PositionImpl p3 = (PositionImpl) ledger.addEntry("entry-3".getBytes());
    // Now entry-2 should have been written before entry-3
    assertEquals(3, c1.getNumberOfEntries());
    assertEquals(3, c1.getNumberOfEntriesInBacklog());
    assertTrue(p1.getLedgerId() != p3.getLedgerId());
    factory.shutdown();
}
Also used : ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 24 with ManagedLedgerConfig

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

the class ManagedLedgerBkTest method ledgerFencedByFailover.

/**
     * When another process steals the ML, the old instance should not succeed in any operation
     */
@Test
public void ledgerFencedByFailover() throws Exception {
    ManagedLedgerFactoryImpl factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerConfig config = new ManagedLedgerConfig();
    config.setEnsembleSize(2).setAckQuorumSize(2).setMetadataEnsembleSize(2);
    ManagedLedgerImpl ledger1 = (ManagedLedgerImpl) factory1.open("my_test_ledger", config);
    ledger1.openCursor("c");
    ledger1.addEntry("entry-1".getBytes());
    // Open the ML from another factory
    ManagedLedgerFactoryImpl factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedgerImpl ledger2 = (ManagedLedgerImpl) factory2.open("my_test_ledger", config);
    ManagedCursor c2 = ledger2.openCursor("c");
    try {
        ledger1.addEntry("entry-2".getBytes());
        fail("Should have failed");
    } catch (ManagedLedgerException e) {
    // Ok
    }
    ledger2.addEntry("entry-2".getBytes());
    try {
        ledger1.addEntry("entry-2".getBytes());
        fail("Should have failed");
    } catch (ManagedLedgerException bve) {
    // Ok
    }
    assertEquals(2, c2.getNumberOfEntriesInBacklog());
    factory1.shutdown();
    factory2.shutdown();
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Example 25 with ManagedLedgerConfig

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

the class ManagedLedgerTest method readFromOlderLedgers.

@Test(timeOut = 20000)
public void readFromOlderLedgers() throws Exception {
    ManagedLedgerConfig config = new ManagedLedgerConfig().setMaxEntriesPerLedger(1);
    ManagedLedger ledger = factory.open("my_test_ledger", config);
    ManagedCursor cursor = ledger.openCursor("test");
    ledger.addEntry("entry-1".getBytes(Encoding));
    ledger.addEntry("entry-2".getBytes(Encoding));
    ledger.addEntry("entry-3".getBytes(Encoding));
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.readEntries(1).forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.readEntries(1).forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), true);
    cursor.readEntries(1).forEach(e -> e.release());
    assertEquals(cursor.hasMoreEntries(), false);
}
Also used : ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Test(org.testng.annotations.Test)

Aggregations

ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)82 Test (org.testng.annotations.Test)77 ManagedLedger (org.apache.bookkeeper.mledger.ManagedLedger)69 ManagedCursor (org.apache.bookkeeper.mledger.ManagedCursor)63 Position (org.apache.bookkeeper.mledger.Position)35 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)32 Entry (org.apache.bookkeeper.mledger.Entry)27 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)24 CountDownLatch (java.util.concurrent.CountDownLatch)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 CyclicBarrier (java.util.concurrent.CyclicBarrier)10 ManagedLedgerFactoryConfig (org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig)8 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)7 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)7 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 BKException (org.apache.bookkeeper.client.BKException)6 Lists (com.google.common.collect.Lists)5 Executors (java.util.concurrent.Executors)5