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

fix: changed the calculating logic of epoll timeout provided by TimerTaskManager #2794

Merged

Conversation

cheniujh
Copy link
Collaborator

@cheniujh cheniujh commented Jul 10, 2024

问题
原本的TimerTaskManager(定时器)提供给epoll的timeout值为min_interval_ms,即注册在定时器内部所有定时任务中,执行间隔最小的任务的那个执行间隔。但是这样可能导致一个问题:假如有2个定时任务,一个每隔 20s执行一次,一个每隔30s执行一次,那这个定时器取的timeout就都是20s(min_time_interval), 也就是每隔20s才会检查一下有没有任务过期。这样就会导致那个30s间隔的定时任务每次都要延迟10s才能被执行到。

解决
直接取下一个即将过期的定时任务的过期时间戳减去当前时间戳,得到的值交给epoll_wait作为timeout参数即可。这样逻辑更加简单直接,而且可以规避上面提到的间隔误差问题。

Question: Originally, the TimerTaskManager (timer) provided the timeout value for epoll as min_interval_ms, which is the execution interval of the task with the shortest interval among all the scheduled tasks within the timer. However, this approach can lead to a problem: if there are two scheduled tasks, one that executes every 20 seconds and another that executes every 30 seconds, the timer will take 20 seconds (min_time_interval) as the timeout value. This means it will check for expired tasks every 20 seconds. Consequently, the task with a 30-second interval will always be delayed by 10 seconds before it is executed.

Solution:
Directly take the difference between the expiration timestamp of the next task to expire and the current timestamp, and pass this value to epoll_wait as the timeout parameter. This approach is simpler and more direct, and it avoids the interval error problem mentioned above.

Summary by CodeRabbit

  • Bug Fixes
    • Improved accuracy in task execution times by modifying the return type of ExecTimerTask.
  • Refactor
    • Removed outdated RenewMinIntervalMs method to streamline code.
  • Performance
    • Enhanced execution and management logic for TimerTaskManager, resulting in better task handling and timeout calculation.

Copy link

coderabbitai bot commented Jul 10, 2024

Walkthrough

The TimerTaskManager class has been updated to improve task execution and management. This includes changing the return type of ExecTimerTask to int64_t, removing the RenewMinIntervalMs method, and casting the return type in ThreadMain. Additionally, type changes were made in related structures to enhance consistency and functionality.

Changes

Files Change Summary
src/.../net_util.cc Updated ExecTimerTask to return int64_t, revised task execution logic, removed RenewMinIntervalMs, and cast return type in ThreadMain.
src/.../net_util.h Changed ExecTimerTask return type to int64_t, modified Empty method to be const, and updated last_task_id_ type.

Poem

In the code where tasks do tread,
Changes made with careful thread,
Int to int64_t they have grown,
Enhancing logic, wisdom shown.
Timers tick with greater might,
Horizons broadened, future bright.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the ☢️ Bug Something isn't working label Jul 10, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (2)
src/net/src/net_util.h (1)

62-62: Inconsistent return type for ExecTimerTask function.

The return type of the ExecTimerTask function has been changed to int32_t in the implementation file (src/net/src/net_util.cc) but remains as int in the header file (src/net/src/net_util.h). Please update the header file to maintain consistency.

  • src/net/src/net_util.cc: line 1
  • src/net/src/net_util.h: line 62
Analysis chain

Verify the impact of changing the return type to int32_t.

The return type of ExecTimerTask has been changed from int to int32_t. Ensure that the rest of the codebase correctly handles this new return type.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `ExecTimerTask` handle the new return type `int32_t`.

# Test: Search for the function usage. Expect: Correct handling of the new return type.
rg --type cpp -A 5 $'ExecTimerTask'

Length of output: 1002

src/net/src/dispatch_thread.cc (1)

69-69: Typo in thread name.

The thread name "DispacherTimerTaskThread" in the provided code snippet appears to be a typo. The expected name "DispatcherTimerTaskThread" is not used elsewhere in the codebase.

  • src/net/src/dispatch_thread.cc: timer_task_thread_.set_thread_name("DispacherTimerTaskThread");
Analysis chain

Ensure consistency in thread naming.

The thread name has been changed to "DispatcherTimerTaskThread". Ensure that this new name is used consistently throughout the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent use of the new thread name.

# Test: Search for the thread name usage. Expect: Consistent use of `"DispatcherTimerTaskThread"`.
rg --type cpp -A 5 $'DispatcherTimerTaskThread'

Length of output: 46


Script:

#!/bin/bash
# Description: Verify consistent use of the new thread name.

# Test: Search for the thread name usage in all file types. Expect: Consistent use of "DispatcherTimerTaskThread".
rg -i 'Dispatcher\s*Timer\s*Task\s*Thread'

Length of output: 44


Script:

#!/bin/bash
# Description: Verify consistent use of the new thread name.

# Test: Search for variations of the thread name usage in all file types. Expect: Consistent use of "DispatcherTimerTaskThread".
rg -i 'Dispacher\s*Timer\s*Task\s*Thread|Dispatcher\s*Timer\s*Task\s*Thread'

Length of output: 175

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1de6598 and 8c4cdbe.

Files selected for processing (3)
  • src/net/src/dispatch_thread.cc (1 hunks)
  • src/net/src/net_util.cc (4 hunks)
  • src/net/src/net_util.h (1 hunks)
Additional comments not posted (5)
src/net/src/net_util.h (2)

66-66: LGTM!

The Empty method has been correctly updated to be a const member function, ensuring it does not modify the state of the object.


76-79: LGTM!

The added comment clearly explains the absence of locks in the current version of TimerTaskThread and provides guidance for adding dynamic task management.

