Repository navigation
Conversation
Completes PR 3 of 3 in the diagnostic logging hardening stack. 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.
There was a problem hiding this comment.
Code Review
This pull request refactors the logging mechanism in the BigQuery JDBC driver to use ConnectionLogContext instead of raw connection IDs for MDC context propagation, improving loggability evaluation and isolation. The reviewer identified a potential issue in BigQueryJdbcResultSetLogger where log records might be dispatched with a null MDC context if threadContext is null but connectionContext is non-null, suggesting a simplified check. Additionally, the reviewer recommended renaming the getter explicitGlobalLevelValue() to getExplicitGlobalLevelValue() to avoid naming conflicts with the static field of the same name, along with updating its usage in BigQueryJdbcCustomLogger.
| record.setParameters(newParams); | ||
| } | ||
| ConnectionLogContext threadContext = BigQueryJdbcMdc.getContext(); | ||
| if (threadContext != null && threadContext != connectionContext) { |
There was a problem hiding this comment.
When threadContext is null but connectionContext is non-null, the MDC context is not registered before calling delegate.log(record). This means the log record will be dispatched with a null MDC context, which can cause routing or formatting issues if the downstream handlers or formatters rely on the MDC connection context.
We should simplify the condition to threadContext != connectionContext so that the context is also registered when the current thread has no active MDC context.
| if (threadContext != null && threadContext != connectionContext) { | |
| if (threadContext != connectionContext) { |
| static int explicitGlobalLevelValue() { | ||
| return explicitGlobalLevelValue; | ||
| } |
There was a problem hiding this comment.
The static method explicitGlobalLevelValue() has the exact same name as the static field explicitGlobalLevelValue. While this is technically valid in Java, it is highly confusing, violates standard Java getter naming conventions, and reduces code readability and maintainability.
Consider renaming the getter method to getExplicitGlobalLevelValue() to clearly distinguish it from the field.
| static int explicitGlobalLevelValue() { | |
| return explicitGlobalLevelValue; | |
| } | |
| static int getExplicitGlobalLevelValue() { | |
| return explicitGlobalLevelValue; | |
| } |
| if (rawThreshold == ConnectionLogContext.INHERIT_VALUE) { | ||
| return true; | ||
| } | ||
| int threshold = Math.min(rawThreshold, BigQueryJdbcRootLogger.explicitGlobalLevelValue()); |
There was a problem hiding this comment.
Update the call to use the renamed getter method getExplicitGlobalLevelValue() to maintain consistency and improve readability.
| int threshold = Math.min(rawThreshold, BigQueryJdbcRootLogger.explicitGlobalLevelValue()); | |
| int threshold = Math.min(rawThreshold, BigQueryJdbcRootLogger.getExplicitGlobalLevelValue()); |
What & Why
PR 3 of 3 in the diagnostic logging hardening stack (stacked on PR 2
fix/bq-jdbc-per-conn-log-isolation).After PR 2 isolated per-connection
(LogLevel, LogPath)slots on a shared persistentPerConnectionFileHandler, the JVM-widecom.google.cloud.bigqueryroot logger runs at the minimum (most verbose) active level across open connections. Without per-connection Gate-1 filtering (Logger.isLoggable(Level)/BigQueryJdbcResultSetLogger.isLoggable(Level)), if Connection A ran atFINESTwhile Connection B ran atLogLevel=0(OFF) orLogLevel=3(INFO), Connection B's hot-path calls (ResultSetgetters,Struct/Arrayaccessors,BigQueryJdbcContextProxy, andSupplier<String>/String.formatlog calls) would still evaluate message suppliers and allocateLogRecordobjects beforePerConnectionFileHandler.publish()dropped them at Gate 2.Changes in this PR
BigQueryJdbcRootLogger): Registers aRootDriverLoggersubclass oncom.google.cloud.bigquerywith synchronizedaddHandler,removeHandler, andsetUseParentHandlersoverrides maintainingvolatile boolean externalHandlerAttachedwithout allocatingHandler[]snapshots on everyisLoggablecall (plus a pure read-onlyscanForExternalHandlerfallback if an external framework pre-initialized the logger before driver classload, andexplicitGlobalLevelValuetracking forBigQueryJdbcRootLogger.setLevel(...)).ConnectionLogContextpropagation acrossBigQueryJdbcMdc&BigQueryJdbcContextProxy:BigQueryJdbcMdcfromInheritableThreadLocal<String>toInheritableThreadLocal<ConnectionLogContext>(registerContext/registerAndBindConnection/getContext), propagatingConnectionLogContextacrossMdcThreadPoolExecutorworker tasks.ConnectionLogContextonce perBigQueryJdbcContextProxyinstance and binds it directly inBigQueryDriver.connectandBigQueryConnection.<init>, avoidingConcurrentHashMaplookups per JDBC call.INHERIT_VALUE) vs. explicit connection thresholds (OFF_VALUE..ALL_VALUE) in a singlevolatile intonConnectionLogContext, and flips closed connections toOFF_VALUEso post-closeResultSet/ detachedStructcalls also short-circuit at Gate 1.BigQueryJdbcCustomLogger&BigQueryJdbcResultSetLogger:BigQueryJdbcCustomLogger.isLoggable(Level)andBigQueryJdbcResultSetLogger.isLoggable(Level)check the connection'sConnectionLogContextthreshold directly (falling back to thread-local MDC for globalResultSetfacades) whenever no custom child handler or external JUL sink is attached.BigQueryJdbcResultSetLogger(isLoggable,warning, andlog(LogRecord)) against enclosing-thread MDC cross-talk when two connections execute nested calls on the same thread.How It Was Tested
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make lint(fmt-maven-plugin:format).JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make unittest(Checkstyle, Java 8animal-sniffer, and the full JUnit 5 unit test suite) andmvn -q -Denforcer.skip=true -Dclirr.skip=true test-compilefor integration test compilation.BigQueryJdbcCustomLoggerTestcovering concurrentFINEST/INFO/OFFconnections, global and connection-scopedResultSetfacades, post-closeOFF_VALUEshort-circuiting underLevel.INFOroot, nested cross-connection MDC file output and restoration, child loggerhasOwnHandlers,MdcThreadPoolExecutorsubmit/executepropagation,explicitGlobalLevelfloor override,setUseParentHandlers(true)external sink visibility, andscanForExternalHandlerfallback scanning.IT*) were not executed against production BigQuery in this step.