-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_handler.js
712 lines (594 loc) · 19.8 KB
/
relay_handler.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// 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 errors = require('./errors');
var v2 = require('./v2');
RelayHandler.RelayRequest = RelayRequest;
module.exports = RelayHandler;
function RelayHandler(channel, circuits) {
var self = this;
self.channel = channel;
self.circuits = circuits || null;
self.logger = self.channel.logger;
}
RelayHandler.prototype.type = 'tchannel.relay-handler';
RelayHandler.prototype.handleLazily = function handleLazily(conn, reqFrame) {
var self = this;
// TODO: provide a by-service-name config hook?
var rereq = new LazyRelayInReq(conn, reqFrame);
var err = rereq.initRead();
if (err) {
rereq.onError(err);
return true;
}
rereq.peer = self.channel.peers.choosePeer(null);
if (!rereq.peer) {
rereq.sendErrorFrame('Declined', 'no peer available for request');
self.logger.warn('no relay peer available', rereq.extendLogInfo({}));
return true;
}
if (self.circuits) {
self.logger.warn('circuit breaking for lazy realying isn\'t implemented', {
serviceName: self.channel.serviceName
});
return false;
}
conn.ops.addInReq(rereq);
rereq.createOutRequest();
return true;
};
RelayHandler.prototype.handleRequest = function handleRequest(req, buildRes) {
var self = this;
if (self.circuits) {
self._monitorRequest(req, buildRes);
} else {
self._handleRequest(req, buildRes);
}
};
RelayHandler.prototype._monitorRequest = function _monitorRequest(req, buildRes) {
var self = this;
// TODO: null-return is error / declined indication... could be clearer
buildRes = self.circuits.monitorRequest(req, buildRes);
if (buildRes) {
self._handleRequest(req, buildRes);
}
};
RelayHandler.prototype._handleRequest = function _handleRequest(req, buildRes) {
var self = this;
// TODO add this back in a performant way ??
// if (rereq) {
// self.logger.error('relay request already exists for incoming request', {
// inReqId: req.id,
// priorInResId: rereq.inres && rereq.inres.id,
// priorOutResId: rereq.outres && rereq.outres.id,
// priorOutReqId: rereq.outreq && rereq.outreq.id
// // TODO more context, like outreq remote addr
// });
// buildRes().sendError(
// 'UnexpectedError', 'request id exists in relay handler'
// );
// return;
// }
req.forwardTrace = true;
var peer = self.channel.peers.choosePeer(null);
if (!peer) {
// TODO: stat
// TODO: allow for customization of this message so hyperbahn can
// augment it with things like "at entry node", "at exit node", etc
buildRes().sendError('Declined', 'no peer available for request');
self.logger.warn('no relay peer available', req.extendLogInfo({}));
return;
}
var rereq = new RelayRequest(self.channel, peer, req, buildRes);
rereq.createOutRequest();
};
// TODO: lazy reqs
// - #onTimeout
// - audit #extendLogInfo vs regular reqs
function LazyRelayInReq(conn, reqFrame) {
var self = this;
self.start = conn.timers.now();
self.remoteAddr = conn.remoteName;
self.conn = conn;
self.logger = conn.logger;
self.peer = null;
self.outreq = null;
self.reqFrame = reqFrame;
self.id = self.reqFrame.id;
self.serviceName = '';
self.callerName = '';
self.timeout = 0;
self.timedOut = false;
self.alive = true;
self.boundExtendLogInfo = extendLogInfo;
self.boundOnIdentified = onIdentified;
function extendLogInfo(info) {
return self.extendLogInfo(info);
}
function onIdentified(err) {
if (err) {
self.onError(err);
} else {
self.onIdentified();
}
}
}
LazyRelayInReq.prototype.type = 'tchannel.lazy.incoming-request';
LazyRelayInReq.prototype.initRead =
function initRead() {
var self = this;
var res = self.reqFrame.bodyRW.lazy.readTTL(self.reqFrame);
if (res.err) {
// TODO: wrap? protocol read error?
return res.err;
}
self.timeout = res.value;
res = self.reqFrame.bodyRW.lazy.readService(self.reqFrame);
if (res.err) {
// TODO: wrap? protocol read error?
return res.err;
}
self.serviceName = res.value;
// TODO: lazy read self.callerName
return null;
};
LazyRelayInReq.prototype.extendLogInfo =
function extendLogInfo(info) {
var self = this;
if (self.outreq) {
info = self.outreq._extendLogInfo(info);
}
info = self._extendLogInfo(info);
return info;
};
LazyRelayInReq.prototype._extendLogInfo =
function _extendLogInfo(info) {
var self = this;
info.requestType = self.type;
info.inRemoteAddr = self.remoteAddr;
info.inRequestId = self.id;
info.serviceName = self.serviceName;
// TODO: why not full peer.extendLogInfo
if (self.peer) {
info.hostPort = self.peer.hostPort;
}
return info;
};
LazyRelayInReq.prototype.logError =
function relayRequestlogError(err, codeName) {
var self = this;
logError(self.conn.logger, err, codeName, function extendLogInfo(info) {
return self.extendLogInfo(info);
});
};
LazyRelayInReq.prototype.checkTimeout =
function checkTimeout() {
var self = this;
if (!self.timedOut) {
var elapsed = self.conn.timers.now() - self.start;
if (elapsed > self.timeout) {
self.timedOut = true;
// TODO: cancel any outreq?
self.onError(errors.RequestTimeoutError({
id: self.id,
start: self.start,
elapsed: elapsed,
timeout: self.timeout
}));
}
}
return self.timedOut;
};
LazyRelayInReq.prototype.createOutRequest =
function createOutRequest() {
var self = this;
if (self.outreq) {
self.conn.logger.warn('relay request already started', self.extendLogInfo({}));
return;
}
self.peer.waitForIdentified(self.boundOnIdentified);
};
LazyRelayInReq.prototype.onIdentified =
function onIdentified(err) {
var self = this;
if (err) {
self.onError(err);
return;
}
var conn = chooseRelayPeerConnection(self.peer);
if (!conn.remoteName) {
// we get the problem
self.logger.warn('onIdentified called on unidentified connection', self.extendLogInfo({}));
}
if (conn.closing) {
// most likely
self.logger.warn('onIdentified called on closing connection', self.extendLogInfo({}));
}
self.outreq = new LazyRelayOutReq(conn, self);
var ttl = self.updateTTL(self.outreq.start);
if (!ttl || ttl < 0) {
// error or timeout, observability handled already by #updateTTL
return;
}
self.outreq.timeout = ttl;
conn.ops.addOutReq(self.outreq);
self.handleFrameLazily(self.reqFrame);
self.reqFrame = null;
};
LazyRelayInReq.prototype.updateTTL =
function updateTTL(now) {
var self = this;
var elapsed = now - self.start;
var timeout = self.timeout - elapsed;
if (timeout <= 0) {
self.sendErrorFrame('Timeout', 'relay ttl expired');
// TODO: log/stat
return timeout;
}
var res = self.reqFrame.bodyRW.lazy.writeTTL(timeout, self.reqFrame);
if (res.err) {
// TODO: wrap? protocol write error?
self.onError(res.err);
return NaN;
}
return timeout;
};
LazyRelayInReq.prototype.onError =
function onError(err) {
var self = this;
if (!self.alive) {
self.logger.warn('dropping error from dead relay request', self.extendLogInfo({
error: err
}));
return;
}
self.alive = false;
var codeName = errors.classify(err) || 'UnexpectedError';
self.sendErrorFrame(codeName, err.message);
logError(self.conn.logger, err, codeName, self.boundExtendLogInfo);
// TODO: stat in some cases, e.g. declined / peer not available
self.conn.ops.popInReq(self.id, self.extendLogInfo({
info: 'lazy relay request error',
relayDirection: 'in'
}));
};
LazyRelayInReq.prototype.sendErrorFrame =
function sendErrorFrame(codeName, message) {
var self = this;
self.conn.sendLazyErrorFrame(self.reqFrame, codeName, message);
};
LazyRelayInReq.prototype.handleFrameLazily =
function handleFrameLazily(frame) {
// frame.type will be one of:
// - v2.Types.CallRequest
// - v2.Types.CallRequestCont
var self = this;
if (!self.alive) {
self.logger.warn('dropping frame from dead relay request', self.extendLogInfo({}));
return;
}
frame.setId(self.outreq.id);
self.outreq.conn.socket.write(frame.buffer);
if (frame.bodyRW.lazy.isFrameTerminal(frame)) {
self.alive = false;
self.conn.ops.popInReq(self.id, self.extendLogInfo({
info: 'lazy relay request done',
relayDirection: 'in'
}));
}
};
function LazyRelayOutReq(conn, inreq) {
var self = this;
self.start = conn.timers.now();
self.remoetAddr = conn.remoteName;
self.conn = conn;
self.logger = conn.logger;
self.inreq = inreq;
self.id = self.conn.nextFrameId();
self.serviceName = self.inreq.serviceName;
self.callerName = self.inreq.callerName;
self.timeout = 0;
self.timedOut = false;
}
LazyRelayOutReq.prototype.type = 'tchannel.lazy.outgoing-request';
LazyRelayOutReq.prototype.extendLogInfo =
function extendLogInfo(info) {
var self = this;
if (self.inreq) {
info = self.inreq._extendLogInfo(info);
}
info = self._extendLogInfo(info);
return info;
};
LazyRelayOutReq.prototype._extendLogInfo =
function _extendLogInfo(info) {
var self = this;
info.requestType = self.type;
info.outRequestAddr = self.remoteAddr;
info.outRequestId = self.id;
return info;
};
LazyRelayOutReq.prototype.logError =
function relayRequestlogError(err, codeName) {
var self = this;
logError(self.conn.logger, err, codeName, function extendLogInfo(info) {
return self.inreq.extendLogInfo(info);
});
};
LazyRelayOutReq.prototype.checkTimeout =
function checkTimeout() {
var self = this;
if (!self.timedOut) {
var elapsed = self.conn.timers.now() - self.start;
if (elapsed > self.timeout) {
self.timedOut = true;
// TODO: send cancel?
// TODO: lighter interface that doesn't create an error to send a frame
self.inreq.onError(errors.RequestTimeoutError({
id: self.id,
start: self.start,
elapsed: elapsed,
timeout: self.timeout
}));
}
}
return self.timedOut;
};
LazyRelayOutReq.prototype.handleFrameLazily =
function handleFrameLazily(frame) {
// frame.type will be one of:
// - v2.Types.CallResponse
// - v2.Types.CallResponseCont
// - v2.Types.ErrorResponse
var self = this;
if (frame.type === v2.Types.ErrorResponse) {
self.logForwardedError(frame);
}
frame.setId(self.inreq.id);
self.inreq.conn.socket.write(frame.buffer);
if (frame.bodyRW.lazy.isFrameTerminal(frame)) {
self.conn.ops.popOutReq(self.id, self.extendLogInfo({
info: 'lazy relay request done',
relayDirection: 'out'
}));
}
};
LazyRelayOutReq.prototype.logForwardedError =
function logForwardedError(errFrame) {
var self = this;
var res = errFrame.bodyRW.lazy.readCode(errFrame);
if (res.err) {
self.logger.error('failed to read error frame code', self.extendLogInfo({
error: res.err
}));
return;
}
var code = res.value;
res = errFrame.bodyRW.lazy.readMessage(errFrame);
if (res.err) {
self.logger.error('failed to read error frame message', self.extendLogInfo({
error: res.err
}));
return;
}
var message = res.value;
// TODO: thinner logErrorFrame that doesn't need to instantiate an error
// just to log an error frame
var codeErrorType = v2.ErrorResponse.CodeErrors[code];
var err = new codeErrorType({
originalId: errFrame.id,
message: message
});
self.logError(err, errors.classify(err) || 'UnexpectedError');
};
function RelayRequest(channel, peer, inreq, buildRes) {
var self = this;
self.channel = channel;
self.logger = self.channel.logger;
self.inreq = inreq;
self.inres = null;
self.outres = null;
self.outreq = null;
self.buildRes = buildRes;
self.peer = peer;
self.error = null;
self.boundOnError = onError;
self.boundExtendLogInfo = extendLogInfo;
self.boundOnIdentified = onIdentified;
function onError(err) {
self.onError(err);
}
function extendLogInfo(info) {
return self.extendLogInfo(info);
}
function onIdentified(err) {
if (err) {
self.onError(err);
} else {
self.onIdentified();
}
}
}
RelayRequest.prototype.createOutRequest = function createOutRequest() {
var self = this;
if (self.outreq) {
self.logger.warn('relay request already started', self.extendLogInfo({}));
return;
}
self.peer.waitForIdentified(self.boundOnIdentified);
};
RelayRequest.prototype.onIdentified = function onIdentified() {
var self = this;
var conn = chooseRelayPeerConnection(self.peer);
if (!conn.remoteName) {
// we get the problem
self.logger.error('onIdentified called on no connection identified', {
hostPort: self.peer.hostPort
});
}
if (conn.closing) {
// most likely
self.logger.error('onIdentified called on connection closing', {
hostPort: self.peer.hostPort
});
}
var elapsed = self.channel.timers.now() - self.inreq.start;
var timeout = Math.max(self.inreq.timeout - elapsed, 1);
// TODO use a type for this literal
self.outreq = self.channel.request({
peer: self.peer,
streamed: self.inreq.streamed,
timeout: timeout,
parent: self.inreq,
tracing: self.inreq.tracing,
checksum: self.inreq.checksum,
forwardTrace: true,
serviceName: self.inreq.serviceName,
headers: self.inreq.headers,
retryFlags: self.inreq.retryFlags
});
self.outreq.responseEvent.on(onResponse);
self.outreq.errorEvent.on(self.boundOnError);
self.channel.emitFastStat(self.channel.buildStat(
'tchannel.relay.latency',
'timing',
elapsed,
{}
));
if (self.outreq.streamed) {
self.outreq.sendStreams(self.inreq.arg1, self.inreq.arg2, self.inreq.arg3);
} else {
self.outreq.send(self.inreq.arg1, self.inreq.arg2, self.inreq.arg3);
}
function onResponse(res) {
self.onResponse(res);
}
};
RelayRequest.prototype.createOutResponse = function createOutResponse(options) {
var self = this;
if (self.outres) {
self.logger.warn('relay request already responded', self.extendLogInfo({
error: self.error,
options: options // TODO: seems like a Bad Idea ™
}));
return null;
}
// It is possible that the inreq gets reaped with a timeout
// It is also possible that the out request gets repead with a timeout
// Both the in & out req try to create an outgoing response
if (self.inreq.res && self.inreq.res.codeString === 'Timeout') {
self.logger.debug('relay request already timed out', {
codeString: self.inreq.res.codeString,
responseMessage: self.inreq.res.message,
serviceName: self.outreq && self.outreq.serviceName,
arg1: self.outreq && String(self.outreq.arg1),
outRemoteAddr: self.outreq && self.outreq.remoteAddr,
inRemoteAddr: self.inreq.remoteAddr,
inSocketRemoteAddr: self.inreq.connection.socketRemoteAddr,
error: self.error
});
return null;
}
self.outres = self.buildRes(options);
return self.outres;
};
RelayRequest.prototype.onResponse = function onResponse(res) {
var self = this;
if (self.inres) {
self.logger.warn('relay request got more than one response callback', {
// TODO: better context
remoteAddr: res.remoteAddr,
id: res.id
});
return;
}
self.inres = res;
if (!self.createOutResponse({
streamed: self.inres.streamed,
headers: self.inres.headers,
code: self.inres.code
})) return;
if (self.outres.streamed) {
self.inres.arg2.pipe(self.outres.arg2);
self.inres.arg3.pipe(self.outres.arg3);
} else {
self.outres.send(self.inres.arg2, self.inres.arg3);
}
};
RelayRequest.prototype.onError = function onError(err) {
var self = this;
if (self.error) {
// TODO: verify
// remoteAddr: self.inreq.remoteAddr,
// serviceName: self.inreq.serviceName,
// endpoint: self.inreq.endpoint,
// callerName: self.inreq.callerName,
self.logger.warn('Unexpected double onError', self.inreq.extendLogInfo({
error: err,
oldError: self.error
}));
}
self.error = err;
if (!self.createOutResponse()) return;
var codeName = errors.classify(err) || 'UnexpectedError';
self.outres.sendError(codeName, err.message);
self.logError(err, codeName);
};
RelayRequest.prototype.extendLogInfo = function extendLogInfo(info) {
var self = this;
// XXX does inreq give:
// info.remoteAddr = self.inreq.remoteAddr;
// info.id = self.inreq.id;
info.outRemoteAddr = self.outreq && self.outreq.remoteAddr;
info = self.inreq.extendLogInfo(info);
return info;
};
RelayRequest.prototype.logError = function relayRequestLogError(err, codeName) {
var self = this;
logError(self.logger, err, codeName, self.boundExtendLogInfo);
};
function logError(logger, err, codeName, extendLogInfo) {
var level = errors.logLevel(err, codeName);
var info = extendLogInfo({
error: err,
isErrorFrame: err.isErrorFrame
});
if (err.isErrorFrame) {
if (level === 'warn') {
logger.warn('forwarding error frame', info);
} else if (level === 'info') {
logger.info('forwarding expected error frame', info);
}
} else if (level === 'error') {
logger.error('unexpected error while forwarding', info);
} else if (level === 'warn') {
logger.warn('error while forwarding', info);
} else if (level === 'info') {
logger.info('expected error while forwarding', info);
}
}
function chooseRelayPeerConnection(peer) {
var conn = null;
for (var i = 0; i < peer.connections.length; i++) {
conn = peer.connections[i];
if (conn.remoteName && !conn.closing) break;
}
return conn;
}