Skip to content
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

[Optimization] Replace Timer to ScheduleExecutorService #4208

Merged
merged 6 commits into from
Apr 27, 2022

Conversation

Oliverwqcwrw
Copy link
Member

Make sure set the target branch to develop

What is the purpose of the change

Replace Timer to ScheduleExecutorService

Follow this checklist to help us incorporate your contribution quickly and easily. Notice, it would be helpful if you could finish the following 5 checklist(the last one is not necessary)before request the community to review your PR.

  • Make sure there is a Github issue filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [ISSUE #123] Fix UnknownException when host config not exist. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test(over 80% coverage) to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn -B clean apache-rat:check findbugs:findbugs checkstyle:checkstyle to make sure basic checks pass. Run mvn clean install -DskipITs to make sure unit-test pass. Run mvn clean test-compile failsafe:integration-test to make sure integration-test pass.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

@lwclover
Copy link
Contributor

Two small suggestions.

@@ -317,7 +320,8 @@ class StatsBenchmarkBatchProducer {

private final LongAdder sendMessageFailedCount = new LongAdder();

private final Timer timer = new Timer("BenchmarkTimerThread", true);
private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("BenchmarkTimerThread-%d").daemon(true).build());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to use ThreadFactoryImpl, just like this:

private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emmm, I prefer to like present way

@@ -360,17 +364,17 @@ public LongAdder getSendMessageFailedCount() {

public void start() {

timer.scheduleAtFixedRate(new TimerTask() {
executorService.scheduleAtFixedRate(new TimerTask() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code can be more concise, such as using a lambda expression

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

@HScarb
Copy link
Contributor

HScarb commented Apr 26, 2022

The client module will be compile with jdk 1.7 or jdk 1.6, which not support lambda

@@ -360,17 +364,17 @@ public LongAdder getSendMessageFailedCount() {

public void start() {

timer.scheduleAtFixedRate(new TimerTask() {
executorService.scheduleAtFixedRate(new TimerTask() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems better to implement Runnable anonymously

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@@ -317,7 +320,8 @@ class StatsBenchmarkBatchProducer {

private final LongAdder sendMessageFailedCount = new LongAdder();

private final Timer timer = new Timer("BenchmarkTimerThread", true);
private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("BenchmarkTimerThread-%d").daemon(true).build());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A unified style is recommended

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get your meaning,Can you provide more detail info

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as @cserwen mentioned.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok,Thanks for your suggestions

@Oliverwqcwrw
Copy link
Member Author

I have modified it,Please check if have other issue

@@ -317,7 +318,8 @@ class StatsBenchmarkBatchProducer {

private final LongAdder sendMessageFailedCount = new LongAdder();

private final Timer timer = new Timer("BenchmarkTimerThread", true);
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl(
"BenchmarkTimerThread"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

daemon thread

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your checking,I have solved it

@codecov-commenter
Copy link

codecov-commenter commented Apr 26, 2022

Codecov Report

Merging #4208 (1a61d3c) into develop (5d01023) will decrease coverage by 0.03%.
The diff coverage is n/a.

@@              Coverage Diff              @@
##             develop    #4208      +/-   ##
=============================================
- Coverage      48.13%   48.09%   -0.04%     
+ Complexity      5038     5030       -8     
=============================================
  Files            636      636              
  Lines          42506    42506              
  Branches        5568     5568              
=============================================
- Hits           20459    20443      -16     
- Misses         19564    19575      +11     
- Partials        2483     2488       +5     
Impacted Files Coverage Δ
...rg/apache/rocketmq/common/stats/StatsSnapshot.java 84.61% <0.00%> (-15.39%) ⬇️
.../apache/rocketmq/common/stats/MomentStatsItem.java 38.09% <0.00%> (-9.53%) ⬇️
...ache/rocketmq/common/stats/MomentStatsItemSet.java 39.13% <0.00%> (-8.70%) ⬇️
...org/apache/rocketmq/common/stats/StatsItemSet.java 46.26% <0.00%> (-7.47%) ⬇️
...va/org/apache/rocketmq/common/stats/StatsItem.java 50.00% <0.00%> (-5.00%) ⬇️
...ketmq/common/protocol/body/ConsumerConnection.java 95.83% <0.00%> (-4.17%) ⬇️
...rocketmq/common/message/MessageClientIDSetter.java 73.80% <0.00%> (-1.20%) ⬇️
...a/org/apache/rocketmq/store/StoreStatsService.java 36.97% <0.00%> (-1.13%) ⬇️
...ent/impl/consumer/DefaultLitePullConsumerImpl.java 68.79% <0.00%> (-0.52%) ⬇️
...mq/client/impl/producer/DefaultMQProducerImpl.java 45.21% <0.00%> (-0.38%) ⬇️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d01023...1a61d3c. Read the comment docs.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 52.068% when pulling 1a61d3c on Oliverwqcwrw:develop-replace-timer-2 into 9de7be4 on apache:develop.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 52.068% when pulling 1a61d3c on Oliverwqcwrw:develop-replace-timer-2 into 9de7be4 on apache:develop.

@@ -317,7 +318,8 @@ class StatsBenchmarkBatchProducer {

private final LongAdder sendMessageFailedCount = new LongAdder();

private final Timer timer = new Timer("BenchmarkTimerThread", true);
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl(
"BenchmarkTimerThread", Boolean.TRUE));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent here maybe wrong, pls import rmq_codeStyle.xml in style folder and reformat the code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, if indent is wrong,the checking will not passing

@duhenglucky duhenglucky merged commit a800706 into apache:develop Apr 27, 2022
@duhenglucky duhenglucky added this to the 4.9.4 milestone Apr 27, 2022
GenerousMan pushed a commit to GenerousMan/rocketmq that referenced this pull request Aug 12, 2022
* replace Timer to ScheduleExecutorService

* replace timetask to runnable

* Change the style of creating ThreadPoolExecutor

* Remove unused import

* set daemon

* checkstyle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants