diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java index 949485008b69..4f70de993177 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java @@ -58,10 +58,10 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.UUID; 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 @@ -74,7 +74,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; @@ -150,7 +149,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { } BigQueryConnection(String url, DataSource ds) throws IOException { - this.connectionId = String.valueOf(connectionIdCounter.getAndIncrement()); + this.connectionId = UUID.randomUUID().toString(); try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(this, this.connectionId)) { LOG.finest("++enter++"); diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdc.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdc.java index c27ec67e4560..12ea04c30628 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdc.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdc.java @@ -16,8 +16,8 @@ package com.google.cloud.bigquery.jdbc; +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. @@ -25,7 +25,6 @@ * instance. */ class BigQueryJdbcMdc { - private static final AtomicLong nextId = new AtomicLong(1); private static final ConcurrentHashMap> instanceLocals = new ConcurrentHashMap<>(); private static final ConcurrentHashMap instanceIds = @@ -41,9 +40,8 @@ static MdcCloseable registerInstance(BigQueryConnection connection, String id) { 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 : UUID.randomUUID().toString(); + return baseId; }); currentConnectionId.set(cleanId); diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java index 6048d42eac31..1d6c177537e8 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandler.java @@ -20,6 +20,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.FileHandler; import java.util.logging.Handler; @@ -53,7 +55,13 @@ class PerConnectionFileHandler extends Handler { } private String getLogFilePath(String id) { - return baseLogPath.resolve("BigQuery-" + id + ".log").toString(); + String uuid = id; + if (id.startsWith("BQ-JDBC-")) { + uuid = id.substring("BQ-JDBC-".length()); + } + String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); + String shortUuid = uuid.length() >= 4 ? uuid.substring(0, 4) : uuid; + return baseLogPath.resolve("BQ-JDBC-" + timestamp + "-" + shortUuid + ".log").toString(); } private FileHandler createFileHandler(String id) { diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdcTest.java b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdcTest.java index 70cb56a53f10..63c5883fd9c5 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdcTest.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdcTest.java @@ -47,18 +47,18 @@ public void tearDown() { @Test public void testRegisterAndRetrieveConnectionId() { BigQueryJdbcMdc.registerInstance(mockConnection1, "123"); - assertEquals("JdbcConnection-123", BigQueryJdbcMdc.getConnectionId()); + assertEquals("123", BigQueryJdbcMdc.getConnectionId()); } @Test public void testRemoveInstance() { BigQueryJdbcMdc.registerInstance(mockConnection1, "1"); - assertEquals("JdbcConnection-1", BigQueryJdbcMdc.getConnectionId()); + assertEquals("1", BigQueryJdbcMdc.getConnectionId()); BigQueryJdbcMdc.removeInstance(mockConnection1); // Note: removeInstance does not clear currentConnectionId on the current thread // based on current implementation. - assertEquals("JdbcConnection-1", BigQueryJdbcMdc.getConnectionId()); + assertEquals("1", BigQueryJdbcMdc.getConnectionId()); BigQueryJdbcMdc.clear(); assertNull(BigQueryJdbcMdc.getConnectionId()); @@ -67,7 +67,7 @@ public void testRemoveInstance() { @Test public void testClearContext() { BigQueryJdbcMdc.registerInstance(mockConnection1, "456"); - assertEquals("JdbcConnection-456", BigQueryJdbcMdc.getConnectionId()); + assertEquals("456", BigQueryJdbcMdc.getConnectionId()); BigQueryJdbcMdc.clear(); assertNull(BigQueryJdbcMdc.getConnectionId()); @@ -76,7 +76,7 @@ public void testClearContext() { @Test public void testThreadInheritance() throws InterruptedException { BigQueryJdbcMdc.registerInstance(mockConnection1, "parent"); - assertEquals("JdbcConnection-parent", BigQueryJdbcMdc.getConnectionId()); + assertEquals("parent", BigQueryJdbcMdc.getConnectionId()); AtomicReference childConnectionId = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); @@ -90,7 +90,7 @@ public void testThreadInheritance() throws InterruptedException { childThread.start(); assertTrue(latch.await(5, TimeUnit.SECONDS)); - assertEquals("JdbcConnection-parent", childConnectionId.get()); + assertEquals("parent", childConnectionId.get()); } @Test @@ -144,17 +144,17 @@ public void testThreadIsolation() throws InterruptedException { assertTrue(testFinished.await(5, TimeUnit.SECONDS)); - assertEquals("JdbcConnection-A", threadAIdBeforeB.get()); + assertEquals("A", threadAIdBeforeB.get()); assertNull(threadBIdBeforeRegister.get()); - assertEquals("JdbcConnection-B", threadBIdAfterRegister.get()); - assertEquals("JdbcConnection-A", threadAIdAfterB.get()); + assertEquals("B", threadBIdAfterRegister.get()); + assertEquals("A", threadAIdAfterB.get()); } @Test public void testMdcCloseableClearsContext() { try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(mockConnection1, "789")) { - assertEquals("JdbcConnection-789", BigQueryJdbcMdc.getConnectionId()); + assertEquals("789", BigQueryJdbcMdc.getConnectionId()); } assertNull(BigQueryJdbcMdc.getConnectionId()); } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java index 94f8b4f5e6b2..f905fa80fe5a 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/PerConnectionFileHandlerTest.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; import java.util.logging.Level; import java.util.logging.LogRecord; import org.junit.jupiter.api.AfterEach; @@ -51,10 +52,18 @@ public void tearDown() { BigQueryJdbcMdc.clear(); } + private Optional findLogFile(String suffix) throws IOException { + return Files.list(tempDir) + .filter( + p -> + p.getFileName().toString().startsWith("BQ-JDBC-") + && p.getFileName().toString().endsWith(suffix)) + .findFirst(); + } + @Test - public void testInitialization() { - Path defaultLog = tempDir.resolve("BigQuery-Jdbc-default.log"); - assertTrue(Files.exists(defaultLog)); + public void testInitialization() throws IOException { + assertTrue(findLogFile("-Jdbc.log").isPresent()); } @Test @@ -63,8 +72,9 @@ public void testPublishDefault() throws IOException { handler.publish(record); handler.flush(); - Path defaultLog = tempDir.resolve("BigQuery-Jdbc-default.log"); - String content = new String(Files.readAllBytes(defaultLog)); + Optional defaultLog = findLogFile("-Jdbc.log"); + assertTrue(defaultLog.isPresent()); + String content = new String(Files.readAllBytes(defaultLog.get())); assertTrue(content.contains("Test message default")); } @@ -76,24 +86,24 @@ public void testPublishConnectionSpecific() throws IOException { handler.publish(record); handler.flush(); - Path connLog = tempDir.resolve("BigQuery-JdbcConnection-123.log"); - assertTrue(Files.exists(connLog)); - String content = new String(Files.readAllBytes(connLog)); + Optional connLog = findLogFile("-123.log"); + assertTrue(connLog.isPresent()); + String content = new String(Files.readAllBytes(connLog.get())); assertTrue(content.contains("Test message connection 123")); } @Test - public void testCloseHandler() { + public void testCloseHandler() throws IOException { BigQueryJdbcMdc.registerInstance(mockConnection, "456"); LogRecord record = new LogRecord(Level.INFO, "Test message connection 456"); handler.publish(record); handler.flush(); - Path connLog = tempDir.resolve("BigQuery-JdbcConnection-456.log"); - assertTrue(Files.exists(connLog)); + Optional connLog = findLogFile("-456.log"); + assertTrue(connLog.isPresent()); - handler.closeHandler("JdbcConnection-456"); - assertTrue(Files.exists(connLog)); + handler.closeHandler("BQ-JDBC-456"); + assertTrue(Files.exists(connLog.get())); } }