-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bqjdbc): Per connection logs - Add BigQueryJdbcMdc #12833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Neenu1995
wants to merge
9
commits into
main
Choose a base branch
from
ns/mdc-per-conn-logs2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9044bd3
add BigQueryJdbcMdc
Neenu1995 066f36b
Use DateTimeFormatter instead of SimpleDateFormat for better performance
Neenu1995 f7ac0eb
Add BigQueryJdbcMdcTest
Neenu1995 93d7b5d
reformat
Neenu1995 17920d4
fix clear method
Neenu1995 0d5aa13
chore: generate libraries at Mon Apr 20 18:16:36 UTC 2026
cloud-java-bot 32a14ac
Merge branch 'main' into ns/mdc-per-conn-logs2
Neenu1995 b8299bf
Merge branch 'ns/mdc-per-conn-logs2' of github.com:googleapis/google-…
Neenu1995 d77b0a7
revert unknown file changes
Neenu1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...gle-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * 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.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. | ||
| */ | ||
| public class BigQueryJdbcMdc { | ||
| 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 = | ||
| new ConcurrentHashMap<>(); | ||
|
Neenu1995 marked this conversation as resolved.
|
||
|
|
||
| /** Allocates an exclusive InheritableThreadLocal and registers the connection mapping. */ | ||
| private static final InheritableThreadLocal<String> currentConnectionId = | ||
| new InheritableThreadLocal<>(); | ||
|
|
||
| public static void 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()); | ||
|
Neenu1995 marked this conversation as resolved.
|
||
| return "JdbcConnection-" + suffix; | ||
| }); | ||
|
|
||
| currentConnectionId.set(cleanId); | ||
| InheritableThreadLocal<String> threadLocal = | ||
| instanceLocals.computeIfAbsent(connection, k -> new InheritableThreadLocal<>()); | ||
|
Neenu1995 marked this conversation as resolved.
|
||
| threadLocal.set(cleanId); | ||
| } | ||
| } | ||
|
|
||
| /** Retrieves the connection ID mapped to a specific BigQueryConnection instance. */ | ||
| public static String getConnectionId(BigQueryConnection connection) { | ||
| if (connection != null) { | ||
| InheritableThreadLocal<String> local = instanceLocals.get(connection); | ||
| if (local != null) { | ||
| String val = local.get(); | ||
| if (val != null) { | ||
| return val; | ||
| } | ||
| } | ||
| return instanceIds.get(connection); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the connection ID carried by any registered active connection on the current thread. | ||
| */ | ||
| public static String getConnectionId() { | ||
| return currentConnectionId.get(); | ||
| } | ||
|
|
||
| /** Clears the connection ID context from all active connection contexts on the current thread. */ | ||
| public static void removeInstance(BigQueryConnection connection) { | ||
| if (connection != null) { | ||
| InheritableThreadLocal<String> local = instanceLocals.remove(connection); | ||
| if (local != null) { | ||
| local.remove(); | ||
| } | ||
| instanceIds.remove(connection); | ||
| } | ||
| } | ||
|
Neenu1995 marked this conversation as resolved.
|
||
|
|
||
| public static void clear() { | ||
|
keshavdandeva marked this conversation as resolved.
|
||
| currentConnectionId.remove(); | ||
| for (InheritableThreadLocal<String> local : instanceLocals.values()) { | ||
| local.remove(); | ||
| } | ||
| instanceLocals.clear(); | ||
| instanceIds.clear(); | ||
| } | ||
|
Neenu1995 marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcMdcTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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 static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| public class BigQueryJdbcMdcTest { | ||
|
|
||
| private BigQueryConnection mockConnection1; | ||
| private BigQueryConnection mockConnection2; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| mockConnection1 = Mockito.mock(BigQueryConnection.class); | ||
| mockConnection2 = Mockito.mock(BigQueryConnection.class); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| BigQueryJdbcMdc.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRegisterAndRetrieveConnectionId() { | ||
| BigQueryJdbcMdc.registerInstance(mockConnection1, "123"); | ||
|
|
||
| assertEquals("JdbcConnection-123", BigQueryJdbcMdc.getConnectionId(mockConnection1)); | ||
| assertEquals("JdbcConnection-123", BigQueryJdbcMdc.getConnectionId()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClearContext() { | ||
| BigQueryJdbcMdc.registerInstance(mockConnection1, "456"); | ||
| assertEquals("JdbcConnection-456", BigQueryJdbcMdc.getConnectionId(mockConnection1)); | ||
|
|
||
| BigQueryJdbcMdc.clear(); | ||
|
|
||
| assertNull(BigQueryJdbcMdc.getConnectionId()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMultipleConnectionsSameThread() { | ||
| BigQueryJdbcMdc.registerInstance(mockConnection1, "111"); | ||
| BigQueryJdbcMdc.registerInstance(mockConnection2, "222"); | ||
|
|
||
| assertEquals("JdbcConnection-111", BigQueryJdbcMdc.getConnectionId(mockConnection1)); | ||
| assertEquals("JdbcConnection-222", BigQueryJdbcMdc.getConnectionId(mockConnection2)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doubt: I think design doc says we use connection ID or UUID but here we are using incremental atomicLong? Am I confusing two different things?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using this Atomic counter to generate a connection ID like JdbcConnection-N. Figured this pattern has better readability in logs.