use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project 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()));
}
use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project pulsar by apache.
the class ReadOnlyCursorTest method simple.
@Test
void simple() throws Exception {
ManagedLedger ledger = factory.open("simple", new ManagedLedgerConfig().setRetentionTime(1, TimeUnit.HOURS));
int N = 10;
for (int i = 0; i < N; i++) {
ledger.addEntry(("entry-" + i).getBytes());
}
ReadOnlyCursor cursor = factory.openReadOnlyCursor("simple", PositionImpl.EARLIEST, new ManagedLedgerConfig());
assertEquals(cursor.getNumberOfEntries(), N);
assertTrue(cursor.hasMoreEntries());
List<Entry> entries = cursor.readEntries(N);
assertEquals(entries.size(), N);
assertEquals(cursor.getNumberOfEntries(), 0);
assertFalse(cursor.hasMoreEntries());
entries.forEach(Entry::release);
cursor.close();
// Ensure we can still write to ledger
for (int i = 0; i < N; i++) {
ledger.addEntry(("entry-" + i).getBytes());
}
// Open a new cursor
cursor = factory.openReadOnlyCursor("simple", PositionImpl.EARLIEST, new ManagedLedgerConfig());
assertEquals(cursor.getNumberOfEntries(), 2 * N);
assertTrue(cursor.hasMoreEntries());
entries = cursor.readEntries(N);
assertEquals(entries.size(), N);
assertEquals(cursor.getNumberOfEntries(), N);
assertTrue(cursor.hasMoreEntries());
entries.forEach(Entry::release);
entries = cursor.readEntries(N);
assertEquals(entries.size(), N);
assertEquals(cursor.getNumberOfEntries(), 0);
assertFalse(cursor.hasMoreEntries());
entries.forEach(Entry::release);
cursor.close();
}
use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project pulsar by apache.
the class ReadOnlyCursorTest method empty.
@Test
void empty() throws Exception {
factory.open("empty", new ManagedLedgerConfig().setRetentionTime(1, TimeUnit.HOURS));
ReadOnlyCursor cursor = factory.openReadOnlyCursor("empty", PositionImpl.EARLIEST, new ManagedLedgerConfig());
assertEquals(cursor.getNumberOfEntries(), 0);
assertFalse(cursor.hasMoreEntries());
cursor.close();
}
use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project 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();
}
use of org.apache.bookkeeper.mledger.ReadOnlyCursor in project pulsar by apache.
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;
}
Aggregations