src/net/src/net_util.cc (3)

47-47: Verify the impact of changing the return type to int32_t.

The return type of ExecTimerTask has been changed from int to int32_t. Ensure that the rest of the codebase correctly handles this new return type.


80-82: Ensure correct calculation of the time gap.

The calculation of gap_between_now_and_next_task ensures that it is non-negative. This is correct and prevents potential issues with negative timeout values.


50-62: Ensure correct handling of expired tasks.

The logic for executing expired tasks seems correct. Ensure that all potential edge cases (e.g., tasks expiring exactly at now_in_ms) are handled properly.

Comment on lines +76 to +79
if (exec_queue_.empty()) {
//to avoid wasting of cpu resources, epoll use 5000ms as timeout value when no task to exec
return 5000;
}
Copy link

@coderabbitai coderabbitai bot Jul 10, 2024

Choose a reason for hiding this comment

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

Consider reducing the default epoll timeout value.

The default timeout value of 5000ms might be too high and could lead to inefficiencies. Consider reducing it to a lower value.

-    return 5000;
+    return 1000; // or a suitable lower value
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (exec_queue_.empty()) {
//to avoid wasting of cpu resources, epoll use 5000ms as timeout value when no task to exec
return 5000;
}
if (exec_queue_.empty()) {
//to avoid wasting of cpu resources, epoll use 5000ms as timeout value when no task to exec
return 1000; // or a suitable lower value
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, this 5000 will be returned only when the timer has no registered task inside. there's no need of reduce it to 1000 to use more cpu.

Copy link

Choose a reason for hiding this comment

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

@cheniujh, thank you for the clarification. Given that the 5000ms timeout is only used when there are no registered tasks, it makes sense to keep it as is to avoid unnecessary CPU usage. Your rationale is clear and well understood.


Learnings added
Learnt from: cheniujh
PR: OpenAtomFoundation/pika#2794
File: src/net/src/net_util.cc:76-79
Timestamp: 2024-07-10T10:05:58.987Z
Learning: When the TimerTaskManager in the Pika project has no registered tasks, it returns a timeout value of 5000ms to the epoll_wait function to avoid wasting CPU resources.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8c4cdbe and 2a1b950.

Files selected for processing (1)
  • src/net/src/net_util.cc (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/net/src/net_util.cc

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2a1b950 and 0409f3f.

Files selected for processing (2)
  • src/net/src/net_util.cc (5 hunks)
  • src/net/src/net_util.h (3 hunks)
Additional comments not posted (10)
src/net/src/net_util.h (5)

24-24: LGTM!

The introduction of TimerTaskID improves code readability and maintainability.


26-26: LGTM!

Changing the type of task_id to TimerTaskID maintains consistency with the new type alias.


37-37: LGTM!

Changing the type of id to TimerTaskID maintains consistency with the new type alias.


62-62: LGTM!

Updating the return type of ExecTimerTask to int64_t allows for a larger range of return values, which is necessary for handling time intervals accurately.


65-65: LGTM!

Updating the Empty method to be a const member function ensures that it does not modify the state of the object, which is a good practice for methods that are intended to be read-only.

src/net/src/net_util.cc (5)

47-47: LGTM!

Updating the return type of ExecTimerTask to int64_t allows for a larger range of return values, which is necessary for handling time intervals accurately.


Line range hint 50-84: LGTM!

The revised logic within ExecTimerTask ensures that tasks are executed at the correct times and that the timeout value for epoll_wait is accurately calculated.


47-47: LGTM!

The removal of the RenewMinIntervalMs method simplifies the code and is consistent with the updated logic in ExecTimerTask.


137-137: LGTM!

Explicitly casting the result of ExecTimerTask to int32_t ensures that the timeout value passed to epoll_wait is within the expected range for an int32_t.


76-79: Appropriate timeout value.

The timeout value of 5000ms is appropriate to avoid unnecessary CPU usage when there are no tasks to execute.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0409f3f and b01e41a.

Files selected for processing (1)
  • src/net/src/net_util.h (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/net/src/net_util.h

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b01e41a and 09dd7a1.

Files selected for processing (2)
  • src/net/src/net_util.cc (5 hunks)
  • src/net/src/net_util.h (4 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/net/src/net_util.cc
  • src/net/src/net_util.h

@lqxhub
Copy link
Collaborator

lqxhub commented Jul 18, 2024

现在 exec_queue_ 是用 set 去存的, 是不是可以考虑用 优先级队列 priority_queue 来存, 下一个执行时间最近的在堆顶, 这样在遍历的时候就不用遍历这个 set 了,只要在找到执行时间 > 当前时间的就可以return了

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Now exec_queue_ is stored using set. Is it possible to consider using priority queue priority_queue to store it? The next execution time with the closest execution time is on the top of the heap, so that there is no need to traverse the set when traversing. , as long as you find the execution time > the current time, you can return

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 09dd7a1 and 72e9d49.

Files selected for processing (1)
  • src/net/src/net_util.cc (5 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/net/src/net_util.cc

@wanghenshui wanghenshui self-requested a review July 18, 2024 12:02
@AlexStocks AlexStocks merged commit 6c0e0d4 into OpenAtomFoundation:unstable Jul 19, 2024
12 checks passed
cheniujh added a commit that referenced this pull request Aug 15, 2024
…TaskManager (#2794)

* changed the returned timeout of TimerTaskManager

---------

Co-authored-by: cheniujh <[email protected]>
cheniujh added a commit to cheniujh/pika that referenced this pull request Sep 24, 2024
…TaskManager (OpenAtomFoundation#2794)

* changed the returned timeout of TimerTaskManager

---------

Co-authored-by: cheniujh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.5.5 4.0.1 ☢️ Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants