use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.
the class ManagedLedgerImpl method addEntry.
@Override
public Position addEntry(byte[] data, int offset, int length) throws InterruptedException, ManagedLedgerException {
final CountDownLatch counter = new CountDownLatch(1);
// position
class Result {
ManagedLedgerException status = null;
Position position = null;
}
final Result result = new Result();
asyncAddEntry(data, offset, length, new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
result.position = position;
counter.countDown();
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
result.status = exception;
counter.countDown();
}
}, null);
counter.await();
if (result.status != null) {
log.error("[{}] Error adding entry", name, result.status);
throw result.status;
}
return result.position;
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.
the class ManagedCursorTest method asyncMarkDeleteBlocking.
@Test(timeOut = 20000)
public void asyncMarkDeleteBlocking() throws Exception {
ManagedLedgerConfig config = new ManagedLedgerConfig();
config.setMaxEntriesPerLedger(10);
config.setMetadataMaxEntriesPerLedger(5);
ManagedLedger ledger = factory.open("my_test_ledger", config);
final ManagedCursor c1 = ledger.openCursor("c1");
final AtomicReference<Position> lastPosition = new AtomicReference<Position>();
final int N = 100;
final CountDownLatch latch = new CountDownLatch(N);
for (int i = 0; i < N; i++) {
ledger.asyncAddEntry("entry".getBytes(Encoding), new AddEntryCallback() {
public void addFailed(ManagedLedgerException exception, Object ctx) {
}
public void addComplete(Position position, Object ctx) {
lastPosition.set(position);
c1.asyncMarkDelete(position, new MarkDeleteCallback() {
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
}
public void markDeleteComplete(Object ctx) {
latch.countDown();
}
}, null);
}
}, null);
}
latch.await();
assertEquals(c1.getNumberOfEntries(), 0);
// Reopen
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ledger = factory2.open("my_test_ledger");
ManagedCursor c2 = ledger.openCursor("c1");
assertEquals(c2.getMarkDeletedPosition(), lastPosition.get());
factory2.shutdown();
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.
the class ManagedCursorTest method markDeleteWithErrors.
@Test(timeOut = 20000)
void markDeleteWithErrors() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
ManagedCursor cursor = ledger.openCursor("c1");
ledger.addEntry("dummy-entry-1".getBytes(Encoding));
List<Entry> entries = cursor.readEntries(100);
stopBookKeeper();
assertEquals(entries.size(), 1);
try {
cursor.markDelete(entries.get(0).getPosition());
fail("call should have failed");
} catch (ManagedLedgerException e) {
// ok
}
entries.forEach(e -> e.release());
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.
the class ManagedCursorTest method asyncReadWithInvalidParameter.
@Test(timeOut = 20000, expectedExceptions = IllegalArgumentException.class)
void asyncReadWithInvalidParameter() 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);
stopBookKeeper();
cursor.asyncReadEntries(0, new ReadEntriesCallback() {
public void readEntriesComplete(List<Entry> entries, Object ctx) {
fail("async-call should have failed");
}
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
counter.countDown();
}
}, null);
counter.await();
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException in project pulsar by yahoo.
the class ManagedCursorTest method testConcurrentResetCursor.
@Test(timeOut = 20000)
void testConcurrentResetCursor() throws Exception {
ManagedLedger ledger = factory.open("my_test_concurrent_move_ledger");
final int Messages = 100;
final int Consumers = 5;
List<Future<AtomicBoolean>> futures = Lists.newArrayList();
ExecutorService executor = Executors.newCachedThreadPool();
final CyclicBarrier barrier = new CyclicBarrier(Consumers + 1);
for (int i = 0; i < Messages; i++) {
ledger.addEntry("test".getBytes());
}
final PositionImpl lastPosition = (PositionImpl) ledger.addEntry("dummy-entry-4".getBytes(Encoding));
for (int i = 0; i < Consumers; i++) {
final ManagedCursor cursor = ledger.openCursor("tcrc" + i);
final int idx = i;
futures.add(executor.submit(new Callable<AtomicBoolean>() {
public AtomicBoolean call() throws Exception {
barrier.await();
final AtomicBoolean moveStatus = new AtomicBoolean(false);
CountDownLatch countDownLatch = new CountDownLatch(1);
final PositionImpl resetPosition = new PositionImpl(lastPosition.getLedgerId(), lastPosition.getEntryId() - (5 * idx));
cursor.asyncResetCursor(resetPosition, new AsyncCallbacks.ResetCursorCallback() {
@Override
public void resetComplete(Object ctx) {
moveStatus.set(true);
PositionImpl pos = (PositionImpl) ctx;
log.info("move to [{}] completed for consumer [{}]", pos.toString(), idx);
countDownLatch.countDown();
}
@Override
public void resetFailed(ManagedLedgerException exception, Object ctx) {
moveStatus.set(false);
PositionImpl pos = (PositionImpl) ctx;
log.warn("move to [{}] failed for consumer [{}]", pos.toString(), idx);
countDownLatch.countDown();
}
});
countDownLatch.await();
assertTrue(cursor.getReadPosition().equals(resetPosition));
cursor.close();
return moveStatus;
}
}));
}
barrier.await();
for (Future<AtomicBoolean> f : futures) {
assertTrue(f.get().get());
}
ledger.close();
}
Aggregations