-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[SessionPool] Fix graceful shutdown by notifying waiting threads in close() #17381
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
ShemShadrack
wants to merge
2
commits into
apache:master
Choose a base branch
from
ShemShadrack:master
base: master
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.
+72
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -74,8 +74,11 @@ | |||||||
| import java.util.List; | ||||||||
| import java.util.Map; | ||||||||
| import java.util.concurrent.ConcurrentLinkedDeque; | ||||||||
| import java.util.concurrent.CountDownLatch; | ||||||||
| import java.util.concurrent.TimeUnit; | ||||||||
|
|
||||||||
| import static org.junit.Assert.assertEquals; | ||||||||
| import static org.junit.Assert.assertNotNull; | ||||||||
| import static org.junit.Assert.assertTrue; | ||||||||
| import static org.mockito.ArgumentMatchers.any; | ||||||||
| import static org.mockito.ArgumentMatchers.eq; | ||||||||
|
|
@@ -1623,4 +1626,69 @@ private List<ByteBuffer> FakedFirstFetchTsBlockResult() { | |||||||
|
|
||||||||
| return Collections.singletonList(tsBlock); | ||||||||
| } | ||||||||
|
|
||||||||
| // Regression test for graceful shutdown | ||||||||
| @Test(timeout = 5000) | ||||||||
| public void testCloseNotifiesWaitingThreads() throws Exception { | ||||||||
| SessionPool pool = | ||||||||
| new SessionPool.Builder() | ||||||||
| .host("localhost") | ||||||||
| .port(6667) | ||||||||
| .user("root") | ||||||||
| .password("root") | ||||||||
| .maxSize(1) | ||||||||
| .waitToGetSessionTimeoutInMs(10000) | ||||||||
| .build(); | ||||||||
|
|
||||||||
| try { | ||||||||
| Session mockSession = Mockito.mock(Session.class); | ||||||||
| ConcurrentLinkedDeque<ISession> queue = | ||||||||
| (ConcurrentLinkedDeque<ISession>) Whitebox.getInternalState(pool, "queue"); | ||||||||
| queue.push(mockSession); | ||||||||
| Whitebox.setInternalState(pool, "size", 1); | ||||||||
|
|
||||||||
| ISession occupiedSession = (ISession) Whitebox.invokeMethod(pool, "getSession"); | ||||||||
| assertEquals(mockSession, occupiedSession); | ||||||||
| assertEquals(0, queue.size()); | ||||||||
|
|
||||||||
| final Exception[] caughtException = {null}; | ||||||||
| CountDownLatch latch = new CountDownLatch(1); | ||||||||
|
|
||||||||
| Thread waiterThread = | ||||||||
| new Thread( | ||||||||
| () -> { | ||||||||
| try { | ||||||||
| latch.countDown(); | ||||||||
| Whitebox.invokeMethod(pool, "getSession"); | ||||||||
| } catch (Exception e) { | ||||||||
| caughtException[0] = e; | ||||||||
| } | ||||||||
| }); | ||||||||
| waiterThread.start(); | ||||||||
|
|
||||||||
| assertTrue("Waiter thread should have started", latch.await(10, TimeUnit.SECONDS)); | ||||||||
| // Give it a moment to enter the wait(1000) block in getSession() | ||||||||
| Thread.sleep(200); | ||||||||
|
|
||||||||
| pool.close(); | ||||||||
|
|
||||||||
| waiterThread.join(500); | ||||||||
| assertTrue("Waiter thread should be unblocked quickly", !waiterThread.isAlive()); | ||||||||
|
|
||||||||
| assertNotNull("Waiter thread should have caught an exception", caughtException[0]); | ||||||||
| assertTrue( | ||||||||
| "Exception should be IoTDBConnectionException", | ||||||||
| caughtException[0] instanceof IoTDBConnectionException); | ||||||||
| assertTrue( | ||||||||
| "Exception message should indicate pool is closed", | ||||||||
| caughtException[0].getMessage().contains("closed")); | ||||||||
|
|
||||||||
| } finally { | ||||||||
| try { | ||||||||
| pool.close(); | ||||||||
| } catch (Exception e) { | ||||||||
|
||||||||
| } catch (Exception e) { | |
| } catch (Exception e) { | |
| // ignore: best-effort cleanup in test |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description says waiting threads could remain blocked for “up to the full timeout period”, but
getSession()currently useswait(1000), so the pre-fix worst-case delay appears to be ~1s rather thanwaitToGetSessionTimeoutInMs. Consider updating the description (or the implementation, if full-timeout blocking is still expected) so the rationale matches the current code.