Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xds: Support least_request LB in LoadBalancingPolicy #9262

Merged
merged 1 commit into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions repositories.bzl
Expand Up @@ -135,10 +135,10 @@ def grpc_java_repositories():
if not native.existing_rule("envoy_api"):
http_archive(
name = "envoy_api",
sha256 = "621577591d48cee20b61d4e71466bf4019791f9991da4813ccf75f3b9898de5f",
strip_prefix = "data-plane-api-bb6d6abe8b4d035c2f4ba3acf924cec0cbec5f70",
sha256 = "a0c58442cc2038ccccad9616dd1bab5ff1e65da2bbc0ae41020ef6010119eb0e",
strip_prefix = "data-plane-api-869b00336913138cad96a653458aab650c4e70ea",
urls = [
"https://github.com/envoyproxy/data-plane-api/archive/bb6d6abe8b4d035c2f4ba3acf924cec0cbec5f70.tar.gz",
"https://github.com/envoyproxy/data-plane-api/archive/869b00336913138cad96a653458aab650c4e70ea.tar.gz",
],
)

Expand Down
1 change: 1 addition & 0 deletions xds/BUILD.bazel
Expand Up @@ -83,6 +83,7 @@ java_proto_library(
"@envoy_api//envoy/extensions/filters/http/rbac/v3:pkg",
"@envoy_api//envoy/extensions/filters/http/router/v3:pkg",
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg",
"@envoy_api//envoy/extensions/load_balancing_policies/least_request/v3:pkg",
"@envoy_api//envoy/extensions/load_balancing_policies/ring_hash/v3:pkg",
"@envoy_api//envoy/extensions/load_balancing_policies/round_robin/v3:pkg",
"@envoy_api//envoy/extensions/load_balancing_policies/wrr_locality/v3:pkg",
Expand Down
12 changes: 12 additions & 0 deletions xds/src/main/java/io/grpc/xds/LoadBalancerConfigFactory.java
Expand Up @@ -28,6 +28,7 @@
import io.envoyproxy.envoy.config.cluster.v3.Cluster.RingHashLbConfig;
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy;
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy.Policy;
import io.envoyproxy.envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest;
import io.envoyproxy.envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash;
import io.envoyproxy.envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin;
import io.envoyproxy.envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality;
Expand Down Expand Up @@ -167,6 +168,8 @@ static class LoadBalancingPolicyConverter {
recursionDepth);
} else if (typedConfig.is(RoundRobin.class)) {
serviceConfig = convertRoundRobinConfig();
} else if (typedConfig.is(LeastRequest.class)) {
serviceConfig = convertLeastRequestConfig(typedConfig.unpack(LeastRequest.class));
} else if (typedConfig.is(com.github.xds.type.v3.TypedStruct.class)) {
serviceConfig = convertCustomConfig(
typedConfig.unpack(com.github.xds.type.v3.TypedStruct.class));
Expand Down Expand Up @@ -231,6 +234,15 @@ static class LoadBalancingPolicyConverter {
return buildRoundRobinConfig();
}

/**
* Converts a least_request {@link Any} configuration to service config format.
*/
private static ImmutableMap<String, ?> convertLeastRequestConfig(LeastRequest leastRequest)
throws ResourceInvalidException {
return buildLeastRequestConfig(
leastRequest.hasChoiceCount() ? leastRequest.getChoiceCount().getValue() : null);
}

/**
* Converts a custom TypedStruct LB config to service config format.
*/
Expand Down
24 changes: 22 additions & 2 deletions xds/src/test/java/io/grpc/xds/LoadBalancerConfigFactoryTest.java
Expand Up @@ -36,6 +36,7 @@
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy;
import io.envoyproxy.envoy.config.cluster.v3.LoadBalancingPolicy.Policy;
import io.envoyproxy.envoy.config.core.v3.TypedExtensionConfig;
import io.envoyproxy.envoy.extensions.load_balancing_policies.least_request.v3.LeastRequest;
import io.envoyproxy.envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash;
import io.envoyproxy.envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin;
import io.envoyproxy.envoy.extensions.load_balancing_policies.wrr_locality.v3.WrrLocality;
Expand Down Expand Up @@ -71,6 +72,12 @@ public class LoadBalancerConfigFactoryTest {
.setMaximumRingSize(UInt64Value.of(RING_HASH_MAX_RING_SIZE))
.setHashFunction(RingHash.HashFunction.XX_HASH).build()))).build();

private static final int LEAST_REQUEST_CHOICE_COUNT = 10;
private static final Policy LEAST_REQUEST_POLICY = Policy.newBuilder().setTypedExtensionConfig(
TypedExtensionConfig.newBuilder().setTypedConfig(Any.pack(
LeastRequest.newBuilder().setChoiceCount(UInt32Value.of(LEAST_REQUEST_CHOICE_COUNT))
.build()))).build();

private static final String CUSTOM_POLICY_NAME = "myorg.MyCustomLeastRequestPolicy";
private static final String CUSTOM_POLICY_FIELD_KEY = "choiceCount";
private static final double CUSTOM_POLICY_FIELD_VALUE = 2;
Expand Down Expand Up @@ -103,6 +110,9 @@ public class LoadBalancerConfigFactoryTest {
"wrr_locality_experimental", ImmutableMap.of("childPolicy", ImmutableList.of(
ImmutableMap.of(VALID_CUSTOM_CONFIG.getPolicyName(),
VALID_CUSTOM_CONFIG.getRawConfigValue()))));
private static final LbConfig VALID_LEAST_REQUEST_CONFIG = new LbConfig(
"least_request_experimental",
ImmutableMap.of("choiceCount", (double) LEAST_REQUEST_CHOICE_COUNT));

@After
public void deregisterCustomProvider() {
Expand Down Expand Up @@ -162,13 +172,23 @@ public void ringHash_invalidHash_legacy() {
assertResourceInvalidExceptionThrown(cluster, true, true, "invalid ring hash function");
}

@Test
public void leastRequest() throws ResourceInvalidException {
Cluster cluster = Cluster.newBuilder()
.setLoadBalancingPolicy(LoadBalancingPolicy.newBuilder().addPolicies(LEAST_REQUEST_POLICY))
.build();

assertThat(newLbConfig(cluster, true, true)).isEqualTo(VALID_LEAST_REQUEST_CONFIG);
}

@Test
public void leastRequest_legacy() throws ResourceInvalidException {
System.setProperty("io.grpc.xds.experimentalEnableLeastRequest", "true");

Cluster cluster = Cluster.newBuilder().setLbPolicy(LbPolicy.LEAST_REQUEST)
.setLeastRequestLbConfig(
LeastRequestLbConfig.newBuilder().setChoiceCount(UInt32Value.of(10))).build();
LeastRequestLbConfig.newBuilder()
.setChoiceCount(UInt32Value.of(LEAST_REQUEST_CHOICE_COUNT))).build();

LbConfig lbConfig = newLbConfig(cluster, true, true);
assertThat(lbConfig.getPolicyName()).isEqualTo("wrr_locality_experimental");
Expand All @@ -178,7 +198,7 @@ public void leastRequest_legacy() throws ResourceInvalidException {
assertThat(childConfigs.get(0).getPolicyName()).isEqualTo("least_request_experimental");
assertThat(
JsonUtil.getNumberAsLong(childConfigs.get(0).getRawConfigValue(), "choiceCount")).isEqualTo(
10);
LEAST_REQUEST_CHOICE_COUNT);
}

@Test
Expand Down