use of org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback 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.AsyncCallbacks.MarkDeleteCallback in project pulsar by yahoo.
the class ManagedCursorTest method cursorPersistenceAsyncMarkDeleteSameThread.
@Test(timeOut = 20000)
void cursorPersistenceAsyncMarkDeleteSameThread() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMetadataMaxEntriesPerLedger(5));
final ManagedCursor c1 = ledger.openCursor("c1");
final int N = 100;
List<Position> positions = Lists.newArrayList();
for (int i = 0; i < N; i++) {
Position p = ledger.addEntry("dummy-entry".getBytes(Encoding));
positions.add(p);
}
Position lastPosition = positions.get(N - 1);
final CountDownLatch latch = new CountDownLatch(N);
for (final Position p : positions) {
c1.asyncMarkDelete(p, new MarkDeleteCallback() {
public void markDeleteComplete(Object ctx) {
latch.countDown();
}
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
log.error("Failed to markdelete", exception);
latch.countDown();
}
}, null);
}
latch.await();
// Reopen
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ledger = factory2.open("my_test_ledger");
ManagedCursor c2 = ledger.openCursor("c1");
assertEquals(c2.getMarkDeletedPosition(), lastPosition);
factory2.shutdown();
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback in project pulsar by yahoo.
the class ManagedCursorImpl method asyncDelete.
@Override
public void asyncDelete(Position pos, final AsyncCallbacks.DeleteCallback callback, Object ctx) {
checkArgument(pos instanceof PositionImpl);
if (STATE_UPDATER.get(this) == State.Closed) {
callback.deleteFailed(new ManagedLedgerException("Cursor was already closed"), ctx);
return;
}
PositionImpl position = (PositionImpl) pos;
PositionImpl previousPosition = ledger.getPreviousPosition(position);
PositionImpl newMarkDeletePosition = null;
lock.writeLock().lock();
try {
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Deleting single message at {}. Current status: {} - md-position: {} - previous-position: {}", ledger.getName(), name, pos, individualDeletedMessages, markDeletePosition, previousPosition);
}
if (individualDeletedMessages.contains(position) || position.compareTo(markDeletePosition) <= 0) {
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Position was already deleted {}", ledger.getName(), name, position);
}
callback.deleteComplete(ctx);
return;
}
if (previousPosition.compareTo(markDeletePosition) == 0 && individualDeletedMessages.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("[{}][{}] Immediately mark-delete to position {}", ledger.getName(), name, position);
}
newMarkDeletePosition = position;
} else {
// Add a range (prev, pos] to the set. Adding the previous entry as an open limit to the range will make
// the RangeSet recognize the "continuity" between adjacent Positions
individualDeletedMessages.add(Range.openClosed(previousPosition, position));
++messagesConsumedCounter;
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Individually deleted messages: {}", ledger.getName(), name, individualDeletedMessages);
}
// If the lower bound of the range set is the current mark delete position, then we can trigger a new
// mark
// delete to the upper bound of the first range segment
Range<PositionImpl> range = individualDeletedMessages.asRanges().iterator().next();
// If the lowerBound is ahead of MarkDelete, verify if there are any entries in-between
if (range.lowerEndpoint().compareTo(markDeletePosition) <= 0 || ledger.getNumberOfEntries(Range.openClosed(markDeletePosition, range.lowerEndpoint())) <= 0) {
if (log.isDebugEnabled()) {
log.debug("[{}] Found a position range to mark delete for cursor {}: {} ", ledger.getName(), name, range);
}
newMarkDeletePosition = range.upperEndpoint();
}
}
if (newMarkDeletePosition != null) {
newMarkDeletePosition = setAcknowledgedPosition(newMarkDeletePosition);
} else {
newMarkDeletePosition = markDeletePosition;
}
} catch (Exception e) {
log.warn("[{}] [{}] Error while updating individualDeletedMessages [{}]", ledger.getName(), name, e.getMessage(), e);
callback.deleteFailed(new ManagedLedgerException(e), ctx);
return;
} finally {
lock.writeLock().unlock();
}
// Apply rate limiting to mark-delete operations
if (markDeleteLimiter != null && !markDeleteLimiter.tryAcquire()) {
callback.deleteComplete(ctx);
return;
}
try {
internalAsyncMarkDelete(newMarkDeletePosition, new MarkDeleteCallback() {
@Override
public void markDeleteComplete(Object ctx) {
callback.deleteComplete(ctx);
}
@Override
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
callback.deleteFailed(exception, ctx);
}
}, ctx);
} catch (Exception e) {
log.warn("[{}] [{}] Error doing asyncDelete [{}]", ledger.getName(), name, e.getMessage(), e);
if (log.isDebugEnabled()) {
log.debug("[{}] Consumer {} cursor asyncDelete error, counters: consumed {} mdPos {} rdPos {}", ledger.getName(), name, messagesConsumedCounter, markDeletePosition, readPosition);
}
callback.deleteFailed(new ManagedLedgerException(e), ctx);
}
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback in project pulsar by yahoo.
the class ManagedLedgerTest method asyncAPI.
@Test(timeOut = 20000)
public void asyncAPI() throws Throwable {
final CountDownLatch counter = new CountDownLatch(1);
factory.asyncOpen("my_test_ledger", new ManagedLedgerConfig(), new OpenLedgerCallback() {
@Override
public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
ledger.asyncOpenCursor("test-cursor", new OpenCursorCallback() {
@Override
public void openCursorComplete(ManagedCursor cursor, Object ctx) {
ManagedLedger ledger = (ManagedLedger) ctx;
ledger.asyncAddEntry("test".getBytes(Encoding), new AddEntryCallback() {
@Override
public void addComplete(Position position, Object ctx) {
@SuppressWarnings("unchecked") Pair<ManagedLedger, ManagedCursor> pair = (Pair<ManagedLedger, ManagedCursor>) ctx;
ManagedLedger ledger = pair.first;
ManagedCursor cursor = pair.second;
assertEquals(ledger.getNumberOfEntries(), 1);
assertEquals(ledger.getTotalSize(), "test".getBytes(Encoding).length);
cursor.asyncReadEntries(2, new ReadEntriesCallback() {
@Override
public void readEntriesComplete(List<Entry> entries, Object ctx) {
ManagedCursor cursor = (ManagedCursor) ctx;
assertEquals(entries.size(), 1);
Entry entry = entries.get(0);
assertEquals(new String(entry.getDataAndRelease(), Encoding), "test");
log.debug("Mark-Deleting to position {}", entry.getPosition());
cursor.asyncMarkDelete(entry.getPosition(), new MarkDeleteCallback() {
@Override
public void markDeleteComplete(Object ctx) {
log.debug("Mark delete complete");
ManagedCursor cursor = (ManagedCursor) ctx;
assertEquals(cursor.hasMoreEntries(), false);
counter.countDown();
}
@Override
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, cursor);
}
@Override
public void readEntriesFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, cursor);
}
@Override
public void addFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, new Pair<ManagedLedger, ManagedCursor>(ledger, cursor));
}
@Override
public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, ledger);
}
@Override
public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
fail(exception.getMessage());
}
}, null);
counter.await();
log.info("Test completed");
}
use of org.apache.bookkeeper.mledger.AsyncCallbacks.MarkDeleteCallback in project incubator-pulsar by apache.
the class ManagedCursorTest method cursorPersistenceAsyncMarkDeleteSameThread.
@Test(timeOut = 20000)
void cursorPersistenceAsyncMarkDeleteSameThread() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMetadataMaxEntriesPerLedger(5));
final ManagedCursor c1 = ledger.openCursor("c1");
final int N = 100;
List<Position> positions = Lists.newArrayList();
for (int i = 0; i < N; i++) {
Position p = ledger.addEntry("dummy-entry".getBytes(Encoding));
positions.add(p);
}
Position lastPosition = positions.get(N - 1);
final CountDownLatch latch = new CountDownLatch(N);
for (final Position p : positions) {
c1.asyncMarkDelete(p, new MarkDeleteCallback() {
@Override
public void markDeleteComplete(Object ctx) {
latch.countDown();
}
@Override
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
log.error("Failed to markdelete", exception);
latch.countDown();
}
}, null);
}
latch.await();
// Reopen
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ledger = factory2.open("my_test_ledger");
ManagedCursor c2 = ledger.openCursor("c1");
assertEquals(c2.getMarkDeletedPosition(), lastPosition);
factory2.shutdown();
}
Aggregations