-
Notifications
You must be signed in to change notification settings - Fork 253
Handle poller resubmission during shutdown #3076
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
frojasg
wants to merge
2
commits into
temporalio:main
Choose a base branch
from
frojasg:frojas/fix-poller-shutdown-race
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.
+130
−2
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
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
121 changes: 121 additions & 0 deletions
121
temporal-sdk/src/test/java/io/temporal/internal/worker/MultiThreadedPollerTest.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,121 @@ | ||
| package io.temporal.internal.worker; | ||
|
|
||
| import static org.junit.Assert.*; | ||
| import static org.mockito.AdditionalAnswers.delegatesTo; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import com.uber.m3.tally.NoopScope; | ||
| import io.temporal.worker.tuning.PollerBehaviorSimpleMaximum; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.RejectedExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.Test; | ||
|
|
||
| public class MultiThreadedPollerTest { | ||
|
|
||
| @Test(timeout = 10000) | ||
| public void shutdownDuringResubmissionPreservesTaskHandoff() throws Exception { | ||
| assertRejectedResubmission(true, false, new Object()); | ||
| } | ||
|
|
||
| @Test(timeout = 10000) | ||
| public void shutdownDuringResubmissionAfterEmptyPoll() throws Exception { | ||
| assertRejectedResubmission(true, false, null); | ||
| } | ||
|
|
||
| @Test(timeout = 10000) | ||
| public void rejectionWhileRunningIsReported() throws Exception { | ||
| assertRejectedResubmission(false, false, new Object()); | ||
| } | ||
|
|
||
| @Test(timeout = 10000) | ||
| public void interruptionDoesNotSuppressRejectionWhileRunning() throws Exception { | ||
| assertRejectedResubmission(false, true, new Object()); | ||
| } | ||
|
|
||
| private void assertRejectedResubmission(boolean shutdown, boolean interrupt, Object task) | ||
| throws Exception { | ||
| CountDownLatch pollStarted = new CountDownLatch(1); | ||
| CountDownLatch finishPoll = new CountDownLatch(1); | ||
| AtomicReference<Thread> pollThread = new AtomicReference<>(); | ||
| AtomicReference<Throwable> uncaught = new AtomicReference<>(); | ||
| AtomicInteger polls = new AtomicInteger(); | ||
| AtomicInteger resubmissions = new AtomicInteger(); | ||
| ShutdownableTaskExecutor<Object> taskExecutor = mock(ShutdownableTaskExecutor.class); | ||
| PollerOptions options = | ||
| PollerOptions.newBuilder() | ||
| .setPollThreadNamePrefix("test-poller") | ||
| .setPollerBehavior(new PollerBehaviorSimpleMaximum(1)) | ||
| .setUncaughtExceptionHandler((thread, error) -> uncaught.set(error)) | ||
| .build(); | ||
| MultiThreadedPoller<Object> poller = | ||
| new MultiThreadedPoller<>( | ||
| "test", | ||
| () -> { | ||
| polls.incrementAndGet(); | ||
| pollThread.set(Thread.currentThread()); | ||
| pollStarted.countDown(); | ||
| try { | ||
| assertTrue(finishPoll.await(5, TimeUnit.SECONDS)); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(e); | ||
| } | ||
| return task; | ||
| }, | ||
| taskExecutor, | ||
| options, | ||
| new NoopScope(), | ||
| new NamespaceCapabilities()); | ||
|
|
||
| assertTrue(poller.start()); | ||
| ExecutorService executor = poller.pollExecutor; | ||
| try { | ||
| assertTrue(pollStarted.await(5, TimeUnit.SECONDS)); | ||
| ExecutorService resubmissionExecutor = mock(ExecutorService.class, delegatesTo(executor)); | ||
| RejectedExecutionException rejection = new RejectedExecutionException("Executor is running"); | ||
| doAnswer( | ||
| invocation -> { | ||
| resubmissions.incrementAndGet(); | ||
| // Force shutdown after shouldTerminate() but before the executor accepts the task. | ||
| if (shutdown) { | ||
| executor.shutdown(); | ||
| executor.execute(invocation.getArgument(0)); | ||
| } else { | ||
| if (interrupt) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| throw rejection; | ||
| } | ||
| return null; | ||
| }) | ||
| .when(resubmissionExecutor) | ||
| .execute(any(Runnable.class)); | ||
| poller.pollExecutor = resubmissionExecutor; | ||
| finishPoll.countDown(); | ||
|
|
||
| // Join the thread so its uncaught exception handler has also finished. | ||
| pollThread.get().join(5000); | ||
| assertFalse(pollThread.get().isAlive()); | ||
| assertEquals(1, polls.get()); | ||
| assertEquals(1, resubmissions.get()); | ||
| if (shutdown) { | ||
| assertNull(uncaught.get()); | ||
| assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); | ||
| } else { | ||
| assertSame(rejection, uncaught.get()); | ||
| } | ||
| if (task != null) { | ||
| verify(taskExecutor).process(task); | ||
| } | ||
| verifyNoMoreInteractions(taskExecutor); | ||
| } finally { | ||
| finishPoll.countDown(); | ||
| executor.shutdownNow(); | ||
| assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); | ||
| } | ||
| } | ||
| } |
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.
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.
If we hit this case where the Resubmit fails and the poller is shutting down shouldn't we log the "poll loop is terminated" like we do if we detect shutdown before we submit