mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
lib,src: update cluster to use Parent
Doc deprecate isMaster and setupMaster in favor of isPrimary and setupPrimary. Signed-off-by: Michael Dawson <mdawson@devrus.com> PR-URL: https://github.com/nodejs/node/pull/36478 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com>
This commit is contained in:
parent
3c49ee298f
commit
15164cebce
105 changed files with 341 additions and 315 deletions
|
@ -18,8 +18,8 @@ const cluster = require('cluster');
|
|||
const http = require('http');
|
||||
const numCPUs = require('os').cpus().length;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
console.log(`Master ${process.pid} is running`);
|
||||
if (cluster.isPrimary) {
|
||||
console.log(`Primary ${process.pid} is running`);
|
||||
|
||||
// Fork workers.
|
||||
for (let i = 0; i < numCPUs; i++) {
|
||||
|
@ -45,7 +45,7 @@ Running Node.js will now share port 8000 between the workers:
|
|||
|
||||
```console
|
||||
$ node server.js
|
||||
Master 3596 is running
|
||||
Primary 3596 is running
|
||||
Worker 4324 started
|
||||
Worker 4520 started
|
||||
Worker 6056 started
|
||||
|
@ -66,12 +66,12 @@ The cluster module supports two methods of distributing incoming
|
|||
connections.
|
||||
|
||||
The first one (and the default one on all platforms except Windows),
|
||||
is the round-robin approach, where the master process listens on a
|
||||
is the round-robin approach, where the primary process listens on a
|
||||
port, accepts new connections and distributes them across the workers
|
||||
in a round-robin fashion, with some built-in smarts to avoid
|
||||
overloading a worker process.
|
||||
|
||||
The second approach is where the master process creates the listen
|
||||
The second approach is where the primary process creates the listen
|
||||
socket and sends it to interested workers. The workers then accept
|
||||
incoming connections directly.
|
||||
|
||||
|
@ -81,16 +81,16 @@ to operating system scheduler vagaries. Loads have been observed
|
|||
where over 70% of all connections ended up in just two processes,
|
||||
out of a total of eight.
|
||||
|
||||
Because `server.listen()` hands off most of the work to the master
|
||||
Because `server.listen()` hands off most of the work to the primary
|
||||
process, there are three cases where the behavior between a normal
|
||||
Node.js process and a cluster worker differs:
|
||||
|
||||
1. `server.listen({fd: 7})` Because the message is passed to the master,
|
||||
1. `server.listen({fd: 7})` Because the message is passed to the primary,
|
||||
file descriptor 7 **in the parent** will be listened on, and the
|
||||
handle passed to the worker, rather than listening to the worker's
|
||||
idea of what the number 7 file descriptor references.
|
||||
2. `server.listen(handle)` Listening on handles explicitly will cause
|
||||
the worker to use the supplied handle, rather than talk to the master
|
||||
the worker to use the supplied handle, rather than talk to the primary
|
||||
process.
|
||||
3. `server.listen(0)` Normally, this will cause servers to listen on a
|
||||
random port. However, in a cluster, each worker will receive the
|
||||
|
@ -121,7 +121,7 @@ added: v0.7.0
|
|||
* Extends: {EventEmitter}
|
||||
|
||||
A `Worker` object contains all public information and method about a worker.
|
||||
In the master it can be obtained using `cluster.workers`. In a worker
|
||||
In the primary it can be obtained using `cluster.workers`. In a worker
|
||||
it can be obtained using `cluster.worker`.
|
||||
|
||||
### Event: `'disconnect'`
|
||||
|
@ -201,14 +201,14 @@ Within a worker, `process.on('message')` may also be used.
|
|||
|
||||
See [`process` event: `'message'`][].
|
||||
|
||||
Here is an example using the message system. It keeps a count in the master
|
||||
Here is an example using the message system. It keeps a count in the primary
|
||||
process of the number of HTTP requests received by the workers:
|
||||
|
||||
```js
|
||||
const cluster = require('cluster');
|
||||
const http = require('http');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
|
||||
// Keep track of http requests
|
||||
let numReqs = 0;
|
||||
|
@ -240,7 +240,7 @@ if (cluster.isMaster) {
|
|||
res.writeHead(200);
|
||||
res.end('hello world\n');
|
||||
|
||||
// Notify master about the request
|
||||
// Notify primary about the request
|
||||
process.send({ cmd: 'notifyRequest' });
|
||||
}).listen(8000);
|
||||
}
|
||||
|
@ -275,7 +275,7 @@ changes:
|
|||
In a worker, this function will close all servers, wait for the `'close'` event
|
||||
on those servers, and then disconnect the IPC channel.
|
||||
|
||||
In the master, an internal message is sent to the worker causing it to call
|
||||
In the primary, an internal message is sent to the worker causing it to call
|
||||
`.disconnect()` on itself.
|
||||
|
||||
Causes `.exitedAfterDisconnect` to be set.
|
||||
|
@ -299,7 +299,7 @@ close them. It also may be useful to implement a timeout, killing a worker if
|
|||
the `'disconnect'` event has not been emitted after some time.
|
||||
|
||||
```js
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
let timeout;
|
||||
|
||||
|
@ -343,7 +343,7 @@ This property is `true` if the worker exited due to `.kill()` or
|
|||
worker has not exited, it is `undefined`.
|
||||
|
||||
The boolean [`worker.exitedAfterDisconnect`][] allows distinguishing between
|
||||
voluntary and accidental exit, the master may choose not to respawn a worker
|
||||
voluntary and accidental exit, the primary may choose not to respawn a worker
|
||||
based on this value.
|
||||
|
||||
```js
|
||||
|
@ -375,8 +375,8 @@ While a worker is alive, this is the key that indexes it in
|
|||
added: v0.11.14
|
||||
-->
|
||||
|
||||
This function returns `true` if the worker is connected to its master via its
|
||||
IPC channel, `false` otherwise. A worker is connected to its master after it
|
||||
This function returns `true` if the worker is connected to its primary via its
|
||||
IPC channel, `false` otherwise. A worker is connected to its primary after it
|
||||
has been created. It is disconnected after the `'disconnect'` event is emitted.
|
||||
|
||||
### `worker.isDead()`
|
||||
|
@ -392,8 +392,8 @@ const cluster = require('cluster');
|
|||
const http = require('http');
|
||||
const numCPUs = require('os').cpus().length;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
console.log(`Master ${process.pid} is running`);
|
||||
if (cluster.isPrimary) {
|
||||
console.log(`Primary ${process.pid} is running`);
|
||||
|
||||
// Fork workers.
|
||||
for (let i = 0; i < numCPUs; i++) {
|
||||
|
@ -425,9 +425,10 @@ added: v0.9.12
|
|||
* `signal` {string} Name of the kill signal to send to the worker
|
||||
process. **Default**: `'SIGTERM'`
|
||||
|
||||
This function will kill the worker. In the master, it does this by disconnecting
|
||||
the `worker.process`, and once disconnected, killing with `signal`. In the
|
||||
worker, it does it by disconnecting the channel, and then exiting with code `0`.
|
||||
This function will kill the worker. In the primary, it does this
|
||||
by disconnecting the `worker.process`, and once disconnected, killing
|
||||
with `signal`. In the worker, it does it by disconnecting the channel,
|
||||
and then exiting with code `0`.
|
||||
|
||||
Because `kill()` attempts to gracefully disconnect the worker process, it is
|
||||
susceptible to waiting indefinitely for the disconnect to complete. For example,
|
||||
|
@ -478,18 +479,18 @@ changes:
|
|||
* `callback` {Function}
|
||||
* Returns: {boolean}
|
||||
|
||||
Send a message to a worker or master, optionally with a handle.
|
||||
Send a message to a worker or primary, optionally with a handle.
|
||||
|
||||
In the master this sends a message to a specific worker. It is identical to
|
||||
In the primary this sends a message to a specific worker. It is identical to
|
||||
[`ChildProcess.send()`][].
|
||||
|
||||
In a worker this sends a message to the master. It is identical to
|
||||
In a worker this sends a message to the primary. It is identical to
|
||||
`process.send()`.
|
||||
|
||||
This example will echo back all messages from the master:
|
||||
This example will echo back all messages from the primary:
|
||||
|
||||
```js
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
worker.send('hi there');
|
||||
|
||||
|
@ -583,7 +584,7 @@ added: v0.7.0
|
|||
|
||||
After calling `listen()` from a worker, when the `'listening'` event is emitted
|
||||
on the server a `'listening'` event will also be emitted on `cluster` in the
|
||||
master.
|
||||
primary.
|
||||
|
||||
The event handler is executed with two arguments, the `worker` contains the
|
||||
worker object and the `address` object contains the following connection
|
||||
|
@ -617,7 +618,7 @@ changes:
|
|||
* `message` {Object}
|
||||
* `handle` {undefined|Object}
|
||||
|
||||
Emitted when the cluster master receives a message from any worker.
|
||||
Emitted when the cluster primary receives a message from any worker.
|
||||
|
||||
See [`child_process` event: `'message'`][].
|
||||
|
||||
|
@ -629,9 +630,9 @@ added: v0.7.0
|
|||
* `worker` {cluster.Worker}
|
||||
|
||||
After forking a new worker, the worker should respond with an online message.
|
||||
When the master receives an online message it will emit this event.
|
||||
When the primary receives an online message it will emit this event.
|
||||
The difference between `'fork'` and `'online'` is that fork is emitted when the
|
||||
master forks a worker, and `'online'` is emitted when the worker is running.
|
||||
primary forks a worker, and `'online'` is emitted when the worker is running.
|
||||
|
||||
```js
|
||||
cluster.on('online', (worker) => {
|
||||
|
@ -646,11 +647,11 @@ added: v0.7.1
|
|||
|
||||
* `settings` {Object}
|
||||
|
||||
Emitted every time [`.setupMaster()`][] is called.
|
||||
Emitted every time [`.setupPrimary()`][] is called.
|
||||
|
||||
The `settings` object is the `cluster.settings` object at the time
|
||||
[`.setupMaster()`][] was called and is advisory only, since multiple calls to
|
||||
[`.setupMaster()`][] can be made in a single tick.
|
||||
[`.setupPrimary()`][] was called and is advisory only, since multiple calls to
|
||||
[`.setupPrimary()`][] can be made in a single tick.
|
||||
|
||||
If accuracy is important, use `cluster.settings`.
|
||||
|
||||
|
@ -665,12 +666,12 @@ added: v0.7.7
|
|||
Calls `.disconnect()` on each worker in `cluster.workers`.
|
||||
|
||||
When they are disconnected all internal handles will be closed, allowing the
|
||||
master process to die gracefully if no other event is waiting.
|
||||
primary process to die gracefully if no other event is waiting.
|
||||
|
||||
The method takes an optional callback argument which will be called when
|
||||
finished.
|
||||
|
||||
This can only be called from the master process.
|
||||
This can only be called from the primary process.
|
||||
|
||||
## `cluster.fork([env])`
|
||||
<!-- YAML
|
||||
|
@ -682,18 +683,27 @@ added: v0.6.0
|
|||
|
||||
Spawn a new worker process.
|
||||
|
||||
This can only be called from the master process.
|
||||
This can only be called from the primary process.
|
||||
|
||||
## `cluster.isMaster`
|
||||
<!-- YAML
|
||||
added: v0.8.1
|
||||
deprecated: REPLACEME
|
||||
-->
|
||||
|
||||
Deprecated alias for [`cluster.isPrimary`][].
|
||||
details.
|
||||
|
||||
## `cluster.isPrimary`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* {boolean}
|
||||
|
||||
True if the process is a master. This is determined
|
||||
True if the process is a primary. This is determined
|
||||
by the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is
|
||||
undefined, then `isMaster` is `true`.
|
||||
undefined, then `isPrimary` is `true`.
|
||||
|
||||
## `cluster.isWorker`
|
||||
<!-- YAML
|
||||
|
@ -702,7 +712,7 @@ added: v0.6.0
|
|||
|
||||
* {boolean}
|
||||
|
||||
True if the process is not a master (it is the negation of `cluster.isMaster`).
|
||||
True if the process is not a primary (it is the negation of `cluster.isPrimary`).
|
||||
|
||||
## `cluster.schedulingPolicy`
|
||||
<!-- YAML
|
||||
|
@ -712,7 +722,7 @@ added: v0.11.2
|
|||
The scheduling policy, either `cluster.SCHED_RR` for round-robin or
|
||||
`cluster.SCHED_NONE` to leave it to the operating system. This is a
|
||||
global setting and effectively frozen once either the first worker is spawned,
|
||||
or [`.setupMaster()`][] is called, whichever comes first.
|
||||
or [`.setupPrimary()`][] is called, whichever comes first.
|
||||
|
||||
`SCHED_RR` is the default on all operating systems except Windows.
|
||||
Windows will change to `SCHED_RR` once libuv is able to effectively
|
||||
|
@ -767,11 +777,11 @@ changes:
|
|||
* `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
|
||||
master's `process.debugPort`.
|
||||
primary's `process.debugPort`.
|
||||
* `windowsHide` {boolean} Hide the forked processes console window that would
|
||||
normally be created on Windows systems. **Default:** `false`.
|
||||
|
||||
After calling [`.setupMaster()`][] (or [`.fork()`][]) this settings object will
|
||||
After calling [`.setupPrimary()`][] (or [`.fork()`][]) this settings object will
|
||||
contain the settings, including the default values.
|
||||
|
||||
This object is not intended to be changed or set manually.
|
||||
|
@ -779,42 +789,50 @@ This object is not intended to be changed or set manually.
|
|||
## `cluster.setupMaster([settings])`
|
||||
<!-- YAML
|
||||
added: v0.7.1
|
||||
deprecated: REPLACEME
|
||||
changes:
|
||||
- version: v6.4.0
|
||||
pr-url: https://github.com/nodejs/node/pull/7838
|
||||
description: The `stdio` option is supported now.
|
||||
-->
|
||||
|
||||
Deprecated alias for [`.setupPrimary()`][].
|
||||
|
||||
## `cluster.setupPrimary([settings])`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* `settings` {Object} See [`cluster.settings`][].
|
||||
|
||||
`setupMaster` is used to change the default 'fork' behavior. Once called,
|
||||
`setupPrimary` is used to change the default 'fork' behavior. Once called,
|
||||
the settings will be present in `cluster.settings`.
|
||||
|
||||
Any settings changes only affect future calls to [`.fork()`][] and have no
|
||||
effect on workers that are already running.
|
||||
|
||||
The only attribute of a worker that cannot be set via `.setupMaster()` is
|
||||
The only attribute of a worker that cannot be set via `.setupPrimary()` is
|
||||
the `env` passed to [`.fork()`][].
|
||||
|
||||
The defaults above apply to the first call only; the defaults for later
|
||||
calls are the current values at the time of `cluster.setupMaster()` is called.
|
||||
calls are the current values at the time of `cluster.setupPrimary()` is called.
|
||||
|
||||
```js
|
||||
const cluster = require('cluster');
|
||||
cluster.setupMaster({
|
||||
cluster.setupPrimary({
|
||||
exec: 'worker.js',
|
||||
args: ['--use', 'https'],
|
||||
silent: true
|
||||
});
|
||||
cluster.fork(); // https worker
|
||||
cluster.setupMaster({
|
||||
cluster.setupPrimary({
|
||||
exec: 'worker.js',
|
||||
args: ['--use', 'http']
|
||||
});
|
||||
cluster.fork(); // http worker
|
||||
```
|
||||
|
||||
This can only be called from the master process.
|
||||
This can only be called from the primary process.
|
||||
|
||||
## `cluster.worker`
|
||||
<!-- YAML
|
||||
|
@ -823,13 +841,13 @@ added: v0.7.0
|
|||
|
||||
* {Object}
|
||||
|
||||
A reference to the current worker object. Not available in the master process.
|
||||
A reference to the current worker object. Not available in the primary process.
|
||||
|
||||
```js
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
console.log('I am master');
|
||||
if (cluster.isPrimary) {
|
||||
console.log('I am primary');
|
||||
cluster.fork();
|
||||
cluster.fork();
|
||||
} else if (cluster.isWorker) {
|
||||
|
@ -845,7 +863,7 @@ added: v0.7.0
|
|||
* {Object}
|
||||
|
||||
A hash that stores the active worker objects, keyed by `id` field. Makes it
|
||||
easy to loop through all the workers. It is only available in the master
|
||||
easy to loop through all the workers. It is only available in the primary
|
||||
process.
|
||||
|
||||
A worker is removed from `cluster.workers` after the worker has disconnected
|
||||
|
@ -876,13 +894,14 @@ socket.on('data', (id) => {
|
|||
[Advanced serialization for `child_process`]: child_process.md#child_process_advanced_serialization
|
||||
[Child Process module]: child_process.md#child_process_child_process_fork_modulepath_args_options
|
||||
[`.fork()`]: #cluster_cluster_fork_env
|
||||
[`.setupMaster()`]: #cluster_cluster_setupmaster_settings
|
||||
[`.setupPrimary()`]: #cluster_cluster_setupprimary_settings
|
||||
[`ChildProcess.send()`]: child_process.md#child_process_subprocess_send_message_sendhandle_options_callback
|
||||
[`child_process.fork()`]: child_process.md#child_process_child_process_fork_modulepath_args_options
|
||||
[`child_process` event: `'exit'`]: child_process.md#child_process_event_exit
|
||||
[`child_process` event: `'message'`]: child_process.md#child_process_event_message
|
||||
[`cluster.settings`]: #cluster_cluster_settings
|
||||
[`disconnect()`]: child_process.md#child_process_subprocess_disconnect
|
||||
[`cluster.isPrimary`]: #cluster_cluster_isprimary
|
||||
[`kill()`]: process.md#process_process_kill_pid_signal
|
||||
[`process` event: `'message'`]: process.md#process_event_message
|
||||
[`server.close()`]: net.md#net_event_close
|
||||
|
|
|
@ -126,7 +126,7 @@ When sharing a UDP socket across multiple `cluster` workers, the
|
|||
```js
|
||||
const cluster = require('cluster');
|
||||
const dgram = require('dgram');
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork(); // Works ok.
|
||||
cluster.fork(); // Fails with EADDRINUSE.
|
||||
} else {
|
||||
|
|
|
@ -55,7 +55,7 @@ triggered the error, while letting the others finish in their normal
|
|||
time, and stop listening for new requests in that worker.
|
||||
|
||||
In this way, `domain` usage goes hand-in-hand with the cluster module,
|
||||
since the master process can fork a new worker when a worker
|
||||
since the primary process can fork a new worker when a worker
|
||||
encounters an error. For Node.js programs that scale to multiple
|
||||
machines, the terminating proxy or service registry can take note of
|
||||
the failure, and react accordingly.
|
||||
|
@ -90,9 +90,9 @@ appropriately, and handle errors with much greater safety.
|
|||
const cluster = require('cluster');
|
||||
const PORT = +process.env.PORT || 1337;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
// A more realistic scenario would have more than 2 workers,
|
||||
// and perhaps not put the master and worker in the same file.
|
||||
// and perhaps not put the primary and worker in the same file.
|
||||
//
|
||||
// It is also possible to get a bit fancier about logging, and
|
||||
// implement whatever custom logic is needed to prevent DoS
|
||||
|
@ -100,7 +100,7 @@ if (cluster.isMaster) {
|
|||
//
|
||||
// See the options in the cluster documentation.
|
||||
//
|
||||
// The important thing is that the master does very little,
|
||||
// The important thing is that the primary does very little,
|
||||
// increasing our resilience to unexpected errors.
|
||||
|
||||
cluster.fork();
|
||||
|
@ -142,8 +142,8 @@ if (cluster.isMaster) {
|
|||
// Stop taking new requests.
|
||||
server.close();
|
||||
|
||||
// Let the master know we're dead. This will trigger a
|
||||
// 'disconnect' in the cluster master, and then it will fork
|
||||
// Let the primary know we're dead. This will trigger a
|
||||
// 'disconnect' in the cluster primary, and then it will fork
|
||||
// a new worker.
|
||||
cluster.worker.disconnect();
|
||||
|
||||
|
|
|
@ -21,5 +21,5 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
const childOrMaster = 'NODE_UNIQUE_ID' in process.env ? 'child' : 'master';
|
||||
module.exports = require(`internal/cluster/${childOrMaster}`);
|
||||
const childOrPrimary = 'NODE_UNIQUE_ID' in process.env ? 'child' : 'primary';
|
||||
module.exports = require(`internal/cluster/${childOrPrimary}`);
|
||||
|
|
|
@ -157,13 +157,13 @@ function replaceHandle(self, newHandle) {
|
|||
const state = self[kStateSymbol];
|
||||
const oldHandle = state.handle;
|
||||
|
||||
// Set up the handle that we got from master.
|
||||
// Set up the handle that we got from primary.
|
||||
newHandle.lookup = oldHandle.lookup;
|
||||
newHandle.bind = oldHandle.bind;
|
||||
newHandle.send = oldHandle.send;
|
||||
newHandle[owner_symbol] = self;
|
||||
|
||||
// Replace the existing handle by the handle we got from master.
|
||||
// Replace the existing handle by the handle we got from primary.
|
||||
oldHandle.close();
|
||||
state.handle = newHandle;
|
||||
// Check if the udp handle was connected and set the state accordingly
|
||||
|
@ -183,7 +183,7 @@ function bufferSize(self, size, buffer) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
// Query master process to get the server handle and utilize it.
|
||||
// Query primary process to get the server handle and utilize it.
|
||||
function bindServerHandle(self, options, errCb) {
|
||||
if (!cluster)
|
||||
cluster = require('cluster');
|
||||
|
|
|
@ -853,7 +853,7 @@ function setupChannel(target, channel, serializationMode) {
|
|||
}
|
||||
}
|
||||
|
||||
/* If the master is > 2 read() calls behind, please stop sending. */
|
||||
/* If the primary is > 2 read() calls behind, please stop sending. */
|
||||
return channel.writeQueueSize < (65536 * 2);
|
||||
};
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ const noop = FunctionPrototype;
|
|||
module.exports = cluster;
|
||||
|
||||
cluster.isWorker = true;
|
||||
cluster.isMaster = false;
|
||||
cluster.isMaster = false; // Deprecated alias. Must be same as isPrimary.
|
||||
cluster.isPrimary = false;
|
||||
cluster.worker = null;
|
||||
cluster.Worker = Worker;
|
||||
|
||||
|
@ -39,7 +40,7 @@ cluster._setupWorker = function() {
|
|||
worker.emit('disconnect');
|
||||
|
||||
if (!worker.exitedAfterDisconnect) {
|
||||
// Unexpected disconnect, master exited, or some such nastiness, so
|
||||
// Unexpected disconnect, primary exited, or some such nastiness, so
|
||||
// worker exits immediately.
|
||||
process.exit(0);
|
||||
}
|
||||
|
@ -132,7 +133,7 @@ function shared(message, handle, indexesKey, cb) {
|
|||
cb(message.errno, handle);
|
||||
}
|
||||
|
||||
// Round-robin. Master distributes handles across workers.
|
||||
// Round-robin. Primary distributes handles across workers.
|
||||
function rr(message, indexesKey, cb) {
|
||||
if (message.errno)
|
||||
return cb(message.errno, null);
|
||||
|
@ -140,7 +141,7 @@ function rr(message, indexesKey, cb) {
|
|||
let key = message.key;
|
||||
|
||||
function listen(backlog) {
|
||||
// TODO(bnoordhuis) Send a message to the master that tells it to
|
||||
// TODO(bnoordhuis) Send a message to the primary that tells it to
|
||||
// update the backlog size. The actual backlog should probably be
|
||||
// the largest requested size by any worker.
|
||||
return 0;
|
||||
|
@ -149,9 +150,9 @@ function rr(message, indexesKey, cb) {
|
|||
function close() {
|
||||
// lib/net.js treats server._handle.close() as effectively synchronous.
|
||||
// That means there is a time window between the call to close() and
|
||||
// the ack by the master process in which we can still receive handles.
|
||||
// the ack by the primary process in which we can still receive handles.
|
||||
// onconnection() below handles that by sending those handles back to
|
||||
// the master.
|
||||
// the primary.
|
||||
if (key === undefined)
|
||||
return;
|
||||
|
||||
|
@ -199,7 +200,7 @@ function send(message, cb) {
|
|||
return sendHelper(process, message, null, cb);
|
||||
}
|
||||
|
||||
function _disconnect(masterInitiated) {
|
||||
function _disconnect(primaryInitiated) {
|
||||
this.exitedAfterDisconnect = true;
|
||||
let waitingCount = 1;
|
||||
|
||||
|
@ -208,10 +209,10 @@ function _disconnect(masterInitiated) {
|
|||
|
||||
if (waitingCount === 0) {
|
||||
// If disconnect is worker initiated, wait for ack to be sure
|
||||
// exitedAfterDisconnect is properly set in the master, otherwise, if
|
||||
// it's master initiated there's no need to send the
|
||||
// exitedAfterDisconnect is properly set in the primary, otherwise, if
|
||||
// it's primary initiated there's no need to send the
|
||||
// exitedAfterDisconnect message
|
||||
if (masterInitiated) {
|
||||
if (primaryInitiated) {
|
||||
process.disconnect();
|
||||
} else {
|
||||
send({ act: 'exitedAfterDisconnect' }, () => process.disconnect());
|
||||
|
|
|
@ -30,12 +30,13 @@ module.exports = cluster;
|
|||
|
||||
const handles = new SafeMap();
|
||||
cluster.isWorker = false;
|
||||
cluster.isMaster = true;
|
||||
cluster.isMaster = true; // Deprecated alias. Must be same as isPrimary.
|
||||
cluster.isPrimary = true;
|
||||
cluster.Worker = Worker;
|
||||
cluster.workers = {};
|
||||
cluster.settings = {};
|
||||
cluster.SCHED_NONE = SCHED_NONE; // Leave it to the operating system.
|
||||
cluster.SCHED_RR = SCHED_RR; // Master distributes connections.
|
||||
cluster.SCHED_RR = SCHED_RR; // Primary distributes connections.
|
||||
|
||||
let ids = 0;
|
||||
let debugPortOffset = 1;
|
||||
|
@ -56,7 +57,7 @@ else if (process.platform === 'win32') {
|
|||
|
||||
cluster.schedulingPolicy = schedulingPolicy;
|
||||
|
||||
cluster.setupMaster = function(options) {
|
||||
cluster.setupPrimary = function(options) {
|
||||
const settings = {
|
||||
args: ArrayPrototypeSlice(process.argv, 2),
|
||||
exec: process.argv[1],
|
||||
|
@ -105,6 +106,9 @@ cluster.setupMaster = function(options) {
|
|||
});
|
||||
};
|
||||
|
||||
// Deprecated alias must be same as setupPrimary
|
||||
cluster.setupMaster = cluster.setupPrimary;
|
||||
|
||||
function setupSettingsNT(settings) {
|
||||
cluster.emit('setup', settings);
|
||||
}
|
||||
|
@ -170,7 +174,7 @@ function removeHandlesForWorker(worker) {
|
|||
}
|
||||
|
||||
cluster.fork = function(env) {
|
||||
cluster.setupMaster();
|
||||
cluster.setupPrimary();
|
||||
const id = ++ids;
|
||||
const workerProcess = createWorkerProcess(id, env);
|
||||
const worker = new Worker({
|
||||
|
@ -203,7 +207,7 @@ cluster.fork = function(env) {
|
|||
/*
|
||||
* Now is a good time to remove the handles
|
||||
* associated with this worker because it is
|
||||
* not connected to the master anymore.
|
||||
* not connected to the primary anymore.
|
||||
*/
|
||||
removeHandlesForWorker(worker);
|
||||
|
||||
|
@ -357,7 +361,7 @@ function send(worker, message, handle, cb) {
|
|||
return sendHelper(worker.process, message, handle, cb);
|
||||
}
|
||||
|
||||
// Extend generic Worker with methods specific to the master process.
|
||||
// Extend generic Worker with methods specific to the primary process.
|
||||
Worker.prototype.disconnect = function() {
|
||||
this.exitedAfterDisconnect = true;
|
||||
send(this, { act: 'disconnect' });
|
|
@ -9,7 +9,7 @@ const EventEmitter = require('events');
|
|||
|
||||
module.exports = Worker;
|
||||
|
||||
// Common Worker implementation shared between the cluster master and workers.
|
||||
// Common Worker implementation shared between the cluster primary and workers.
|
||||
function Worker(options) {
|
||||
if (!(this instanceof Worker))
|
||||
return new Worker(options);
|
||||
|
|
12
lib/net.js
12
lib/net.js
|
@ -1359,7 +1359,7 @@ function listenInCluster(server, address, port, addressType,
|
|||
|
||||
if (cluster === undefined) cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster || exclusive) {
|
||||
if (cluster.isPrimary || exclusive) {
|
||||
// Will create a new handle
|
||||
// _listen2 sets up the listened handle, it is still named like this
|
||||
// to avoid breaking code that wraps this method
|
||||
|
@ -1375,10 +1375,10 @@ function listenInCluster(server, address, port, addressType,
|
|||
flags,
|
||||
};
|
||||
|
||||
// Get the master's server handle, and listen on it
|
||||
cluster._getServer(server, serverQuery, listenOnMasterHandle);
|
||||
// Get the primary's server handle, and listen on it
|
||||
cluster._getServer(server, serverQuery, listenOnPrimaryHandle);
|
||||
|
||||
function listenOnMasterHandle(err, handle) {
|
||||
function listenOnPrimaryHandle(err, handle) {
|
||||
err = checkBindError(err, port, handle);
|
||||
|
||||
if (err) {
|
||||
|
@ -1386,7 +1386,7 @@ function listenInCluster(server, address, port, addressType,
|
|||
return server.emit('error', ex);
|
||||
}
|
||||
|
||||
// Reuse master's server handle
|
||||
// Reuse primary's server handle
|
||||
server._handle = handle;
|
||||
// _listen2 sets up the listened handle, it is still named like this
|
||||
// to avoid breaking code that wraps this method
|
||||
|
@ -1449,7 +1449,7 @@ Server.prototype.listen = function(...args) {
|
|||
lookupAndListen(this, options.port | 0, options.host, backlog,
|
||||
options.exclusive, flags);
|
||||
} else { // Undefined host, listens on unspecified address
|
||||
// Default addressType 4 will be used to search for master server
|
||||
// Default addressType 4 will be used to search for primary server
|
||||
listenInCluster(this, null, options.port | 0, 4,
|
||||
backlog, undefined, options.exclusive);
|
||||
}
|
||||
|
|
2
node.gyp
2
node.gyp
|
@ -117,7 +117,7 @@
|
|||
'lib/internal/child_process.js',
|
||||
'lib/internal/child_process/serialization.js',
|
||||
'lib/internal/cluster/child.js',
|
||||
'lib/internal/cluster/master.js',
|
||||
'lib/internal/cluster/primary.js',
|
||||
'lib/internal/cluster/round_robin_handle.js',
|
||||
'lib/internal/cluster/shared_handle.js',
|
||||
'lib/internal/cluster/utils.js',
|
||||
|
|
|
@ -30,7 +30,7 @@ switch (arg) {
|
|||
return;
|
||||
}
|
||||
|
||||
// This part should run only for the master test
|
||||
// This part should run only for the primary test
|
||||
assert.ok(!arg);
|
||||
{
|
||||
// console.log should stay until this test's flakiness is solved
|
||||
|
@ -75,7 +75,7 @@ assert.ok(!arg);
|
|||
assert.strictEqual(child.status, null);
|
||||
// Most posix systems will show 'SIGABRT', but alpine34 does not
|
||||
if (child.signal !== 'SIGABRT') {
|
||||
console.log(`parent received signal ${child.signal}\nchild's stderr:`);
|
||||
console.log(`primary received signal ${child.signal}\nchild's stderr:`);
|
||||
console.log(child.stderr);
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ if (process.argv.length === 2 &&
|
|||
isMainThread &&
|
||||
hasCrypto &&
|
||||
require.main &&
|
||||
require('cluster').isMaster) {
|
||||
require('cluster').isPrimary) {
|
||||
// The copyright notice is relatively big and the flags could come afterwards.
|
||||
const bytesToRead = 1500;
|
||||
const buffer = Buffer.allocUnsafe(bytesToRead);
|
||||
|
|
2
test/fixtures/cluster-preload-test.js
vendored
2
test/fixtures/cluster-preload-test.js
vendored
|
@ -1,5 +1,5 @@
|
|||
const cluster = require('cluster');
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork(); // one child
|
||||
cluster.on('exit', function(worker, code, signal) {
|
||||
console.log(`worker terminated with code ${code}`);
|
||||
|
|
2
test/fixtures/cluster-preload.js
vendored
2
test/fixtures/cluster-preload.js
vendored
|
@ -8,5 +8,5 @@ const expectedPaths = require('module')._nodeModulePaths(process.cwd());
|
|||
assert.deepStrictEqual(module.parent.paths, expectedPaths);
|
||||
|
||||
const cluster = require('cluster');
|
||||
cluster.isMaster || process.exit(42 + cluster.worker.id); // +42 to distinguish
|
||||
cluster.isPrimary || process.exit(42 + cluster.worker.id); // +42 to distinguish
|
||||
// from exit(1) for other random reasons
|
||||
|
|
2
test/fixtures/clustered-server/app.js
vendored
2
test/fixtures/clustered-server/app.js
vendored
|
@ -10,7 +10,7 @@ function handleRequest(request, response) {
|
|||
const NUMBER_OF_WORKERS = 2;
|
||||
var workersOnline = 0;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.on('online', function() {
|
||||
if (++workersOnline === NUMBER_OF_WORKERS) {
|
||||
console.error('all workers are running');
|
||||
|
|
|
@ -13,7 +13,7 @@ const cluster = require('cluster');
|
|||
const dgram = require('dgram');
|
||||
const BYE = 'bye';
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker1 = cluster.fork();
|
||||
|
||||
// Verify that Windows doesn't support this scenario
|
||||
|
@ -40,7 +40,7 @@ if (cluster.isMaster) {
|
|||
worker2.send(BYE);
|
||||
});
|
||||
});
|
||||
// end master code
|
||||
// end primary code
|
||||
} else {
|
||||
// worker code
|
||||
process.on('message', (msg) => msg === BYE && process.exit(0));
|
||||
|
|
|
@ -47,7 +47,7 @@ function serialFork() {
|
|||
});
|
||||
}
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.on('online', common.mustCall((worker) => worker.send('dbgport'), 2));
|
||||
|
||||
// Block one of the ports with a listening socket.
|
||||
|
|
|
@ -8,8 +8,8 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (!cluster.isMaster) {
|
||||
// Exit on first received handle to leave the queue non-empty in master
|
||||
if (!cluster.isPrimary) {
|
||||
// Exit on first received handle to leave the queue non-empty in primary
|
||||
process.on('message', function() {
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
|
@ -31,9 +31,9 @@ const spawn = require('child_process').spawn;
|
|||
if (process.argv[2] === 'worker')
|
||||
worker();
|
||||
else
|
||||
master();
|
||||
primary();
|
||||
|
||||
function master() {
|
||||
function primary() {
|
||||
// spawn() can only create one IPC channel so we use stdin/stdout as an
|
||||
// ad-hoc command channel.
|
||||
const proc = spawn(process.execPath, [
|
||||
|
|
|
@ -33,11 +33,11 @@ if (process.argv[2] === 'pipe') {
|
|||
// Child IPC test
|
||||
process.send('message from child');
|
||||
process.on('message', function() {
|
||||
process.send('got message from master');
|
||||
process.send('got message from primary');
|
||||
});
|
||||
|
||||
} else if (process.argv[2] === 'parent') {
|
||||
// Parent | start child pipe test
|
||||
} else if (process.argv[2] === 'primary') {
|
||||
// Primary | start child pipe test
|
||||
|
||||
const child = childProcess.fork(process.argv[1], ['pipe'], { silent: true });
|
||||
|
||||
|
@ -49,19 +49,19 @@ if (process.argv[2] === 'pipe') {
|
|||
});
|
||||
|
||||
} else {
|
||||
// Testcase | start parent && child IPC test
|
||||
// Testcase | start primary && child IPC test
|
||||
|
||||
// testing: is stderr and stdout piped to parent
|
||||
const args = [process.argv[1], 'parent'];
|
||||
const parent = childProcess.spawn(process.execPath, args);
|
||||
// testing: is stderr and stdout piped to primary
|
||||
const args = [process.argv[1], 'primary'];
|
||||
const primary = childProcess.spawn(process.execPath, args);
|
||||
|
||||
// Got any stderr or std data
|
||||
let stdoutData = false;
|
||||
parent.stdout.on('data', function() {
|
||||
primary.stdout.on('data', function() {
|
||||
stdoutData = true;
|
||||
});
|
||||
let stderrData = false;
|
||||
parent.stderr.on('data', function() {
|
||||
primary.stderr.on('data', function() {
|
||||
stderrData = true;
|
||||
});
|
||||
|
||||
|
@ -80,7 +80,7 @@ if (process.argv[2] === 'pipe') {
|
|||
}
|
||||
|
||||
if (childReceiving === false) {
|
||||
childReceiving = (message === 'got message from master');
|
||||
childReceiving = (message === 'got message from primary');
|
||||
}
|
||||
|
||||
if (childReceiving === true) {
|
||||
|
@ -93,7 +93,7 @@ if (process.argv[2] === 'pipe') {
|
|||
process.on('exit', function() {
|
||||
// clean up
|
||||
child.kill();
|
||||
parent.kill();
|
||||
primary.kill();
|
||||
|
||||
// Check std(out|err) pipes
|
||||
assert.ok(!stdoutData);
|
||||
|
|
|
@ -3,7 +3,7 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.settings.serialization = 'advanced';
|
||||
const worker = cluster.fork();
|
||||
const circular = {};
|
||||
|
|
|
@ -38,7 +38,7 @@ function forEach(obj, fn) {
|
|||
|
||||
if (cluster.isWorker) {
|
||||
require('http').Server(common.mustNotCall()).listen(0, '127.0.0.1');
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const checks = {
|
||||
cluster: {
|
||||
|
|
|
@ -39,7 +39,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork().on('exit', common.mustCall((exitCode) => {
|
||||
assert.strictEqual(exitCode, 0);
|
||||
}));
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
//
|
||||
// <supervisor>
|
||||
// / \
|
||||
// <master 1> <master 2>
|
||||
// <primary 1> <primary 2>
|
||||
// / \
|
||||
// <worker 1> <worker 2>
|
||||
//
|
||||
|
@ -78,7 +78,7 @@ if (!id) {
|
|||
}));
|
||||
|
||||
} else if (id === 'one') {
|
||||
if (cluster.isMaster) return startWorker();
|
||||
if (cluster.isPrimary) return startWorker();
|
||||
|
||||
const server = http.createServer(common.mustNotCall());
|
||||
server.listen(0, common.mustCall(() => {
|
||||
|
@ -89,7 +89,7 @@ if (!id) {
|
|||
if (m === 'QUIT') process.exit();
|
||||
}));
|
||||
} else if (id === 'two') {
|
||||
if (cluster.isMaster) return startWorker();
|
||||
if (cluster.isPrimary) return startWorker();
|
||||
|
||||
const server = http.createServer(common.mustNotCall());
|
||||
process.on('message', common.mustCall((m) => {
|
||||
|
|
|
@ -3,7 +3,7 @@ const common = require('../common');
|
|||
const cluster = require('cluster');
|
||||
const assert = require('assert');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
worker.on('disconnect', common.mustCall(() => {
|
||||
assert.strictEqual(worker.isConnected(), false);
|
||||
|
|
|
@ -8,7 +8,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const os = require('os');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const workers = [];
|
||||
const numCPUs = os.cpus().length;
|
||||
let waitOnline = numCPUs;
|
||||
|
|
|
@ -4,7 +4,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
tmpdir.refresh();
|
||||
|
||||
assert.strictEqual(cluster.settings.cwd, undefined);
|
||||
|
@ -12,7 +12,7 @@ if (cluster.isMaster) {
|
|||
assert.strictEqual(msg, process.cwd());
|
||||
}));
|
||||
|
||||
cluster.setupMaster({ cwd: tmpdir.path });
|
||||
cluster.setupPrimary({ cwd: tmpdir.path });
|
||||
assert.strictEqual(cluster.settings.cwd, tmpdir.path);
|
||||
cluster.fork().on('message', common.mustCall((msg) => {
|
||||
assert.strictEqual(msg, tmpdir.path);
|
||||
|
|
|
@ -31,13 +31,13 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const dgram = require('dgram');
|
||||
|
||||
if (cluster.isMaster)
|
||||
master();
|
||||
if (cluster.isPrimary)
|
||||
primary();
|
||||
else
|
||||
worker();
|
||||
|
||||
|
||||
function master() {
|
||||
function primary() {
|
||||
let listening = 0;
|
||||
|
||||
// Fork 4 workers.
|
||||
|
@ -100,7 +100,7 @@ function worker() {
|
|||
socket.on('message', common.mustCall((data, info) => {
|
||||
received++;
|
||||
|
||||
// Every 10 messages, notify the master.
|
||||
// Every 10 messages, notify the primary.
|
||||
if (received === PACKETS_PER_WORKER) {
|
||||
process.send({ received });
|
||||
socket.close();
|
||||
|
|
|
@ -31,13 +31,13 @@ const cluster = require('cluster');
|
|||
const dgram = require('dgram');
|
||||
const assert = require('assert');
|
||||
|
||||
if (cluster.isMaster)
|
||||
master();
|
||||
if (cluster.isPrimary)
|
||||
primary();
|
||||
else
|
||||
worker();
|
||||
|
||||
|
||||
function master() {
|
||||
function primary() {
|
||||
let received = 0;
|
||||
|
||||
// Start listening on a socket.
|
||||
|
@ -69,7 +69,7 @@ function master() {
|
|||
|
||||
|
||||
function worker() {
|
||||
// Create udp socket and send packets to master.
|
||||
// Create udp socket and send packets to primary.
|
||||
const socket = dgram.createSocket('udp4');
|
||||
const buf = Buffer.from('hello world');
|
||||
|
||||
|
|
|
@ -11,13 +11,13 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const dgram = require('dgram');
|
||||
|
||||
if (cluster.isMaster)
|
||||
master();
|
||||
if (cluster.isPrimary)
|
||||
primary();
|
||||
else
|
||||
worker();
|
||||
|
||||
|
||||
function master() {
|
||||
function primary() {
|
||||
const { internalBinding } = require('internal/test/binding');
|
||||
const { UDP } = internalBinding('udp_wrap');
|
||||
|
||||
|
@ -97,7 +97,7 @@ function worker() {
|
|||
socket.on('message', common.mustCall((data, info) => {
|
||||
received++;
|
||||
|
||||
// Every 10 messages, notify the master.
|
||||
// Every 10 messages, notify the primary.
|
||||
if (received === PACKETS_PER_WORKER) {
|
||||
process.send({ received });
|
||||
socket.close();
|
||||
|
|
|
@ -12,7 +12,7 @@ const dgram = require('dgram');
|
|||
|
||||
// This test ensures that the `ipv6Only` option in `dgram.createSock()`
|
||||
// works as expected.
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork().on('exit', common.mustCall((code) => {
|
||||
assert.strictEqual(code, 0);
|
||||
}));
|
||||
|
|
|
@ -7,7 +7,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const dgram = require('dgram');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork().on('exit', common.mustCall((code) => {
|
||||
assert.strictEqual(code, 0);
|
||||
}));
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
const common = require('../common');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork().on('online', common.mustCall(disconnect));
|
||||
|
||||
function disconnect() {
|
||||
|
|
|
@ -7,7 +7,7 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.on('exit', (worker, code) => {
|
||||
assert.strictEqual(code, 0, `worker exited with code: ${code}, expected 0`);
|
||||
});
|
||||
|
|
|
@ -25,7 +25,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const fork = cluster.fork;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
fork(); // It is intentionally called `fork` instead of
|
||||
fork(); // `cluster.fork` to test that `this` is not used
|
||||
cluster.disconnect(common.mustCall(() => {
|
||||
|
|
|
@ -8,7 +8,7 @@ const cluster = require('cluster');
|
|||
|
||||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
|
||||
// This is the important part of the test: Confirm that `disconnect` fires.
|
||||
|
|
|
@ -13,7 +13,7 @@ const cluster = require('cluster');
|
|||
|
||||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
let worker2;
|
||||
|
||||
const worker1 = cluster.fork();
|
||||
|
|
|
@ -26,7 +26,7 @@ process.env.NODE_CLUSTER_SCHED_POLICY = 'none';
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const unbound = cluster.fork().on('online', bind);
|
||||
|
||||
function bind() {
|
||||
|
|
|
@ -29,7 +29,7 @@ if (common.isWindows)
|
|||
const cluster = require('cluster');
|
||||
const dgram = require('dgram');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const unbound = cluster.fork().on('online', bind);
|
||||
|
||||
function bind() {
|
||||
|
|
|
@ -33,7 +33,7 @@ if (cluster.isWorker) {
|
|||
net.createServer((socket) => {
|
||||
socket.end('echo');
|
||||
}).listen(0, '127.0.0.1');
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
const servers = 2;
|
||||
const serverPorts = new Set();
|
||||
|
||||
|
|
|
@ -23,22 +23,22 @@
|
|||
const common = require('../common');
|
||||
|
||||
// Test that errors propagated from cluster workers are properly
|
||||
// received in their master. Creates an EADDRINUSE condition by forking
|
||||
// a process in child cluster and propagates the error to the master.
|
||||
// received in their primary. Creates an EADDRINUSE condition by forking
|
||||
// a process in child cluster and propagates the error to the primary.
|
||||
|
||||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
const fork = require('child_process').fork;
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster && process.argv.length !== 3) {
|
||||
// cluster.isMaster
|
||||
if (cluster.isPrimary && process.argv.length !== 3) {
|
||||
// cluster.isPrimary
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
const PIPE_NAME = common.PIPE;
|
||||
const worker = cluster.fork({ PIPE_NAME });
|
||||
|
||||
// Makes sure master is able to fork the worker
|
||||
// Makes sure primary is able to fork the worker
|
||||
cluster.on('fork', common.mustCall());
|
||||
|
||||
// Makes sure the worker is ready
|
||||
|
@ -59,14 +59,14 @@ if (cluster.isMaster && process.argv.length !== 3) {
|
|||
const server = net.createServer().listen(PIPE_NAME, function() {
|
||||
// Message child process so that it can exit
|
||||
cp.send('end');
|
||||
// Inform master about the unexpected situation
|
||||
// Inform primary about the unexpected situation
|
||||
process.send('PIPE should have been in use.');
|
||||
});
|
||||
|
||||
server.on('error', function(err) {
|
||||
// Message to child process tells it to exit
|
||||
cp.send('end');
|
||||
// Propagate error to parent
|
||||
// Propagate error to primary
|
||||
process.send(err);
|
||||
});
|
||||
}));
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
'use strict';
|
||||
// Check that having a worker bind to a port that's already taken doesn't
|
||||
// leave the master process in a confused state. Releasing the port and
|
||||
// leave the primary process in a confused state. Releasing the port and
|
||||
// trying again should Just Work[TM].
|
||||
|
||||
const common = require('../common');
|
||||
|
|
|
@ -36,7 +36,7 @@ if (cluster.isWorker) {
|
|||
});
|
||||
|
||||
assert.strictEqual(result, true);
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const checks = {
|
||||
using: false,
|
||||
|
|
|
@ -4,10 +4,10 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const buf = Buffer.from('foobar');
|
||||
|
||||
cluster.setupMaster({
|
||||
cluster.setupPrimary({
|
||||
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
|
||||
});
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ if (!process.argv[2]) {
|
|||
// by D with `detached: true`, no console window will pop up for W.
|
||||
//
|
||||
// So, we have to spawn a detached process first to run the actual test.
|
||||
const master = child_process.spawn(
|
||||
const primary = child_process.spawn(
|
||||
process.argv[0],
|
||||
[process.argv[1], '--cluster'],
|
||||
{ detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
|
||||
|
@ -31,19 +31,19 @@ if (!process.argv[2]) {
|
|||
})
|
||||
};
|
||||
|
||||
master.on('message', (msg) => {
|
||||
primary.on('message', (msg) => {
|
||||
const handler = messageHandlers[msg.type];
|
||||
assert.ok(handler);
|
||||
handler(msg);
|
||||
});
|
||||
|
||||
master.on('exit', common.mustCall((code, signal) => {
|
||||
primary.on('exit', common.mustCall((code, signal) => {
|
||||
assert.strictEqual(code, 0);
|
||||
assert.strictEqual(signal, null);
|
||||
}));
|
||||
|
||||
} else if (cluster.isMaster) {
|
||||
cluster.setupMaster({
|
||||
} else if (cluster.isPrimary) {
|
||||
cluster.setupPrimary({
|
||||
silent: true,
|
||||
windowsHide: true
|
||||
});
|
||||
|
|
|
@ -31,7 +31,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const http = require('http');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
tmpdir.refresh();
|
||||
const worker = cluster.fork();
|
||||
|
|
|
@ -3,7 +3,7 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
|
||||
worker.on('exit', common.mustCall((code, signal) => {
|
||||
|
|
|
@ -8,7 +8,7 @@ cluster.schedulingPolicy = cluster.SCHED_RR;
|
|||
|
||||
const server = http.createServer();
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
server.listen({ port: 0 }, common.mustCall(() => {
|
||||
const worker = cluster.fork({ PORT: server.address().port });
|
||||
worker.on('exit', common.mustCall(() => {
|
||||
|
|
|
@ -9,7 +9,7 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
function forkWorker(action) {
|
||||
const worker = cluster.fork({ action });
|
||||
worker.on('disconnect', common.mustCall(() => {
|
||||
|
|
|
@ -3,7 +3,7 @@ const common = require('../common');
|
|||
const cluster = require('cluster');
|
||||
const assert = require('assert');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
|
||||
worker.on('online', common.mustCall(() => {
|
||||
|
|
|
@ -4,7 +4,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork();
|
||||
cluster.on('listening', common.mustCall(function(worker, address) {
|
||||
const port = address.port;
|
||||
|
|
|
@ -40,7 +40,7 @@ if (cluster.isWorker) {
|
|||
function maybeReply() {
|
||||
if (!socket || !message) return;
|
||||
|
||||
// Tell master using TCP socket that a message is received.
|
||||
// Tell primary using TCP socket that a message is received.
|
||||
socket.write(JSON.stringify({
|
||||
code: 'received message',
|
||||
echo: message
|
||||
|
@ -61,14 +61,14 @@ if (cluster.isWorker) {
|
|||
});
|
||||
|
||||
server.listen(0, '127.0.0.1');
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const checks = {
|
||||
global: {
|
||||
'receive': false,
|
||||
'correct': false
|
||||
},
|
||||
master: {
|
||||
primary: {
|
||||
'receive': false,
|
||||
'correct': false
|
||||
},
|
||||
|
@ -101,7 +101,7 @@ if (cluster.isWorker) {
|
|||
|
||||
// When a IPC message is received from the worker
|
||||
worker.on('message', function(message) {
|
||||
check('master', message === 'message from worker');
|
||||
check('primary', message === 'message from worker');
|
||||
});
|
||||
cluster.on('message', function(worker_, message) {
|
||||
assert.strictEqual(worker_, worker);
|
||||
|
@ -113,7 +113,7 @@ if (cluster.isWorker) {
|
|||
|
||||
client = net.connect(address.port, function() {
|
||||
// Send message to worker.
|
||||
worker.send('message from master');
|
||||
worker.send('message from primary');
|
||||
});
|
||||
|
||||
client.on('data', function(data) {
|
||||
|
@ -121,7 +121,7 @@ if (cluster.isWorker) {
|
|||
data = JSON.parse(data.toString());
|
||||
|
||||
if (data.code === 'received message') {
|
||||
check('worker', data.echo === 'message from master');
|
||||
check('worker', data.echo === 'message from primary');
|
||||
} else {
|
||||
throw new Error(`wrong TCP message received: ${data}`);
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ const net = require('net');
|
|||
const host = '::';
|
||||
const WORKER_COUNT = 3;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const workers = [];
|
||||
let address;
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ const socketName = 'A'.repeat(101 - socketDir.length);
|
|||
assert.ok(path.resolve(socketDir, socketName).length > 100,
|
||||
'absolute socket path should be longer than 100 bytes');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
// Ensure that the worker exits peacefully.
|
||||
tmpdir.refresh();
|
||||
process.chdir(tmpdir.path);
|
||||
|
|
|
@ -25,7 +25,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
// Ensure that the worker exits peacefully
|
||||
cluster.fork().on('exit', common.mustCall(function(statusCode) {
|
||||
assert.strictEqual(statusCode, 0);
|
||||
|
|
|
@ -26,7 +26,7 @@ const fork = require('child_process').fork;
|
|||
const net = require('net');
|
||||
|
||||
if (process.argv[2] !== 'child') {
|
||||
console.error(`[${process.pid}] master`);
|
||||
console.error(`[${process.pid}] primary`);
|
||||
|
||||
const worker = fork(__filename, ['child']);
|
||||
let called = false;
|
||||
|
|
|
@ -73,10 +73,10 @@ if (cluster.isWorker) {
|
|||
const workers = [];
|
||||
|
||||
// Spawn a cluster process
|
||||
const master = fork(process.argv[1], ['cluster'], { silent: true });
|
||||
const primary = fork(process.argv[1], ['cluster'], { silent: true });
|
||||
|
||||
// Handle messages from the cluster
|
||||
master.on('message', common.mustCall((data) => {
|
||||
primary.on('message', common.mustCall((data) => {
|
||||
// Add worker pid to list and progress tracker
|
||||
if (data.cmd === 'worker') {
|
||||
workers.push(data.workerPID);
|
||||
|
@ -84,7 +84,7 @@ if (cluster.isWorker) {
|
|||
}, totalWorkers));
|
||||
|
||||
// When cluster is dead
|
||||
master.on('exit', common.mustCall((code) => {
|
||||
primary.on('exit', common.mustCall((code) => {
|
||||
// Check that the cluster died accidentally (non-zero exit code)
|
||||
assert.strictEqual(code, 1);
|
||||
|
||||
|
@ -92,7 +92,7 @@ if (cluster.isWorker) {
|
|||
// flaky – another process might end up being started right after the
|
||||
// workers finished and receive the same PID.
|
||||
const pollWorkers = () => {
|
||||
// When master is dead all workers should be dead too
|
||||
// When primary is dead all workers should be dead too
|
||||
if (workers.some((pid) => common.isAlive(pid))) {
|
||||
setTimeout(pollWorkers, 50);
|
||||
}
|
|
@ -52,19 +52,19 @@ if (cluster.isWorker) {
|
|||
const fork = require('child_process').fork;
|
||||
|
||||
// Spawn a cluster process
|
||||
const master = fork(process.argv[1], ['cluster']);
|
||||
const primary = fork(process.argv[1], ['cluster']);
|
||||
|
||||
// get pid info
|
||||
let pid = null;
|
||||
master.once('message', (data) => {
|
||||
primary.once('message', (data) => {
|
||||
pid = data.pid;
|
||||
});
|
||||
|
||||
// When master is dead
|
||||
// When primary is dead
|
||||
let alive = true;
|
||||
master.on('exit', common.mustCall((code) => {
|
||||
primary.on('exit', common.mustCall((code) => {
|
||||
|
||||
// Make sure that the master died on purpose
|
||||
// Make sure that the primary died on purpose
|
||||
assert.strictEqual(code, 0);
|
||||
|
||||
// Check worker process status
|
||||
|
@ -82,7 +82,8 @@ if (cluster.isWorker) {
|
|||
assert.strictEqual(typeof pid, 'number',
|
||||
`got ${pid} instead of a worker pid`);
|
||||
assert.strictEqual(alive, false,
|
||||
`worker was alive after master died (alive = ${alive})`);
|
||||
`worker was alive after primary died (alive = ${alive})`
|
||||
);
|
||||
});
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', common.mustCall((code, signal) => {
|
||||
assert.strictEqual(
|
||||
|
|
|
@ -34,7 +34,7 @@ if (cluster.isWorker) {
|
|||
const http = require('http');
|
||||
http.Server(() => {}).listen(0, '127.0.0.1');
|
||||
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
// Kill worker when listening
|
||||
cluster.on('listening', function() {
|
||||
|
|
|
@ -4,7 +4,7 @@ const common = require('../common');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork().on('message', function(msg) {
|
||||
if (msg === 'done') this.kill();
|
||||
});
|
||||
|
|
|
@ -20,15 +20,15 @@
|
|||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
// Testing mutual send of handles: from master to worker, and from worker to
|
||||
// master.
|
||||
// Testing mutual send of handles: from primary to worker, and from worker to
|
||||
// primary.
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', (code, signal) => {
|
||||
assert.strictEqual(code, 0, `Worker exited with an error code: ${code}`);
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
// Testing to send an handle twice to the parent process.
|
||||
// Testing to send an handle twice to the primary process.
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
|
@ -31,7 +31,7 @@ const workers = {
|
|||
toStart: 1
|
||||
};
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
for (let i = 0; i < workers.toStart; ++i) {
|
||||
const worker = cluster.fork();
|
||||
worker.on('exit', common.mustCall(function(code, signal) {
|
||||
|
|
|
@ -10,7 +10,7 @@ const cluster = require('cluster');
|
|||
const http = require('http');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
const server = net.createServer(common.mustCall((socket) => {
|
||||
worker.send('socket', socket);
|
||||
|
|
|
@ -5,7 +5,7 @@ const cluster = require('cluster');
|
|||
|
||||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker1 = cluster.fork();
|
||||
worker1.on('listening', common.mustCall(() => {
|
||||
const worker2 = cluster.fork();
|
||||
|
|
|
@ -5,7 +5,7 @@ const cluster = require('cluster');
|
|||
|
||||
cluster.schedulingPolicy = cluster.SCHED_RR;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker1 = cluster.fork();
|
||||
worker1.on('listening', common.mustCall(() => {
|
||||
const worker2 = cluster.fork();
|
||||
|
|
|
@ -36,4 +36,4 @@ cluster.on('setup', common.mustCall(function() {
|
|||
assert.notStrictEqual(process.argv[process.argv.length - 1], 'OMG,OMG');
|
||||
process.argv.push('OMG,OMG');
|
||||
process.argv.push('OMG,OMG');
|
||||
cluster.setupMaster();
|
||||
cluster.setupPrimary();
|
|
@ -24,12 +24,12 @@ require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
assert(cluster.isMaster);
|
||||
assert(cluster.isPrimary);
|
||||
|
||||
// cluster.settings should not be initialized until needed
|
||||
assert.deepStrictEqual(cluster.settings, {});
|
||||
|
||||
cluster.setupMaster();
|
||||
cluster.setupPrimary();
|
||||
assert.deepStrictEqual(cluster.settings, {
|
||||
args: process.argv.slice(2),
|
||||
exec: process.argv[1],
|
||||
|
@ -38,21 +38,21 @@ assert.deepStrictEqual(cluster.settings, {
|
|||
});
|
||||
console.log('ok sets defaults');
|
||||
|
||||
cluster.setupMaster({ exec: 'overridden' });
|
||||
cluster.setupPrimary({ exec: 'overridden' });
|
||||
assert.strictEqual(cluster.settings.exec, 'overridden');
|
||||
console.log('ok overrides defaults');
|
||||
|
||||
cluster.setupMaster({ args: ['foo', 'bar'] });
|
||||
cluster.setupPrimary({ args: ['foo', 'bar'] });
|
||||
assert.strictEqual(cluster.settings.exec, 'overridden');
|
||||
assert.deepStrictEqual(cluster.settings.args, ['foo', 'bar']);
|
||||
|
||||
cluster.setupMaster({ execArgv: ['baz', 'bang'] });
|
||||
cluster.setupPrimary({ execArgv: ['baz', 'bang'] });
|
||||
assert.strictEqual(cluster.settings.exec, 'overridden');
|
||||
assert.deepStrictEqual(cluster.settings.args, ['foo', 'bar']);
|
||||
assert.deepStrictEqual(cluster.settings.execArgv, ['baz', 'bang']);
|
||||
console.log('ok preserves unchanged settings on repeated calls');
|
||||
|
||||
cluster.setupMaster();
|
||||
cluster.setupPrimary();
|
||||
assert.deepStrictEqual(cluster.settings, {
|
||||
args: ['foo', 'bar'],
|
||||
exec: 'overridden',
|
|
@ -24,14 +24,14 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
assert(cluster.isMaster);
|
||||
assert(cluster.isPrimary);
|
||||
|
||||
function emitAndCatch(next) {
|
||||
cluster.once('setup', common.mustCall(function(settings) {
|
||||
assert.strictEqual(settings.exec, 'new-exec');
|
||||
setImmediate(next);
|
||||
}));
|
||||
cluster.setupMaster({ exec: 'new-exec' });
|
||||
cluster.setupPrimary({ exec: 'new-exec' });
|
||||
}
|
||||
|
||||
function emitAndCatch2(next) {
|
||||
|
@ -39,7 +39,7 @@ function emitAndCatch2(next) {
|
|||
assert('exec' in settings);
|
||||
setImmediate(next);
|
||||
}));
|
||||
cluster.setupMaster();
|
||||
cluster.setupPrimary();
|
||||
}
|
||||
|
||||
emitAndCatch(common.mustCall(function() {
|
|
@ -25,7 +25,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const debug = require('util').debuglog('test');
|
||||
|
||||
assert(cluster.isMaster);
|
||||
assert(cluster.isPrimary);
|
||||
|
||||
// The cluster.settings object is cloned even though the current implementation
|
||||
// makes that unnecessary. This is to make the test less fragile if the
|
||||
|
@ -48,7 +48,7 @@ const execs = [
|
|||
];
|
||||
|
||||
process.on('exit', () => {
|
||||
// Tests that "setup" is emitted for every call to setupMaster
|
||||
// Tests that "setup" is emitted for every call to setupPrimary
|
||||
assert.strictEqual(configs.length, execs.length);
|
||||
|
||||
assert.strictEqual(configs[0].exec, execs[0]);
|
||||
|
@ -59,7 +59,7 @@ process.on('exit', () => {
|
|||
// Make changes to cluster settings
|
||||
execs.forEach((v, i) => {
|
||||
setTimeout(() => {
|
||||
cluster.setupMaster({ exec: v });
|
||||
cluster.setupPrimary({ exec: v });
|
||||
}, i * 100);
|
||||
});
|
||||
|
|
@ -29,7 +29,7 @@ if (cluster.isWorker) {
|
|||
// Just keep the worker alive
|
||||
process.send(process.argv[2]);
|
||||
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const checks = {
|
||||
args: false,
|
||||
|
@ -40,8 +40,8 @@ if (cluster.isWorker) {
|
|||
const totalWorkers = 2;
|
||||
let settings;
|
||||
|
||||
// Setup master
|
||||
cluster.setupMaster({
|
||||
// Setup primary
|
||||
cluster.setupPrimary({
|
||||
args: ['custom argument'],
|
||||
silent: true
|
||||
});
|
|
@ -25,8 +25,8 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
// Master opens and binds the socket and shares it with the worker.
|
||||
if (cluster.isPrimary) {
|
||||
// Primary opens and binds the socket and shares it with the worker.
|
||||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
// Hog the TCP port so that when the worker tries to bind, it'll fail.
|
||||
const server = net.createServer(common.mustNotCall());
|
||||
|
|
|
@ -39,8 +39,8 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
// Master opens and binds the socket and shares it with the worker.
|
||||
if (cluster.isPrimary) {
|
||||
// Primary opens and binds the socket and shares it with the worker.
|
||||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
cluster.fork().on('exit', common.mustCall(function(exitCode) {
|
||||
assert.strictEqual(exitCode, 0);
|
||||
|
|
|
@ -9,7 +9,7 @@ const net = require('net');
|
|||
const cluster = require('cluster');
|
||||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
let conn, worker2;
|
||||
|
||||
const worker1 = cluster.fork();
|
||||
|
|
|
@ -34,16 +34,16 @@ const MAGIC_EXIT_CODE = 42;
|
|||
const isTestRunner = process.argv[2] !== 'child';
|
||||
|
||||
if (isTestRunner) {
|
||||
const master = fork(__filename, ['child']);
|
||||
master.on('exit', common.mustCall((code) => {
|
||||
const primary = fork(__filename, ['child']);
|
||||
primary.on('exit', common.mustCall((code) => {
|
||||
assert.strictEqual(code, MAGIC_EXIT_CODE);
|
||||
}));
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
process.on('uncaughtException', common.mustCall(() => {
|
||||
process.nextTick(() => process.exit(MAGIC_EXIT_CODE));
|
||||
}));
|
||||
cluster.fork();
|
||||
throw new Error('kill master');
|
||||
throw new Error('kill primary');
|
||||
} else { // worker
|
||||
process.exit();
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ const common = require('../common');
|
|||
const assert = require('assert');
|
||||
const cluster = require('cluster');
|
||||
|
||||
if (!cluster.isMaster) {
|
||||
if (!cluster.isPrimary) {
|
||||
process.exit(42);
|
||||
} else {
|
||||
const worker = cluster.fork();
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
// The goal of this test is to cover the Workers' implementation of
|
||||
// Worker.prototype.destroy. Worker.prototype.destroy is called within
|
||||
// the worker's context: once when the worker is still connected to the
|
||||
// master, and another time when it's not connected to it, so that we cover
|
||||
// primary, and another time when it's not connected to it, so that we cover
|
||||
// both code paths.
|
||||
|
||||
const common = require('../common');
|
||||
|
@ -32,7 +32,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
let worker1, worker2;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
worker1 = cluster.fork();
|
||||
worker2 = cluster.fork();
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ const assert = require('assert');
|
|||
cluster.schedulingPolicy = cluster.SCHED_NONE;
|
||||
|
||||
const server = http.createServer();
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
let worker;
|
||||
|
||||
server.listen(0, common.mustSucceed(() => {
|
||||
|
|
|
@ -34,7 +34,7 @@ if (cluster.isWorker) {
|
|||
process.exit(42);
|
||||
}));
|
||||
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const checks = {
|
||||
cluster: {
|
||||
|
|
|
@ -26,7 +26,7 @@ const cluster = require('cluster');
|
|||
|
||||
const OK = 2;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
|
||||
const worker = cluster.fork();
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
'use strict';
|
||||
// test-cluster-worker-exit.js
|
||||
// verifies that, when a child process exits (by calling `process.exit(code)`)
|
||||
// - the parent receives the proper events in the proper order, no duplicates
|
||||
// - the primary receives the proper events in the proper order, no duplicates
|
||||
// - the exitCode and signalCode are correct in the 'exit' event
|
||||
// - the worker.exitedAfterDisconnect flag, and worker.state are correct
|
||||
// - the worker process actually goes away
|
||||
|
@ -42,7 +42,7 @@ if (cluster.isWorker) {
|
|||
}));
|
||||
server.listen(0, '127.0.0.1');
|
||||
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const expected_results = {
|
||||
cluster_emitDisconnect: [1, "the cluster did not emit 'disconnect'"],
|
||||
|
|
|
@ -29,7 +29,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const msg = 'foo';
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
|
||||
worker.on('message', common.mustCall((message) => {
|
||||
|
|
|
@ -3,7 +3,7 @@ const common = require('../common');
|
|||
const cluster = require('cluster');
|
||||
const assert = require('assert');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
|
||||
assert.strictEqual(worker.isConnected(), true);
|
||||
|
|
|
@ -3,7 +3,7 @@ require('../common');
|
|||
const cluster = require('cluster');
|
||||
const assert = require('assert');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker = cluster.fork();
|
||||
let workerDead = worker.isDead();
|
||||
assert.ok(!workerDead,
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
'use strict';
|
||||
// test-cluster-worker-kill.js
|
||||
// verifies that, when a child process is killed (we use SIGKILL)
|
||||
// - the parent receives the proper events in the proper order, no duplicates
|
||||
// - the primary receives the proper events in the proper order, no duplicates
|
||||
// - the exitCode and signalCode are correct in the 'exit' event
|
||||
// - the worker.exitedAfterDisconnect flag, and worker.state are correct
|
||||
// - the worker process actually goes away
|
||||
|
@ -38,7 +38,7 @@ if (cluster.isWorker) {
|
|||
server.once('listening', common.mustCall(() => { }));
|
||||
server.listen(0, '127.0.0.1');
|
||||
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
|
||||
const KILL_SIGNAL = 'SIGKILL';
|
||||
const expected_results = {
|
||||
|
|
|
@ -40,7 +40,7 @@ let server;
|
|||
// 3 wait to confirm it did not exit
|
||||
// 4 destroy connection
|
||||
// 5 confirm it does exit
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
server = net.createServer(function(conn) {
|
||||
server.close();
|
||||
worker.disconnect();
|
||||
|
|
|
@ -28,7 +28,7 @@ if (cluster.isWorker) {
|
|||
assert(serverClosed);
|
||||
clearInterval(keepOpen);
|
||||
});
|
||||
} else if (cluster.isMaster) {
|
||||
} else if (cluster.isPrimary) {
|
||||
// start worker
|
||||
const worker = cluster.fork();
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ const dgram = require('dgram');
|
|||
const { internalBinding } = require('internal/test/binding');
|
||||
const { UV_UNKNOWN } = internalBinding('uv');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork();
|
||||
} else {
|
||||
// When the socket attempts to bind, it requests a handle from the cluster.
|
||||
|
|
|
@ -7,7 +7,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const dgram = require('dgram');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
cluster.fork();
|
||||
} else {
|
||||
// When the socket attempts to bind, it requests a handle from the cluster.
|
||||
|
|
|
@ -30,16 +30,16 @@ const dgram = require('dgram');
|
|||
// number causes all sockets bound to that number to share a port.
|
||||
//
|
||||
// The 2 workers that call bind() will share a port, the two workers that do
|
||||
// not will not share a port, so master will see 3 unique source ports.
|
||||
// not will not share a port, so primary will see 3 unique source ports.
|
||||
|
||||
// Note that on Windows, clustered dgram is not supported. Since explicit
|
||||
// binding causes the dgram to be clustered, don't fork the workers that bind.
|
||||
// This is a useful test, still, because it demonstrates that by avoiding
|
||||
// clustering, client (ephemeral, implicitly bound) dgram sockets become
|
||||
// supported while using cluster, though servers still cause the master to error
|
||||
// with ENOTSUP.
|
||||
// supported while using cluster, though servers still cause the primary
|
||||
// to error with ENOTSUP.
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
let messages = 0;
|
||||
const ports = {};
|
||||
const pids = [];
|
||||
|
|
|
@ -13,7 +13,7 @@ function checkForInspectSupport(flag) {
|
|||
const numWorkers = 2;
|
||||
process.env.NODE_OPTIONS = flag;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
for (let i = 0; i < numWorkers; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
|
|
@ -28,11 +28,11 @@ function serialFork() {
|
|||
});
|
||||
}
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
Promise.all([serialFork(), serialFork(), serialFork()])
|
||||
.then(common.mustCall((ports) => {
|
||||
ports.splice(0, 0, process.debugPort);
|
||||
// 4 = [master, worker1, worker2, worker3].length()
|
||||
// 4 = [primary, worker1, worker2, worker3].length()
|
||||
assert.strictEqual(ports.length, 4);
|
||||
assert(ports.every((port) => port > 0));
|
||||
assert(ports.every((port) => port < 65536));
|
||||
|
|
|
@ -34,10 +34,10 @@ console.error('Cluster listen fd test', process.argv[2] || 'runner');
|
|||
// Process relationship is:
|
||||
//
|
||||
// parent: the test main script
|
||||
// -> master: the cluster master
|
||||
// -> primary: the cluster primary
|
||||
// -> worker: the cluster worker
|
||||
switch (process.argv[2]) {
|
||||
case 'master': return master();
|
||||
case 'primary': return primary();
|
||||
case 'worker': return worker();
|
||||
}
|
||||
|
||||
|
@ -88,35 +88,35 @@ function test(cb) {
|
|||
console.error(`server listening on ${port}`);
|
||||
|
||||
const spawn = require('child_process').spawn;
|
||||
const master = spawn(process.execPath, [__filename, 'master'], {
|
||||
const primary = spawn(process.execPath, [__filename, 'primary'], {
|
||||
stdio: [ 0, 'pipe', 2, server._handle, 'ipc' ],
|
||||
detached: true
|
||||
});
|
||||
|
||||
// Now close the parent, so that the master is the only thing
|
||||
// Now close the parent, so that the primary is the only thing
|
||||
// referencing that handle. Note that connections will still
|
||||
// be accepted, because the master has the fd open.
|
||||
// be accepted, because the primary has the fd open.
|
||||
server.close();
|
||||
|
||||
master.on('exit', function(code) {
|
||||
console.error('master exited', code);
|
||||
primary.on('exit', function(code) {
|
||||
console.error('primary exited', code);
|
||||
});
|
||||
|
||||
master.on('close', function() {
|
||||
console.error('master closed');
|
||||
primary.on('close', function() {
|
||||
console.error('primary closed');
|
||||
});
|
||||
console.error('master spawned');
|
||||
master.on('message', function(msg) {
|
||||
console.error('primary spawned');
|
||||
primary.on('message', function(msg) {
|
||||
if (msg === 'started worker') {
|
||||
cb(master, port);
|
||||
cb(primary, port);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function master() {
|
||||
console.error('in master, spawning worker');
|
||||
cluster.setupMaster({
|
||||
function primary() {
|
||||
console.error('in primary, spawning worker');
|
||||
cluster.setupPrimary({
|
||||
args: [ 'worker' ]
|
||||
});
|
||||
const worker = cluster.fork();
|
||||
|
@ -128,7 +128,7 @@ function master() {
|
|||
// Prevent outliving our parent process in case it is abnormally killed -
|
||||
// under normal conditions our parent kills this process before exiting.
|
||||
process.on('disconnect', function() {
|
||||
console.error('master exit on disconnect');
|
||||
console.error('primary exit on disconnect');
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const net = require('net');
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const worker1 = cluster.fork();
|
||||
|
||||
worker1.on('message', function(port1) {
|
||||
|
|
|
@ -26,7 +26,7 @@ function test(sock, readable, writable) {
|
|||
assert.strictEqual(socket.writable, writable);
|
||||
}
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
test(undefined, true, true);
|
||||
|
||||
const server = net.createServer(common.mustCall((socket) => {
|
||||
|
@ -48,7 +48,7 @@ if (cluster.isMaster) {
|
|||
test(socket, true, true);
|
||||
}));
|
||||
|
||||
cluster.setupMaster({
|
||||
cluster.setupPrimary({
|
||||
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ const assert = require('assert');
|
|||
const spawn = require('child_process').spawn;
|
||||
|
||||
if (process.argv[2] !== '--do-test') {
|
||||
// We are the master, fork a child so we can verify it exits with correct
|
||||
// We are the primary, fork a child so we can verify it exits with correct
|
||||
// status.
|
||||
process.env.DOTEST = 'y';
|
||||
const child = spawn(process.execPath, [__filename, '--do-test']);
|
||||
|
|
|
@ -32,7 +32,7 @@ const fixtures = require('../common/fixtures');
|
|||
const workerCount = 4;
|
||||
const expectedReqCount = 16;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
let reusedCount = 0;
|
||||
let reqCount = 0;
|
||||
let lastSession = null;
|
||||
|
@ -40,7 +40,8 @@ if (cluster.isMaster) {
|
|||
let workerPort = null;
|
||||
|
||||
function shoot() {
|
||||
console.error('[master] connecting', workerPort, 'session?', !!lastSession);
|
||||
console.error('[primary] connecting',
|
||||
workerPort, 'session?', !!lastSession);
|
||||
const c = tls.connect(workerPort, {
|
||||
session: lastSession,
|
||||
rejectUnauthorized: false
|
||||
|
@ -69,7 +70,7 @@ if (cluster.isMaster) {
|
|||
function fork() {
|
||||
const worker = cluster.fork();
|
||||
worker.on('message', ({ msg, port }) => {
|
||||
console.error('[master] got %j', msg);
|
||||
console.error('[primary] got %j', msg);
|
||||
if (msg === 'reused') {
|
||||
++reusedCount;
|
||||
} else if (msg === 'listening' && !shootOnce) {
|
||||
|
@ -80,7 +81,7 @@ if (cluster.isMaster) {
|
|||
});
|
||||
|
||||
worker.on('exit', () => {
|
||||
console.error('[master] worker died');
|
||||
console.error('[primary] worker died');
|
||||
});
|
||||
}
|
||||
for (let i = 0; i < workerCount; i++) {
|
||||
|
|
|
@ -9,10 +9,10 @@ const assert = require('assert');
|
|||
const cluster = require('cluster');
|
||||
const debuggerPort = common.PORT;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
function test(execArgv) {
|
||||
|
||||
cluster.setupMaster({
|
||||
cluster.setupPrimary({
|
||||
execArgv: execArgv,
|
||||
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
|
||||
});
|
||||
|
|
|
@ -14,7 +14,7 @@ cluster.schedulingPolicy = cluster.SCHED_NONE;
|
|||
const host = '::';
|
||||
const WORKER_ACCOUNT = 3;
|
||||
|
||||
if (cluster.isMaster) {
|
||||
if (cluster.isPrimary) {
|
||||
const workers = [];
|
||||
|
||||
for (let i = 0; i < WORKER_ACCOUNT; i += 1) {
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue