|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.jdbc; |
| 18 | + |
| 19 | +import java.util.concurrent.ConcurrentHashMap; |
| 20 | +import java.util.concurrent.atomic.AtomicLong; |
| 21 | + |
| 22 | +/** |
| 23 | + * Lightweight MDC implementation for the BigQuery JDBC driver using InheritableThreadLocal. |
| 24 | + * Allocates a dedicated, independent InheritableThreadLocal object per concrete BigQueryConnection |
| 25 | + * instance. |
| 26 | + */ |
| 27 | +public class BigQueryJdbcMdc { |
| 28 | + private static final AtomicLong nextId = new AtomicLong(1); |
| 29 | + private static final ConcurrentHashMap<BigQueryConnection, InheritableThreadLocal<String>> |
| 30 | + instanceLocals = new ConcurrentHashMap<>(); |
| 31 | + private static final ConcurrentHashMap<BigQueryConnection, String> instanceIds = |
| 32 | + new ConcurrentHashMap<>(); |
| 33 | + |
| 34 | + /** Allocates an exclusive InheritableThreadLocal and registers the connection mapping. */ |
| 35 | + private static final InheritableThreadLocal<String> currentConnectionId = |
| 36 | + new InheritableThreadLocal<>(); |
| 37 | + |
| 38 | + public static void registerInstance(BigQueryConnection connection, String id) { |
| 39 | + if (connection != null) { |
| 40 | + String cleanId = |
| 41 | + instanceIds.computeIfAbsent( |
| 42 | + connection, |
| 43 | + k -> { |
| 44 | + String suffix = |
| 45 | + (id != null && !id.isEmpty()) ? id : String.valueOf(nextId.getAndIncrement()); |
| 46 | + return "JdbcConnection-" + suffix; |
| 47 | + }); |
| 48 | + |
| 49 | + currentConnectionId.set(cleanId); |
| 50 | + InheritableThreadLocal<String> threadLocal = |
| 51 | + instanceLocals.computeIfAbsent(connection, k -> new InheritableThreadLocal<>()); |
| 52 | + threadLocal.set(cleanId); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns the connection ID carried by any registered active connection on the current thread. |
| 58 | + */ |
| 59 | + public static String getConnectionId() { |
| 60 | + return currentConnectionId.get(); |
| 61 | + } |
| 62 | + |
| 63 | + /** Clears the connection ID context from all active connection contexts on the current thread. */ |
| 64 | + public static void removeInstance(BigQueryConnection connection) { |
| 65 | + if (connection != null) { |
| 66 | + InheritableThreadLocal<String> local = instanceLocals.remove(connection); |
| 67 | + if (local != null) { |
| 68 | + local.remove(); |
| 69 | + } |
| 70 | + instanceIds.remove(connection); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void clear() { |
| 75 | + currentConnectionId.remove(); |
| 76 | + for (InheritableThreadLocal<String> local : instanceLocals.values()) { |
| 77 | + local.remove(); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments