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();
}
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);
}
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();
}
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();
}
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);
}
Aggregations