Skip to content

Commit 0589f5a

Browse files
committed
[fix][broker] Try to fix race condition when setting mark delete properties in ManagedLedgerTest.testTrimmerRaceCondition
1 parent 6475bca commit 0589f5a

3 files changed

Lines changed: 22 additions & 20 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,11 +2334,15 @@ protected void internalAsyncMarkDelete(final Position newPosition, Map<String, L
23342334
final MarkDeleteCallback callback, final Object ctx, Runnable alignAcknowledgeStatusAfterPersisted) {
23352335
ledger.mbean.addMarkDeleteOp();
23362336

2337-
MarkDeleteEntry mdEntry = new MarkDeleteEntry(newPosition, properties, callback, ctx,
2338-
alignAcknowledgeStatusAfterPersisted);
2339-
23402337
// We cannot write to the ledger during the switch, need to wait until the new metadata ledger is available
23412338
synchronized (pendingMarkDeleteOps) {
2339+
// use given properties or when missing, use the properties from the previous field value
2340+
MarkDeleteEntry last = pendingMarkDeleteOps.peekLast();
2341+
Map<String, Long> propertiesToUse =
2342+
properties != null ? properties : (last != null ? last.properties : getProperties());
2343+
MarkDeleteEntry mdEntry = new MarkDeleteEntry(newPosition, propertiesToUse, callback, ctx,
2344+
alignAcknowledgeStatusAfterPersisted);
2345+
23422346
// The state might have changed while we were waiting on the queue mutex
23432347
switch (state) {
23442348
case Closed:

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,22 +2739,20 @@ public CompletableFuture<Void> maybeUpdateCursorBeforeTrimmingConsumedLedger() {
27392739
Position finalPosition = lastAckedPosition;
27402740
log.info("Mark deleting cursor:{} from {} to {} since ledger consumed completely.", cursor,
27412741
markDeletedPosition, lastAckedPosition);
2742-
cursor.asyncMarkDelete(lastAckedPosition, cursor.getProperties(),
2743-
new MarkDeleteCallback() {
2744-
@Override
2745-
public void markDeleteComplete(Object ctx) {
2746-
log.info("Successfully persisted cursor position for cursor:{} to {}",
2747-
cursor, finalPosition);
2748-
future.complete(null);
2749-
}
2742+
cursor.asyncMarkDelete(lastAckedPosition, null, new MarkDeleteCallback() {
2743+
@Override
2744+
public void markDeleteComplete(Object ctx) {
2745+
log.info("Successfully persisted cursor position for cursor:{} to {}", cursor, finalPosition);
2746+
future.complete(null);
2747+
}
27502748

2751-
@Override
2752-
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
2753-
log.warn("Failed to mark delete: {} from {} to {}. ", cursor,
2754-
cursor.getMarkDeletedPosition(), finalPosition, exception);
2755-
future.completeExceptionally(exception);
2756-
}
2757-
}, null);
2749+
@Override
2750+
public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
2751+
log.warn("Failed to mark delete: {} from {} to {}. ", cursor, cursor.getMarkDeletedPosition(),
2752+
finalPosition, exception);
2753+
future.completeExceptionally(exception);
2754+
}
2755+
}, null);
27582756
} else if (compareResult == 0) {
27592757
log.debug("No need to reset cursor: {}, last acked position equals to current mark-delete position {}.",
27602758
cursor, markDeletedPosition);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5204,7 +5204,7 @@ public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
52045204
latch.await();
52055205
assertThat(cursor.getPersistentMarkDeletedPosition()).isGreaterThanOrEqualTo(lastPosition);
52065206
assertThat(ledger.getCursors().getSlowestCursorPosition()).isGreaterThanOrEqualTo(lastPosition);
5207-
// assertEquals(cursor.getProperties(), properties);
5207+
assertEquals(cursor.getProperties(), properties);
52085208

52095209
// 3. Add Entry 2. Triggers second rollover process.
52105210
// This implicitly calls maybeUpdateCursorBeforeTrimmingConsumedLedger due to rollover
@@ -5217,6 +5217,6 @@ public void markDeleteFailed(ManagedLedgerException exception, Object ctx) {
52175217
assertEquals(cursor.getPersistentMarkDeletedPosition(), PositionFactory.create(p.getLedgerId(), -1));
52185218

52195219
// Verify properties are preserved after cursor reset
5220-
// assertEquals(cursor.getProperties(), properties);
5220+
assertEquals(cursor.getProperties(), properties);
52215221
}
52225222
}

0 commit comments

Comments
 (0)