Skip to content

perf(bigquery-jdbc): short-circuit disabled connection logs at Gate 1 - #15169

Closed
Neenu1995 wants to merge 33 commits into
fix/bq-jdbc-per-conn-log-isolationfrom
perf/bq-jdbc-gate1-log-filtering
Closed

Neenu1995 wants to merge 33 commits into
fix/bq-jdbc-per-conn-log-isolationfrom
perf/bq-jdbc-gate1-log-filtering

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Summary

PR 6 of 6 in the diagnostic logging stack (stacked on fix/bq-jdbc-per-conn-log-isolation). Adds per-connection Gate-1 isLoggable(Level) short-circuiting so connections at LogLevel=0 (OFF) or higher thresholds avoid Supplier<String> evaluation and LogRecord allocation even when a concurrent connection raises the shared root logger to FINEST.

Environment Variables & System Properties

  • New Environment Variables / System Properties: None added in this PR.

Design Decisions

  • Per-connection Gate-1 short-circuiting (ConnectionLogContext): With per-connection handler isolation (PR 5), the shared com.google.cloud.bigquery logger runs at the minimum active level across open connections. Checking the active connection's ConnectionLogContext.isLoggable(levelValue) directly inside BigQueryJdbcCustomLogger.isLoggable and BigQueryJdbcResultSetLogger.isLoggable restores zero-allocation fast-path filtering for quiet connections on hot ResultSet read paths.
  • Zero-allocation external JUL sink detection (RootDriverLogger): Subclasses Logger for com.google.cloud.bigquery to update a volatile boolean externalHandlerAttached flag only when addHandler, removeHandler, or setUseParentHandlers is called, avoiding Logger.getHandlers() array cloning on every isLoggable check while ensuring externally attached JUL handlers still receive all records allowed by the root logger.
  • Cached proxy context + InheritableThreadLocal propagation: Caches ConnectionLogContext on BigQueryJdbcContextProxy, propagates it across MdcThreadPoolExecutor tasks, and flips closed connections to OFF_VALUE immediately upon closeConnectionHandler.

…g files

PR 1 of 6 in the diagnostic logging series (b/584840147, part 1 of 3).

Replaces java.util.logging.FileHandler inside PerConnectionFileHandler with an NIO SeekableByteChannel writer enforcing 0700/0600 POSIX permissions, symlink rejection, collision-safe exclusive creation, literal % path handling, and graceful ErrorManager degradation.
…tionLogContext

PR 2 of 6 in the diagnostic logging series (b/584840147, part 2 of 3).

Adds bounded 1 GiB two-file (.log + .1.log 0600 backup) rotation with graceful ErrorManager fallback to PerConnectionFileHandler, and replaces raw String LogRecord parameter stamping with typed ConnectionLogContext across BigQueryJdbcResultSetLogger and OpenTelemetryJulHandler.
…ion lifecycle

PR 3 of 6 in the diagnostic logging series (b/584840147, part 3 of 3).

Tracks active connections in BigQueryJdbcRootLogger with BigQueryConnection constructor finally cleanup and wraps per-connection writers in synchronized ConnectionFileSlot entries so post-close ResultSet/Struct calls and concurrent close() vs publish() calls do not reopen closed writers.
…UrlLogPath option

PR 4 of 6 in the diagnostic logging series (b/584840083, part 1 of 2).

Applies BigQueryConnection.SAFE_TO_LOG_PROPERTIES allowlist masking in BigQueryJdbcUrlUtility.appendPropertiesToURL, binds reentrant BigQueryJdbcMdc context at the start of BigQueryDriver.connect with failure cleanup, and adds -Dcom.google.cloud.bigquery.jdbc.allowUrlLogPath.
… path degradation

PR 5 of 6 in the diagnostic logging series (b/584840083, part 2 of 2).

Isolates per-connection LogLevel and LogPath slots inside a single persistent PerConnectionFileHandler without stripping OpenTelemetryJulHandler or external JUL handlers on LogLevel=0, degrades gracefully on unwritable/invalid/symlinked LogPath without failing Driver.connect, and computes the root logger threshold dynamically across open connections.
PR 6 of 6 in the diagnostic logging series.

Tracks external JUL handlers and parent-handler delegation on com.google.cloud.bigquery without per-call Handler[] allocations, propagates ConnectionLogContext through BigQueryJdbcMdc and BigQueryJdbcContextProxy, and short-circuits disabled or higher-threshold connection logs at Gate 1 (isLoggable) across BigQueryJdbcCustomLogger and BigQueryJdbcResultSetLogger.
@Neenu1995
Neenu1995 requested review from a team as code owners November 23, 2026 20:01

