| 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 |
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
556
278
278
278
278
278
278
278
278
278
278
278
278
278
278
1
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
278
1
71
1
278
278
278
278
278
1
278
278
278
278
278
278
278
278
278
278
278
1
278
278
1
278
278
278
278
278
278
278
278
1
349
349
349
349
349
349
349
1
349
349
349
2
347
1
349
349
349
18
331
1
349
349
349
1
349
349
349
1
349
349
349
1
349
349
349
349
349
349
20
20
20
20
| // Copyright (c) 2015 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.
'use strict';
var assert = require('assert');
var http = require('http');
// TODO use better module. This sometimes fails when you
// move around and change wifi networks.
var myLocalIp = require('my-local-ip');
var os = require('os');
var RingPop = require('ringpop');
var process = require('process');
var uncaught = require('uncaught-exception');
var TChannel = require('tchannel');
var TChannelAsJSON = require('tchannel/as/json');
var HyperbahnHandler = require('tchannel/hyperbahn/handler');
var HyperbahnEgressNodes = require('tchannel/hyperbahn/egress-nodes');
var ServiceProxy = require('tchannel/hyperbahn/service_proxy.js');
var CountedReadySignal = require('ready-signal/counted');
var fs = require('fs');
var ProcessReporter = require('process-reporter');
var NullStatsd = require('uber-statsd-client/null');
var createLogger = require('./logger.js');
var DualStatsd = require('./dual-statsd.js');
var createRepl = require('./repl.js');
var HeapDumper = require('./heap-dumper.js');
var RemoteConfig = require('./remote-config.js');
module.exports = ApplicationClients;
function ApplicationClients(options) {
/*eslint max-statements: [2, 40] */
if (!(this instanceof ApplicationClients)) {
return new ApplicationClients(options);
}
var self = this;
var config = options.config;
// We need to move away from myLocalIp(); this fails in weird
// ways when moving around and changing wifi networks.
// host & port are internal fields since they are just used
// in bootstrap and are not actually client objects.
self._host = config.get('tchannel.host') || myLocalIp();
self._port = options.argv.port;
self._controlPort = options.argv.controlPort;
self._bootFile = options.argv.bootstrapFile !== undefined ?
JSON.parse(options.argv.bootstrapFile) :
config.get('hyperbahn.ringpop.bootstrapFile');
var statsOptions = config.get('clients.uber-statsd-client');
self.statsd = options.seedClients.statsd || (
(statsOptions && statsOptions.host && statsOptions.port) ?
DualStatsd({
host: statsOptions.host,
port: statsOptions.port,
project: config.get('info.project'),
processTitle: options.processTitle
}) :
NullStatsd()
);
self.logger = options.seedClients.logger ||
createLogger({
team: config.get('info.team'),
project: config.get('info.project'),
kafka: config.get('clients.logtron.kafka'),
logFile: config.get('clients.logtron.logFile'),
console: config.get('clients.logtron.console'),
sentry: config.get('clients.logtron.sentry'),
statsd: self.statsd
});
/*eslint no-process-env: 0*/
var uncaughtTimeouts = config.get('clients.uncaught-exception.timeouts');
self.onError = uncaught({
logger: self.logger,
statsd: self.statsd,
statsdKey: 'uncaught-exception',
prefix: [
config.get('info.project'),
process.env.NODE_ENV,
os.hostname().split('.')[0]
].join('.') + ' ',
backupFile: config.get('clients.uncaught-exception.file'),
loggerTimeout: uncaughtTimeouts.loggerTimeout,
statsdTimeout: uncaughtTimeouts.statsdTimeout,
statsdWaitPeriod: uncaughtTimeouts.statsdWaitPeriod
});
self.processReporter = ProcessReporter({
statsd: self.statsd
});
// This is dead code; really really soon.
// Need HTTP server or I get fucking paged at 5am
// Fix the nagios LOL.
self._controlServer = http.createServer(onRequest);
function onRequest(req, res) {
res.end('OK');
}
// Store the tchannel object with its peers on clients
// Also store a json sender and a raw sender
self.tchannel = TChannel({
statTags: {
app: 'autobahn',
host: os.hostname()
},
emitConnectionMetrics: false,
connectionStalePeriod: 1.5 * 1000,
trace: false,
logger: self.logger,
statsd: self.statsd
});
self.autobahnHostPortList = self.loadHostList();
self.tchannelJSON = TChannelAsJSON({
logger: self.logger
});
self.repl = createRepl();
self.autobahnChannel = self.tchannel.makeSubChannel({
serviceName: 'autobahn'
});
self.ringpopChannel = self.tchannel.makeSubChannel({
trace: false,
serviceName: 'ringpop'
});
var ringpopTimeouts = config.get('hyperbahn.ringpop.timeouts');
self.ringpop = RingPop({
app: config.get('info.project'),
hostPort: self._host + ':' + self._port,
channel: self.ringpopChannel,
logger: self.logger,
statsd: self.statsd,
pingReqTimeout: ringpopTimeouts.pingReqTimeout,
pingTimeout: ringpopTimeouts.pingTimeout,
joinTimeout: ringpopTimeouts.joinTimeout
});
self.egressNodes = HyperbahnEgressNodes({
ringpop: self.ringpop,
defaultKValue: 10
});
var hyperbahnTimeouts = config.get('hyperbahn.timeouts');
self.hyperbahnChannel = self.tchannel.makeSubChannel({
serviceName: 'hyperbahn',
trace: false
});
self.hyperbahnHandler = HyperbahnHandler({
channel: self.hyperbahnChannel,
ringpop: self.ringpop,
egressNodes: self.egressNodes,
callerName: 'autobahn',
relayAdTimeout: hyperbahnTimeouts.relayAdTimeout
});
self.hyperbahnChannel.handler = self.hyperbahnHandler;
// Circuit health monitor and control
var circuitsConfig = config.get('hyperbahn.circuits');
var serviceProxyOpts = {
channel: self.tchannel,
logger: self.logger,
statsd: self.statsd,
egressNodes: self.egressNodes,
servicePurgePeriod: options.servicePurgePeriod,
serviceReqDefaults: options.serviceReqDefaults,
rateLimiterEnabled: false,
rateLimiterBuckets: options.rateLimiterBuckets,
circuitsConfig: circuitsConfig
};
self.serviceProxy = ServiceProxy(serviceProxyOpts);
self.tchannel.handler = self.serviceProxy;
self.heapDumper = HeapDumper({
heapFolder: config.get('clients.heapsnapshot').folder,
logger: self.logger
});
self.remoteConfig = RemoteConfig({
configFile: config.get('clients.remote-config.file'),
pollInterval: config.get('clients.remote-config').pollInterval,
logger: self.logger,
logError: config.get('clients.remote-config.logError')
});
self.remoteConfig.on('update', onRemoteConfigUpdate);
// initlialize to default
self.onRemoteConfigUpdate();
self.remoteConfig.loadSync();
self.remoteConfig.startPolling();
function onRemoteConfigUpdate() {
self.onRemoteConfigUpdate();
}
}
ApplicationClients.prototype.loadHostList = function loadHostList() {
var self = this;
var bootFile = self._bootFile;
var autobahnHostPortList;
Eif (Array.isArray(bootFile)) {
return bootFile;
}
try {
// load sync because startup
autobahnHostPortList = JSON.parse(fs.readFileSync(bootFile, 'utf8'));
} catch (e) {
return null;
}
return autobahnHostPortList;
};
ApplicationClients.prototype.bootstrap =
function bootstrap(cb) {
var self = this;
assert(typeof cb === 'function', 'cb required');
var listenReady = CountedReadySignal(3);
listenReady(onListen);
self.processReporter.bootstrap();
self.ringpop.setupChannel();
self.tchannel.on('listening', listenReady.signal);
self.tchannel.listen(self._port, self._host);
self.repl.once('listening', listenReady.signal);
self.repl.start();
self._controlServer.listen(self._controlPort, listenReady.signal);
function onListen() {
Eif (self.autobahnHostPortList) {
self.ringpop.bootstrap(self.autobahnHostPortList, cb);
} else {
process.nextTick(cb);
}
}
};
ApplicationClients.prototype.destroy = function destroy() {
var self = this;
self.serviceProxy.destroy();
self.remoteConfig.destroy();
self.ringpop.destroy();
Iif (!self.tchannel.destroyed) {
self.tchannel.close();
}
self.processReporter.destroy();
self.repl.close();
self._controlServer.close();
};
ApplicationClients.prototype.onRemoteConfigUpdate = function onRemoteConfigUpdate() {
var self = this;
self.updateCircuitsEnabled();
self.updateRateLimitingEnabled();
self.updateTotalRpsLimit();
self.updateExemptServices();
self.updateRpsLimitForServiceName();
self.updateKValues();
};
ApplicationClients.prototype.updateCircuitsEnabled = function updateCircuitsEnabled() {
var self = this;
var enabled = self.remoteConfig.get('circuits.enabled', false);
if (enabled) {
self.serviceProxy.enableCircuits();
} else {
self.serviceProxy.disableCircuits();
}
};
ApplicationClients.prototype.updateRateLimitingEnabled = function updateRateLimitingEnabled() {
var self = this;
var enabled = self.remoteConfig.get('rateLimiting.enabled', false);
if (enabled) {
self.serviceProxy.enableRateLimiter();
} else {
self.serviceProxy.disableRateLimiter();
}
};
ApplicationClients.prototype.updateTotalRpsLimit = function updateTotalRpsLimit() {
var self = this;
var limit = self.remoteConfig.get('rateLimiting.totalRpsLimit', 1200);
self.serviceProxy.rateLimiter.updateTotalLimit(limit);
};
ApplicationClients.prototype.updateExemptServices = function updateExemptServices() {
var self = this;
var exemptServices = self.remoteConfig.get('rateLimiting.exemptServices', ['autobahn', 'ringpop']);
self.serviceProxy.rateLimiter.updateExemptServices(exemptServices);
};
ApplicationClients.prototype.updateRpsLimitForServiceName = function updateRpsLimitForServiceName() {
var self = this;
var rpsLimitForServiceName = self.remoteConfig.get('rateLimiting.rpsLimitForServiceName', {});
self.serviceProxy.rateLimiter.updateRpsLimitForAllServices(rpsLimitForServiceName);
};
ApplicationClients.prototype.updateKValues = function updateKValues() {
var self = this;
var defaultKValue = self.remoteConfig.get('kValue.default', 10);
self.egressNodes.setDefaultKValue(defaultKValue);
var serviceKValues = self.remoteConfig.get('kValue.services', {});
var keys = Object.keys(serviceKValues);
for (var i = 0; i < keys.length; i++) {
var serviceName = keys[i];
var kValue = serviceKValues[serviceName];
self.egressNodes.setKValueFor(serviceName, kValue);
self.serviceProxy.updateServiceChannels();
}
};
|