Skip to content

Commit 296bd2f

Browse files
committed
grpc-js-xds: Add wrr_locality to LB policy registry
1 parent cbece42 commit 296bd2f

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

packages/grpc-js-xds/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"prepare": "npm run generate-types && npm run generate-interop-types && npm run generate-test-types && npm run compile",
1313
"pretest": "npm run compile",
1414
"posttest": "npm run check",
15-
"generate-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs deps/envoy-api/ deps/xds/ deps/googleapis/ deps/protoc-gen-validate/ -O src/generated/ --grpcLib @grpc/grpc-js envoy/service/discovery/v3/ads.proto envoy/service/load_stats/v3/lrs.proto envoy/config/listener/v3/listener.proto envoy/config/route/v3/route.proto envoy/config/cluster/v3/cluster.proto envoy/config/endpoint/v3/endpoint.proto envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.proto udpa/type/v1/typed_struct.proto xds/type/v3/typed_struct.proto envoy/extensions/filters/http/fault/v3/fault.proto envoy/service/status/v3/csds.proto envoy/extensions/load_balancing_policies/wrr_locality/v3/wrr_locality.proto envoy/extensions/load_balancing_policies/ring_hash/v3/ring_hash.proto envoy/extensions/load_balancing_policies/pick_first/v3/pick_first.proto envoy/extensions/clusters/aggregate/v3/cluster.proto envoy/extensions/transport_sockets/tls/v3/tls.proto envoy/config/rbac/v3/rbac.proto envoy/extensions/filters/http/rbac/v3/rbac.proto envoy/extensions/load_balancing_policies/client_side_weighted_round_robin/v3/client_side_weighted_round_robin.proto",
15+
"generate-types": "proto-loader-gen-types --keepCase --longs String --enums String --defaults --oneofs --includeComments --includeDirs deps/envoy-api/ deps/xds/ deps/googleapis/ deps/protoc-gen-validate/ -O src/generated/ --grpcLib @grpc/grpc-js envoy/service/discovery/v3/ads.proto envoy/service/load_stats/v3/lrs.proto envoy/config/listener/v3/listener.proto envoy/config/route/v3/route.proto envoy/config/cluster/v3/cluster.proto envoy/config/endpoint/v3/endpoint.proto envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.proto udpa/type/v1/typed_struct.proto xds/type/v3/typed_struct.proto envoy/extensions/filters/http/fault/v3/fault.proto envoy/service/status/v3/csds.proto envoy/extensions/load_balancing_policies/wrr_locality/v3/wrr_locality.proto envoy/extensions/load_balancing_policies/ring_hash/v3/ring_hash.proto envoy/extensions/load_balancing_policies/pick_first/v3/pick_first.proto envoy/extensions/clusters/aggregate/v3/cluster.proto envoy/extensions/transport_sockets/tls/v3/tls.proto envoy/config/rbac/v3/rbac.proto envoy/extensions/filters/http/rbac/v3/rbac.proto envoy/extensions/load_balancing_policies/client_side_weighted_round_robin/v3/client_side_weighted_round_robin.proto envoy/extensions/load_balancing_policies/wrr_locality/v3/wrr_locality.proto",
1616
"generate-interop-types": "proto-loader-gen-types --keep-case --longs String --enums String --defaults --oneofs --json --includeComments --includeDirs proto/ -O interop/generated --grpcLib @grpc/grpc-js grpc/testing/test.proto",
1717
"generate-test-types": "proto-loader-gen-types --keep-case --longs String --enums String --defaults --oneofs --json --includeComments --includeDirs proto/ -O test/generated --grpcLib @grpc/grpc-js grpc/testing/echo.proto"
1818
},

packages/grpc-js-xds/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import * as round_robin_lb from './lb-policy-registry/round-robin';
3131
import * as typed_struct_lb from './lb-policy-registry/typed-struct';
3232
import * as pick_first_lb from './lb-policy-registry/pick-first';
3333
import * as weighted_round_robin_lb from './lb-policy-registry/weighted-round-robin';
34+
import * as wrr_locality from './lb-policy-registry/wrr-locality';
3435

3536
export { XdsServer } from './server';
3637
export { XdsChannelCredentials, XdsServerCredentials } from './xds-credentials';
@@ -61,4 +62,5 @@ export function register() {
6162
typed_struct_lb.setup();
6263
pick_first_lb.setup();
6364
weighted_round_robin_lb.setup();
65+
wrr_locality.setup();
6466
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 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+
import { LoadBalancingConfig } from "@grpc/grpc-js";
19+
import { LoadBalancingPolicy__Output } from "../generated/envoy/config/cluster/v3/LoadBalancingPolicy";
20+
import { TypedExtensionConfig__Output } from "../generated/envoy/config/core/v3/TypedExtensionConfig";
21+
import { loadProtosWithOptionsSync } from "@grpc/proto-loader/build/src/util";
22+
import { Any__Output } from "../generated/google/protobuf/Any";
23+
import { registerLbPolicy } from "../lb-policy-registry";
24+
import { WrrLocality__Output } from "../generated/envoy/extensions/load_balancing_policies/wrr_locality/v3/WrrLocality";
25+
26+
const WRR_LOCALITY_TYPE_URL = 'envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality';
27+
28+
const resourceRoot = loadProtosWithOptionsSync([
29+
'envoy/extensions/load_balancing_policies/wrr_locality/v3/wrr_locality.proto'], {
30+
keepCase: true,
31+
includeDirs: [
32+
// Paths are relative to src/build/lb-policy-registry
33+
__dirname + '/../../../deps/envoy-api/',
34+
__dirname + '/../../../deps/xds/',
35+
__dirname + '/../../../deps/protoc-gen-validate'
36+
],
37+
}
38+
);
39+
40+
const toObjectOptions = {
41+
longs: String,
42+
enums: String,
43+
defaults: true,
44+
oneofs: true
45+
}
46+
47+
function decodePickFirstConfig(message: Any__Output): WrrLocality__Output {
48+
const name = message.type_url.substring(message.type_url.lastIndexOf('/') + 1);
49+
const type = resourceRoot.lookup(name);
50+
if (type) {
51+
const decodedMessage = (type as any).decode(message.value);
52+
return decodedMessage.$type.toObject(decodedMessage, toObjectOptions) as WrrLocality__Output;
53+
} else {
54+
throw new Error(`WRR Locality parsing error: unexpected type URL ${message.type_url}`);
55+
}
56+
}
57+
58+
function convertToLoadBalancingPolicy(protoPolicy: TypedExtensionConfig__Output, selectChildPolicy: (childPolicy: LoadBalancingPolicy__Output) => LoadBalancingConfig): LoadBalancingConfig | null {
59+
if (protoPolicy.typed_config?.type_url !== WRR_LOCALITY_TYPE_URL) {
60+
throw new Error(`WRR Locality LB policy parsing error: unexpected type URL ${protoPolicy.typed_config?.type_url}`);
61+
}
62+
const wrrLocalityMessage = decodePickFirstConfig(protoPolicy.typed_config);
63+
if (!wrrLocalityMessage.endpoint_picking_policy) {
64+
throw new Error('WRR Locality LB policy parsing error: no endpoint_picking_policy set');
65+
}
66+
return {
67+
wrr_locality: {
68+
shuffleAddressList: selectChildPolicy(wrrLocalityMessage.endpoint_picking_policy)
69+
}
70+
};
71+
}
72+
73+
export function setup() {
74+
registerLbPolicy(WRR_LOCALITY_TYPE_URL, convertToLoadBalancingPolicy);
75+
}

0 commit comments

Comments
 (0)