Repository navigation
Conversation
…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.
…log-handler-lifecycle
There was a problem hiding this comment.
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.
| 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; | ||
| }); |
There was a problem hiding this comment.
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.
| 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; | |
| }); |
| ConnectionLogContext active = BigQueryJdbcRootLogger.findActiveContext(connectionId); | ||
| ConnectionLogContext context = | ||
| active != null | ||
| ? active | ||
| : new ConnectionLogContext(connectionId, ConnectionLogContext.ALL_VALUE); | ||
| return getLogger(clazz, context); |
There was a problem hiding this comment.
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.
| 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); |
…log-handler-lifecycle # Conflicts: # java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java
…log-handler-lifecycle
Summary
PR 3 of 6 in the diagnostic logging stack (
b/584840147, part 3 of 3; stacked onfix/bq-jdbc-log-rotation-and-context). Tracks active connections inBigQueryJdbcRootLoggerand gates per-connection log writers behind synchronizedConnectionFileSlotlifecycle state so closed or failed connections never leak or reopen file descriptors.Environment Variables & System Properties
Design Decisions
BigQueryJdbcRootLoggermaintainsACTIVE_CONNECTIONSfor openBigQueryConnectioninstances, andBigQueryConnection's constructor cleans up its handler registration in afinallyblock if initialization throws before returning a connection.ConnectionFileSlotlifecycle: Wraps each connection'sBoundedLogFileWriterin aConnectionFileSlotwith avolatile closedflag checked both outside and inside synchronized slot methods (avoiding nested lock acquisition insideConcurrentHashMap.compute). OncecloseConnection(connectionId)runs, late logs from post-closeResultSet,Struct,Array, or concurrent worker threads cannot resurrect a closed log file.