Skip to content

Commit f0619ec

Browse files
committed
[fix][test] Modify code for debug reason
1 parent 3de1d97 commit f0619ec

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,14 +1243,15 @@ public boolean hasMoreEntries() {
12431243

12441244
@Override
12451245
public long getNumberOfEntries() {
1246-
if (readPosition.compareTo(ledger.getLastPosition().getNext()) > 0) {
1246+
Position lastPosition = ledger.getLastPosition();
1247+
if (readPosition.compareTo(lastPosition.getNext()) > 0) {
12471248
if (log.isDebugEnabled()) {
12481249
log.debug("[{}] [{}] Read position {} is ahead of last position {}. There are no entries to read",
1249-
ledger.getName(), name, readPosition, ledger.getLastPosition());
1250+
ledger.getName(), name, readPosition, lastPosition);
12501251
}
12511252
return 0;
12521253
} else {
1253-
return getNumberOfEntries(Range.closedOpen(readPosition, ledger.getLastPosition().getNext()));
1254+
return getNumberOfEntries(Range.closedOpen(readPosition, lastPosition.getNext()));
12541255
}
12551256
}
12561257

@@ -2182,25 +2183,41 @@ public void asyncMarkDelete(final Position position, Map<String, Long> propertie
21822183
}
21832184

21842185
Position newPosition = ackBatchPosition(position);
2185-
if (ledger.getLastConfirmedEntry().compareTo(newPosition) < 0) {
2186+
Position markDeletePos = markDeletePosition;
2187+
Position lastConfirmedEntry = ledger.getLastConfirmedEntry();
2188+
if (lastConfirmedEntry.compareTo(newPosition) < 0) {
2189+
// Extract local variables to here for debug reason.
21862190
boolean shouldCursorMoveForward = false;
2191+
long ledgerEntries = 0;
2192+
Long nextValidLedger = null;
21872193
try {
2188-
long ledgerEntries = ledger.getLedgerInfo(markDeletePosition.getLedgerId()).get().getEntries();
2189-
Long nextValidLedger = ledger.getNextValidLedger(ledger.getLastConfirmedEntry().getLedgerId());
2190-
shouldCursorMoveForward = nextValidLedger != null
2191-
&& (markDeletePosition.getEntryId() + 1 >= ledgerEntries)
2192-
&& (newPosition.getLedgerId() == nextValidLedger);
2194+
LedgerInfo markDeleteLedgerInfo = ledger.getLedgerInfo(markDeletePos.getLedgerId()).get();
2195+
LedgerInfo newPositionLedgerInfo = null;
2196+
if (markDeleteLedgerInfo == null) {
2197+
newPositionLedgerInfo = ledger.getLedgerInfo(newPosition.getLedgerId()).get();
2198+
shouldCursorMoveForward = newPositionLedgerInfo != null
2199+
&& newPosition.getEntryId() + 1 == newPositionLedgerInfo.getEntries();
2200+
} else {
2201+
ledgerEntries = markDeleteLedgerInfo.getEntries();
2202+
nextValidLedger = ledger.getNextValidLedger(lastConfirmedEntry.getLedgerId());
2203+
shouldCursorMoveForward = nextValidLedger != null
2204+
&& (markDeletePos.getEntryId() + 1 >= ledgerEntries)
2205+
&& (newPosition.getLedgerId() == nextValidLedger);
2206+
}
2207+
if (!shouldCursorMoveForward) {
2208+
log.info("test");
2209+
}
21932210
} catch (Exception e) {
21942211
log.warn("Failed to get ledger entries while setting mark-delete-position", e);
21952212
}
21962213

21972214
if (shouldCursorMoveForward) {
21982215
log.info("[{}] move mark-delete-position from {} to {} since all the entries have been consumed",
2199-
ledger.getName(), markDeletePosition, newPosition);
2216+
ledger.getName(), markDeletePos, newPosition);
22002217
} else {
22012218
if (log.isDebugEnabled()) {
22022219
log.debug("[{}] Failed mark delete due to invalid markDelete {} is ahead of last-confirmed-entry {}"
2203-
+ " for cursor [{}]", ledger.getName(), position, ledger.getLastConfirmedEntry(), name);
2220+
+ " for cursor [{}]", ledger.getName(), position, lastConfirmedEntry, name);
22042221
}
22052222
callback.markDeleteFailed(new ManagedLedgerException("Invalid mark deleted position"), ctx);
22062223
return;

managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,8 +2717,8 @@ public CompletableFuture<Void> maybeUpdateCursorBeforeTrimmingConsumedLedger() {
27172717

27182718
// Snapshot cursor.getMarkDeletedPosition() into a local variable to avoid race condition.
27192719
Position markDeletedPosition = cursor.getMarkDeletedPosition();
2720-
Position lastAckedPosition = cursor.getPersistentMarkDeletedPosition() != null
2721-
? cursor.getPersistentMarkDeletedPosition() : markDeletedPosition;
2720+
// When doing mark delete operation, persistentMarkDeletedPosition is updated after markDeletedPosition
2721+
Position lastAckedPosition = markDeletedPosition;
27222722
LedgerInfo curPointedLedger = ledgers.get(lastAckedPosition.getLedgerId());
27232723
LedgerInfo nextPointedLedger = Optional.ofNullable(ledgers.higherEntry(lastAckedPosition.getLedgerId()))
27242724
.map(Map.Entry::getValue).orElse(null);
@@ -2767,14 +2767,14 @@ public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
27672767
cursor, markDeletedPosition);
27682768
future.complete(null);
27692769
} else {
2770-
// Should not happen
2770+
// Could not happen
27712771
log.warn("Trying to mark delete an already mark-deleted position. Current mark-delete:"
27722772
+ " {} -- attempted position: {}", markDeletedPosition, lastAckedPosition);
2773-
future.completeExceptionally(null);
2774-
// future.completeExceptionally(new ManagedLedgerException(
2775-
// "Trying to mark delete an already mark-deleted position when update cursor. Current "
2776-
// + "mark-delete: " + markDeletedPosition + " -- attempted mark delete: "
2777-
// + lastAckedPosition));
2773+
// future.complete(null);
2774+
future.completeExceptionally(new ManagedLedgerException(
2775+
"Trying to mark delete an already mark-deleted position when update cursor. Current "
2776+
+ "mark-delete: " + markDeletedPosition + " -- attempted mark delete: "
2777+
+ lastAckedPosition));
27782778
}
27792779
}
27802780
return FutureUtil.waitForAll(cursorMarkDeleteFutures);

managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3787,7 +3787,7 @@ public void testExpiredLedgerDeletionAfterManagedLedgerRestart() throws Exceptio
37873787
}
37883788

37893789
// Huh, maybeUpdateCursorBeforeTrimmingConsumedLedger seems not working well.
3790-
@Test(timeOut = 20000)
3790+
@Test(timeOut = 20000, invocationCount = 200)
37913791
public void testNeverThrowsMarkDeletingMarkedPositionInMaybeUpdateCursorBeforeTrimmingConsumedLedger()
37923792
throws Exception {
37933793
ManagedLedgerConfig config = new ManagedLedgerConfig();
@@ -3822,6 +3822,7 @@ public void addComplete(Position position, ByteBuf entryData, Object ctx) {
38223822
cursor.asyncMarkDelete(position, new MarkDeleteCallback() {
38233823
@Override
38243824
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
3825+
fail("Should not happen");
38253826
}
38263827

38273828
@Override

0 commit comments

Comments
 (0)