-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(all): tune single Thread into SingleThreadExecutor #5410
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
halibobo1205
merged 5 commits into
tronprotocol:develop
from
halibobo1205:feat/thread_opt
Aug 15, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbbb75d
feat(*): tune single Thread into SingleThreadExecutor
halibobo1205 5708796
add test
halibobo1205 d92bcca
keep filterEs none-daemon as before
halibobo1205 3ac0795
simplify the shutdownAndAwaitTermination logs
halibobo1205 87c1b5d
adjust logback shutdown delay
halibobo1205 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
55 changes: 55 additions & 0 deletions
55
common/src/main/java/org/tron/common/es/ExecutorServiceManager.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,55 @@ | ||
| package org.tron.common.es; | ||
|
|
||
| import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j(topic = "common") | ||
| public class ExecutorServiceManager { | ||
|
|
||
| public static ExecutorService newSingleThreadExecutor(String name) { | ||
| return newSingleThreadExecutor(name, false); | ||
| } | ||
|
|
||
| public static ExecutorService newSingleThreadExecutor(String name, boolean isDaemon) { | ||
| return Executors.newSingleThreadExecutor( | ||
| new ThreadFactoryBuilder().setNameFormat(name).setDaemon(isDaemon).build()); | ||
| } | ||
|
|
||
|
|
||
| public static ScheduledExecutorService newSingleThreadScheduledExecutor(String name) { | ||
| return newSingleThreadScheduledExecutor(name, false); | ||
| } | ||
|
|
||
| public static ScheduledExecutorService newSingleThreadScheduledExecutor(String name, | ||
| boolean isDaemon) { | ||
| return Executors.newSingleThreadScheduledExecutor( | ||
| new ThreadFactoryBuilder().setNameFormat(name).setDaemon(isDaemon).build()); | ||
| } | ||
|
|
||
| public static void shutdownAndAwaitTermination(ExecutorService pool, String name) { | ||
| if (pool == null) { | ||
| return; | ||
| } | ||
| logger.info("Pool {} shutdown...", name); | ||
| pool.shutdown(); // Disable new tasks from being submitted | ||
| try { | ||
| // Wait a while for existing tasks to terminate | ||
| if (!pool.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS)) { | ||
| pool.shutdownNow(); // Cancel currently executing tasks | ||
| // Wait a while for tasks to respond to being cancelled | ||
| if (!pool.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS)) { | ||
| logger.warn("Pool {} did not terminate", name); | ||
| } | ||
| } | ||
| } catch (InterruptedException ie) { | ||
| // (Re-)Cancel if current thread also interrupted | ||
| pool.shutdownNow(); | ||
| // Preserve interrupt status | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| logger.info("Pool {} shutdown done", name); | ||
| } | ||
| } | ||
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
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
44 changes: 44 additions & 0 deletions
44
framework/src/test/java/org/tron/common/backup/BackupServerTest.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,44 @@ | ||
| package org.tron.common.backup; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
| import org.tron.common.backup.socket.BackupServer; | ||
| import org.tron.common.parameter.CommonParameter; | ||
| import org.tron.core.Constant; | ||
| import org.tron.core.config.args.Args; | ||
|
|
||
|
|
||
| public class BackupServerTest { | ||
|
|
||
| @Rule | ||
| public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
| private BackupServer backupServer; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); | ||
| CommonParameter.getInstance().setBackupPort(80); | ||
| List<String> members = new ArrayList<>(); | ||
| members.add("127.0.0.2"); | ||
| CommonParameter.getInstance().setBackupMembers(members); | ||
| BackupManager backupManager = new BackupManager(); | ||
| backupManager.init(); | ||
| backupServer = new BackupServer(backupManager); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| backupServer.close(); | ||
| Args.clearParam(); | ||
| } | ||
|
|
||
| @Test | ||
| public void test() throws InterruptedException { | ||
| backupServer.initServer(); | ||
| } | ||
| } |
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.
Why there are two waits of 60s, will the 60s time be too long?
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.
60s is the maximum value to be waited until the pool stops, if it doesn't, the pool may have a problem executing the task and needs to be fixed.
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.
copy from java.util.concurrent.ExecutorService