-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryJdbcMdc.java
More file actions
177 lines (154 loc) · 5.25 KB
/
Copy pathBigQueryJdbcMdc.java
File metadata and controls
177 lines (154 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* 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.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** Lightweight MDC implementation for the BigQuery JDBC driver using InheritableThreadLocal. */
class BigQueryJdbcMdc {
private static final BigQueryJdbcCustomLogger LOG =
new BigQueryJdbcCustomLogger(BigQueryJdbcMdc.class.getName());
private static final InheritableThreadLocal<String> currentConnectionId =
new InheritableThreadLocal<>();
/** Sets the current connection context on the executing thread. */
static MdcCloseable registerInstance(String connectionId) {
if (connectionId != null) {
currentConnectionId.set(connectionId);
}
return () -> clear();
}
/**
* Returns the connection ID carried by any registered active connection on the current thread.
*/
static String getConnectionId() {
return currentConnectionId.get();
}
static void clear() {
currentConnectionId.remove();
}
/**
* Creates a new fixed thread pool ExecutorService that automatically propagates MDC connection
* context from the submitting thread to the executing thread.
*/
static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new MdcThreadPoolExecutor(
nThreads,
nThreads,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new MdcThreadFactory(threadFactory));
}
/**
* Creates a new fixed thread pool ExecutorService that automatically propagates MDC connection
* context from the submitting thread to the executing thread.
*/
static ExecutorService newFixedThreadPool(int nThreads) {
return newFixedThreadPool(nThreads, Executors.defaultThreadFactory());
}
private static class MdcThreadFactory implements ThreadFactory {
private final ThreadFactory delegate;
public MdcThreadFactory(ThreadFactory delegate) {
this.delegate = delegate;
}
@Override
public Thread newThread(Runnable r) {
return delegate.newThread(
() -> {
clear();
r.run();
});
}
}
private static class MdcThreadPoolExecutor extends ThreadPoolExecutor {
public MdcThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
@Override
public void execute(Runnable command) {
if (command == null) {
throw new NullPointerException();
}
int queueSize = getQueue().size();
if (queueSize > 0 && queueSize % 10 == 0) {
LOG.warning(
"Thread pool queue size reached "
+ queueSize
+ ". Tasks are waiting for threads to free up.");
}
if (command instanceof MdcFutureTask) {
super.execute(command);
} else {
super.execute(wrap(command));
}
}
@Override
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return new MdcFutureTask<>(runnable, value, getConnectionId());
}
@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return new MdcFutureTask<>(callable, getConnectionId());
}
private static Runnable wrap(Runnable runnable) {
String connectionId = getConnectionId();
return () -> {
try (MdcCloseable mdc = registerInstance(connectionId)) {
runnable.run();
}
};
}
}
private static class MdcFutureTask<V> extends FutureTask<V> {
private final String connectionId;
public MdcFutureTask(Runnable runnable, V result, String connectionId) {
super(runnable, result);
this.connectionId = connectionId;
}
public MdcFutureTask(Callable<V> callable, String connectionId) {
super(callable);
this.connectionId = connectionId;
}
@Override
public void run() {
try (MdcCloseable mdc = registerInstance(connectionId)) {
super.run();
}
}
}
/**
* Functional interface that extends AutoCloseable to avoid throwing checked exceptions in
* try-with-resources.
*/
@FunctionalInterface
interface MdcCloseable extends AutoCloseable {
@Override
void close();
}
}