-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathwait_kill_nodes.js
executable file
·94 lines (83 loc) · 3.17 KB
/
wait_kill_nodes.js
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
#!/usr/bin/env node
// Usage:
// - Start rtc_server.js first:
// ./rtc_server.js --port 8000 --home chat.html
//
// - Now run the test using the rtc_server listen address and the
// number of nodes:
// node test/wait_kill_nodes.js 10.0.01:8001 3 1
var getIP = require('twst').getIP,
RtcTwst = require('./rtctwst').RtcTwst,
port = 9000,
rtc_address = process.argv[2],
clientCount = process.argv.length >= 4 ? parseInt(process.argv[3]) : 1,
killCount = (process.argv.length >= 5) ? parseInt(process.argv[4]) : parseInt((clientCount-1)/2, 10),
timeout = (clientCount*20)*1000,
channel = Math.round(Math.random()*100000),
url = 'http://' + rtc_address +
'/chat.html?channel=' + channel +
'&console_logging=true' +
'&twst_address=' + getIP() + ':' + port + '&paused=1',
rtwst = null;
if (killCount > parseInt((clientCount-1)/2, 10)) {
console.log('Kill count must be less than half of client count');
process.exit(2);
}
rtwst = new RtcTwst({port: port,
startPages: true,
url: url,
prefix: 'p',
timeout: timeout,
clientCount: clientCount,
pagesCallback: delay_do_start});
function delay_do_start() {
console.log('All clients started, delaying for 5 seconds before starting cluster');
setTimeout(do_start, 5000);
}
function do_start() {
rtwst.broadcast('startChat()');
rtwst.wait_cluster_up(timeout, function(status, nodes, elapsed) {
if (status) {
console.log('Cluster is up after ' + elapsed + 'ms');
console.log('Delaying for 3 seconds before killing node(s)');
setTimeout(do_kill, 3000);
} else {
console.log('Cluster failed to come up after ' +
elapsed + 'ms');
rtwst.cleanup_exit(1);
}
});
}
function do_kill() {
console.log('Removing ' + killCount + ' nodes/pages (including the leader)');
rtwst.get_leader_idx(2000, function(status, leader_idx) {
if (!status) {
console.log('Could not determine cluster leader');
rtwst.cleanup_exit(1);
return;
}
console.log('Removing page index ' + leader_idx + ' (current leader)');
rtwst.remove(leader_idx);
for (var j=0; j<killCount-1; j++) {
var cids = Object.keys(rtwst.clients),
kill_id = cids[parseInt(Math.random()*cids.length, 10)];
console.log('Removing page index ' + kill_id);
rtwst.remove(kill_id);
}
console.log('Waiting for cluster to stabilize');
rtwst.wait_cluster_up(timeout, function(status, nodes, elapsed) {
if (status) {
console.log('Cluster recovered after ' + elapsed + 'ms');
rtwst.cleanup_exit(0);
} else {
console.log('Cluster failed to recover after ' + elapsed + 'ms');
rtwst.cleanup_exit(1);
}
});
});
}
setTimeout(function() {
console.log("timeout waiting for clients");
rtwst.broadcast('window.callPhantom("QUIT")');
rtwst.cleanup_exit(1);
}, timeout);