-
Notifications
You must be signed in to change notification settings - Fork 1.2k
chore(bigquery-jdbc): add the bridge Thread wrap ahead of Executor migration #13482
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
Changes from 3 commits
23d9ce9
baf073f
1821bf0
a08c8ad
553a4d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,7 @@ | |
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.LinkedBlockingQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
@@ -5264,4 +5265,89 @@ private void loadDriverVersionProperties() { | |
| throw ex; | ||
| } | ||
| } | ||
|
|
||
| // TODO(keshav): This is a temporary compatibility bridge to wrap raw Threads into Futures. | ||
| // This should be removed when BigQueryDatabaseMetaData is refactored to use the ExecutorService | ||
| // directly. | ||
| static Future<?>[] wrapThread(final Thread thread) { | ||
| if (thread == null) { | ||
| return null; | ||
| } | ||
| return new Future<?>[] { | ||
| new Future<Object>() { | ||
| private volatile boolean cancelled = false; | ||
|
|
||
| @Override | ||
| public synchronized boolean cancel(boolean mayInterruptIfRunning) { | ||
| if (cancelled || thread.getState() == Thread.State.TERMINATED) { | ||
| return false; | ||
| } | ||
| cancelled = true; | ||
| if (mayInterruptIfRunning) { | ||
| thread.interrupt(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCancelled() { | ||
| return cancelled; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isDone() { | ||
| return cancelled || thread.getState() == Thread.State.TERMINATED; | ||
| } | ||
|
|
||
| @Override | ||
| public Object get() throws InterruptedException, CancellationException { | ||
| if (isCancelled()) { | ||
| throw new CancellationException(); | ||
| } | ||
| while (thread.getState() != Thread.State.TERMINATED) { | ||
| if (isCancelled()) { | ||
| throw new CancellationException(); | ||
| } | ||
| if (thread.getState() == Thread.State.NEW) { | ||
| Thread.sleep(50); | ||
| } else { | ||
| thread.join(50); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Object get(long timeout, TimeUnit unit) | ||
| throws InterruptedException, CancellationException, TimeoutException { | ||
| if (isCancelled()) { | ||
| throw new CancellationException(); | ||
| } | ||
| long remainingNanos = unit.toNanos(timeout); | ||
| long deadline = System.nanoTime() + remainingNanos; | ||
| while (thread.getState() != Thread.State.TERMINATED) { | ||
| if (isCancelled()) { | ||
| throw new CancellationException(); | ||
| } | ||
| if (remainingNanos <= 0) { | ||
| throw new TimeoutException(); | ||
| } | ||
| long remainingMillis = TimeUnit.NANOSECONDS.toMillis(remainingNanos); | ||
| if (remainingMillis == 0) { | ||
| remainingMillis = 1; | ||
| } | ||
|
|
||
| long delay = Math.min(remainingMillis, 50); | ||
| if (thread.getState() == Thread.State.NEW) { | ||
| Thread.sleep(delay); | ||
|
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. Why the difference sleep vs join? Also we can just call
Contributor
Author
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. We use |
||
| } else { | ||
| thread.join(delay); | ||
| } | ||
| remainingNanos = deadline - System.nanoTime(); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.