Skip to content

Commit 157de1a

Browse files
authored
Merge pull request #2058 from murgatroid99/grpc-js_outlier_detection
grpc-js: Add outlier detection LB policy
2 parents c2e138c + 2062062 commit 157de1a

15 files changed

+841
-30
lines changed

packages/grpc-js/gulpfile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const compile = checkTask(() => execNpmCommand('compile'));
6767
const copyTestFixtures = checkTask(() => ncpP(`${jsCoreDir}/test/fixtures`, `${outDir}/test/fixtures`));
6868

6969
const runTests = checkTask(() => {
70+
process.env.GRPC_EXPERIMENTAL_ENABLE_OUTLIER_DETECTION = 'true';
7071
return gulp.src(`${outDir}/test/**/*.js`)
7172
.pipe(mocha({reporter: 'mocha-jenkins-reporter',
7273
require: ['ts-node/register']}));

packages/grpc-js/src/channel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { Filter } from './filter';
4949

5050
import { ConnectivityState } from './connectivity-state';
5151
import { ChannelInfo, ChannelRef, ChannelzCallTracker, ChannelzChildrenTracker, ChannelzTrace, registerChannelzChannel, SubchannelRef, unregisterChannelzRef } from './channelz';
52+
import { Subchannel } from './subchannel';
5253

5354
/**
5455
* See https://nodejs.org/api/timers.html#timers_setinterval_callback_delay_args
@@ -451,7 +452,7 @@ export class ChannelImplementation implements Channel {
451452
if (subchannelState === ConnectivityState.READY) {
452453
try {
453454
const pickExtraFilters = pickResult.extraFilterFactories.map(factory => factory.createFilter(callStream));
454-
pickResult.subchannel!.startCallStream(
455+
pickResult.subchannel?.getRealSubchannel().startCallStream(
455456
finalMetadata,
456457
callStream,
457458
[...dynamicFilters, ...pickExtraFilters]

packages/grpc-js/src/duration.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2022 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
export interface Duration {
19+
seconds: number;
20+
nanos: number;
21+
}
22+
23+
export function msToDuration(millis: number): Duration {
24+
return {
25+
seconds: (millis / 1000) | 0,
26+
nanos: (millis % 1000) * 1_000_000 | 0
27+
};
28+
}
29+
30+
export function durationToMs(duration: Duration): number {
31+
return (duration.seconds * 1000 + duration.nanos / 1_000_000) | 0;
32+
}
33+
34+
export function isDuration(value: any): value is Duration {
35+
return (typeof value.seconds === 'number') && (typeof value.nanos === 'number');
36+
}

packages/grpc-js/src/experimental.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export {
66
ConfigSelector,
77
} from './resolver';
88
export { GrpcUri, uriToString } from './uri-parser';
9-
export { ServiceConfig, Duration } from './service-config';
9+
export { Duration } from './duration';
10+
export { ServiceConfig } from './service-config';
1011
export { BackoffTimeout } from './backoff-timeout';
1112
export {
1213
LoadBalancer,
@@ -34,3 +35,4 @@ export { Call as CallStream } from './call-stream';
3435
export { Filter, BaseFilter, FilterFactory } from './filter';
3536
export { FilterStackFactory } from './filter-stack';
3637
export { registerAdminService } from './admin';
38+
export { SubchannelInterface, BaseSubchannelWrapper, ConnectivityStateListener } from './subchannel-interface'

packages/grpc-js/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ import * as resolver_uds from './resolver-uds';
273273
import * as resolver_ip from './resolver-ip';
274274
import * as load_balancer_pick_first from './load-balancer-pick-first';
275275
import * as load_balancer_round_robin from './load-balancer-round-robin';
276+
import * as load_balancer_outlier_detection from './load-balancer-outlier-detection';
276277
import * as channelz from './channelz';
277278

278279
const clientVersion = require('../../package.json').version;
@@ -284,5 +285,6 @@ const clientVersion = require('../../package.json').version;
284285
resolver_ip.setup();
285286
load_balancer_pick_first.setup();
286287
load_balancer_round_robin.setup();
288+
load_balancer_outlier_detection.setup();
287289
channelz.setup();
288290
})();

packages/grpc-js/src/load-balancer-child-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import {
2121
LoadBalancingConfig,
2222
createLoadBalancer,
2323
} from './load-balancer';
24-
import { Subchannel } from './subchannel';
2524
import { SubchannelAddress } from './subchannel-address';
2625
import { ChannelOptions } from './channel-options';
2726
import { ConnectivityState } from './connectivity-state';
2827
import { Picker } from './picker';
2928
import { ChannelRef, SubchannelRef } from './channelz';
29+
import { SubchannelInterface } from './subchannel-interface';
3030

3131
const TYPE_NAME = 'child_load_balancer_helper';
3232

@@ -40,7 +40,7 @@ export class ChildLoadBalancerHandler implements LoadBalancer {
4040
createSubchannel(
4141
subchannelAddress: SubchannelAddress,
4242
subchannelArgs: ChannelOptions
43-
): Subchannel {
43+
): SubchannelInterface {
4444
return this.parent.channelControlHelper.createSubchannel(
4545
subchannelAddress,
4646
subchannelArgs

0 commit comments

Comments
 (0)