Skip to content

refactor(bigquery-jdbc): optimize thread management and unify discovery logic in DatabaseMetaData methods#13560

Merged
keshavdandeva merged 16 commits into
mainfrom
jdbc/refactor-metadata-methods
Jul 1, 2026
Merged

refactor(bigquery-jdbc): optimize thread management and unify discovery logic in DatabaseMetaData methods#13560
keshavdandeva merged 16 commits into
mainfrom
jdbc/refactor-metadata-methods

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

b/520407325
b/520406763

This PR refactors and optimizes the database metadata retrieval methods in the BigQuery JDBC driver. It resolves thread management inefficiencies, eliminates duplicate API blocks, ensures consistent project discovery support, and implements strict, fail-fast error propagation across all asynchronous metadata methods.

Key Changes

1. Catalog-Based Routing in getSchemas

  • Synchronous Single-Catalog Path: If a specific catalog (project) is requested, getSchemas now executes completely synchronously on the calling thread, eliminating background executor and queue overhead.
  • Parallel Multi-Catalog Path: If no catalog is specified, the query executor runs parallel scans across all accessible projects (primary, additional, and discovered) in the background.

2. Unified Dataset Discovery & Deduplication

  • Introduced a single, shared helper method fetchMatchingDatasets to serve as the sole entry point for listing and filtering datasets.
  • Deduplicated dataset-listing logic across 7 metadata methods (getSchemas, getTables, getColumns, getProcedures, getProcedureColumns, getFunctions, and getFunctionColumns), ensuring consistent support for project discovery and SQL wildcard matching across all of them.

3. Strict Fail-Fast Error & Interruption Handling

  • Preventing Silent Truncation: Background metadata fetchers used to silently swallow InterruptedExceptions and ExecutionExceptions, returning partial or empty results to the user (which severely violates the JDBC contract during query timeouts or connection drops).
  • Consistent Propagation: All 7 background methods now strictly push an SQLException to the client's queue whenever an interruption or runtime error occurs, ensuring the user gets an explicit failure when calling rs.next().
  • Boilerplate Elimination: Extracted the complex error-handling and queue-finalization boilerplate into centralized helper methods (handleFetcherException and finalizeFetcher). This removed over 50 lines of duplicate code, deleted 14+ redundant isInterrupted() checks, and established a clean, unified standard for all asynchronous metadata threads.

@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 refactors BigQueryDatabaseMetaData.java to clean up unused imports, correct catalog null-checks, consolidate dataset fetching logic into a helper method, and optimize getSchemas with a synchronous single-catalog path and a parallel multi-catalog path. The review feedback highlights several critical improvements: ensuring submitted futures are cancelled in the finally block of getSchemas to prevent resource leaks, wrapping project scans in a try-catch block to make sequential dataset fetching robust against single-project failures, restoring the thread's interrupted status when catching InterruptedException, and replacing an unnecessary Collections.synchronizedList with a plain ArrayList.

@keshavdandeva

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 refactors metadata fetching in BigQueryDatabaseMetaData by consolidating dataset fetching into a helper method, introducing a synchronous path for single-catalog queries in getSchemas, and improving error handling. It also adds a null check in BigQueryJsonResultSet when cancelling tasks. The review comments point out three critical issues: a potential deadlock in the synchronous path of getSchemas when the schema count exceeds the bounded queue capacity, a compilation error due to a missing parameter in a BigQueryJsonResultSet.of call, and swallowed InterruptedException along with silent failures in the new fetchMatchingDatasets helper.

@keshavdandeva
keshavdandeva marked this pull request as ready for review June 26, 2026 17:47
@keshavdandeva
keshavdandeva requested review from a team as code owners June 26, 2026 17:47
@keshavdandeva
keshavdandeva requested a review from logachev June 30, 2026 23:15
@keshavdandeva

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 refactors BigQueryDatabaseMetaData to simplify metadata fetching by consolidating duplicate logic into reusable helper methods like fetchMatchingDatasets, handleFetcherException, and finalizeFetcher. It also introduces a synchronous single-catalog path for getSchemas and improves task cancellation safety in BigQueryJsonResultSet. The review feedback suggests two important improvements: first, using queue.offer before queue.put in writeErrorToQueue to prevent blocking when the thread is already interrupted; second, unwrapping ExecutionException to its root cause in handleFetcherException to avoid nested exception chains in client-facing errors.

@keshavdandeva
keshavdandeva enabled auto-merge (squash) July 1, 2026 14:46
@keshavdandeva
keshavdandeva merged commit 96c4420 into main Jul 1, 2026
209 checks passed
@keshavdandeva
keshavdandeva deleted the jdbc/refactor-metadata-methods branch July 1, 2026 15:03
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.

2 participants