| 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 |
1
1
1
1
33
33
33
33
33
33
33
33
33
33
33
33
1
1
1
22
22
22
22
17
5
1
5
5
5
1
98
98
1
98
98
98
88
88
88
98
1
93
93
93
1
101
101
91
101
88
101
101
1
88
88
88
88
88
88
88
3
3
8
88
88
1
1
1
1
1
1
| // 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 RelayHandler = require('../relay_handler');
var DEFAULT_LOG_GRACE_PERIOD = 5 * 60 * 1000;
function ServiceDispatchHandler(options) {
Iif (!(this instanceof ServiceDispatchHandler)) {
return new ServiceDispatchHandler(options);
}
var self = this;
self.options = options;
assert(options, 'service dispatch handler options not actually optional');
self.channel = self.options.channel;
self.logger = self.options.logger;
self.statsd = self.options.statsd;
self.egressNodes = self.options.egressNodes;
self.createdAt = self.channel.timers.now();
self.logGracePeriod = self.options.logGracePeriod ||
DEFAULT_LOG_GRACE_PERIOD;
self.permissionsCache = options.permissionsCache;
self.egressNodes.on('membershipChanged', onMembershipChanged);
function onMembershipChanged() {
self.updateServiceChannels();
}
}
ServiceDispatchHandler.prototype.type = 'tchannel.hyperbahn.service-dispatch-handler';
ServiceDispatchHandler.prototype.handleRequest =
function handleRequest(req, buildRes) {
/* eslint max-statements:[2,20] */
var self = this;
Iif (!req.serviceName) {
self.logger.error('Got incoming req with no service', {
serviceName: req.serviceName,
arg1: String(req.arg1)
});
buildRes().sendError('BadRequest', 'no service name given');
return;
}
var chan = self.channel.subChannels[req.serviceName];
if (chan) {
// Temporary hack. Need to set json by default because
// we want to upgrade without breaking ncar
chan.handler.handleRequest(req, buildRes);
} else {
self.handleDefault(req, buildRes);
}
};
ServiceDispatchHandler.prototype.handleDefault =
function handleDefault(req, buildRes) {
var self = this;
var svcchan = self.getOrCreateServiceChannel(req.serviceName);
svcchan.handler.handleRequest(req, buildRes);
};
ServiceDispatchHandler.prototype.getOrCreateServiceChannel =
function getOrCreateServiceChannel(serviceName) {
var self = this;
return self.getServiceChannel(serviceName, true);
};
ServiceDispatchHandler.prototype.getServiceChannel =
function getServiceChannel(serviceName, create) {
var self = this;
var svcchan = self.channel.subChannels[serviceName];
if (!svcchan && create) {
var now = self.channel.timers.now();
Iif (now >= self.createdAt + self.logGracePeriod) {
self.logger.info('Creating new sub channel', {
serviceName: serviceName
});
}
svcchan = self.createServiceChannel(serviceName);
}
return svcchan;
};
ServiceDispatchHandler.prototype.getServicePeer =
function getServicePeer(serviceName, hostPort) {
var self = this;
var svcchan = self.getOrCreateServiceChannel(serviceName);
return self._getServicePeer(svcchan, hostPort);
};
ServiceDispatchHandler.prototype._getServicePeer =
function _getServicePeer(svcchan, hostPort) {
var peer = svcchan.peers.get(hostPort);
if (!peer) {
peer = svcchan.peers.add(hostPort);
}
if (!peer.serviceProxyServices) {
peer.serviceProxyServices = {};
}
peer.serviceProxyServices[svcchan.serviceName] = true;
return peer;
};
ServiceDispatchHandler.prototype.createServiceChannel =
function createServiceChannel(serviceName) {
var self = this;
var exitNodes = self.egressNodes.exitsFor(serviceName);
var isExit = self.egressNodes.isExitFor(serviceName);
var mode = isExit ? 'exit' : 'forward';
var svcchan = self.channel.makeSubChannel({
serviceName: serviceName
});
svcchan.serviceProxyMode = mode; // duck: punched
if (mode === 'forward') {
var exitNames = Object.keys(exitNodes);
for (var i = 0; i < exitNames.length; i++) {
self._getServicePeer(svcchan, exitNames[i]);
}
}
svcchan.handler = new RelayHandler(svcchan);
return svcchan;
};
ServiceDispatchHandler.prototype.updateServiceChannels =
function updateServiceChannels() {
var self = this;
var serviceNames = Object.keys(self.channel.subChannels);
for (var i = 0; i < serviceNames.length; i++) {
var serviceName = serviceNames[i];
var chan = self.channel.subChannels[serviceName];
if (chan.serviceProxyMode) {
self.updateServiceChannel(chan);
}
}
};
ServiceDispatchHandler.prototype.updateServiceChannel =
function updateServiceChannel(svcchan) {
var self = this;
var exitNodes = self.egressNodes.exitsFor(svcchan.serviceName);
var isExit = self.egressNodes.isExitFor(svcchan.serviceName);
if (isExit && svcchan.serviceProxyMode === 'forward') {
self.changeToExit(exitNodes, svcchan);
} else if (!isExit) {
if (svcchan.serviceProxyMode === 'exit') {
self.changeToForward(exitNodes, svcchan);
} else {
self.updateExitNodes(exitNodes, svcchan);
}
}
};
ServiceDispatchHandler.prototype.changeToExit =
function changeToExit(exitNodes, svcchan) {
var self = this;
var oldMode = svcchan.serviceProxyMode;
svcchan.serviceProxyMode = 'exit';
svcchan.peers.clear();
self.emit('roleTransition', svcchan, {
oldMode: oldMode,
newMode: 'exit'
});
self.logger.info('Changing to exit node', {
oldMode: oldMode,
newMode: 'exit',
serviceName: svcchan.serviceName
});
};
ServiceDispatchHandler.prototype.changeToForward =
function changeToForward(exitNodes, svcchan) {
var self = this;
var oldMode = svcchan.serviceProxyMode;
svcchan.serviceProxyMode = 'forward';
// TODO make sure we close all connections.
svcchan.peers.clear();
// TODO: transmit prior known registration data to new owner(s) to
// speed convergence / deal with transitions better:
// var oldPeers = svcchan.peers.clear();
// ... send rpc to new exit nodes
var exitNames = Object.keys(exitNodes);
for (var i = 0; i < exitNames.length; i++) {
self._getServicePeer(svcchan, exitNames[i]);
}
self.emit('roleTransition', svcchan, {
oldMode: oldMode,
newMode: 'forward'
});
self.logger.info('Changing to forward node', {
oldMode: oldMode,
newMode: 'forward',
serviceName: svcchan.serviceName
});
};
ServiceDispatchHandler.prototype.updateExitNodes =
function updateExitNodes(exitNodes, svcchan) {
var self = this;
var i;
var oldNames = svcchan.peers.keys();
for (i = 0; i < oldNames.length; i++) {
if (!exitNodes[oldNames[i]]) {
svcchan.peers.delete(oldNames[i]);
}
}
var exitNames = Object.keys(exitNodes);
for (i = 0; i < exitNames.length; i++) {
self._getServicePeer(svcchan, exitNames[i]);
}
};
// TODO Consider sharding by hostPort and indexing exit exitNodes by hostPort.
// We also have to shard by serviceName and store the serviceName <-> hostPort
// information under the "service exitNodes". This means that sharding by
// hostPort gives an even spread of socket distribution. i.e. if we shard
// dispatch to 5 exit exitNodes and some small lulzy service to 5 exit
// exitNodes we wont have massive imbalance of dispatch having 500 workers and
// the small service having 2 workers. We would need two hops to find an exit
// node though
module.exports = ServiceDispatchHandler;
|