Repository navigation
[WIP] fix(bigquery-jdbc): harden Storage Write API StreamWriter concurrency and batch lifecycle - #15185
[WIP] fix(bigquery-jdbc): harden Storage Write API StreamWriter concurrency and batch lifecycle#15185Neenu1995 wants to merge 1 commit into
Conversation
… 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`).
There was a problem hiding this comment.
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).
| closeWriter(); | ||
|
|
||
| synchronized (this.streamLock) { | ||
| if (this.error != null) { | ||
| throw this.error; | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
| closeWriter(); | |
| synchronized (this.streamLock) { | |
| if (this.error != null) { | |
| throw this.error; | |
| } | |
| } | |
| synchronized (this.streamLock) { | |
| if (this.error != null) { | |
| closeQuietly(); | |
| throw this.error; | |
| } | |
| } | |
| closeWriter(); |
Stack: PR 3 of 6 (base:
fix/swa-v2-2-proto-stream-writer)Summary
Fix
Phaserregistration races, interrupt handling, and stream cleanup inBigQueryJdbcBulkInsertWriter, and fixBigQueryWriteClientlifecycle, batch parameter validation/clearing, and error telemetry inBigQueryPreparedStatement.Design Decisions
PhaserRegistration & Idempotent Cleanup: Register onopenRequestCountbeforestreamWriter.append(...)withAtomicBoolean-guarded deregistration so synchronously completed futures cannot drop party count to zero and terminate thePhaserearly; useawaitAdvanceInterruptiblyincleanup()andcloseQuietly()infinallyso failed/interrupted batches never hang or mask the original exception.BigQueryWriteClientReuse: Remove try-with-resources aroundconnection.getBigQueryWriteClient()inexecuteBatch()because the client is cached per connection and closed byBigQueryConnection.close().validateBatchParameters), clearbatchParametersinclearBatch(),close(), andexecuteBatch()'sfinally, restore the thread interrupt flag onInterruptedException, recordSTATUS_ERRORtelemetry, and surfaceStorageErrordetails on commit failures.