-
Notifications
You must be signed in to change notification settings - Fork 12k
Optimize shutdownScheduledExecutorService to ensure complete task termination #9872
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
base: develop
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1663,11 +1663,35 @@ protected void shutdownScheduledExecutorService(ScheduledExecutorService schedul | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (scheduledExecutorService == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Graceful shutdown: stop accepting new tasks and wait for submitted tasks to complete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheduledExecutorService.shutdown(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheduledExecutorService.awaitTermination(5000, TimeUnit.MILLISECONDS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (InterruptedException ignore) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.warn("shutdown ScheduledExecutorService was Interrupted! ", ignore); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Wait for tasks to complete, at most 60 seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!scheduledExecutorService.awaitTermination(60000, TimeUnit.MILLISECONDS)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If timeout, force shutdown all tasks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.warn("ScheduledExecutorService did not terminate gracefully, forcing shutdown..."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<Runnable> pendingTasks = scheduledExecutorService.shutdownNow(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!pendingTasks.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.warn("ScheduledExecutorService had {} pending tasks that were cancelled", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pendingTasks.size()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Wait again for a period to ensure all tasks are terminated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!scheduledExecutorService.awaitTermination(5000, TimeUnit.MILLISECONDS)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.error("ScheduledExecutorService did not terminate after forced shutdown"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.info("ScheduledExecutorService terminated successfully after forced shutdown"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.debug("ScheduledExecutorService terminated gracefully"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (InterruptedException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If interrupted during waiting, force shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BrokerController.LOG.warn("shutdown ScheduledExecutorService was Interrupted, forcing shutdown...", e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheduledExecutorService.shutdownNow(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1683
to
+1694
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!scheduledExecutorService.awaitTermination(5000, TimeUnit.MILLISECONDS)) { | |
| BrokerController.LOG.error("ScheduledExecutorService did not terminate after forced shutdown"); | |
| } else { | |
| BrokerController.LOG.info("ScheduledExecutorService terminated successfully after forced shutdown"); | |
| } | |
| } else { | |
| BrokerController.LOG.debug("ScheduledExecutorService terminated gracefully"); | |
| } | |
| } catch (InterruptedException e) { | |
| // If interrupted during waiting, force shutdown | |
| BrokerController.LOG.warn("shutdown ScheduledExecutorService was Interrupted, forcing shutdown...", e); | |
| scheduledExecutorService.shutdownNow(); | |
| try { | |
| if (!scheduledExecutorService.awaitTermination(5000, TimeUnit.MILLISECONDS)) { | |
| BrokerController.LOG.error("ScheduledExecutorService did not terminate after forced shutdown"); | |
| } else { | |
| BrokerController.LOG.info("ScheduledExecutorService terminated successfully after forced shutdown"); | |
| } | |
| } catch (InterruptedException ie) { | |
| BrokerController.LOG.warn("Interrupted while waiting for ScheduledExecutorService to terminate after forced shutdown", ie); | |
| Thread.currentThread().interrupt(); | |
| } | |
| } else { | |
| BrokerController.LOG.debug("ScheduledExecutorService terminated gracefully"); | |
| } | |
| } catch (InterruptedException e) { | |
| // If interrupted during initial waiting, force shutdown | |
| BrokerController.LOG.warn("shutdown ScheduledExecutorService was Interrupted during initial wait, forcing shutdown...", e); |
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.
[nitpick] The timeout has been increased from 5 seconds to 60 seconds per executor shutdown. Since
shutdownScheduledExecutorServiceis called three times during broker shutdown (lines 1508, 1603, 1604), this could potentially extend total shutdown time from 15 seconds to 180 seconds (3 minutes) in the worst-case scenario where all three executors time out.While this provides more time for graceful task completion, consider:
BrokerConfigto allow operators to tune it based on their workloadNote: The test at
BrokerShutdownTest.testBrokerGracefulShutdowncurrently expects shutdown within 40 seconds, which would need to be updated if this long timeout is retained.