Fix race condition in updating readPosition in ManagedCursorImpl.setAcknowledgePosition
@@ -1534,18 +1534,21 @@ public class ManagedCursorImpl implements ManagedCursor {
markDeletePosition = newMarkDeletePosition;
individualDeletedMessages.removeAtMost(markDeletePosition.getLedgerId(), markDeletePosition.getEntryId());
- if (readPosition.compareTo(newMarkDeletePosition) <= 0) {
+ READ_POSITION_UPDATER.updateAndGet(this, currentReadPosition -> {
+ if (currentReadPosition.compareTo(markDeletePosition) <= 0) {
// If the position that is mark-deleted is past the read position, it
// means that the client has skipped some entries. We need to move
// read position forward
- PositionImpl oldReadPosition = readPosition;
- readPosition = ledger.getNextValidPosition(newMarkDeletePosition);
-
+ PositionImpl newReadPosition = ledger.getNextValidPosition(markDeletePosition);
if (log.isDebugEnabled()) {
log.debug("[{}] Moved read position from: {} to: {}, and new mark-delete position {}", ledger.getName(),
- oldReadPosition, readPosition, markDeletePosition);
+ currentReadPosition, newReadPosition, markDeletePosition);
}
+ return newReadPosition;
+ } else {
+ return currentReadPosition;
}
+ });
return newMarkDeletePosition;
}
