Skip to content

fix(bigquery-jdbc): delete generated LargeResultDataset temp tables on close and re-execution - #15334

Open
keshavdandeva wants to merge 1 commit into
jdbc/large-results-4-table-expirationfrom
jdbc/large-results-5-temp-table-cleanup
Open

keshavdandeva wants to merge 1 commit into
jdbc/large-results-4-table-expirationfrom
jdbc/large-results-5-temp-table-cleanup

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

b/584840569

Summary of Changes

  1. Single-ownership temp-table lifecycle (BigQueryBaseResultSet, BigQueryStatement):
    • BigQueryBaseResultSet owns private final AtomicReference<TableId> generatedTempTableId (attachGeneratedTempTable / claimGeneratedTempTable).
    • On successful SELECT, BigQueryStatement.runQuery transfers ownership under cancelLock to the ResultSet. When the ResultSet closes (directly, via closeOnCompletion, via re-execution / PreparedStatement.executeBatch(), via non-singular executeQuery, or via Statement.close() / Connection.close()), BigQueryJsonResultSet.close() / BigQueryArrowResultSet.close() cancel background reader workers first, then super.close() → onResultSetClosedInternal atomically claims (getAndSet(null)) and deletes the temp table once.
    • If runQuery unwinds without handing off a ResultSet (DML, non-terminal error, concurrent cancel()/close()), runQuery's finally deletes the temp table once — skipping guaranteed-404 tables.delete / tables.update on terminal 400 job failures where BigQuery never created the table.
    • Explicit LargeResultTable tables are never attached and never deleted.
  2. Non-blocking bounded async cleanup (LargeResultDestinations, BigQueryConnection):
    • LargeResultDestinations runs best-effort bigQuery.delete(tableId) on a driver-scoped bounded daemon ThreadPoolExecutor (4 max threads with allowCoreThreadTimeOut(true), 60s keep-alive, 1024-entry queue with warning-only rejection handler), binding BigQueryJdbcMdc.registerInstance(connectionId) for log correlation.
    • ResultSet.close(), Statement.close(), Connection.close(), and Connection.resetForReuse() never block on tables.delete RPCs.
    • Memoizes 403 accessDenied per connection (tempTableDeleteForbidden, cleared on resetForReuse()); non-IAM failures (503, 403 rateLimitExceeded) are logged once without poisoning the connection or accumulating retry queues (table expiration from PR 3/PR 4 handles stragglers). If delete fails or is forbidden on an aborted no-TTL execution before per-table expiration ran, applies applyTempTableExpirationBestEffort as fallback on the background task.
    • Adds a @VisibleForTesting package-private constructor BigQueryConnection(String url, DataSource ds, Executor tempTableCleanupExecutor) (delegated to by existing package-private constructors using CLEANUP_EXECUTOR) so unit tests run deterministically without cross-thread Mockito races while keeping largeResultDestinations strictly final.
  3. Docs & Tests (USER_GUIDE.md, BigQueryStatementTest, ITStatementTest):
    • Documents async best-effort deletion of generated temp tables and explicit table retention.
    • Unit tests cover JSON + Arrow worker-cancelled-before-delete ordering, double-close idempotency, re-execution / closeOnCompletion / non-singular DML / PreparedStatement.executeBatch(), concurrent close/cancel unwind + expiration fallback, terminal 400 skip vs. non-terminal 503 cleanup, 403 accessDenied memoization + resetForReuse(), non-IAM error non-retry, and a real CLEANUP_EXECUTOR test proving rs.close()/resetForReuse()/conn.close() never block on a hung tables.delete and preserve MDC connectionId.
    • Updates ITStatementTest (testTemporaryDatasetLocation + testLargeResultDatasetLifecycleAndExplicitTableRetention) with an awaitTableDeleted poll helper for async deletion.

Decisions Taken & Open Review Points

  • Single ownership + fire-and-forget async executor (no retry state machine): Eliminated the prior trackedTempTables / IdentityHashMap / finalSweep retry loop and synchronous close-path blocking. If a single tables.delete fails transiently, table TTL (PR 3/PR 4) reaps it; if the JVM exits during a queued delete, daemon threads allow clean shutdown and TTL reaps it.
  • Safety after Connection.close(): Verified BigQueryConnection.closeImpl() only closes gRPC Read/Write clients (bigQueryReadClient / bigQueryWriteClient) and does not close this.bigQuery, so background HTTP tables.delete calls finish cleanly even after Connection.close() returns.

@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 implements best-effort automatic deletion of generated temporary destination tables when a ResultSet or Statement is closed, or during Statement re-execution. It updates the user guide, introduces tracking of temporary tables and memoization of delete permission failures on the connection to avoid redundant RPCs, and adds comprehensive unit and integration tests to verify the lifecycle and cleanup behavior under various execution and error scenarios. There are no review comments, and I have no additional feedback to provide.

@keshavdandeva
keshavdandeva force-pushed the jdbc/large-results-5-temp-table-cleanup branch from a8961e9 to 866d40e Compare December 1, 2026 17:51
@keshavdandeva
keshavdandeva force-pushed the jdbc/large-results-5-temp-table-cleanup branch from 866d40e to 30eb6eb Compare December 1, 2026 20:09

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