-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(bigquery-jdbc): support connection-scoped executor isolation and dynamic queue warnings #13413
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
11
commits into
main
Choose a base branch
from
per-conn-thread-pool-2
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.
+328
−12
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c0278c6
feat(bigquery-jdbc): support connection-scoped executor isolation and…
Neenu1995 6765ee7
fix shutdown
Neenu1995 22defa2
address comments
Neenu1995 b0021ad
update default count
Neenu1995 513a3eb
cached thread pool
Neenu1995 aea274d
clean up unused connection property
Neenu1995 41e00f3
fix test
Neenu1995 99dd5c6
refactor close
Neenu1995 afad269
remove fake.p12
Neenu1995 992d4dd
add agents to gitignore
Neenu1995 95ab1dd
chore: restore fake.p12
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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 |
|---|---|---|
|
|
@@ -26,9 +26,13 @@ | |
| import java.util.concurrent.ThreadFactory; | ||
| import java.util.concurrent.ThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| /** 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<>(); | ||
|
|
||
|
|
@@ -73,6 +77,28 @@ static ExecutorService newFixedThreadPool(int nThreads) { | |
| return newFixedThreadPool(nThreads, Executors.defaultThreadFactory()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new cached thread pool ExecutorService that automatically propagates MDC connection | ||
| * context from the submitting thread to the executing thread. | ||
| */ | ||
| static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { | ||
| return new MdcThreadPoolExecutor( | ||
| 0, | ||
| Integer.MAX_VALUE, | ||
| 60L, | ||
| TimeUnit.SECONDS, | ||
| new java.util.concurrent.SynchronousQueue<>(), | ||
| new MdcThreadFactory(threadFactory)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new cached thread pool ExecutorService that automatically propagates MDC connection | ||
| * context from the submitting thread to the executing thread. | ||
| */ | ||
| static ExecutorService newCachedThreadPool() { | ||
| return newCachedThreadPool(Executors.defaultThreadFactory()); | ||
| } | ||
|
|
||
| private static class MdcThreadFactory implements ThreadFactory { | ||
| private final ThreadFactory delegate; | ||
|
|
||
|
|
@@ -82,11 +108,16 @@ public MdcThreadFactory(ThreadFactory delegate) { | |
|
|
||
| @Override | ||
| public Thread newThread(Runnable r) { | ||
| return delegate.newThread( | ||
| () -> { | ||
| clear(); | ||
| r.run(); | ||
| }); | ||
| Thread t = | ||
| delegate.newThread( | ||
| () -> { | ||
| clear(); | ||
| r.run(); | ||
| }); | ||
| if (t != null) { | ||
| t.setDaemon(true); | ||
| } | ||
| return t; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -102,11 +133,37 @@ public MdcThreadPoolExecutor( | |
| super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); | ||
| } | ||
|
|
||
| private final AtomicBoolean warningLogged = new AtomicBoolean(false); | ||
|
|
||
| private void monitorQueueSaturation(int queueSize) { | ||
| int corePoolSize = getCorePoolSize(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it needs to be maxPoolSize? Exceeding core is fine, new threads are being added. |
||
| // Warn when queue size is >= corePoolSize * 5, with a minimum of 10 tasks to avoid false | ||
| // alerts for tiny pools | ||
| int warnThreshold = Math.max(10, corePoolSize * 5); | ||
| // Recovery reset threshold is corePoolSize * 2, with a minimum of 4 tasks | ||
| int recoveryThreshold = Math.max(4, corePoolSize * 2); | ||
|
|
||
| if (queueSize >= warnThreshold) { | ||
| if (warningLogged.compareAndSet(false, true)) { | ||
| LOG.warning( | ||
| "Thread pool is saturating. Core pool size: %d, Active threads: %d, Queued tasks: %d. Consider increasing the thread count property.", | ||
| corePoolSize, getActiveCount(), queueSize); | ||
| } | ||
| } else if (queueSize <= recoveryThreshold) { | ||
| if (warningLogged.get()) { | ||
| warningLogged.set(false); | ||
| } | ||
| } | ||
|
Neenu1995 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| public void execute(Runnable command) { | ||
| if (command == null) { | ||
| throw new NullPointerException(); | ||
| } | ||
|
|
||
| monitorQueueSaturation(getQueue().size()); | ||
|
|
||
| if (command instanceof MdcFutureTask) { | ||
| super.execute(command); | ||
| } else { | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.