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: revised TimerTaskManager and add some comments #2776

Merged

Conversation

cheniujh
Copy link
Collaborator

@cheniujh cheniujh commented Jul 3, 2024

1 原本的TimerTaskManager正确性没有问题,但是逻辑有改进的空间,具体地:TimerTaskManager::ExecTimerTask()会返回一个timeout, 用于给epoll_wait作为timeout使用,原本返回的这个timeout值是当前定时器内所有任务中执行间隔最小的那个时间间隔的值,但实际上可以直接返回:下一个要执行的定时任务的过期时间戳 - 当前时间戳,直接让epoll等待这个差值即可,没必要找什么mininum time interval,减去这部分逻辑的同时还能降低复杂度
(经过讨论,如果只是为了精简逻辑,增加可读性的话,还是暂时不动这块逻辑,所以本pr去掉了这部分的改动,后续如果有改造的需要,可以再去参考本pr的历史commit)

2 加了一些注释,如果将来要增加动态添加/删除定时任务的特性(届时如果有这个需求的话),可以按照注释的思路继续改造

3 规范了一处命名以和pika保持一致:timerTaskThread_改为了 timer_task_thread_

1 The correctness of the original TimerTaskManager was not an issue, but there is room for logical improvement. Specifically, TimerTaskManager::ExecTimerTask() returns a timeout that is used as the timeout for epoll_wait. Originally, this returned timeout value was the minimum interval among all tasks in the current timer. However, it can directly return the difference between the expiration timestamp of the next scheduled task and the current timestamp. This difference can be used directly for epoll to wait, without the need to find the minimum time interval.

2 Added some comments. If there is a future requirement to add features for dynamically adding/deleting timer tasks, the modifications can be made following the approach outlined in the comments.

3 Standardized a naming convention to keep it consistent with pika: timerTaskThread_ was changed to timer_task_thread_.

Copy link

coderabbitai bot commented Jul 3, 2024

Walkthrough

The changes primarily focus on renaming variables and updating thread names in the DispatchThread class to improve readability and consistency. Additionally, the modifications clarify comments in AddTimerTask and other methods, remove unused functions, and adjust constant member functions in the TimerTaskManager class within src/net/src/net_util.h.

Changes

File Change Summary
src/net/src/dispatch_thread.cc Renamed timerTaskThread_ to timer_task_thread_ and updated thread name and related method calls for consistency.
src/net/src/net_util.h Clarified return value in AddTimerTask, added comment to ExecTimerTask, removed GetMinIntervalMs, and adjusted Empty to const.

Poem

In the code where threads do dance,
Renamed for a better glance,
Timer tasks now more clear,
Dispatcher threads we hold dear.
Comments bright like morning dew,
Symbols shining, always new.
CodeRabbit's joy, a clearer view! 🐇✨


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 Configration 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 ☢️ Bug Something isn't working ✏️ Feature New feature or request 📒 Documentation Improvements or additions to documentation labels Jul 3, 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d859e24 and ef92915.

Files selected for processing (4)
  • src/net/src/dispatch_thread.cc (2 hunks)
  • src/net/src/dispatch_thread.h (1 hunks)
  • src/net/src/net_util.cc (4 hunks)
  • src/net/src/net_util.h (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/net/src/dispatch_thread.h
Additional comments not posted (7)
src/net/src/net_util.h (3)

56-56: Comment addition is clear and useful.

The comment provides clarity on the return value of the AddTimerTask method.


57-57: Return type change is appropriate.

Changing the return type to int32_t ensures type consistency and precision.


60-60: Const-correctness improvement.

Updating the Empty method to be a const member function ensures it does not modify the object's state.

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

70-75: Comment addition is clear and useful.

The detailed comment provides guidance for future enhancements, improving maintainability.


Line range hint 47-84: Logic update and return type change are appropriate.

The updates simplify the timeout calculation for epoll_wait and improve type consistency. Ensure the function's behavior is verified.

Verification successful

Verified: The logic update and return type change in ExecTimerTask are appropriate.

The updates simplify the timeout calculation for epoll_wait and improve type consistency. The function's behavior is correctly handled in TimerTaskThread::ThreadMain.

  • src/net/src/net_util.cc: ExecTimerTask is used to set the timeout for NetPoll.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the behavior of the updated ExecTimerTask method.

# Test: Search for the function usage. Expect: Proper handling of the new logic.
rg --type cpp $'ExecTimerTask'

Length of output: 218



Script:

#!/bin/bash
# Extract lines around the usage of ExecTimerTask in src/net/src/net_util.cc
rg --type cpp -A 10 -B 10 'ExecTimerTask' src/net/src/net_util.cc

Length of output: 1197

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

67-70: Naming consistency improvements.

Renaming timerTaskThread_ to timer_task_thread_ and updating the thread name to "DispacherTimerTaskThread" improve clarity and consistency.


91-91: Naming consistency improvement.

Renaming timerTaskThread_ to timer_task_thread_ improves clarity and consistency.

@AlexStocks
Copy link
Collaborator

“下一个要执行的定时任务的过期时间戳 - 当前时间戳,直接让epoll等待这个差值即可,没必要找什么mininum time interval,减去这部分逻辑的同时还能降低复杂度” 关键是 epoll wait 的过期时间并不精准呀

@Issues-translate-bot
Copy link

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


"The expiration timestamp of the next scheduled task to be executed - the current timestamp, just let epoll wait for the difference. There is no need to find a minimum time interval. Subtracting this part of the logic can also reduce the complexity." The key is The expiration time of epoll wait is not accurate.

return min_interval_ms_;

if (exec_queue_.empty()) {
//no task to exec, epoll will use -1 as timeout value, and sink into endless wait
Copy link
Collaborator

Choose a reason for hiding this comment

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

好嘛,原始的 RenewMinIntervalMs() 函数避免使用 -1,你这干脆来个 -1

@@ -35,31 +35,31 @@ uint32_t TimerTaskManager::AddTimerTask(const std::string& task_name, int interv
int64_t next_expired_time = NowInMs() + interval_ms;
exec_queue_.insert({next_expired_time, new_task.task_id});

if (min_interval_ms_ > interval_ms || min_interval_ms_ == -1) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个逻辑不要删掉

@AlexStocks
Copy link
Collaborator

epoll_wait timeout 设置为 -1 尤为不合适,咱们这里一个网络 worker 是要处理网络和超时事件,如果只处理网络,那你设定为 -1 还是可以的

@Issues-translate-bot
Copy link

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


It is particularly inappropriate to set epoll_wait timeout to -1. We have a network worker here that handles network and timeout events. If it only handles the network, then you can set it to -1.

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 ef92915 and 7d28a78.

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

@cheniujh cheniujh requested a review from AlexStocks July 9, 2024 10:22
@cheniujh cheniujh removed the ☢️ Bug Something isn't working label Jul 9, 2024
@AlexStocks AlexStocks merged commit 1de6598 into OpenAtomFoundation:unstable Jul 10, 2024
15 of 16 checks passed
chejinge pushed a commit that referenced this pull request Aug 1, 2024
* 1 rename DisPatcherThread::timerTaskThread_ to timer_task_thread_
* 2 add comments for TimerTaskManager

---------

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

* 1 rename DisPatcherThread::timerTaskThread_ to timer_task_thread_
* 2 add comments for TimerTaskManager

---------

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

* 1 rename DisPatcherThread::timerTaskThread_ to timer_task_thread_
* 2 add comments for 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
4.0.1 📒 Documentation Improvements or additions to documentation ✏️ Feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants