Skip to content

Commit 513a3eb

Browse files
committed
cached thread pool
1 parent b0021ad commit 513a3eb

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
349349

350350
this.headerProvider = createHeaderProvider();
351351
this.bigQuery = getBigQueryConnection();
352-
this.metadataExecutor = BigQueryJdbcMdc.newFixedThreadPool(this.metadataFetchThreadCount);
353-
this.queryExecutor = BigQueryJdbcMdc.newFixedThreadPool(this.queryExecutionThreadCount);
352+
this.metadataExecutor = BigQueryJdbcMdc.newCachedThreadPool();
353+
this.queryExecutor = BigQueryJdbcMdc.newCachedThreadPool();
354354
}
355355
}
356356

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,28 @@ static ExecutorService newFixedThreadPool(int nThreads) {
7777
return newFixedThreadPool(nThreads, Executors.defaultThreadFactory());
7878
}
7979

80+
/**
81+
* Creates a new cached thread pool ExecutorService that automatically propagates MDC connection
82+
* context from the submitting thread to the executing thread.
83+
*/
84+
static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
85+
return new MdcThreadPoolExecutor(
86+
0,
87+
Integer.MAX_VALUE,
88+
60L,
89+
TimeUnit.SECONDS,
90+
new java.util.concurrent.SynchronousQueue<>(),
91+
new MdcThreadFactory(threadFactory));
92+
}
93+
94+
/**
95+
* Creates a new cached thread pool ExecutorService that automatically propagates MDC connection
96+
* context from the submitting thread to the executing thread.
97+
*/
98+
static ExecutorService newCachedThreadPool() {
99+
return newCachedThreadPool(Executors.defaultThreadFactory());
100+
}
101+
80102
private static class MdcThreadFactory implements ThreadFactory {
81103
private final ThreadFactory delegate;
82104

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,14 @@ public void testConnectionScopedExecutorLifecycle() throws Exception {
281281
assertTrue(metadataExec1 instanceof ThreadPoolExecutor);
282282
assertTrue(metadataExec2 instanceof ThreadPoolExecutor);
283283

284-
assertEquals(6, ((ThreadPoolExecutor) exec1).getCorePoolSize());
285-
assertEquals(12, ((ThreadPoolExecutor) exec2).getCorePoolSize());
286-
assertEquals(5, ((ThreadPoolExecutor) metadataExec1).getCorePoolSize());
287-
assertEquals(10, ((ThreadPoolExecutor) metadataExec2).getCorePoolSize());
284+
assertEquals(0, ((ThreadPoolExecutor) exec1).getCorePoolSize());
285+
assertEquals(Integer.MAX_VALUE, ((ThreadPoolExecutor) exec1).getMaximumPoolSize());
286+
assertEquals(0, ((ThreadPoolExecutor) exec2).getCorePoolSize());
287+
assertEquals(Integer.MAX_VALUE, ((ThreadPoolExecutor) exec2).getMaximumPoolSize());
288+
assertEquals(0, ((ThreadPoolExecutor) metadataExec1).getCorePoolSize());
289+
assertEquals(Integer.MAX_VALUE, ((ThreadPoolExecutor) metadataExec1).getMaximumPoolSize());
290+
assertEquals(0, ((ThreadPoolExecutor) metadataExec2).getCorePoolSize());
291+
assertEquals(Integer.MAX_VALUE, ((ThreadPoolExecutor) metadataExec2).getMaximumPoolSize());
288292

289293
try (BigQueryJdbcMdc.MdcCloseable mdc =
290294
BigQueryJdbcMdc.registerInstance(conn1.getConnectionId())) {

0 commit comments

Comments
 (0)