-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Cannot set up cluster.schedulingPolicy
in ESM
#49240
Comments
I found a nasty workaround: since process.env.NODE_CLUSTER_SCHED_POLICY = 'none'
//import * as cluster from 'cluster'
const cluster = await import('cluster')
if (cluster.isPrimary) {
console.log(`process.env.NODE_CLUSTER_SCHED_POLICY: ${process.env.NODE_CLUSTER_SCHED_POLICY}`)
for (let index = 0; index < 2; index++) {
cluster.fork()
setTimeout(() => console.log(`cluster.schedulingPolicy: ${cluster.schedulingPolicy}`), 1000)
}
} else {
setTimeout(() => null, 1000)
} This is quite tricky; but it works, so I leave it here for other people to find it. Also posted to Stack Overflow for |
cluster.schedulingPolicy
in a JS module (with import)cluster.schedulingPolicy
in ESM
Can you clarify if you are reporting a difference of behavior between CJS (with |
To clarify, I have not reported anything at all about |
Could you test with |
Sure, it works as expected. For reference, the code is: process.env.NODE_CLUSTER_SCHED_POLICY = 'none'
const cluster = require('cluster')
console.log(cluster)
//import * as cluster from 'cluster'
if (cluster.isPrimary) {
console.log(`process.env.NODE_CLUSTER_SCHED_POLICY: ${process.env.NODE_CLUSTER_SCHED_POLICY}`)
for (let index = 0; index < 2; index++) {
cluster.fork()
setTimeout(() => console.log(`cluster.schedulingPolicy: ${cluster.schedulingPolicy}`), 1000)
}
} else {
setTimeout(() => null, 1000)
} The fix is trivial: adding |
Sure, sending a PR would be fantastic :) |
Some more info about possible solutions. The proper solution seems to me to add When using ESM import '../common/index.mjs';
import assert from 'node:assert';
import * as cluster from 'cluster';
assert.strictEqual(cluster.schedulingPolicy, cluster.SCHED_RR);
cluster.setupPrimary({ schedulingPolicy: cluster.SCHED_NONE });
const settings = cluster.getSettings();
assert.strictEqual(settings.schedulingPolicy, cluster.SCHED_NONE); I have a preliminary implementation ready that I can post for comments, only the documentation is missing. Is this going too far? |
I would have a hard time answering you without looking at the code – and even then, I have not worked on |
Sounds good. I will wait for triage and more feedback before opening the draft PR if you don't mind; meanwhile this is the branch as a RFC, with three commits:
Thanks! |
@nodejs/cluster |
I don't think a new Supporting |
@bnoordhuis Thanks for the feedback!
Good for me. My concern was that in ESM there is no way to programmatically access
I will do that, it's also significantly easier to implement and document. As stated above I don't know how to work around read-only |
I usually take a very reactive approach: don't do anything until users start filing bug reports :-) |
Excuse my ignorance @bnoordhuis, is an "internal method" just undocumented? Or do I need to mark it with some special code? 🤔 |
I sent this PR to move this issue forward: #49292. I'm not sure if it should be a notable PR, it definitely changes the external API. Hope everything is OK! |
Version
v18.17.1
Platform
Linux xxx 6.2.0-26-generic #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jul 13 16:27:29 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
cluster
What steps will reproduce the bug?
Run the following code in Node.js:
cluster-error.txt
Attached as
cluster-error.txt
. You will see that it prints the following:So the env variable
NODE_CLUSTER_SCHED_POLICY
has no effect,cluster.schedulingPolicy
has the value2
which is the defaultcluster.SCHED_RR
. However running it with the env variable from the console works as expected:I cannot set the
cluster.schedulingPolicy
as per the docs:cluster.schedulingPolicy = cluster.SCHED_NONE
since the cluster module isimport
ed:so I get a
TypeError
:How often does it reproduce? Is there a required condition?
With the attached code it works (as in doesn't work) always.
What is the expected behavior? Why is that the expected behavior?
I expect to see the same output as with the env variable
NODE_CLUSTER_SCHED_POLICY='none'
, i.e. thecluster.schedulingPolicy
is set to 1cluster.SCHED_NONE
:(Fake output.)
What do you see instead?
Instead I see
cluster.schedulingPolicy
has the default value of 2cluster.SCHED_RR
:Additional information
I am the author of the loadtest package and I am converting it to multi-core. I really see a big difference of more than 30% when running the test server in the default round-robin mode and in the
none
mode I want to set:cluster.SCHED_RR
: 9600 rps.cluster.SCHED_NONE
: 12700 rps.It is true that load on processors is a bit more uneven in
none
, but that doesn't seem to matter much. I want my lightning fast speed! ⚡Besides, I think that the documentation should be updated because the recommended way of setting
cluster.schedulingPolicy
does not work. I would think that the best solution is to have a setting incluster.settings
that can be set withcluster.setupPrimary([settings])
. As suggested by @bnoordhuis in this hopeful comment.The text was updated successfully, but these errors were encountered: