Skip to content

fix(bigquery-jdbc): add 1 GiB .1.log backup rotation and typed ConnectionLogContext - #15165

Open
Neenu1995 wants to merge 5 commits into
fix/bq-jdbc-log-file-hardeningfrom
fix/bq-jdbc-log-rotation-and-context
Open

Neenu1995 wants to merge 5 commits into
fix/bq-jdbc-log-file-hardeningfrom
fix/bq-jdbc-log-rotation-and-context

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Summary

PR 2 of 6 in the diagnostic logging stack (b/584840147, part 2 of 3; stacked on fix/bq-jdbc-log-file-hardening). Upgrades ChannelLogWriter to BoundedLogFileWriter with bounded two-file (1 GiB + .1.log backup) size rotation and introduces typed ConnectionLogContext LogRecord parameter stamping.

Environment Variables & System Properties

  • Added System Property: -Dcom.google.cloud.bigquery.jdbc.maxLogFileBytes=<bytes> (default: 1073741824 / 1 GiB, parsed as long). Configures the maximum byte size of an active driver .log file before rotating onto a .1.log backup file. Documented in README.MD and docs/USER_GUIDE.md.
  • New Environment Variables: None added in this PR.

Design Decisions

  • 1 GiB (long) cap + single .1.log (0600) backup rotation (revisable): High-throughput ResultSet iteration at LogLevel=6/7 (FINER/FINEST) can emit tens of megabytes per minute, so a small cap or in-place truncate(0) rollover would discard recent context right before an error. Rotating the full active file onto a single owner-only 0600 .1.log backup (retaining 1–2 GiB / 30+ minutes of history per connection while bounding total disk usage to 2x cap) preserves diagnostic continuity. If .1.log rotation fails, the writer reopens/truncates cleanly and reports via JUL ErrorManager. (The 1 GiB default cap is a revisable threshold.)
  • Backup naming (<base>.1.log): Uses a .1.log suffix so rotated backups do not match active *-<shortId>.log filters while remaining easy to locate alongside the active log.
  • Typed ConnectionLogContext over raw String params[0]: Replaces raw String connection-ID stamping in BigQueryJdbcResultSetLogger with package-private ConnectionLogContext so plain String parameters logged by internal helpers are never misread as connection IDs, while preserving {0}..{9} positional formatting in OpenTelemetryJulHandler.

…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.
@Neenu1995
Neenu1995 requested review from a team as code owners November 23, 2026 19:58

@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 introduces diagnostic logging configuration and bounded log file rotation (using .log and .1.log backup files) configured via environment variables, JDBC URL properties, or JVM system properties. It also introduces ConnectionLogContext to carry per-connection logging identity and threshold safely across threads. The review feedback identifies two potential resource leaks in PerConnectionFileHandler.java where newly opened or recovered channels could be leaked if channel.size() throws an IOException during the rotation recovery or reopening phases.

Comment on lines +532 to +539
OpenedChannel recovered =
DEFAULT_HANDLER_ID.equals(id)
? openLockedGlobalChannel(activePath)
: new OpenedChannel(activePath, null, openSecureAppendChannel(activePath));
activePath = recovered.path;
claimedGlobalPath = recovered.claimedGlobalPath;
channel = recovered.channel;
writtenBytes = channel.size();

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.

medium

Similarly, if channel.size() throws an IOException during the recovery path after a failed move, the recovered channel will be leaked.

Wrap the assignment and size check in a try-catch block and call recovered.releaseOnFailure(e) to ensure the channel is properly closed on failure. Catch IOException specifically instead of Throwable since this is not an asynchronous operation or Future boundary.

          OpenedChannel recovered =
              DEFAULT_HANDLER_ID.equals(id)
                  ? openLockedGlobalChannel(activePath)
                  : new OpenedChannel(activePath, null, openSecureAppendChannel(activePath));
          try {
            activePath = recovered.path;
            claimedGlobalPath = recovered.claimedGlobalPath;
            channel = recovered.channel;
            writtenBytes = channel.size();
          } catch (IOException e) {
            recovered.releaseOnFailure(e);
            throw e;
          }
References
  1. Catching Throwable instead of Exception is acceptable at Future boundaries or during stream cleanup in asynchronous operations to ensure all failures are captured and propagated. For synchronous operations, catch specific exceptions like IOException.

Comment on lines +556 to +560
OpenedChannel reopened = reopenOrAllocate(logDir, id, activePath);
activePath = reopened.path;
claimedGlobalPath = reopened.claimedGlobalPath;
channel = reopened.channel;
writtenBytes = channel.size();

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.

medium

If channel.size() throws an IOException after reopening the channel, the newly opened channel in reopened will be leaked because it is never closed.

To prevent this resource leak, wrap the assignment and size check in a try-catch block, and call reopened.releaseOnFailure(e) to safely close the channel if an exception occurs during initialization. Catch IOException specifically instead of Throwable since this is not an asynchronous operation or Future boundary.

      OpenedChannel reopened = reopenOrAllocate(logDir, id, activePath);
      try {
        activePath = reopened.path;
        claimedGlobalPath = reopened.claimedGlobalPath;
        channel = reopened.channel;
        writtenBytes = channel.size();
      } catch (IOException e) {
        reopened.releaseOnFailure(e);
        throw e;
      }
References
  1. Catching Throwable instead of Exception is acceptable at Future boundaries or during stream cleanup in asynchronous operations to ensure all failures are captured and propagated. For synchronous operations, catch specific exceptions like IOException.

@Neenu1995
Neenu1995 added this pull request to stack #15170 November 23, 2026 20:21
…tation-and-context

# Conflicts:
#	java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
…tation-and-context

# Conflicts:
#	java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
@Neenu1995 Neenu1995 changed the title [WIP] fix(bigquery-jdbc): add 1 GiB .1.log backup rotation and typed ConnectionLogContext fix(bigquery-jdbc): add 1 GiB .1.log backup rotation and typed ConnectionLogContext Nov 24, 2026

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