This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
main.js
executable file
·230 lines (193 loc) · 7.46 KB
/
main.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
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
#!/usr/bin/env node
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var fs = require('fs');
var path = require('path');
var program = require('commander');
var RingPop = require('./index');
var TChannel = require('tchannel');
function main(args) {
program
.version(require('./package.json').version)
.usage('[options]')
.option('-l, --listen <listen>',
'Host and port on which server listens (also node\'s identity in cluster)')
.option('-h, --hosts <hosts>',
'Seed file of list of hosts to join')
.option('--suspect-period <suspectPeriod>',
'The lifetime of a suspect member in ms. After that the member becomes faulty.',
parseInt10, 5000)
.option('--faulty-period <faultyPeriod>',
'The lifetime of a faulty member in ms. After that the member becomes a tombstone.',
parseInt10, 24*60*60*1000) // 24hours
.option('--tombstone-period <tombstonePeriod>',
'The lifetime of a tombstone member in ms. After that the member is removed from the membership.',
parseInt10, 5000)
.option('--stats-file <stats-file>',
'Enable stats emitting to a file. Stats-file can be a relative or absolute path. '+
'Note: this flag is mutually exclusive with --stats-udp and you need to manually install "uber-statsd-client" to be able to emit stats')
.option('--stats-udp <stats-udp>',
'Enable stats emitting over udp. Destination is in the host-port format (e.g. localhost:8125 or 127.0.0.1:8125) ' +
'Note: this flag is mutually exclusive with --stats-file and you need to manually install "uber-statsd-client" to be able to emit stats',
/^(.+):(\d+)$/)
.parse(args);
var listen = program.listen;
if (!listen) {
console.error('Error: listen arg is required');
program.outputHelp();
process.exit(1);
}
var stats = createStatsClient(program);
var tchannel = new TChannel({
});
var ringpop = new RingPop({
app: 'ringpop',
hostPort: listen,
logger: createLogger('ringpop'),
channel: tchannel.makeSubChannel({
serviceName: 'ringpop',
trace: false
}),
isCrossPlatform: true,
useLatestHash32: false,
stateTimeouts: {
suspect: program.suspectPeriod,
faulty: program.faultyPeriod,
tombstone: program.tombstonePeriod,
},
statsd: stats
});
ringpop.setupChannel();
process.once('SIGTERM', signalHandler(false));
process.once('SIGINT', signalHandler(true));
function signalHandler(interactive) {
return function() {
if (interactive) {
console.error('triggered graceful shutdown. Press Ctrl+C again to force exit.');
process.on('SIGINT', function forceExit() {
console.error('Force exiting...');
process.exit(1);
});
}
ringpop.selfEvict(function afterSelfEvict(err) {
if (err) {
console.error('Failure during selfEvict: ' + err);
process.exit(1);
return;
}
process.exit(0);
});
};
}
var listenParts = listen.split(':');
var port = Number(listenParts[1]);
var host = listenParts[0];
tchannel.listen(port, host, onListening);
function onListening() {
ringpop.bootstrap(program.hosts);
}
}
function createStatsClient(program) {
if (!program.statsUdp && !program.statsFile) {
return null;
}
if (program.statsUdp && program.statsFile) {
console.error("--stats-udp and --stats-file are mutually exclusive.");
console.error("Please specify only one of the two options!");
process.exit(1);
}
var opts = null;
if (program.statsUdp) {
var matchesHostPort = program.statsUdp.match(/^(.+):(\d+)$/);
opts = {
host: matchesHostPort[1],
port: parseInt(matchesHostPort[2])
};
} else if (program.statsFile) {
var file = path.resolve(program.statsFile);
opts = {
// passing in our own 'socket' implementation here so we can write to file instead.
// note: this is non-public api and could change without warning.
_ephemeralSocket: new FileStatsLogger(file)
};
}
var createStatsdClient;
// Wrap the require in a try/catch so we're don't have to add uber-statsd-client
// as a dependency but fail gracefully when not available.
try {
createStatsdClient = require('uber-statsd-client');
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") {
throw e;
}
console.error("To be able to emit stats you need to have uber-statsd-client installed.");
console.error("Please run \"npm install uber-statsd-client\" and try again!");
process.exit(1);
}
return createStatsdClient(opts);
}
function FileStatsLogger(file) {
if (!(this instanceof FileStatsLogger)) {
return new FileStatsLogger(file);
}
this.file = file;
this.stream = null;
this.ensureStream();
}
FileStatsLogger.prototype.ensureStream = function ensureStream() {
if (this.stream) {
return;
}
this.stream = fs.createWriteStream(this.file, {flags: 'a'});
};
FileStatsLogger.prototype.close = function close() {
if (this.stream) {
this.stream.end();
this.stream = null;
}
};
FileStatsLogger.prototype._writeToSocket = function _writeToSocket(data, cb) {
this.ensureStream();
this.stream.write(new Date().toISOString() + ': ' + data + '\n', cb);
};
FileStatsLogger.prototype.send = FileStatsLogger.prototype._writeToSocket;
function parseInt10(str) {
return parseInt(str, 10);
}
function createLogger(name) {
return {
trace: function noop() {},
debug: enrich('debug', 'log'),
info: enrich('info', 'log'),
warn: enrich('warn', 'error'),
error: enrich('error', 'error')
};
function enrich(level, method) {
return function log() {
var args = [].slice.call(arguments);
args[0] = name + ' ' + level + ' ' + args[0];
console[method].apply(console, args);
};
}
}
if (require.main === module) {
main(process.argv);
}
module.exports = main;