@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 the logging mechanism in the BigQuery JDBC driver to use connection-specific logging contexts (ConnectionLogContext) and improves MDC context propagation across threads. It introduces a custom logger (BigQueryJdbcCustomLogger) to handle context-aware loggability checks and adds comprehensive tests to verify short-circuiting behavior. However, a critical issue was identified in BigQueryJdbcCustomLogger where connections defaulting to INHERIT_VALUE immediately bypass the threshold check. This defeats the Gate-1 short-circuiting optimization when another connection is configured with FINEST logging, leading to unnecessary log evaluation and allocations on hot paths. It is recommended to resolve INHERIT_VALUE to the explicit global level instead.

@Neenu1995
Neenu1995 added this pull request to stack #15170 November 23, 2026 20:21
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-per-conn-log-isolation branch from 63f8a7c to 3b01d92 Compare November 23, 2026 20:23
@Neenu1995
Neenu1995 force-pushed the perf/bq-jdbc-gate1-log-filtering branch from 8194a83 to 5a4ae22 Compare November 23, 2026 20:23
…tation-and-context

# Conflicts:
#	java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
…log-handler-lifecycle

# Conflicts:
#	java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
@Neenu1995
Neenu1995 force-pushed the fix/bq-jdbc-per-conn-log-isolation branch from 3b01d92 to a209aeb Compare November 23, 2026 20:58
@Neenu1995
Neenu1995 force-pushed the perf/bq-jdbc-gate1-log-filtering branch from 5a4ae22 to 238c473 Compare November 23, 2026 20:58
@Neenu1995

Copy link
Copy Markdown
Contributor Author

/gcbrun

@Neenu1995 Neenu1995 changed the title [WIP] perf(bigquery-jdbc): short-circuit disabled connection logs at Gate 1 perf(bigquery-jdbc): short-circuit disabled connection logs at Gate 1 Nov 24, 2026
@Neenu1995

Copy link
Copy Markdown
Contributor Author

/gcbrun

@keshavdandeva keshavdandeva 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.

The gate works. Repro: with connections at FINEST, INFO and OFF, only the FINEST one evaluated its message suppliers, and a disabled trace call on a quiet connection dropped from ~162 ns to ~8 ns (rough timing loop, not JMH). Two minors inline. On the bot comment: agree with "n/a", since connect() always registers an explicit level.

@Neenu1995

Copy link
Copy Markdown
Contributor Author

Closing as superseded: the changes and review feedback from #15166, #15167, and #15169 have been consolidated into Layer 6 (#15255) of the finalized 6-PR diagnostic logging stack (#15256).

@Neenu1995 Neenu1995 closed this Nov 26, 2026
Neenu1995 added a commit that referenced this pull request Nov 28, 2026
…short-circuit Gate 1 (#15255)

Stack: PR 6 of 6 (base: `fix/bq-jdbc-per-conn-log-isolation`;
consolidates #15166, #15167, and #15169)
Bug: b/584840147, b/584840083

### Summary
Consolidates the final three diagnostic logging layers and incorporates
review feedback across the stack: synchronizes per-connection writer
slot close-vs-publish lifecycle and constructor failure rollback, masks
non-allowlisted URL/connection secrets with ASCII control-character
sanitization and fail-closed
`-Dcom.google.cloud.bigquery.jdbc.allowUrlLogPath`, and adds Gate-1
`isLoggable` short-circuiting with external JUL `Handler` preservation.

### Environment Variables & System Properties
- **Added System Property:**
`-Dcom.google.cloud.bigquery.jdbc.allowUrlLogPath=<true|false|1|0>`
(default: `true`; fail-closed allowing unset/blank, `true`, or `1`).
When disabled, URL/Properties `LogPath` values are ignored with both a
log `WARNING` and a JDBC `SQLWarning` on `Connection`. Documented in
`docs/USER_GUIDE.md`.

### Design Decisions & Review Feedback Addressed
- **Race-free `ConnectionWriterSlot` & `ReentrantLock` I/O locking:**
Guards writer lifecycle and handler coordination with `ReentrantLock`
(avoiding virtual-thread pinning across file I/O), disarms closed
`ConnectionLogContext`s to `OFF_VALUE`, and cleans up executors/OTel
resources on failed construction.
- **Secret masking & log-injection sanitization:** Removes
`BYOID_CredentialSource` from case-insensitive `SAFE_TO_LOG_PROPERTIES`
and replaces ASCII control characters (`< 0x20`, `0x7F`) with `?` in
logged property keys and allowlisted values.
- **Fast-path Gate-1 filtering:** Checks `super.isLoggable(level)` ahead
of thread-local MDC lookup (`~0.6 ns` disabled path) and preserves
externally attached JUL handlers via `RootDriverLogger`.
- **Channel & rotation refinements:** Opens exclusive channels with
`APPEND`, writes `ByteBuffer`s in 8 KiB slices, emits `[LOG TRUNCATED:
...]` markers on fallback truncation, removes redundant backup UID
checks before atomic `Files.move`, reports invalid `maxLogFileBytes`
once, and installs a bounded `DeduplicatingErrorManager`.
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