Skip to content

perf(spanner): complete query stream immediately on PartialResultSet.last - #9258

Open
olavloite wants to merge 1 commit into
mainfrom
fix-partial-result-set-last
Open

perf(spanner): complete query stream immediately on PartialResultSet.last#9258
olavloite wants to merge 1 commit into
mainfrom
fix-partial-result-set-last

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Optimizes query streaming completion and server-side RPC status by leveraging PartialResultSet.last:

  1. Latency: Completes user stream immediately upon receiving chunk.last = true via this.push(null) and next(), bypassing the wait for gRPC trailers, the gRPC 'end' event, and secondary flush ticks.
  2. Graceful Drain: Detaches the underlying gRPC request stream from the pipeline and calls resume() when chunk.last is true. This allows trailing gRPC metadata and EOF to drain asynchronously in the background without canceling the call, preventing successful queries from being reported as CANCELLED in Cloud Monitoring.
  3. Natural Lifecycle: Signals requestsStream.end() on chunk.last so EOF cascades through CheckpointStream and PartialResultStream naturally, allowing 'finish', 'close', and stream.finished() to resolve cleanly without ad-hoc stream destruction.
  4. Error Safety: Symmetrically detaches error/end listeners and attaches a no-op error handler during background drain to prevent late socket resets from triggering retries or unhandled exceptions.

…last

Optimizes query streaming completion and server-side RPC status by leveraging
PartialResultSet.last:
1. Latency: Completes user stream immediately upon receiving chunk.last = true
   via this.push(null) and next(), bypassing the wait for gRPC trailers, the
   gRPC 'end' event, and secondary flush ticks.
2. Graceful Drain: Detaches the underlying gRPC request stream from the pipeline
   and calls resume() when chunk.last is true. This allows trailing gRPC metadata
   and EOF to drain asynchronously in the background without canceling the call,
   preventing successful queries from being reported as CANCELLED in Cloud Monitoring.
3. Natural Lifecycle: Signals requestsStream.end() on chunk.last so EOF cascades
   through CheckpointStream and PartialResultStream naturally, allowing 'finish',
   'close', and stream.finished() to resolve cleanly without ad-hoc stream destruction.
4. Error Safety: Symmetrically detaches error/end listeners and attaches a no-op
   error handler during background drain to prevent late socket resets from
   triggering retries or unhandled exceptions.
@olavloite
olavloite requested a review from a team as a code owner September 8, 2026 11:50
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Sep 8, 2026

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request improves the handling of the final chunk (chunk.last) in PartialResultStream. Specifically, it ensures that when the last chunk is received, the stream is flushed immediately, the request stream is resumed to drain remaining trailers/EOF in the background (preventing CANCELLED status), and the stream is properly cleaned up without premature destruction. A comprehensive suite of unit tests has been added to verify these behaviors. There are no review comments, so I have no feedback to provide.

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

Labels

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant