-
Notifications
You must be signed in to change notification settings - Fork 451
Replace timeout handling to use SharedTimer #920
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
Merged
cheenamalhotra
merged 14 commits into
microsoft:dev
from
sehrope:shared-timer-for-timeouts
Jan 10, 2019
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8bbbebc
Replace timeout handling to use SharedTimer
sehrope 240aea8
Normalize newlines in TimeoutTest
sehrope 4f49dd3
Move TimeoutTest to parent package so that it can access package priv…
sehrope 7b5f81f
Centralize SharedTimer thread checks in TimeoutTest
sehrope 16ba2c4
Centralize and reduce timeout value used in TimeoutTest
sehrope 09bab9f
Centralize SQL for WAIT FOR DELAY in TimeoutTest
sehrope 1a7b6a7
Clean up TimeoutTest to use assertThrows(...)
sehrope e76e819
Add additional SharedTimer tests
sehrope 594e5b2
Improve waiting for SharedTimer thread stoppage
sehrope 693f51c
Remove System.err.println(...) in TdsTimeoutTask
sehrope 261a857
Rename static SharedTimer singleton to instance
sehrope 8c3b1da
Replace SharedTimer ThreadFactory with inline lambda
sehrope 9f30783
Add comments for SharedTimer
sehrope e3e188b
Rename TdsTimeoutTask to TDSTimeoutTask
sehrope 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
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
73 changes: 73 additions & 0 deletions
73
src/main/java/com/microsoft/sqlserver/jdbc/SharedTimer.java
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made | ||
| * available under the terms of the MIT License. See the LICENSE file in the project root for more information. | ||
| */ | ||
| package com.microsoft.sqlserver.jdbc; | ||
|
|
||
| import java.util.concurrent.ScheduledFuture; | ||
| import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
| import java.util.concurrent.ThreadFactory; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
|
|
||
| class SharedTimer { | ||
cheenamalhotra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static final String CORE_THREAD_PREFIX = "mssql-jdbc-shared-timer-core-"; | ||
| private static final AtomicLong CORE_THREAD_COUNTER = new AtomicLong(); | ||
| private static SharedTimer INSTANCE; | ||
cheenamalhotra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private final long id = CORE_THREAD_COUNTER.getAndIncrement(); | ||
| private int refCount = 0; | ||
| private ScheduledThreadPoolExecutor executor; | ||
|
|
||
| private SharedTimer() { | ||
| executor = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { | ||
cheenamalhotra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public Thread newThread(Runnable task) { | ||
| return new Thread(task, CORE_THREAD_PREFIX + id); | ||
| } | ||
| }); | ||
| executor.setRemoveOnCancelPolicy(true); | ||
| } | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| static synchronized boolean isRunning() { | ||
| return INSTANCE != null; | ||
| } | ||
|
|
||
| public synchronized void removeRef() { | ||
| if (refCount <= 0) { | ||
| throw new IllegalStateException("removeRef() called more than actual references"); | ||
| } | ||
| refCount -= 1; | ||
| if (refCount == 0) { | ||
| // Removed last reference so perform cleanup | ||
| executor.shutdownNow(); | ||
| executor = null; | ||
| INSTANCE = null; | ||
| } | ||
| } | ||
|
|
||
| public static synchronized SharedTimer getTimer() { | ||
| if (INSTANCE == null) { | ||
| // No shared object exists so create a new one | ||
| INSTANCE = new SharedTimer(); | ||
| } | ||
| INSTANCE.refCount += 1; | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| public ScheduledFuture<?> schedule(TdsTimeoutTask task, long delaySeconds) { | ||
| return schedule(task, delaySeconds, TimeUnit.SECONDS); | ||
| } | ||
|
|
||
| public ScheduledFuture<?> schedule(TdsTimeoutTask task, long delay, TimeUnit unit) { | ||
| if (executor == null) { | ||
| throw new IllegalStateException("Cannot schedule tasks after shutdown"); | ||
| } | ||
| return executor.schedule(task, delay, unit); | ||
| } | ||
| } | ||
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.