Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
.put(BigQueryJdbcOpenTelemetry.CONNECTION_ID_BAGGAGE_KEY, this.connectionId)
.build();
this.otelContext = Context.current().with(baggage);
boolean initialized = false;
ExecutorService createdQueryExecutor = null;
ExecutorService createdMetadataExecutor = null;
try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(this.connectionId)) {
BigQueryJdbcRootLogger.registerConnection(this.connectionId);
this.connectionUrl = url;
if (LOG.isLoggable(Level.CONFIG)) {
Properties connectionProps = ds.createProperties();
Expand Down Expand Up @@ -413,11 +417,27 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
this.bigQuery = getBigQueryConnection();
// Cached pool executes queries immediately without queueing and reclaims all idle threads
// when inactive, minimizing resources.
this.queryExecutor =
createdQueryExecutor =
BigQueryJdbcMdc.newCachedThreadPool(String.format("BQ-Query-%s", connectionId));
this.metadataExecutor =
this.queryExecutor = createdQueryExecutor;
createdMetadataExecutor =
BigQueryJdbcMdc.newFixedThreadPool(
String.format("BQ-Metadata-%s", connectionId), this.metadataFetchThreadCount);
this.metadataExecutor = createdMetadataExecutor;
initialized = true;
} finally {
if (!initialized) {
if (createdQueryExecutor != null) {
createdQueryExecutor.shutdownNow();
}
if (createdMetadataExecutor != null) {
createdMetadataExecutor.shutdownNow();
}
BigQueryJdbcRootLogger.closeConnectionHandler(this.connectionId);
BigQueryJdbcOpenTelemetry.unregisterConnection(this.connectionId);
BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry);

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

In the finally block, this.openTelemetry can be null if the constructor fails before openTelemetry is initialized (or during its initialization). Calling releaseSdk with a null argument without a guard can cause a NullPointerException, which would mask the original exception that triggered the finally block. Adding a null check prevents this.

        if (this.openTelemetry != null) {
          BigQueryJdbcOpenTelemetry.releaseSdk(this.openTelemetry);
        }
References
  1. When using lazily initialized resources, ensure that teardown or close methods perform explicit null checks before invoking methods on them to prevent NullPointerException.

this.openTelemetry = null;
}
}
}

Expand Down Expand Up @@ -1329,6 +1349,7 @@ private OpenTelemetry getOpenTelemetryInstance() {
effectiveProjectId,
this.credentials,
this.proxyProperties);
this.openTelemetry = openTelemetry;

boolean hasExternalOtel = this.customOpenTelemetry != null || this.useGlobalOpenTelemetry;
Logging localLoggingClient = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static boolean isLoggerClass(String className) {
return className.equals("com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger")
|| className.equals("com.google.cloud.bigquery.jdbc.BigQueryJdbcResultSetLogger")
|| className.startsWith("com.google.cloud.bigquery.jdbc.BigQueryJdbcRootLogger")
|| className.equals("com.google.cloud.bigquery.jdbc.PerConnectionFileHandler")
|| className.startsWith("com.google.cloud.bigquery.jdbc.PerConnectionFileHandler")
|| className.equals(BigQueryJdbcLogRecord.class.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
* plus the connection ID of the ResultSet it serves.
*
* <p>Hot-path methods ({@code finestTrace} etc.) set the source class and method explicitly, so no
* stack-trace walking (caller inference) happens for them. The connection ID is stamped into {@code
* LogRecord.getParameters()[0]} on every record so {@code PerConnectionFileHandler} can route the
* record even when the thread-local MDC (mapped diagnostic context — the per-thread connection ID
* set by the JDBC proxy) is empty, which is the normal case for un-proxied ResultSet calls.
* stack-trace walking (caller inference) happens for them. A typed {@link ConnectionLogContext} is
* stamped into {@code LogRecord.getParameters()[0]} on every record so {@code
* PerConnectionFileHandler} can route the record even when the thread-local MDC (mapped diagnostic
* context — the per-thread connection ID set by the JDBC proxy) is empty, which is the normal case
* for un-proxied ResultSet calls.
*/
public final class BigQueryJdbcResultSetLogger {

Expand All @@ -49,32 +50,49 @@ public final class BigQueryJdbcResultSetLogger {
private final BigQueryJdbcCustomLogger delegate;
private final String targetClassName;
private final String connectionId;
private final ConnectionLogContext connectionContext;

private volatile BigQueryJdbcResultSetLogger arrowStructLogger;
private volatile BigQueryJdbcResultSetLogger jsonStructLogger;
private volatile BigQueryJdbcResultSetLogger arrowArrayLogger;
private volatile BigQueryJdbcResultSetLogger jsonArrayLogger;

private BigQueryJdbcResultSetLogger(
BigQueryJdbcCustomLogger delegate, String targetClassName, String connectionId) {
BigQueryJdbcCustomLogger delegate,
String targetClassName,
ConnectionLogContext connectionContext) {
this.delegate = delegate;
this.targetClassName = targetClassName;
this.connectionId = connectionId;
this.connectionContext = connectionContext;
this.connectionId = connectionContext != null ? connectionContext.connectionId() : null;
}

public static BigQueryJdbcResultSetLogger getLogger(Class<?> clazz) {
return getLogger(clazz, null);
return getLogger(clazz, (ConnectionLogContext) null);
}

public static BigQueryJdbcResultSetLogger getLogger(Class<?> clazz, String connectionId) {
if (connectionId == null || connectionId.isEmpty()) {
return getLogger(clazz, (ConnectionLogContext) null);
}
ConnectionLogContext active = BigQueryJdbcRootLogger.findActiveContext(connectionId);
ConnectionLogContext context =
active != null
? active
: new ConnectionLogContext(connectionId, ConnectionLogContext.ALL_VALUE);
return getLogger(clazz, context);
}

static BigQueryJdbcResultSetLogger getLogger(
Class<?> clazz, ConnectionLogContext connectionContext) {
String className = clazz.getName();
BigQueryJdbcCustomLogger delegate =
DELEGATES.computeIfAbsent(className, BigQueryJdbcCustomLogger::new);
if (connectionId == null) {
if (connectionContext == null) {
return GLOBAL_FACADES.computeIfAbsent(
className, k -> new BigQueryJdbcResultSetLogger(delegate, k, null));
}
return new BigQueryJdbcResultSetLogger(delegate, className, connectionId);
return new BigQueryJdbcResultSetLogger(delegate, className, connectionContext);
}

public String getConnectionId() {
Expand All @@ -92,46 +110,46 @@ public boolean isLoggable(Level level) {

public BigQueryJdbcResultSetLogger getArrowStructLogger() {
if (arrowStructLogger == null) {
arrowStructLogger = getLogger(BigQueryArrowStruct.class, connectionId);
arrowStructLogger = getLogger(BigQueryArrowStruct.class, connectionContext);
}
return arrowStructLogger;
}

public BigQueryJdbcResultSetLogger getJsonStructLogger() {
if (jsonStructLogger == null) {
jsonStructLogger = getLogger(BigQueryJsonStruct.class, connectionId);
jsonStructLogger = getLogger(BigQueryJsonStruct.class, connectionContext);
}
return jsonStructLogger;
}

public BigQueryJdbcResultSetLogger getArrowArrayLogger() {
if (arrowArrayLogger == null) {
arrowArrayLogger = getLogger(BigQueryArrowArray.class, connectionId);
arrowArrayLogger = getLogger(BigQueryArrowArray.class, connectionContext);
}
return arrowArrayLogger;
}

public BigQueryJdbcResultSetLogger getJsonArrayLogger() {
if (jsonArrayLogger == null) {
jsonArrayLogger = getLogger(BigQueryJsonArray.class, connectionId);
jsonArrayLogger = getLogger(BigQueryJsonArray.class, connectionContext);
}
return jsonArrayLogger;
}

/**
* Stamps the connection ID into {@code params[0]} and forwards to the shared logger. Does not
* touch the record's source class/method: hot-path callers set them explicitly, and cold-path
* callers ({@code severe}, {@code warning}) rely on {@link
* Stamps the {@link ConnectionLogContext} into {@code params[0]} and forwards to the shared
* logger. Does not touch the record's source class/method: hot-path callers set them explicitly,
* and cold-path callers ({@code severe}, {@code warning}) rely on {@link
* BigQueryJdbcCustomLogger.BigQueryJdbcLogRecord} to infer them lazily.
*/
public void log(LogRecord record) {
if (connectionId != null) {
if (connectionContext != null) {
Object[] existingParams = record.getParameters();
if (existingParams == null || existingParams.length == 0) {
record.setParameters(new Object[] {connectionId});
} else {
record.setParameters(new Object[] {connectionContext});
} else if (!(existingParams[0] instanceof ConnectionLogContext)) {
Object[] newParams = new Object[existingParams.length + 1];
newParams[0] = connectionId;
newParams[0] = connectionContext;
System.arraycopy(existingParams, 0, newParams, 1, existingParams.length);
record.setParameters(newParams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
Expand All @@ -43,8 +44,10 @@ class BigQueryJdbcRootLogger {

private static final Logger storageLogger = Logger.getLogger("com.google.cloud.bigquery.storage");
private static final boolean isTest = Boolean.getBoolean("JDBC_TESTS");
private static final ConcurrentHashMap<String, ConnectionLogContext> ACTIVE_CONNECTIONS =
new ConcurrentHashMap<>();

private static Handler fileHandler = null;
private static volatile Handler fileHandler = null;

static final String PROCESS_ID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];

Expand Down Expand Up @@ -104,9 +107,9 @@ public String format(LogRecord record) {
String date = DATE_FORMATTER.format(Instant.ofEpochMilli(record.getMillis()));
String connectionId = BigQueryJdbcMdc.getConnectionId();
if (connectionId == null || connectionId.isEmpty()) {
Object[] params = record.getParameters();
if (params != null && params.length > 0 && params[0] instanceof String) {
connectionId = (String) params[0];
ConnectionLogContext context = ConnectionLogContext.extractFromParameters(record);
if (context != null) {
connectionId = context.connectionId();
}
}
String connStr =
Expand Down Expand Up @@ -162,7 +165,7 @@ public static Logger getRootLogger() {
return logger;
}

public static void setLevel(Level level, String logPath) throws IOException {
public static synchronized void setLevel(Level level, String logPath) throws IOException {
if (level != Level.OFF) {
if (logPath != null) {
setPath(logPath, level);
Expand All @@ -177,7 +180,7 @@ public static void setLevel(Level level, String logPath) throws IOException {
logger.setLevel(level);
}

static void setPath(String logPath, Level level) {
static synchronized void setPath(String logPath, Level level) {
try {
if (logPath == null) {
logPath = "";
Expand All @@ -201,9 +204,46 @@ static void setPath(String logPath, Level level) {
}
}

static ConnectionLogContext registerConnection(String connectionId) {
if (connectionId == null || connectionId.isEmpty()) {
return null;
}
ConnectionLogContext context =
ACTIVE_CONNECTIONS.computeIfAbsent(
connectionId, id -> new ConnectionLogContext(id, ConnectionLogContext.ALL_VALUE));
Handler currentHandler = fileHandler;
if (currentHandler instanceof PerConnectionFileHandler) {
((PerConnectionFileHandler) currentHandler).registerConnection(connectionId);
}
return context;
}

static ConnectionLogContext findActiveContext(String connectionId) {
if (connectionId == null || connectionId.isEmpty()) {
return null;
}
return ACTIVE_CONNECTIONS.get(connectionId);
}

static boolean isConnectionActive(String connectionId) {
return connectionId != null && ACTIVE_CONNECTIONS.containsKey(connectionId);
}

static int activeConnectionCount() {
return ACTIVE_CONNECTIONS.size();
}

public static void closeConnectionHandler(String connectionId) {
if (fileHandler instanceof PerConnectionFileHandler) {
((PerConnectionFileHandler) fileHandler).closeHandler(connectionId);
if (connectionId == null) {
return;
}
ConnectionLogContext removed = ACTIVE_CONNECTIONS.remove(connectionId);
if (removed != null) {
removed.setLevelValue(ConnectionLogContext.OFF_VALUE);
}
Handler currentHandler = fileHandler;
if (currentHandler instanceof PerConnectionFileHandler) {
((PerConnectionFileHandler) currentHandler).closeHandler(connectionId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.bigquery.jdbc;

import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
* Carries per-connection logging identity and threshold across threads and unproxied {@code
* ResultSet} log records without colliding with user or library {@code String} log parameters.
*/
final class ConnectionLogContext {
static final int OFF_VALUE = Level.OFF.intValue();
static final int ALL_VALUE = Level.ALL.intValue();

private final String connectionId;
private volatile int levelValue;

ConnectionLogContext(String connectionId, int levelValue) {
this.connectionId = Objects.requireNonNull(connectionId, "connectionId cannot be null");
this.levelValue = levelValue;
}

String connectionId() {
return connectionId;
}

int levelValue() {
return levelValue;
}

void setLevelValue(int levelValue) {
this.levelValue = levelValue;
}

static ConnectionLogContext extractFromParameters(LogRecord record) {
if (record == null) {
return null;
}
Object[] params = record.getParameters();
if (params != null && params.length > 0 && params[0] instanceof ConnectionLogContext) {
return (ConnectionLogContext) params[0];
}
return null;
}

@Override
public String toString() {
return connectionId;
}
}
Loading
Loading