From 13ad0b97b22f02fcfc1453702ad553766a054726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Fern=C3=A1ndez?= Date: Tue, 22 Aug 2023 23:06:31 +0200 Subject: [PATCH 01/12] cluster: fold schedulingPolicy into cluster.settings Fixes: https://github.com/nodejs/node/issues/49240 --- lib/internal/cluster/primary.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/internal/cluster/primary.js b/lib/internal/cluster/primary.js index 945f440cd19797..e4a7f7d17f8484 100644 --- a/lib/internal/cluster/primary.js +++ b/lib/internal/cluster/primary.js @@ -45,6 +45,7 @@ let ids = 0; let initialized = false; // XXX(bnoordhuis) Fold cluster.schedulingPolicy into cluster.settings? +// XXX(alexfernandez) Folded cluster.schedulingPolicy into cluster.settings. let schedulingPolicy = process.env.NODE_CLUSTER_SCHED_POLICY; if (schedulingPolicy === 'rr') schedulingPolicy = SCHED_RR; @@ -65,6 +66,7 @@ cluster.setupPrimary = function(options) { exec: process.argv[1], execArgv: process.execArgv, silent: false, + schedulingPolicy: cluster.schedulingPolicy, ...cluster.settings, ...options, }; @@ -86,9 +88,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); From 7b40262a69b36045a41685443b52bf0508cdd348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Fern=C3=A1ndez?= Date: Tue, 22 Aug 2023 23:08:32 +0200 Subject: [PATCH 02/12] cluster: add internal cluster.getSettings() for test --- lib/internal/cluster/primary.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/internal/cluster/primary.js b/lib/internal/cluster/primary.js index e4a7f7d17f8484..bde38043826100 100644 --- a/lib/internal/cluster/primary.js +++ b/lib/internal/cluster/primary.js @@ -161,6 +161,10 @@ function removeHandlesForWorker(worker) { }); } +cluster.getSettings = function() { + return { ...cluster.settings }; +}; + cluster.fork = function(env) { cluster.setupPrimary(); const id = ++ids; From cfde10764980ee4698ebc0eb4d3e4c45b1411f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Fern=C3=A1ndez?= Date: Tue, 22 Aug 2023 23:09:09 +0200 Subject: [PATCH 03/12] cluster: add test for cluster.setPrimary({schedulingPolicy}) --- .../test-cluster-import-scheduling-policy.mjs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/known_issues/test-cluster-import-scheduling-policy.mjs diff --git a/test/known_issues/test-cluster-import-scheduling-policy.mjs b/test/known_issues/test-cluster-import-scheduling-policy.mjs new file mode 100644 index 00000000000000..2ad3ef313fa2a7 --- /dev/null +++ b/test/known_issues/test-cluster-import-scheduling-policy.mjs @@ -0,0 +1,12 @@ +// 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. +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); From 55af83d42c21a6a0c18bc107f8dcf8f8dc729a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Fern=C3=A1ndez?= Date: Tue, 22 Aug 2023 23:11:45 +0200 Subject: [PATCH 04/12] cluster: document cluster.setPrimary({schedulingPolicy}) --- doc/api/cluster.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index aa2e9b4057e5fc..8b8478f7e47b70 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -900,6 +900,9 @@ values are `'rr'` and `'none'`. + +* Returns: {Object} + * `execArgv` {string\[]} List of string arguments passed to the Node.js + executable. **Default:** `process.execArgv`. + * `exec` {string} File path to worker file. **Default:** `process.argv[1]`. + * `args` {string\[]} String arguments passed to worker. + **Default:** `process.argv.slice(2)`. + * `cwd` {string} Current working directory of the worker process. **Default:** + `undefined` (inherits from parent process). + * `serialization` {string} Specify the kind of serialization used for sending + 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 + cluster module relies on IPC to function, this configuration must contain an + `'ipc'` entry. When this option is provided, it overrides `silent`. See + [`child_process.spawn()`][]'s [`stdio`][]. + * `uid` {number} Sets the user identity of the process. (See setuid(2).) + * `gid` {number} Sets the group identity of the process. (See setgid(2).) + * `inspectPort` {number|Function} Sets inspector port of worker. + This can be a number, or a function that takes no arguments and returns a + number. By default each worker gets its own port, incremented from the + primary's `process.debugPort`. + * `windowsHide` {boolean} Hide the forked processes console window that would + normally be created on Windows systems. **Default:** `false`. + +After calling [`.setupPrimary()`][] (or [`.fork()`][]) this function will return +the cluster settings, including the default values. + ## `cluster.isMaster`