Skip to content

Instantly share code, notes, and snippets.

@alexeyten
Last active October 27, 2015 17:59
Show Gist options
  • Save alexeyten/45fb4319e4158feb793a to your computer and use it in GitHub Desktop.
Save alexeyten/45fb4319e4158feb793a to your computer and use it in GitHub Desktop.
cluster scheduler bug?

I've tested this code with Node.js 4.2.1

This server just response with OK and restart workers after 200-300 completed requests. With default cluster scheduler (which is SCHED_RR) server eventually loses some requests and ab stops with timeout error. When I uncomment line 5 and use SCHED_NONE scheduler, then server works fine.

My Node.js version:

$ apt-cache policy nodejs
nodejs:
  Installed: 4.2.1-2nodesource1~wily1
  Candidate: 4.2.1-2nodesource1~wily1
  Version table:
 *** 4.2.1-2nodesource1~wily1 0
        500 https://deb.nodesource.com/node_4.x/ wily/main amd64 Packages
        100 /var/lib/dpkg/status
     0.10.25~dfsg2-2ubuntu1 0
        500 http://ru.archive.ubuntu.com/ubuntu/ wily/universe amd64 Packages
var cluster = require('cluster');
var http = require('http');
//cluster.schedulingPolicy = cluster.SCHED_RR; // default for 0.12
//cluster.schedulingPolicy = cluster.SCHED_NONE; // default for 0.10
cluster.on('fork', function(w) {
console.log('fork', w.id);
var isWorkerTired = false;
w.on('message', function(msg) {
if (!isWorkerTired && msg === 'tired') {
isWorkerTired = true;
console.log('tired', w.id);
var newWorker = cluster.fork();
newWorker.on('listening', function() {
w.disconnect();
});
}
});
});
cluster.on('exit', function(w) {
console.log('exit', w.id);
});
if (cluster.isWorker) {
var N = (200 + Math.random() * 100)|0;
http.createServer(function(req, res) {
N--;
if (N < 0) {
process.send('tired');
}
res.end('OK\n')
}).listen(1337);
} else {
cluster.fork();
cluster.fork();
cluster.fork();
cluster.fork();
}
$ ab -n 10000 -c 5 http://localhost:1337/
This is ApacheBench, Version 2.3 <$Revision: 1638069 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
apr_pollset_poll: The timeout specified has expired (70007)
Total of 9996 requests completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment