Skip to content

[SqsIO] Acknowledge live and earlier checkpoints on restored finalize or resume - #41506

Open
jbonofre wants to merge 1 commit into
apache:masterfrom
jbonofre:sqsio-checkpoint-registry
Open

jbonofre wants to merge 1 commit into
apache:masterfrom
jbonofre:sqsio-checkpoint-registry

Conversation

@jbonofre

Copy link
Copy Markdown
Member

Similar to the recent JmsIO fixes (#40605, #41013), SqsCheckpointMark previously kept private transient Optional<SqsUnboundedReader> reader:

  1. Restored checkpoints failed with NPE: After serialization/deserialization via SerializableCoder, the transient reader field becomes null, so finalizeCheckpoint() throws NullPointerException on reader.isPresent() and never deletes the messages from SQS or decrements numInFlightCheckpoints.
  2. Unfinalized earlier checkpoints & client leak: Every getCheckpointMark() increments numInFlightCheckpoints, which is only decremented in finalizeCheckpoint(). Any skipped/unfinalized checkpoint (or checkpoint finalized on a restored copy) prevents numInFlightCheckpoints from reaching 0, leaking sqsClient on close(), and SqsUnboundedSource.createReader(..., checkpointMark) did not finalize the committed checkpoint on same-JVM resume.

Changes

  • Add a per-reader weak-valued PendingCheckpoints registry (readerId + monotonic sequence on SqsCheckpointMark, with serialVersionUID pinned for backward compatibility).
  • finalizeCheckpoint() resolves the live reader's PendingCheckpoints (even from a restored copy), deletes safeToDeleteIds for this checkpoint and all earlier unfinalized checkpoints of the same reader, decrements numInFlightCheckpoints for all of them, and triggers maybeCloseClient(). If the originating reader is no longer in this JVM, it returns cleanly instead of throwing.
  • SqsUnboundedSource.createReader(options, checkpointMark) finalizes the resumed checkpoint when its originating reader lives in this JVM.
  • SqsUnboundedReader.close() schedules a 60s fallback timeout to abandon any unfinalized checkpoints and close sqsClient if finalization never arrives.
  • Added unit test SqsUnboundedReaderTest.testRestoredCheckpointDeletesMessagesAndCatchesUpEarlierCheckpoints.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR introduces a per-reader PendingCheckpoints registry for SqsIO to support finalizing restored checkpoints, catching up unfinalized earlier checkpoints, finalizing resumed checkpoints on reader creation, and timing out unfinalized checkpoints when closing the reader. However, there are a couple of thread-safety and concurrency issues around sqsClient lifecycle management during concurrent finalization/close timeouts and inFlight map access during delete().

🔍 General Feedback

  • Synchronizing maybeCloseClient() and introducing the 60s close timeout creates a race condition with concurrent finalizeCheckpoint() calls, where sqsClient can be closed and set to null just before or during pending.reader.delete(...).
  • Since finalizeCheckpoint() can now be invoked from SqsUnboundedSource.createReader() or asynchronously on restored checkpoints while the original reader or another thread is active, unsynchronized access to the LinkedHashMap inFlight in delete() alongside mutations in advance() risks ConcurrentModificationException or corrupted internal map state.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

}
}
try {
pending.reader.delete(new ArrayList<>(toDelete));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High: pending.reader.delete(...) is called outside closeLock and before numInFlightCheckpoints is decremented, while the close timeout task in SqsUnboundedReader.close() (or a concurrent finalizeCheckpoint() after timeout) can concurrently decrement numInFlightCheckpoints to <= 0 and call maybeCloseClient(), setting sqsClient = null and closing the client. If a late finalizeCheckpoint() races with the close timeout:

  1. finalizeCheckpoint() calls pending.removeUpTo(sequence) and gets the checkpoints.
  2. The close timeout task fires, calls pendingCheckpoints.removeAll() (now empty), and calls maybeCloseClient().
  3. If another checkpoint finishes or if the timeout task closes sqsClient, pending.reader.delete(...) will throw a NullPointerException when dereferencing sqsClient in deleteBatch (or use a closed client).

Even simpler: if checkpoint 1 is removed by removeUpTo(1) right before the timeout task runs, and checkpoint 2 was still in pendingCheckpoints, the timeout task removes checkpoint 2, decrements numInFlightCheckpoints by 1, and then when checkpoint 1 finishes delete() and enters finally, clearAndDecrement() decrements numInFlightCheckpoints to 0 and maybeCloseClient() closes sqsClient. Conversely, if only checkpoint 1 existed, removeUpTo(1) empties pendingCheckpoints, then the timeout task runs removeAll() (returns 0 items) and calls maybeCloseClient() (does not close yet because numInFlightCheckpoints is still 1), OR if abandon in the timeout task clears checkpoints without coordinating with in-progress delete() calls, clearAndDecrement() on an abandoned checkpoint that was concurrently removed would double-decrement numInFlightCheckpoints if not careful. Moreover, in SqsUnboundedReader.close(), if the timeout fires while no removeUpTo is running, it calls pendingCheckpoints.removeAll(), decrements numInFlightCheckpoints to 0, and closes sqsClient, but does NOT clear safeToDeleteIds / reader on the abandoned SqsCheckpointMark instances! If a direct reference to one of those SqsCheckpointMark instances has pendingCheckpoints == null (or if clearAndDecrement is later called), or if removeAll() raced with finalizeCheckpoint(), sqsClient may already be null.

Consider clearing/abandoning each checkpoint mark inside the timeout task (via a synchronized method on SqsCheckpointMark similar to JmsCheckpointMark.abandon()) and guarding delete() / client closure so delete() safely skips or aborts if the reader client is already closed.

closeTimeoutFuture =
executorService.schedule(
() -> {
List<SqsCheckpointMark> abandoned = pendingCheckpoints.removeAll();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High: When the close timeout expires and pendingCheckpoints.removeAll() is called, the abandoned SqsCheckpointMark instances themselves are not marked as cleared/abandoned (safeToDeleteIds and reader remain non-null on them). Although pendingCheckpoints is non-null on the in-memory mark (so a subsequent finalizeCheckpoint() on it will see pending.removeUpTo(sequence) return empty), clearAndDecrement() is never called on the abandoned marks, leaving their reader (Optional<SqsUnboundedReader>) and safeToDeleteIds references retained in memory if the runner holds onto the CheckpointMark instance, and risking a negative numInFlightCheckpoints (IllegalStateException: Miscounted in-flight checkpoints) if clearAndDecrement() is ever invoked on them. Calling checkpoint.clearAndDecrement() for each checkpoint in abandoned (instead of manually doing numInFlightCheckpoints.addAndGet(-abandoned.size())) will atomically decrement the counter once per checkpoint and release references.

@github-actions github-actions Bot added java io aws size/m M: 50-249 lines changed labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @ahmedabu98 for label java.
Backup reviewer: Abacn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@github-actions

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @ahmedabu98

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant