Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -61,7 +61,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* An implementation of {@link java.sql.Connection} for establishing a connection with BigQuery and
Expand All @@ -74,7 +73,6 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
private final BigQueryJdbcCustomLogger LOG = new BigQueryJdbcCustomLogger(this.toString());
String connectionClassName = this.toString();
private final String connectionId;
private static final AtomicLong connectionIdCounter = new AtomicLong(1);
private static final String DEFAULT_JDBC_TOKEN_VALUE = "Google-BigQuery-JDBC-Driver";
private static final String DEFAULT_VERSION = "0.0.0";
private HeaderProvider headerProvider;
Expand Down Expand Up @@ -149,7 +147,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
}

BigQueryConnection(String url, DataSource ds) throws IOException {
this.connectionId = String.valueOf(connectionIdCounter.getAndIncrement());
this.connectionId = BigQueryJdbcMdc.generateConnectionId();
try (BigQueryJdbcMdc.MdcCloseable mdc =
BigQueryJdbcMdc.registerInstance(this, this.connectionId)) {
LOG.finest("++enter++");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package com.google.cloud.bigquery.jdbc;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
* Lightweight MDC implementation for the BigQuery JDBC driver using InheritableThreadLocal.
* Allocates a dedicated, independent InheritableThreadLocal object per concrete BigQueryConnection
* instance.
*/
class BigQueryJdbcMdc {
Comment thread
keshavdandeva marked this conversation as resolved.
private static final AtomicLong nextId = new AtomicLong(1);
private static final ConcurrentHashMap<BigQueryConnection, InheritableThreadLocal<String>>
instanceLocals = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<BigQueryConnection, String> instanceIds =
Expand All @@ -35,15 +36,20 @@ class BigQueryJdbcMdc {
private static final InheritableThreadLocal<String> currentConnectionId =
new InheritableThreadLocal<>();

static String generateConnectionId() {
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"));
String shortUuid = UUID.randomUUID().toString().substring(0, 8);
return timestamp + "-" + shortUuid;
}

static MdcCloseable registerInstance(BigQueryConnection connection, String id) {
if (connection != null) {
String cleanId =
instanceIds.computeIfAbsent(
connection,
k -> {
String suffix =
(id != null && !id.isEmpty()) ? id : String.valueOf(nextId.getAndIncrement());
return "JdbcConnection-" + suffix;
String baseId = (id != null && !id.isEmpty()) ? id : generateConnectionId();
return "JdbcConnection-" + baseId;
Comment thread
keshavdandeva marked this conversation as resolved.
Outdated
});

currentConnectionId.set(cleanId);
Expand Down
Loading