Skip to content

Commit 99b1894

Browse files
chore(bqjdbc): switch connection identifier to UUID (#12957)
b/507856382 ### Changes #### `BigQueryConnection` - Generates a full `UUID.randomUUID().toString()` as the `connectionId`. #### `BigQueryJdbcMdc` - Prepends `BQ-JDBC-` to the full `UUID` for thread context logging. #### `PerConnectionFileHandler` - Extracts `UUID` from `BQ-JDBC-` prefix and constructs log filenames with the format: `BQ-JDBC-yyyyMMddHHmmss-<first-4-chars-of-UUID>.log`. #### Tests - Updated `BigQueryJdbcMdcTest` to match the new prefix. - Refactored `PerConnectionFileHandlerTest` to use a helper method for finding dynamic log files, making tests cleaner.
1 parent f9c122a commit 99b1894

5 files changed

Lines changed: 47 additions & 32 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
import java.util.Map;
5959
import java.util.Properties;
6060
import java.util.Set;
61+
import java.util.UUID;
6162
import java.util.concurrent.ConcurrentHashMap;
6263
import java.util.concurrent.Executor;
6364
import java.util.concurrent.TimeUnit;
64-
import java.util.concurrent.atomic.AtomicLong;
6565

6666
/**
6767
* An implementation of {@link java.sql.Connection} for establishing a connection with BigQuery and
@@ -74,7 +74,6 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
7474
private final BigQueryJdbcCustomLogger LOG = new BigQueryJdbcCustomLogger(this.toString());
7575
String connectionClassName = this.toString();
7676
private final String connectionId;
77-
private static final AtomicLong connectionIdCounter = new AtomicLong(1);
7877
private static final String DEFAULT_JDBC_TOKEN_VALUE = "Google-BigQuery-JDBC-Driver";
7978
private static final String DEFAULT_VERSION = "0.0.0";
8079
private HeaderProvider headerProvider;
@@ -150,7 +149,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
150149
}
151150

152151
BigQueryConnection(String url, DataSource ds) throws IOException {
153-
this.connectionId = String.valueOf(connectionIdCounter.getAndIncrement());
152+
this.connectionId = UUID.randomUUID().toString();
154153
try (BigQueryJdbcMdc.MdcCloseable mdc =
155154
BigQueryJdbcMdc.registerInstance(this, this.connectionId)) {
156155
LOG.finest("++enter++");

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdc.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616

1717
package com.google.cloud.bigquery.jdbc;
1818

19+
import java.util.UUID;
1920
import java.util.concurrent.ConcurrentHashMap;
20-
import java.util.concurrent.atomic.AtomicLong;
2121

2222
/**
2323
* Lightweight MDC implementation for the BigQuery JDBC driver using InheritableThreadLocal.
2424
* Allocates a dedicated, independent InheritableThreadLocal object per concrete BigQueryConnection
2525
* instance.
2626
*/
2727
class BigQueryJdbcMdc {
28-
private static final AtomicLong nextId = new AtomicLong(1);
2928
private static final ConcurrentHashMap<BigQueryConnection, InheritableThreadLocal<String>>
3029
instanceLocals = new ConcurrentHashMap<>();
3130
private static final ConcurrentHashMap<BigQueryConnection, String> instanceIds =
@@ -41,9 +40,8 @@ static MdcCloseable registerInstance(BigQueryConnection connection, String id) {
4140
instanceIds.computeIfAbsent(
4241
connection,
4342
k -> {
44-
String suffix =
45-
(id != null && !id.isEmpty()) ? id : String.valueOf(nextId.getAndIncrement());
46-
return "JdbcConnection-" + suffix;
43+
String baseId = (id != null && !id.isEmpty()) ? id : UUID.randomUUID().toString();
44+
return baseId;
4745
});
4846

4947
currentConnectionId.set(cleanId);

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.nio.file.Paths;
23+
import java.time.LocalDateTime;
24+
import java.time.format.DateTimeFormatter;
2325
import java.util.concurrent.ConcurrentHashMap;
2426
import java.util.logging.FileHandler;
2527
import java.util.logging.Handler;
@@ -53,7 +55,13 @@ class PerConnectionFileHandler extends Handler {
5355
}
5456

5557
private String getLogFilePath(String id) {
56-
return baseLogPath.resolve("BigQuery-" + id + ".log").toString();
58+
String uuid = id;
59+
if (id.startsWith("BQ-JDBC-")) {
60+
uuid = id.substring("BQ-JDBC-".length());
61+
}
62+
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
63+
String shortUuid = uuid.length() >= 4 ? uuid.substring(0, 4) : uuid;
64+
return baseLogPath.resolve("BQ-JDBC-" + timestamp + "-" + shortUuid + ".log").toString();
5765
}
5866

5967
private FileHandler createFileHandler(String id) {

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdcTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public void tearDown() {
4747
@Test
4848
public void testRegisterAndRetrieveConnectionId() {
4949
BigQueryJdbcMdc.registerInstance(mockConnection1, "123");
50-
assertEquals("JdbcConnection-123", BigQueryJdbcMdc.getConnectionId());
50+
assertEquals("123", BigQueryJdbcMdc.getConnectionId());
5151
}
5252

5353
@Test
5454
public void testRemoveInstance() {
5555
BigQueryJdbcMdc.registerInstance(mockConnection1, "1");
56-
assertEquals("JdbcConnection-1", BigQueryJdbcMdc.getConnectionId());
56+
assertEquals("1", BigQueryJdbcMdc.getConnectionId());
5757

5858
BigQueryJdbcMdc.removeInstance(mockConnection1);
5959
// Note: removeInstance does not clear currentConnectionId on the current thread
6060
// based on current implementation.
61-
assertEquals("JdbcConnection-1", BigQueryJdbcMdc.getConnectionId());
61+
assertEquals("1", BigQueryJdbcMdc.getConnectionId());
6262

6363
BigQueryJdbcMdc.clear();
6464
assertNull(BigQueryJdbcMdc.getConnectionId());
@@ -67,7 +67,7 @@ public void testRemoveInstance() {
6767
@Test
6868
public void testClearContext() {
6969
BigQueryJdbcMdc.registerInstance(mockConnection1, "456");
70-
assertEquals("JdbcConnection-456", BigQueryJdbcMdc.getConnectionId());
70+
assertEquals("456", BigQueryJdbcMdc.getConnectionId());
7171

7272
BigQueryJdbcMdc.clear();
7373
assertNull(BigQueryJdbcMdc.getConnectionId());
@@ -76,7 +76,7 @@ public void testClearContext() {
7676
@Test
7777
public void testThreadInheritance() throws InterruptedException {
7878
BigQueryJdbcMdc.registerInstance(mockConnection1, "parent");
79-
assertEquals("JdbcConnection-parent", BigQueryJdbcMdc.getConnectionId());
79+
assertEquals("parent", BigQueryJdbcMdc.getConnectionId());
8080

8181
AtomicReference<String> childConnectionId = new AtomicReference<>();
8282
CountDownLatch latch = new CountDownLatch(1);
@@ -90,7 +90,7 @@ public void testThreadInheritance() throws InterruptedException {
9090
childThread.start();
9191
assertTrue(latch.await(5, TimeUnit.SECONDS));
9292

93-
assertEquals("JdbcConnection-parent", childConnectionId.get());
93+
assertEquals("parent", childConnectionId.get());
9494
}
9595

9696
@Test
@@ -144,17 +144,17 @@ public void testThreadIsolation() throws InterruptedException {
144144

145145
assertTrue(testFinished.await(5, TimeUnit.SECONDS));
146146

147-
assertEquals("JdbcConnection-A", threadAIdBeforeB.get());
147+
assertEquals("A", threadAIdBeforeB.get());
148148
assertNull(threadBIdBeforeRegister.get());
149-
assertEquals("JdbcConnection-B", threadBIdAfterRegister.get());
150-
assertEquals("JdbcConnection-A", threadAIdAfterB.get());
149+
assertEquals("B", threadBIdAfterRegister.get());
150+
assertEquals("A", threadAIdAfterB.get());
151151
}
152152

153153
@Test
154154
public void testMdcCloseableClearsContext() {
155155
try (BigQueryJdbcMdc.MdcCloseable mdc =
156156
BigQueryJdbcMdc.registerInstance(mockConnection1, "789")) {
157-
assertEquals("JdbcConnection-789", BigQueryJdbcMdc.getConnectionId());
157+
assertEquals("789", BigQueryJdbcMdc.getConnectionId());
158158
}
159159
assertNull(BigQueryJdbcMdc.getConnectionId());
160160
}

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
24+
import java.util.Optional;
2425
import java.util.logging.Level;
2526
import java.util.logging.LogRecord;
2627
import org.junit.jupiter.api.AfterEach;
@@ -51,10 +52,18 @@ public void tearDown() {
5152
BigQueryJdbcMdc.clear();
5253
}
5354

55+
private Optional<Path> findLogFile(String suffix) throws IOException {
56+
return Files.list(tempDir)
57+
.filter(
58+
p ->
59+
p.getFileName().toString().startsWith("BQ-JDBC-")
60+
&& p.getFileName().toString().endsWith(suffix))
61+
.findFirst();
62+
}
63+
5464
@Test
55-
public void testInitialization() {
56-
Path defaultLog = tempDir.resolve("BigQuery-Jdbc-default.log");
57-
assertTrue(Files.exists(defaultLog));
65+
public void testInitialization() throws IOException {
66+
assertTrue(findLogFile("-Jdbc.log").isPresent());
5867
}
5968

6069
@Test
@@ -63,8 +72,9 @@ public void testPublishDefault() throws IOException {
6372
handler.publish(record);
6473
handler.flush();
6574

66-
Path defaultLog = tempDir.resolve("BigQuery-Jdbc-default.log");
67-
String content = new String(Files.readAllBytes(defaultLog));
75+
Optional<Path> defaultLog = findLogFile("-Jdbc.log");
76+
assertTrue(defaultLog.isPresent());
77+
String content = new String(Files.readAllBytes(defaultLog.get()));
6878
assertTrue(content.contains("Test message default"));
6979
}
7080

@@ -76,24 +86,24 @@ public void testPublishConnectionSpecific() throws IOException {
7686
handler.publish(record);
7787
handler.flush();
7888

79-
Path connLog = tempDir.resolve("BigQuery-JdbcConnection-123.log");
80-
assertTrue(Files.exists(connLog));
81-
String content = new String(Files.readAllBytes(connLog));
89+
Optional<Path> connLog = findLogFile("-123.log");
90+
assertTrue(connLog.isPresent());
91+
String content = new String(Files.readAllBytes(connLog.get()));
8292
assertTrue(content.contains("Test message connection 123"));
8393
}
8494

8595
@Test
86-
public void testCloseHandler() {
96+
public void testCloseHandler() throws IOException {
8797
BigQueryJdbcMdc.registerInstance(mockConnection, "456");
8898

8999
LogRecord record = new LogRecord(Level.INFO, "Test message connection 456");
90100
handler.publish(record);
91101
handler.flush();
92102

93-
Path connLog = tempDir.resolve("BigQuery-JdbcConnection-456.log");
94-
assertTrue(Files.exists(connLog));
103+
Optional<Path> connLog = findLogFile("-456.log");
104+
assertTrue(connLog.isPresent());
95105

96-
handler.closeHandler("JdbcConnection-456");
97-
assertTrue(Files.exists(connLog));
106+
handler.closeHandler("BQ-JDBC-456");
107+
assertTrue(Files.exists(connLog.get()));
98108
}
99109
}

0 commit comments

Comments
 (0)