Skip to content

[WIP] fix(bigquery-jdbc): harden Storage Write API StreamWriter concurrency and batch lifecycle - #15185

Open
Neenu1995 wants to merge 1 commit into
fix/swa-v2-2-proto-stream-writerfrom
fix/swa-v2-3-writer-batch-lifecycle
Open

Neenu1995 wants to merge 1 commit into
fix/swa-v2-2-proto-stream-writerfrom
fix/swa-v2-3-writer-batch-lifecycle

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Stack: PR 3 of 6 (base: fix/swa-v2-2-proto-stream-writer)

Summary

Fix Phaser registration races, interrupt handling, and stream cleanup in BigQueryJdbcBulkInsertWriter, and fix BigQueryWriteClient lifecycle, batch parameter validation/clearing, and error telemetry in BigQueryPreparedStatement.

Design Decisions

  • Pre-Append Phaser Registration & Idempotent Cleanup: Register on openRequestCount before streamWriter.append(...) with AtomicBoolean-guarded deregistration so synchronously completed futures cannot drop party count to zero and terminate the Phaser early; use awaitAdvanceInterruptibly in cleanup() and closeQuietly() in finally so failed/interrupted batches never hang or mask the original exception.
  • Connection-Scoped BigQueryWriteClient Reuse: Remove try-with-resources around connection.getBigQueryWriteClient() in executeBatch() because the client is cached per connection and closed by BigQueryConnection.close().
  • Upfront Batch Validation & Deterministic Clearing: Validate unbound/sparse parameters before execution (validateBatchParameters), clear batchParameters in clearBatch(), close(), and executeBatch()'s finally, restore the thread interrupt flag on InterruptedException, record STATUS_ERROR telemetry, and surface StorageError details on commit failures.

… and batch lifecycle

Stack: PR 3 of 6 (base: `fix/swa-v2-2-proto-stream-writer`)

### Summary
- Harden `BigQueryJdbcBulkInsertWriter` concurrency and cleanup on `StreamWriter`: register on `openRequestCount` (`Phaser`) *before* submitting `append(...)`, deregister idempotently on synchronous or callback failure, wait interruptibly in `cleanup()` (`awaitAdvanceInterruptibly`), and add idempotent `closeQuietly()` cleanup on failure.
- Keep the connection-cached `BigQueryWriteClient` open across `BigQueryPreparedStatement.executeBatch()` calls instead of closing it via try-with-resources on the first batch.
- Validate unbound/sparse batch parameters upfront (`validateBatchParameters`), reject `null` SQL in `BigQueryPreparedStatement`, and clear `batchParameters` on `clearBatch()`, `close()`, and `executeBatch()` completion.
- Wrap Storage Write API `IOException`, `DescriptorValidationException`, `RuntimeException`, and `InterruptedException` (restoring the thread interrupt flag) in `BigQueryJdbcException` with `STATUS_ERROR` diagnostic telemetry (`TelemetryManager`), and include per-stream `StorageError` details when `BatchCommitWriteStreamsResponse` has no commit timestamp.

### Testing
- `make lint`, `mvn -B -ntp dependency:analyze -Pquick-build -DfailOnWarning=true -Dmdep.analyze.skip=false`, `mvn -q -Denforcer.skip=true -Dclirr.skip=true test-compile`, and `make unittest` (`BigQueryJdbcBulkInsertWriterTest` and `BigQueryPreparedStatementWriteApiTest`).
@Neenu1995
Neenu1995 requested review from a team as code owners November 24, 2026 01:42

@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 enhances the robustness, thread safety, and error handling of the BigQuery JDBC Storage Write API bulk insert path. Key improvements include refactoring BigQueryJdbcBulkInsertWriter to manage stream writer lifecycles safely, preventing race conditions during asynchronous appends, adding parameter validation in BigQueryPreparedStatement before batch execution, and expanding test coverage. A high-severity feedback item suggests refactoring BigQueryJdbcBulkInsertWriter.cleanup() to prevent a potential RuntimeException in closeWriter() from masking the root append error (this.error).

Comment on lines +150 to 156
closeWriter();

synchronized (this.streamLock) {
if (this.error != null) {
throw this.error;
}
}

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

If closeWriter() throws a RuntimeException (which is common when the stream is already in a broken state), the exception will propagate immediately and mask the actual append error (this.error). To prevent masking the root cause of the failure, check and throw this.error first, using closeQuietly() to clean up the writer safely if an error is present.

Suggested change
closeWriter();
synchronized (this.streamLock) {
if (this.error != null) {
throw this.error;
}
}
synchronized (this.streamLock) {
if (this.error != null) {
closeQuietly();
throw this.error;
}
}
closeWriter();

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant