All files / lib/internal/cluster primary.js

95.35% Statements 369/387
93.24% Branches 69/74
100% Functions 17/17
95.35% Lines 369/387

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 38874x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x       74x 74x 74x 74x 74x 74x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x 310x   310x     310x 310x 310x 310x 310x 109x 109x 109x 310x 310x 310x 310x 310x 310x                         310x 74x 74x 74x 74x 74x 310x 310x 310x 74x 261x 261x 261x 261x 261x 261x 261x 261x 261x 38x 38x 14x 14x 10x 10x 14x 14x 38x 24x 24x 24x 24x 24x 32x 32x 32x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 255x 261x 74x 328x 328x 328x 328x 328x 166x 166x 166x 328x 74x 572x 572x 572x 572x 103x 103x 572x 572x 74x 74x 261x 261x 261x 261x 261x 261x 261x 261x 261x 59x 261x 261x 261x 249x 249x 249x 249x 249x 249x 245x 245x 245x 249x 249x 249x 249x 249x 261x 261x 261x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 261x 261x 261x 261x 261x 261x 74x 74x 255x 255x 255x 74x 74x 10x 10x 10x 2x 10x 8x 29x 29x 29x 29x 8x 10x 10x 10x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 666x 666x 666x 666x 666x 666x 666x 666x 74x 252x 252x 252x 252x 252x 74x 104x 104x 104x 104x 74x 118x 118x 118x 118x 115x 115x 115x 115x 115x 115x 60x 60x 60x 60x 60x 3x 3x 3x 3x 3x 3x 60x 60x 60x 60x 60x 60x 60x 25x 60x 35x 35x 60x 60x 60x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 115x 118x 74x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 74x 74x 74x 91x 91x 91x 91x 91x 91x 91x 74x 298x 298x 298x 74x 74x 74x 79x 79x 79x 79x 79x 74x 74x 74x 14x 14x 14x 14x 14x 13x 13x 13x 13x 1x 1x 74x  
'use strict';
 
const {
  ArrayPrototypePush,
  ArrayPrototypeSlice,
  ArrayPrototypeSome,
  ObjectKeys,
  ObjectValues,
  RegExpPrototypeTest,
  SafeMap,
  StringPrototypeStartsWith,
} = primordials;
 
const assert = require('internal/assert');
const { fork } = require('child_process');
const path = require('path');
const EventEmitter = require('events');
const RoundRobinHandle = require('internal/cluster/round_robin_handle');
const SharedHandle = require('internal/cluster/shared_handle');
const Worker = require('internal/cluster/worker');
const { internal, sendHelper } = require('internal/cluster/utils');
const cluster = new EventEmitter();
const intercom = new EventEmitter();
const SCHED_NONE = 1;
const SCHED_RR = 2;
const minPort = 1024;
const maxPort = 65535;
const { validatePort } = require('internal/validators');
 
module.exports = cluster;
 
const handles = new SafeMap();
cluster.isWorker = false;
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;      // Primary distributes connections.
 
let ids = 0;
let debugPortOffset = 1;
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;
else if (schedulingPolicy === 'none')
  schedulingPolicy = SCHED_NONE;
else if (process.platform === 'win32') {
  // Round-robin doesn't perform well on
  // Windows due to the way IOCP is wired up.
  schedulingPolicy = SCHED_NONE;
} else
  schedulingPolicy = SCHED_RR;
 
cluster.schedulingPolicy = schedulingPolicy;
 
cluster.setupPrimary = function(options) {
  const settings = {
    args: ArrayPrototypeSlice(process.argv, 2),
    exec: process.argv[1],
    execArgv: process.execArgv,
    silent: false,
    ...cluster.settings,
    ...options
  };
 
  // Tell V8 to write profile data for each process to a separate file.
  // Without --logfile=v8-%p.log, everything ends up in a single, unusable
  // file. (Unusable because what V8 logs are memory addresses and each
  // process has its own memory mappings.)
  if (ArrayPrototypeSome(settings.execArgv,
                         (s) => StringPrototypeStartsWith(s, '--prof')) &&
      !ArrayPrototypeSome(settings.execArgv,
                          (s) => StringPrototypeStartsWith(s, '--logfile='))) {
    settings.execArgv = [...settings.execArgv, '--logfile=v8-%p.log'];
  }
 
  cluster.settings = settings;
 
  if (initialized === true)
    return process.nextTick(setupSettingsNT, settings);
 
  initialized = true;
  schedulingPolicy = cluster.schedulingPolicy;  // Freeze policy.
  assert(schedulingPolicy === SCHED_NONE || schedulingPolicy === SCHED_RR,
         `Bad cluster.schedulingPolicy: ${schedulingPolicy}`);
 
  process.nextTick(setupSettingsNT, settings);
 
  process.on('internalMessage', (message) => {
    if (message.cmd !== 'NODE_DEBUG_ENABLED')
      return;

    for (const worker of ObjectValues(cluster.workers)) {
      if (worker.state === 'online' || worker.state === 'listening') {
        process._debugProcess(worker.process.pid);
      } else {
        worker.once('online', function() {
          process._debugProcess(this.process.pid);
        });
      }
    }
  });
};
 
// Deprecated alias must be same as setupPrimary
cluster.setupMaster = cluster.setupPrimary;
 
function setupSettingsNT(settings) {
  cluster.emit('setup', settings);
}
 
function createWorkerProcess(id, env) {
  const workerEnv = { ...process.env, ...env, NODE_UNIQUE_ID: `${id}` };
  const execArgv = [...cluster.settings.execArgv];
  const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
  const nodeOptions = process.env.NODE_OPTIONS || '';
 
  if (ArrayPrototypeSome(execArgv,
                         (arg) => RegExpPrototypeTest(debugArgRegex, arg)) ||
      RegExpPrototypeTest(debugArgRegex, nodeOptions)) {
    let inspectPort;
    if ('inspectPort' in cluster.settings) {
      if (typeof cluster.settings.inspectPort === 'function')
        inspectPort = cluster.settings.inspectPort();
      else
        inspectPort = cluster.settings.inspectPort;
 
      validatePort(inspectPort);
    } else {
      inspectPort = process.debugPort + debugPortOffset;
      if (inspectPort > maxPort)
        inspectPort = inspectPort - maxPort + minPort - 1;
      debugPortOffset++;
    }
 
    ArrayPrototypePush(execArgv, `--inspect-port=${inspectPort}`);
  }
 
  return fork(cluster.settings.exec, cluster.settings.args, {
    cwd: cluster.settings.cwd,
    env: workerEnv,
    serialization: cluster.settings.serialization,
    silent: cluster.settings.silent,
    windowsHide: cluster.settings.windowsHide,
    execArgv: execArgv,
    stdio: cluster.settings.stdio,
    gid: cluster.settings.gid,
    uid: cluster.settings.uid
  });
}
 
function removeWorker(worker) {
  assert(worker);
  delete cluster.workers[worker.id];
 
  if (ObjectKeys(cluster.workers).length === 0) {
    assert(handles.size === 0, 'Resource leak detected.');
    intercom.emit('disconnect');
  }
}
 
function removeHandlesForWorker(worker) {
  assert(worker);
 
  handles.forEach((handle, key) => {
    if (handle.remove(worker))
      handles.delete(key);
  });
}
 
cluster.fork = function(env) {
  cluster.setupPrimary();
  const id = ++ids;
  const workerProcess = createWorkerProcess(id, env);
  const worker = new Worker({
    id: id,
    process: workerProcess
  });
 
  worker.on('message', function(message, handle) {
    cluster.emit('message', this, message, handle);
  });
 
  worker.process.once('exit', (exitCode, signalCode) => {
    /*
     * Remove the worker from the workers list only
     * if it has disconnected, otherwise we might
     * still want to access it.
     */
    if (!worker.isConnected()) {
      removeHandlesForWorker(worker);
      removeWorker(worker);
    }
 
    worker.exitedAfterDisconnect = !!worker.exitedAfterDisconnect;
    worker.state = 'dead';
    worker.emit('exit', exitCode, signalCode);
    cluster.emit('exit', worker, exitCode, signalCode);
  });
 
  worker.process.once('disconnect', () => {
    /*
     * Now is a good time to remove the handles
     * associated with this worker because it is
     * not connected to the primary anymore.
     */
    removeHandlesForWorker(worker);
 
    /*
     * Remove the worker from the workers list only
     * if its process has exited. Otherwise, we might
     * still want to access it.
     */
    if (worker.isDead())
      removeWorker(worker);
 
    worker.exitedAfterDisconnect = !!worker.exitedAfterDisconnect;
    worker.state = 'disconnected';
    worker.emit('disconnect');
    cluster.emit('disconnect', worker);
  });
 
  worker.process.on('internalMessage', internal(worker, onmessage));
  process.nextTick(emitForkNT, worker);
  cluster.workers[worker.id] = worker;
  return worker;
};
 
function emitForkNT(worker) {
  cluster.emit('fork', worker);
}
 
cluster.disconnect = function(cb) {
  const workers = ObjectKeys(cluster.workers);
 
  if (workers.length === 0) {
    process.nextTick(() => intercom.emit('disconnect'));
  } else {
    for (const worker of ObjectValues(cluster.workers)) {
      if (worker.isConnected()) {
        worker.disconnect();
      }
    }
  }
 
  if (typeof cb === 'function')
    intercom.once('disconnect', cb);
};
 
const methodMessageMapping = {
  close,
  exitedAfterDisconnect,
  listening,
  online,
  queryServer,
};
 
function onmessage(message, handle) {
  const worker = this;
 
  const fn = methodMessageMapping[message.act];
 
  if (typeof fn === 'function')
    fn(worker, message);
}
 
function online(worker) {
  worker.state = 'online';
  worker.emit('online');
  cluster.emit('online', worker);
}
 
function exitedAfterDisconnect(worker, message) {
  worker.exitedAfterDisconnect = true;
  send(worker, { ack: message.seq });
}
 
function queryServer(worker, message) {
  // Stop processing if worker already disconnecting
  if (worker.exitedAfterDisconnect)
    return;
 
  const key = `${message.address}:${message.port}:${message.addressType}:` +
              `${message.fd}:${message.index}`;
  let handle = handles.get(key);
 
  if (handle === undefined) {
    let address = message.address;
 
    // Find shortest path for unix sockets because of the ~100 byte limit
    if (message.port < 0 && typeof address === 'string' &&
        process.platform !== 'win32') {
 
      address = path.relative(process.cwd(), address);
 
      if (message.address.length < address.length)
        address = message.address;
    }
 
    // UDP is exempt from round-robin connection balancing for what should
    // be obvious reasons: it's connectionless. There is nothing to send to
    // the workers except raw datagrams and that's pointless.
    if (schedulingPolicy !== SCHED_RR ||
        message.addressType === 'udp4' ||
        message.addressType === 'udp6') {
      handle = new SharedHandle(key, address, message);
    } else {
      handle = new RoundRobinHandle(key, address, message);
    }
 
    handles.set(key, handle);
  }
 
  if (!handle.data)
    handle.data = message.data;
 
  // Set custom server data
  handle.add(worker, (errno, reply, handle) => {
    const { data } = handles.get(key);
 
    if (errno)
      handles.delete(key);  // Gives other workers a chance to retry.
 
    send(worker, {
      errno,
      key,
      ack: message.seq,
      data,
      ...reply
    }, handle);
  });
}
 
function listening(worker, message) {
  const info = {
    addressType: message.addressType,
    address: message.address,
    port: message.port,
    fd: message.fd
  };
 
  worker.state = 'listening';
  worker.emit('listening', info);
  cluster.emit('listening', worker, info);
}
 
// Server in worker is closing, remove from list. The handle may have been
// removed by a prior call to removeHandlesForWorker() so guard against that.
function close(worker, message) {
  const key = message.key;
  const handle = handles.get(key);
 
  if (handle && handle.remove(worker))
    handles.delete(key);
}
 
function send(worker, message, handle, cb) {
  return sendHelper(worker.process, message, handle, cb);
}
 
// Extend generic Worker with methods specific to the primary process.
Worker.prototype.disconnect = function() {
  this.exitedAfterDisconnect = true;
  send(this, { act: 'disconnect' });
  removeHandlesForWorker(this);
  removeWorker(this);
  return this;
};
 
Worker.prototype.destroy = function(signo) {
  const proc = this.process;
 
  signo = signo || 'SIGTERM';
 
  if (this.isConnected()) {
    this.once('disconnect', () => proc.kill(signo));
    this.disconnect();
    return;
  }
 
  proc.kill(signo);
};