Repository navigation
Conversation
There was a problem hiding this comment.
📋 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:
- Calling
nackAll()on an in-memory checkpoint duringcreateReader(options, checkpoint)throwsIllegalStateExceptionunless the checkpoint was previously finalized or cloned. PubsubCheckpointCoder.decode()does not handle backward-compatible decoding of persisted checkpoints created prior to this change (missingreaderIdandsequencefields).- If the 60s close timeout fires while the original
PubsubCheckpointinstance is still held by the runner'sBundleFinalizer, a subsequentfinalizeCheckpoint()call falls through to the legacy path and throwsIllegalStateException("Client has been closed").
- Calling
💡 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 withcheckState(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 containsubscriptionPathandnotYetReadIdsin the stream. Unconditionally readingREADER_ID_CODERandSEQUENCE_CODERwill throwEOFException/CoderExceptionwhen 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 andpendingCheckpoints.removeAll()is called, the abandonedPubsubCheckpointinstances still have non-nullthis.reader,this.pendingCheckpoints, andthis.safeToAckIdsfields.
If a runner's BundleFinalizer invokes finalizeCheckpoint() on an in-memory PubsubCheckpoint after the 60s close timeout has fired:
pending.removeUpTo(sequence)returns an empty list,Wait—actuallypending.removeUpTo(sequence)returns early on line 306 ifpending != null! However, iffinalizeCheckpoint()is called on a checkpoint wherependingCheckpointswas null or ifclearAndDecrementwasn't called, wait: ifabandonedcheckpoints are not cleared viaclearAndDecrement(this), then ifcheckpoint.clearAndDecrementis later called or ifnumInFlightCheckpointsis decremented both here (addAndGet(-abandoned.size())) and later inclearAndDecrement,numInFlightCheckpointswould go negative and failcheckState(remainingInFlight >= 0). Callingcheckpoint.clearAndDecrement(this)for each abandoned checkpoint inremoveAll()/ the timeout handler prevents double-decrementing and releases references tosafeToAckIdsandreader.
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
Similar to the recent JmsIO fixes (#40605, #41013),
PubsubCheckpointpreviously lost itsreaderandsafeToAckIdswhen encoded and decoded throughPubsubCheckpointCoder:PubsubCheckpointCoder.decodesetsreader = nullandsafeToAckIds = null, so callingfinalizeCheckpoint()on a restored mark throwsIllegalStateException("Cannot finalize a restored checkpoint"), even when the originatingPubsubReaderis still live in the same JVM.numInFlightCheckpoints& resume did not finalize: EverygetCheckpointMark()incrementsnumInFlightCheckpoints, which is only decremented infinalizeCheckpoint(). Any skipped/unfinalized checkpoint keepsnumInFlightCheckpoints > 0somaybeCloseClient()inclose()never closespubsubClient, andPubsubSource.createReader(..., checkpoint)did not finalize the resumed checkpoint on same-JVM resume.Changes
PendingCheckpointsregistry (readerId+ monotonicsequenceencoded byPubsubCheckpointCoder).finalizeCheckpoint()resolves the live reader'sPendingCheckpoints(even from a restored copy), acknowledgessafeToAckIdsfor this checkpoint and all earlier unfinalized checkpoints of the same reader, decrementsnumInFlightCheckpointsfor all of them, and triggersmaybeCloseClient(). 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 nackingnotYetReadIds.PubsubReader.close()schedules a 60s fallback timeout to abandon any unfinalized checkpoints and closepubsubClientif finalization never arrives.PubsubUnboundedSourceTest.restoredCheckpointFinalizesLiveMessagesAndCatchesUpEarlierCheckpoints.