Skip to content

[PubsubIO] Acknowledge live and earlier checkpoints on restored finalize or resume - #41509

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

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

Conversation

@jbonofre

Copy link
Copy Markdown
Member

Similar to the recent JmsIO fixes (#40605, #41013), PubsubCheckpoint previously lost its reader and safeToAckIds when encoded and decoded through PubsubCheckpointCoder:

  1. Restored checkpoints failed with IllegalStateException: PubsubCheckpointCoder.decode sets reader = null and safeToAckIds = null, so calling finalizeCheckpoint() on a restored mark throws IllegalStateException("Cannot finalize a restored checkpoint"), even when the originating PubsubReader is still live in the same JVM.
  2. Unfinalized checkpoints leaked numInFlightCheckpoints & resume did not finalize: Every getCheckpointMark() increments numInFlightCheckpoints, which is only decremented in finalizeCheckpoint(). Any skipped/unfinalized checkpoint keeps numInFlightCheckpoints > 0 so maybeCloseClient() in close() never closes pubsubClient, and PubsubSource.createReader(..., checkpoint) did not finalize the resumed checkpoint on same-JVM resume.

Changes

  • Add a per-reader weak-valued PendingCheckpoints registry (readerId + monotonic sequence encoded by PubsubCheckpointCoder).
  • finalizeCheckpoint() resolves the live reader's PendingCheckpoints (even from a restored copy), acknowledges safeToAckIds 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.
  • PubsubSource.createReader(options, checkpoint) finalizes the resumed checkpoint when its originating reader lives in this JVM before nacking notYetReadIds.
  • PubsubReader.close() schedules a 60s fallback timeout to abandon any unfinalized checkpoints and close pubsubClient if finalization never arrives.
  • Added unit test PubsubUnboundedSourceTest.restoredCheckpointFinalizesLiveMessagesAndCatchesUpEarlierCheckpoints.

@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 PendingCheckpoints registry for PubsubUnboundedSource (similar to the recent JmsIO fix in #41013) so that restored checkpoints can locate their originating live reader in the same JVM, acknowledge earlier unfinalized checkpoints, and properly clean up numInFlightCheckpoints and PubsubClient resources.

🔍 General Feedback

  • There are three issues to address before merging:
    1. Calling nackAll() on an in-memory checkpoint during createReader(options, checkpoint) throws IllegalStateException unless the checkpoint was previously finalized or cloned.
    2. PubsubCheckpointCoder.decode() does not handle backward-compatible decoding of persisted checkpoints created prior to this change (missing readerId and sequence fields).
    3. If the 60s close timeout fires while the original PubsubCheckpoint instance is still held by the runner's BundleFinalizer, a subsequent finalizeCheckpoint() call falls through to the legacy path and throws IllegalStateException("Client has been closed").

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

💬 Inline Findings

  • sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubUnboundedSource.java (line 1325): 🔴 Critical: checkpoint.nackAll(reader) starts with checkState(this.reader == null, "Cannot nackAll on persisting checkpoint"); (line 446).

When createReader(options, checkpoint) is called with an unserialized PubsubCheckpoint instance whose finalizeCheckpoint() was already called earlier (e.g., via a restored copy or an earlier call, as in noSubscriptionNoSplitGeneratesSubscription), checkpoint.finalizeCheckpoint() on line 1315 will see checkpoints.isEmpty() in PendingCheckpoints and return early without clearing checkpoint.reader on the passed-in instance if a different copy finalized it, or if finalizeCheckpoint() threw an IOException that was caught and ignored on line 1316.

More importantly, in nackAll: why does nackAll check this.reader == null at all now that createReader finalizes checkpoint beforehand, or if finalizeCheckpoint() failed with IOException (caught on line 1316), this.reader will still be non-null and nackAll will crash with IllegalStateException: Cannot nackAll on persisting checkpoint.

  • sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubUnboundedSource.java (line 488): 🟠 High: Pipeline update / drain compatibility: checkpoints persisted by an older version of Beam will only contain subscriptionPath and notYetReadIds in the stream. Unconditionally reading READER_ID_CODER and SEQUENCE_CODER will throw EOFException / CoderException when upgrading a running streaming job from an older Beam version.

Check inStream.available() > 0 before decoding readerId and sequence (defaulting to null and 0L when at EOF).

      @Nullable String readerId = inStream.available() > 0 ? READER_ID_CODER.decode(inStream) : null;
  • sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubUnboundedSource.java (line 1101): 🟠 High: When the close timeout expires and pendingCheckpoints.removeAll() is called, the abandoned PubsubCheckpoint instances still have non-null this.reader, this.pendingCheckpoints, and this.safeToAckIds fields.

If a runner's BundleFinalizer invokes finalizeCheckpoint() on an in-memory PubsubCheckpoint after the 60s close timeout has fired:

  1. pending.removeUpTo(sequence) returns an empty list,Wait—actually pending.removeUpTo(sequence) returns early on line 306 if pending != null! However, if finalizeCheckpoint() is called on a checkpoint where pendingCheckpoints was null or if clearAndDecrement wasn't called, wait: if abandoned checkpoints are not cleared via clearAndDecrement(this), then if checkpoint.clearAndDecrement is later called or if numInFlightCheckpoints is decremented both here (addAndGet(-abandoned.size())) and later in clearAndDecrement, numInFlightCheckpoints would go negative and fail checkState(remainingInFlight >= 0). Calling checkpoint.clearAndDecrement(this) for each abandoned checkpoint in removeAll() / the timeout handler prevents double-decrementing and releases references to safeToAckIds and reader.

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

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers (or comment /summarize-failures for a summary of the failing checks).

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

Labels

gcp io java size/m M: 50-249 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant