nodejs / node-v0.x-archive

Moved to https://github.com/nodejs/node
34.45k stars 7.31k forks source link

cluster - improve module cluster to autocluster #3308

Closed qxfusion closed 12 years ago

qxfusion commented 12 years ago

existing implementation of cluster required manually manage and control process clusterisation. By default it's feature isn't required for all. Sometimes I'm want only say only "cluster" - and node cluster automatically. (additional note below)

adding module autocluster

require("autocluster")

Executing strategy: 1) Load autocluster module 2) [MODULE] Getting environment context and command line 3) [MODULE] fork (spawn) child worker in amount of ((CPU count) * 4) 4) [MODULE] adding every child worker from main process saved ENV and command line 5) [MODULE] adding every child worker in command line special flag --cluster-instance={$UUID$} 6) [CLIENT] if autocluster detect --cluster-instance flag - forked will been stopped and will be launch of main source code otherwise - fork new process

API

++ string GLOBAL.clusterInstance /* UUID of instance */
++ event clusterDown(string) /* UUID of down cluster worker */
++ event clusterUp(string) /* UUID of up cluster worker */

Proposal On existing implementation

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

Future:

require('autocluster');
var http = require('http');

  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);

NOTE: I'm think about Linux process clusterisation and failover, but I'm want this feature in node :) Now implementation in progress.

Sannis commented 12 years ago

I think you still miss https://github.com/joyent/node/wiki/node-core-vs-userland. Small part of it, that amke most value:

If you believe that something really just needs to be part of node's core library set, you should still build it as a module! It's much more likely to be pulled into node-core if people have a chance to see your great ideas in action, and if its core principles are iterated and polished and tested with real-world use.

Changing functionality that is included in node-core is very costly. We do it sometimes, but it's not easy, and carries a high risk of regressions. Better to experiment outside, and then pull it into node-core once it's stable. Once it's usable as a userland package, you may even find that it's less essential to node-core than you first thought.

qxfusion commented 12 years ago

@Sannis yes you rights. Now I local testing this solution, I'm create highload application but this feature is very important. NOTE: about regression - this separate module (yes), don't modify core structures (yes), solve real problem (yes) (trouble - V8 cannot execute more 1 thread per process on multiple CPU with HT support) P.S. about node-core-vs-userland - I'm read it before.

isaacs commented 12 years ago

No, we are not going to make cluster even more magical. As it is, it's arguably too magical for node core. If scalable web servers wasn't the reason for node's existence, or if sharing a server FD wasn't very hard to get right, we wouldn't have it in at all. (Same with HTTP serving, actually.)

Go build this as a userland module. https://github.com/isaacs/cluster-master is pretty close to what you're describing.