Skip to content

Commit

Permalink
cluster: add scheduling policy to cluster.settings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfernandez authored and aduh95 committed May 11, 2024
1 parent c4d0229 commit 9f797a2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,9 @@ values are `'rr'` and `'none'`.
<!-- YAML
added: v0.7.1
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/49292
description: The `schedulingPolicy` option is supported now.
- version:
- v13.2.0
- v12.16.0
Expand Down Expand Up @@ -931,6 +934,9 @@ changes:
messages between processes. Possible values are `'json'` and `'advanced'`.
See [Advanced serialization for `child_process`][] for more details.
**Default:** `false`.
* `schedulingPolicy` {string} Scheduling policy to use between processes.
**Default:** `cluster.SCHED_RR`. See \[`cluster.schedulingPolicy`]\[] for
details.
* `silent` {boolean} Whether or not to send output to parent's stdio.
**Default:** `false`.
* `stdio` {Array} Configures the stdio of forked processes. Because the
Expand Down
11 changes: 8 additions & 3 deletions lib/internal/cluster/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ cluster.SCHED_RR = SCHED_RR; // Primary distributes connections.
let ids = 0;
let initialized = false;

// XXX(bnoordhuis) Fold cluster.schedulingPolicy into cluster.settings?
let schedulingPolicy = process.env.NODE_CLUSTER_SCHED_POLICY;
if (schedulingPolicy === 'rr')
schedulingPolicy = SCHED_RR;
Expand All @@ -65,6 +64,7 @@ cluster.setupPrimary = function(options) {
exec: process.argv[1],
execArgv: process.execArgv,
silent: false,
schedulingPolicy: cluster.schedulingPolicy,
...cluster.settings,
...options,
};
Expand All @@ -86,9 +86,10 @@ cluster.setupPrimary = function(options) {
return process.nextTick(setupSettingsNT, settings);

initialized = true;
schedulingPolicy = cluster.schedulingPolicy; // Freeze policy.
schedulingPolicy = settings.schedulingPolicy; // Freeze policy.
assert(schedulingPolicy === SCHED_NONE || schedulingPolicy === SCHED_RR,
`Bad cluster.schedulingPolicy: ${schedulingPolicy}`);
`Bad settings.schedulingPolicy: ${schedulingPolicy}`);
cluster.schedulingPolicy = schedulingPolicy; // Show to the world.

process.nextTick(setupSettingsNT, settings);

Expand Down Expand Up @@ -158,6 +159,10 @@ function removeHandlesForWorker(worker) {
});
}

cluster.getSettings = function() {
return { ...cluster.settings };
};

cluster.fork = function(env) {
cluster.setupPrimary();
const id = ++ids;
Expand Down
13 changes: 13 additions & 0 deletions test/known_issues/test-cluster-import-scheduling-policy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Refs: https://github.com/nodejs/node/issues/49240
// When importing cluster in ESM, cluster.schedulingPolicy cannot be set;
// and the env variable doesn't work since imports are hoisted to the top.
// Therefore the scheduling policy is still cluster.SCHED_RR.
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_RR);
2 changes: 2 additions & 0 deletions test/parallel/test-cluster-setup-primary-cumulative.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ assert.deepStrictEqual(cluster.settings, {
exec: process.argv[1],
execArgv: process.execArgv,
silent: false,
schedulingPolicy: 2,
});
console.log('ok sets defaults');

Expand All @@ -58,5 +59,6 @@ assert.deepStrictEqual(cluster.settings, {
exec: 'overridden',
execArgv: ['baz', 'bang'],
silent: false,
schedulingPolicy: 2,
});
console.log('ok preserves current settings');

0 comments on commit 9f797a2

Please sign in to comment.