-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat(rt): read default worker thread from env #4250
feat(rt): read default worker thread from env #4250
Conversation
Kindly ping if you missed this~ @Darksonn @Noah-Kennedy |
I'm in the middle of exam week, so I'm busy with other stuff this week. |
Thanks for submitting the PR. The code seems fine. IIRC, when this was discussed, there was a question about whether or not an env variable should change the default globally. I.e. should an env variable impact a runtime created deep within a library (for example). I think what we could do instead is to limit the env variable to This change should be discussed by @tokio-rs/maintainers though before we decide to move forward. |
My instinct here is the same as @Darksonn's initial take in #4249 (comment), which is that this should be up to each application to do by using the builder construction. I don't know that the addition of a magic env var actually buys you that much compared to the slight increase in complexity for callers when they do need this. If anything, the requirement to be explicit about the size of the thread pool being determined by an environment variable seems valuable. It also makes it easier to do things like pick up variables by other names that may already be set in the environment for this purpose. If we do decide to adopt this, I'm torn as to whether it should affect all executors or just |
Firstly, I'd propose this is because in some cases, people who write code isn't people who really run and operate the application, and the author also doesn't know which environment the application will run in. And the environment sometimes have custom limitations that the num_cpus can't figure out. Second, I think this change should be "global", all the libraries which create tokio multi-thread runtime should apply this change. Because if a user only creates a multi-thread runtime without specify the worker threads using the builder, then I think we can assume this should be self-adaptive in runtime, and the user gives us(tokio) the right to do this. Finally, sorry for my poor English😭. |
Why can't you ask the people responsible for developing the software to read the desired number of cores from an environment variable? |
First, in our situation, there's thousands of developers in the company, and there's thousands of server-side applications, we are not able to ask everyone to write such logic in their code. It's also impossible to ask them to use a internal-version of tokio(we will never do things like fork an internal version of tokio). And this also can't solve the problem that some dependency may use the "official" tokio. Second, we think this is quite a common logic, because the run-time environment (and the operator) should have ability to specify the thread count, that's what called "cloud native": you write the code, and you don't care (and won't know) what environment it runs in. To achieve this, we need to expose this ability. And I think that's why Go supports using "GOMAXPROCS" to specify the P nums. |
As is, I don't think this PR should be merged. It controls a single variable of the runtime, and adds extra code that may or may not end up surprising users when the runtime doesn't behave as they expect simply because an environment variable existed when an application was started. However, I do think there could be a better pattern for allowing runtime configuration to be overridden via environment variables. While this is a personal feeling, I'd be interested in seeing a helper type -- perhaps in While you raise a fair point that it might be difficult to get hundreds or thousands of developers to use a common pattern, such a helper type as I describe could build a runtime from environment variables in only 2-3 lines of code. That's not many more lines than the actual Cargo.toml imports of Tokio itself. Benefits of this approach:
|
Firstly, I think we need to be clear about what the user expects when he specifies to use multi-thread runtime. Has he given tokio the right to decide on the number of threads? I think the answer is "yes". Secondly, I have also considered your approach, but this still does not solve the problem. Almost all users use |
Eh, I agree that putting the feature to do this in |
Hello, do you have any more concerns about this PR? |
Well, the current status is that there is no consensus about the shape we want this feature in. There isn't anything wrong with the implementation per se. |
I think that a better way to do this would be to only use the variable with |
I have thought about this before, but I don't think that make sense, because users may set some other builder configs or create the runtime manually and use it (we have a lot of real production use cases for that). For example, when writing dylib in async rust and call it from c++/go using ffi, we need to create the runtime manually using something like lazy_static. And if this env is set, the operator's intention must be that all runtimes need to use this, unless the developer has explicitly set the worker threads. So I think set it in builder is better than in the |
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.
Sorry about taking so long — I forgot about this. The implementation looks more or less fine. I have a few comments.
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
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.
Thanks for your suggestions!
Please take a look again!
The ci fail seems has nothing to do with this pr, do you know why it fails? |
It's an existing test that is flaky. It should be fixed, but in this PR I will simply restart the CI for you if it fails. |
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.
Looks good to me.
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dependencies | minor | `1.23.0` -> `1.24.1` | | [tokio](https://tokio.rs) ([source](https://github.com/tokio-rs/tokio)) | dev-dependencies | minor | `1.23.0` -> `1.24.1` | --- ### Release Notes <details> <summary>tokio-rs/tokio</summary> ### [`v1.24.1`](https://github.com/tokio-rs/tokio/releases/tag/tokio-1.24.1): Tokio v1.24.1 [Compare Source](tokio-rs/tokio@tokio-1.24.0...tokio-1.24.1) This release fixes a compilation failure on targets without `AtomicU64` when using rustc older than 1.63. ([#​5356]) [#​5356]: tokio-rs/tokio#5356 ### [`v1.24.0`](https://github.com/tokio-rs/tokio/releases/tag/tokio-1.24.0): Tokio v1.24.0 [Compare Source](tokio-rs/tokio@tokio-1.23.1...tokio-1.24.0) The highlight of this release is the reduction of lock contention for all I/O operations ([#​5300](tokio-rs/tokio#5300)). We have received reports of up to a 20% improvement in CPU utilization and increased throughput for real-world I/O heavy applications. ##### Fixed - rt: improve native `AtomicU64` support detection ([#​5284]) ##### Added - rt: add configuration option for max number of I/O events polled from the OS per tick ([#​5186]) - rt: add an environment variable for configuring the default number of worker threads per runtime instance ([#​4250]) ##### Changed - sync: reduce MPSC channel stack usage ([#​5294]) - io: reduce lock contention in I/O operations ([#​5300]) - fs: speed up `read_dir()` by chunking operations ([#​5309]) - rt: use internal `ThreadId` implementation ([#​5329]) - test: don't auto-advance time when a `spawn_blocking` task is running ([#​5115]) [#​5186]: tokio-rs/tokio#5186 [#​5294]: tokio-rs/tokio#5294 [#​5284]: tokio-rs/tokio#5284 [#​4250]: tokio-rs/tokio#4250 [#​5300]: tokio-rs/tokio#5300 [#​5329]: tokio-rs/tokio#5329 [#​5115]: tokio-rs/tokio#5115 [#​5309]: tokio-rs/tokio#5309 ### [`v1.23.1`](https://github.com/tokio-rs/tokio/releases/tag/tokio-1.23.1): Tokio v1.23.1 [Compare Source](tokio-rs/tokio@tokio-1.23.0...tokio-1.23.1) This release forward ports changes from 1.18.4. ##### Fixed - net: fix Windows named pipe server builder to maintain option when toggling pipe mode ([#​5336]). [#​5336]: tokio-rs/tokio#5336 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC44NC4xIiwidXBkYXRlZEluVmVyIjoiMzQuODQuMSJ9--> Co-authored-by: cabr2-bot <cabr2.help@gmail.com> Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1703 Reviewed-by: crapStone <crapstone@noreply.codeberg.org> Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org> Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Implements #4249