Skip to content

Commit c589441

Browse files
authored
xds: Support least_request LB in LoadBalancingPolicy (grpc#9262)
1 parent a4a67b7 commit c589441

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

repositories.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ def grpc_java_repositories():
135135
if not native.existing_rule("envoy_api"):
136136
http_archive(
137137
name = "envoy_api",
138-
sha256 = "621577591d48cee20b61d4e71466bf4019791f9991da4813ccf75f3b9898de5f",
139-
strip_prefix = "data-plane-api-bb6d6abe8b4d035c2f4ba3acf924cec0cbec5f70",
138+
sha256 = "a0c58442cc2038ccccad9616dd1bab5ff1e65da2bbc0ae41020ef6010119eb0e",
139+
strip_prefix = "data-plane-api-869b00336913138cad96a653458aab650c4e70ea",
140140
urls = [
141-
"https://github.com/envoyproxy/data-plane-api/archive/bb6d6abe8b4d035c2f4ba3acf924cec0cbec5f70.tar.gz",
141+
"https://github.com/envoyproxy/data-plane-api/archive/869b00336913138cad96a653458aab650c4e70ea.tar.gz",
142142
],
143143
)
144144

xds/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ java_proto_library(
8383
"@envoy_api//envoy/extensions/filters/http/rbac/v3:pkg",
8484
"@envoy_api//envoy/extensions/filters/http/router/v3:pkg",
8585
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg",
86+
"@envoy_api//envoy/extensions/load_balancing_policies/least_request/v3:pkg",
8687
"@envoy_api//envoy/extensions/load_balancing_policies/ring_hash/v3:pkg",
8788
"@envoy_api//envoy/extensions/load_balancing_policies/round_robin/v3:pkg",
8889
"@envoy_api//envoy/extensions/load_balancing_policies/wrr_locality/v3:pkg",

xds/src/main/java/io/grpc/xds/LoadBalancerConfigFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig;
2929
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy;
3030
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy.Policy;
31+
import io.envoyproxy.envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest;
3132
import io.envoyproxy.envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash;
3233
import io.envoyproxy.envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin;
3334
import io.envoyproxy.envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality;
@@ -167,6 +168,8 @@ static class LoadBalancingPolicyConverter {
167168
recursionDepth);
168169
} else if (typedConfig.is(RoundRobin.class)) {
169170
serviceConfig = convertRoundRobinConfig();
171+
} else if (typedConfig.is(LeastRequest.class)) {
172+
serviceConfig = convertLeastRequestConfig(typedConfig.unpack(LeastRequest.class));
170173
} else if (typedConfig.is(com.github.xds.type.v3.TypedStruct.class)) {
171174
serviceConfig = convertCustomConfig(
172175
typedConfig.unpack(com.github.xds.type.v3.TypedStruct.class));
@@ -231,6 +234,15 @@ static class LoadBalancingPolicyConverter {
231234
return buildRoundRobinConfig();
232235
}
233236

237+
/**
238+
* Converts a least_request {@link Any} configuration to service config format.
239+
*/
240+
private static ImmutableMap<String, ?> convertLeastRequestConfig(LeastRequest leastRequest)
241+
throws ResourceInvalidException {
242+
return buildLeastRequestConfig(
243+
leastRequest.hasChoiceCount() ? leastRequest.getChoiceCount().getValue() : null);
244+
}
245+
234246
/**
235247
* Converts a custom TypedStruct LB config to service config format.
236248
*/

xds/src/test/java/io/grpc/xds/LoadBalancerConfigFactoryTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy;
3737
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy.Policy;
3838
import io.envoyproxy.envoy.config.core.v3.TypedExtensionConfig;
39+
import io.envoyproxy.envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest;
3940
import io.envoyproxy.envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash;
4041
import io.envoyproxy.envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin;
4142
import io.envoyproxy.envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality;
@@ -71,6 +72,12 @@ public class LoadBalancerConfigFactoryTest {
7172
.setMaximumRingSize(UInt64Value.of(RING_HASH_MAX_RING_SIZE))
7273
.setHashFunction(RingHash.HashFunction.XX_HASH).build()))).build();
7374

75+
private static final int LEAST_REQUEST_CHOICE_COUNT = 10;
76+
private static final Policy LEAST_REQUEST_POLICY = Policy.newBuilder().setTypedExtensionConfig(
77+
TypedExtensionConfig.newBuilder().setTypedConfig(Any.pack(
78+
LeastRequest.newBuilder().setChoiceCount(UInt32Value.of(LEAST_REQUEST_CHOICE_COUNT))
79+
.build()))).build();
80+
7481
private static final String CUSTOM_POLICY_NAME = "myorg.MyCustomLeastRequestPolicy";
7582
private static final String CUSTOM_POLICY_FIELD_KEY = "choiceCount";
7683
private static final double CUSTOM_POLICY_FIELD_VALUE = 2;
@@ -103,6 +110,9 @@ public class LoadBalancerConfigFactoryTest {
103110
"wrr_locality_experimental", ImmutableMap.of("childPolicy", ImmutableList.of(
104111
ImmutableMap.of(VALID_CUSTOM_CONFIG.getPolicyName(),
105112
VALID_CUSTOM_CONFIG.getRawConfigValue()))));
113+
private static final LbConfig VALID_LEAST_REQUEST_CONFIG = new LbConfig(
114+
"least_request_experimental",
115+
ImmutableMap.of("choiceCount", (double) LEAST_REQUEST_CHOICE_COUNT));
106116

107117
@After
108118
public void deregisterCustomProvider() {
@@ -162,13 +172,23 @@ public void ringHash_invalidHash_legacy() {
162172
assertResourceInvalidExceptionThrown(cluster, true, true, "invalid ring hash function");
163173
}
164174

175+
@Test
176+
public void leastRequest() throws ResourceInvalidException {
177+
Cluster cluster = Cluster.newBuilder()
178+
.setLoadBalancingPolicy(LoadBalancingPolicy.newBuilder().addPolicies(LEAST_REQUEST_POLICY))
179+
.build();
180+
181+
assertThat(newLbConfig(cluster, true, true)).isEqualTo(VALID_LEAST_REQUEST_CONFIG);
182+
}
183+
165184
@Test
166185
public void leastRequest_legacy() throws ResourceInvalidException {
167186
System.setProperty("io.grpc.xds.experimentalEnableLeastRequest", "true");
168187

169188
Cluster cluster = Cluster.newBuilder().setLbPolicy(LbPolicy.LEAST_REQUEST)
170189
.setLeastRequestLbConfig(
171-
LeastRequestLbConfig.newBuilder().setChoiceCount(UInt32Value.of(10))).build();
190+
LeastRequestLbConfig.newBuilder()
191+
.setChoiceCount(UInt32Value.of(LEAST_REQUEST_CHOICE_COUNT))).build();
172192

173193
LbConfig lbConfig = newLbConfig(cluster, true, true);
174194
assertThat(lbConfig.getPolicyName()).isEqualTo("wrr_locality_experimental");
@@ -178,7 +198,7 @@ public void leastRequest_legacy() throws ResourceInvalidException {
178198
assertThat(childConfigs.get(0).getPolicyName()).isEqualTo("least_request_experimental");
179199
assertThat(
180200
JsonUtil.getNumberAsLong(childConfigs.get(0).getRawConfigValue(), "choiceCount")).isEqualTo(
181-
10);
201+
LEAST_REQUEST_CHOICE_COUNT);
182202
}
183203

184204
@Test

0 commit comments

Comments
 (0)