resolving-load-balancer.js
16.5 KB
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
"use strict";
/*
* Copyright 2019 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
const load_balancer_1 = require("./load-balancer");
const channel_1 = require("./channel");
const resolver_1 = require("./resolver");
const picker_1 = require("./picker");
const backoff_timeout_1 = require("./backoff-timeout");
const constants_1 = require("./constants");
const metadata_1 = require("./metadata");
const logging = require("./logging");
const constants_2 = require("./constants");
const TRACER_NAME = 'resolving_load_balancer';
function trace(text) {
logging.trace(constants_2.LogVerbosity.DEBUG, TRACER_NAME, text);
}
const DEFAULT_LOAD_BALANCER_NAME = 'pick_first';
class ResolvingLoadBalancer {
/**
* Wrapper class that behaves like a `LoadBalancer` and also handles name
* resolution internally.
* @param target The address of the backend to connect to.
* @param channelControlHelper `ChannelControlHelper` instance provided by
* this load balancer's owner.
* @param defaultServiceConfig The default service configuration to be used
* if none is provided by the name resolver. A `null` value indicates
* that the default behavior should be the default unconfigured behavior.
* In practice, that means using the "pick first" load balancer
* implmentation
*/
constructor(target, channelControlHelper, defaultServiceConfig) {
this.target = target;
this.channelControlHelper = channelControlHelper;
this.defaultServiceConfig = defaultServiceConfig;
/**
* Current internal load balancer used for handling calls.
* Invariant: innerLoadBalancer === null => pendingReplacementLoadBalancer === null.
*/
this.innerLoadBalancer = null;
/**
* The load balancer instance that will be used in place of the current
* `innerLoadBalancer` once either that load balancer loses its connection
* or this one establishes a connection. For use when a new name resolution
* result comes in with a different load balancing configuration, and the
* current `innerLoadBalancer` is still connected.
*/
this.pendingReplacementLoadBalancer = null;
/**
* This resolving load balancer's current connectivity state.
*/
this.currentState = channel_1.ConnectivityState.IDLE;
/**
* The service config object from the last successful resolution, if
* available. A value of undefined indicates that there has not yet
* been a successful resolution. A value of null indicates that the last
* successful resolution explicitly provided a null service config.
*/
this.previousServiceConfig = undefined;
/**
* The most recently reported connectivity state of the `innerLoadBalancer`.
*/
this.innerBalancerState = channel_1.ConnectivityState.IDLE;
this.innerBalancerPicker = new picker_1.UnavailablePicker();
/**
* The most recent reported state of the pendingReplacementLoadBalancer.
* Starts at IDLE for type simplicity. This should get updated as soon as the
* pendingReplacementLoadBalancer gets constructed.
*/
this.replacementBalancerState = channel_1.ConnectivityState.IDLE;
/**
* The picker associated with the replacementBalancerState. Starts as an
* UnavailablePicker for type simplicity. This should get updated as soon as
* the pendingReplacementLoadBalancer gets constructed.
*/
this.replacementBalancerPicker = new picker_1.UnavailablePicker();
/**
* Indicates whether we should attempt to resolve again after the backoff
* timer runs out.
*/
this.continueResolving = false;
this.updateState(channel_1.ConnectivityState.IDLE, new picker_1.QueuePicker(this));
this.innerResolver = resolver_1.createResolver(target, {
onSuccessfulResolution: (addressList, serviceConfig, serviceConfigError) => {
let workingServiceConfig = null;
/* This first group of conditionals implements the algorithm described
* in https://github.com/grpc/proposal/blob/master/A21-service-config-error-handling.md
* in the section called "Behavior on receiving a new gRPC Config".
*/
if (serviceConfig === null) {
// Step 4 and 5
if (serviceConfigError === null) {
// Step 5
this.previousServiceConfig = serviceConfig;
workingServiceConfig = this.defaultServiceConfig;
}
else {
// Step 4
if (this.previousServiceConfig === undefined) {
// Step 4.ii
if (this.defaultServiceConfig === null) {
// Step 4.ii.b
this.handleResolutionFailure(serviceConfigError);
}
else {
// Step 4.ii.a
workingServiceConfig = this.defaultServiceConfig;
}
}
else {
// Step 4.i
workingServiceConfig = this.previousServiceConfig;
}
}
}
else {
// Step 3
workingServiceConfig = serviceConfig;
this.previousServiceConfig = serviceConfig;
}
let loadBalancerName = null;
let loadBalancingConfig = null;
if (workingServiceConfig === null ||
workingServiceConfig.loadBalancingConfig.length === 0) {
loadBalancerName = DEFAULT_LOAD_BALANCER_NAME;
}
else {
for (const lbConfig of workingServiceConfig.loadBalancingConfig) {
// Iterating through a oneof looking for whichever one is populated
for (const key in lbConfig) {
if (Object.prototype.hasOwnProperty.call(lbConfig, key)) {
if (load_balancer_1.isLoadBalancerNameRegistered(key)) {
loadBalancerName = key;
loadBalancingConfig = lbConfig;
break;
}
}
}
if (loadBalancerName !== null) {
break;
}
}
if (loadBalancerName === null) {
// There were load balancing configs but none are supported. This counts as a resolution failure
this.handleResolutionFailure({
code: constants_1.Status.UNAVAILABLE,
details: 'All load balancer options in service config are not compatible',
metadata: new metadata_1.Metadata(),
});
return;
}
}
if (this.innerLoadBalancer === null) {
this.innerLoadBalancer = load_balancer_1.createLoadBalancer(loadBalancerName, this.innerChannelControlHelper);
this.innerLoadBalancer.updateAddressList(addressList, loadBalancingConfig);
}
else if (this.innerLoadBalancer.getTypeName() === loadBalancerName) {
this.innerLoadBalancer.updateAddressList(addressList, loadBalancingConfig);
}
else {
if (this.pendingReplacementLoadBalancer === null ||
this.pendingReplacementLoadBalancer.getTypeName() !==
loadBalancerName) {
if (this.pendingReplacementLoadBalancer !== null) {
this.pendingReplacementLoadBalancer.destroy();
}
this.pendingReplacementLoadBalancer = load_balancer_1.createLoadBalancer(loadBalancerName, this.replacementChannelControlHelper);
}
this.pendingReplacementLoadBalancer.updateAddressList(addressList, loadBalancingConfig);
}
},
onError: (error) => {
this.handleResolutionFailure(error);
},
});
this.innerChannelControlHelper = {
createSubchannel: (subchannelAddress, subchannelArgs) => {
return this.channelControlHelper.createSubchannel(subchannelAddress, subchannelArgs);
},
updateState: (connectivityState, picker) => {
this.innerBalancerState = connectivityState;
if (connectivityState === channel_1.ConnectivityState.IDLE) {
picker = new picker_1.QueuePicker(this);
}
this.innerBalancerPicker = picker;
if (connectivityState !== channel_1.ConnectivityState.READY &&
this.pendingReplacementLoadBalancer !== null) {
this.switchOverReplacementBalancer();
}
else {
if (connectivityState === channel_1.ConnectivityState.IDLE) {
if (this.innerLoadBalancer) {
this.innerLoadBalancer.destroy();
this.innerLoadBalancer = null;
}
}
this.updateState(connectivityState, picker);
}
},
requestReresolution: () => {
if (this.pendingReplacementLoadBalancer === null) {
/* If the backoffTimeout is running, we're still backing off from
* making resolve requests, so we shouldn't make another one here.
* In that case, the backoff timer callback will call
* updateResolution */
if (this.backoffTimeout.isRunning()) {
this.continueResolving = true;
}
else {
this.updateResolution();
}
}
},
};
this.replacementChannelControlHelper = {
createSubchannel: (subchannelAddress, subchannelArgs) => {
return this.channelControlHelper.createSubchannel(subchannelAddress, subchannelArgs);
},
updateState: (connectivityState, picker) => {
if (connectivityState === channel_1.ConnectivityState.IDLE) {
picker = new picker_1.QueuePicker(this);
}
this.replacementBalancerState = connectivityState;
this.replacementBalancerPicker = picker;
if (connectivityState === channel_1.ConnectivityState.READY) {
this.switchOverReplacementBalancer();
}
else if (connectivityState === channel_1.ConnectivityState.IDLE) {
if (this.pendingReplacementLoadBalancer) {
this.pendingReplacementLoadBalancer.destroy();
this.pendingReplacementLoadBalancer = null;
}
}
},
requestReresolution: () => {
/* If the backoffTimeout is running, we're still backing off from
* making resolve requests, so we shouldn't make another one here.
* In that case, the backoff timer callback will call
* updateResolution */
if (this.backoffTimeout.isRunning()) {
this.continueResolving = true;
}
else {
this.updateResolution();
}
},
};
this.backoffTimeout = new backoff_timeout_1.BackoffTimeout(() => {
if (this.continueResolving) {
this.updateResolution();
this.continueResolving = false;
}
else {
if (this.innerLoadBalancer === null) {
this.updateState(channel_1.ConnectivityState.IDLE, new picker_1.QueuePicker(this));
}
else {
this.updateState(this.innerBalancerState, this.innerBalancerPicker);
}
}
});
}
updateResolution() {
this.innerResolver.updateResolution();
if (this.innerLoadBalancer === null || this.innerBalancerState === channel_1.ConnectivityState.IDLE) {
this.updateState(channel_1.ConnectivityState.CONNECTING, new picker_1.QueuePicker(this));
}
}
updateState(connectivitystate, picker) {
trace(this.target + ' ' + channel_1.ConnectivityState[this.currentState] + ' -> ' + channel_1.ConnectivityState[connectivitystate]);
this.currentState = connectivitystate;
this.channelControlHelper.updateState(connectivitystate, picker);
}
/**
* Stop using the current innerLoadBalancer and replace it with the
* pendingReplacementLoadBalancer. Must only be called if both of
* those are currently not null.
*/
switchOverReplacementBalancer() {
this.innerLoadBalancer.destroy();
this.innerLoadBalancer = this.pendingReplacementLoadBalancer;
this.innerLoadBalancer.replaceChannelControlHelper(this.innerChannelControlHelper);
this.pendingReplacementLoadBalancer = null;
this.innerBalancerState = this.replacementBalancerState;
this.innerBalancerPicker = this.replacementBalancerPicker;
this.updateState(this.replacementBalancerState, this.replacementBalancerPicker);
}
handleResolutionFailure(error) {
if (this.innerLoadBalancer === null || this.innerBalancerState === channel_1.ConnectivityState.IDLE) {
this.updateState(channel_1.ConnectivityState.TRANSIENT_FAILURE, new picker_1.UnavailablePicker(error));
}
this.backoffTimeout.runOnce();
}
exitIdle() {
if (this.innerLoadBalancer !== null) {
this.innerLoadBalancer.exitIdle();
}
if (this.currentState === channel_1.ConnectivityState.IDLE) {
if (this.backoffTimeout.isRunning()) {
this.continueResolving = true;
}
else {
this.updateResolution();
}
this.updateState(channel_1.ConnectivityState.CONNECTING, new picker_1.QueuePicker(this));
}
}
updateAddressList(addressList, lbConfig) {
throw new Error('updateAddressList not supported on ResolvingLoadBalancer');
}
resetBackoff() {
this.backoffTimeout.reset();
if (this.innerLoadBalancer !== null) {
this.innerLoadBalancer.resetBackoff();
}
if (this.pendingReplacementLoadBalancer !== null) {
this.pendingReplacementLoadBalancer.resetBackoff();
}
}
destroy() {
if (this.innerLoadBalancer !== null) {
this.innerLoadBalancer.destroy();
this.innerLoadBalancer = null;
}
if (this.pendingReplacementLoadBalancer !== null) {
this.pendingReplacementLoadBalancer.destroy();
this.pendingReplacementLoadBalancer = null;
}
this.updateState(channel_1.ConnectivityState.SHUTDOWN, new picker_1.UnavailablePicker());
}
getTypeName() {
return 'resolving_load_balancer';
}
replaceChannelControlHelper(channelControlHelper) {
this.channelControlHelper = channelControlHelper;
}
}
exports.ResolvingLoadBalancer = ResolvingLoadBalancer;
//# sourceMappingURL=resolving-load-balancer.js.map