Skip to content

fix(bigquery-jdbc): gate per-connection log writers on active connection lifecycle - #15166

Open
Neenu1995 wants to merge 4 commits into
fix/bq-jdbc-log-rotation-and-contextfrom
fix/bq-jdbc-log-handler-lifecycle
Open

Neenu1995 wants to merge 4 commits into
fix/bq-jdbc-log-rotation-and-contextfrom
fix/bq-jdbc-log-handler-lifecycle

Conversation

@Neenu1995

Copy link
Copy Markdown
Contributor

Summary

PR 3 of 6 in the diagnostic logging stack (b/584840147, part 3 of 3; stacked on fix/bq-jdbc-log-rotation-and-context). Tracks active connections in BigQueryJdbcRootLogger and gates per-connection log writers behind synchronized ConnectionFileSlot lifecycle state so closed or failed connections never leak or reopen file descriptors.

Environment Variables & System Properties

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

Design Decisions

  • Active-connection registry & constructor failure cleanup: BigQueryJdbcRootLogger maintains ACTIVE_CONNECTIONS for open BigQueryConnection instances, and BigQueryConnection's constructor cleans up its handler registration in a finally block if initialization throws before returning a connection.
  • Lock-order-safe ConnectionFileSlot lifecycle: Wraps each connection's BoundedLogFileWriter in a ConnectionFileSlot with a volatile closed flag checked both outside and inside synchronized slot methods (avoiding nested lock acquisition inside ConcurrentHashMap.compute). Once closeConnection(connectionId) runs, late logs from post-close ResultSet, Struct, Array, or concurrent worker threads cannot resurrect a closed log file.

…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.
@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 robust connection lifecycle tracking and resource cleanup for the BigQuery JDBC driver, particularly around logging and executor services. It adds mechanisms to track active connections, register/unregister them, and refactors PerConnectionFileHandler to use a slot-based architecture (ConnectionFileSlot) to prevent resource leaks and ensure files are closed properly. Unit tests are also added to cover concurrency and post-close scenarios. The review feedback highlights two important improvements: first, fixing a potential issue in PerConnectionFileHandler where log records could be silently dropped if a slot is closed but the connection remains active; second, optimizing BigQueryJdbcResultSetLogger to default to OFF_VALUE for inactive connections to prevent unnecessary logging overhead.

Comment on lines +404 to +414
ConnectionFileSlot slot =
slots.compute(
connectionId,
id ->
handlerClosed
? null
: createLogWriter(id, baseLogPath, this.level, this.maxFileBytes));
if (writer != null) {
if (handlerClosed) {
writers.remove(connectionId, writer);
writer.close();
(id, existing) -> {
if (existing != null) {
return existing.isClosed() ? null : existing;
}
return canOpenSlotFor(id)
? new ConnectionFileSlot(baseLogPath, level, maxFileBytes)
: null;
});

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.

high

In slots.compute, if an existing slot is found but is closed (existing.isClosed() is true), the current implementation returns null. This removes the mapping from the map, but also causes the compute call itself to return null. As a result, the current thread's publish call will skip writing the log record, even if the connection is still active and a new slot could have been successfully opened.

To prevent silent loss of log records, we should check if the connection is still active when the existing slot is closed, and if so, instantiate and return a new ConnectionFileSlot immediately.

Suggested change
ConnectionFileSlot slot =
slots.compute(
connectionId,
id ->
handlerClosed
? null
: createLogWriter(id, baseLogPath, this.level, this.maxFileBytes));
if (writer != null) {
if (handlerClosed) {
writers.remove(connectionId, writer);
writer.close();
(id, existing) -> {
if (existing != null) {
return existing.isClosed() ? null : existing;
}
return canOpenSlotFor(id)
? new ConnectionFileSlot(baseLogPath, level, maxFileBytes)
: null;
});
ConnectionFileSlot slot =
slots.compute(
connectionId,
(id, existing) -> {
if (existing != null && !existing.isClosed()) {
return existing;
}
return canOpenSlotFor(id)
? new ConnectionFileSlot(baseLogPath, level, maxFileBytes)
: null;
});

Comment on lines +78 to +83
ConnectionLogContext active = BigQueryJdbcRootLogger.findActiveContext(connectionId);
ConnectionLogContext context =
active != null
? active
: new ConnectionLogContext(connectionId, ConnectionLogContext.ALL_VALUE);
return getLogger(clazz, context);

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

When a connection is inactive or has been closed, BigQueryJdbcRootLogger.findActiveContext(connectionId) returns null. Currently, a new ConnectionLogContext is created with ConnectionLogContext.ALL_VALUE. This causes any subsequent logging calls (e.g., from post-close ResultSet or Statement operations) to proceed with formatting and publishing the log records, only for them to be discarded later by the handler's canOpenSlotFor check.

By defaulting to ConnectionLogContext.OFF_VALUE instead, we can completely avoid the overhead of the logging pipeline for inactive or closed connections.

Suggested change
ConnectionLogContext active = BigQueryJdbcRootLogger.findActiveContext(connectionId);
ConnectionLogContext context =
active != null
? active
: new ConnectionLogContext(connectionId, ConnectionLogContext.ALL_VALUE);
return getLogger(clazz, context);
ConnectionLogContext active = BigQueryJdbcRootLogger.findActiveContext(connectionId);
ConnectionLogContext context =
active != null
? active
: new ConnectionLogContext(connectionId, ConnectionLogContext.OFF_VALUE);
return getLogger(clazz, context);

@Neenu1995
Neenu1995 added this pull request to stack #15170 November 23, 2026 20:21
…log-handler-lifecycle

# Conflicts:
#	java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
@Neenu1995 Neenu1995 changed the title [WIP] fix(bigquery-jdbc): gate per-connection log writers on active connection lifecycle fix(bigquery-jdbc): gate per-connection log writers on active connection lifecycle 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