xds: generate EDS LB config with hardcoded locality picking policy (#7443)

This commit is contained in:
Chengyuan Zhang 2020-09-22 16:08:41 -07:00 committed by GitHub
parent ef7846496c
commit b434df25cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 38 deletions

View File

@ -20,9 +20,9 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.ConnectivityState.TRANSIENT_FAILURE; import static io.grpc.ConnectivityState.TRANSIENT_FAILURE;
import static io.grpc.xds.XdsLbPolicies.EDS_POLICY_NAME; import static io.grpc.xds.XdsLbPolicies.EDS_POLICY_NAME;
import static io.grpc.xds.XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import io.grpc.EquivalentAddressGroup; import io.grpc.EquivalentAddressGroup;
import io.grpc.InternalLogId; import io.grpc.InternalLogId;
import io.grpc.LoadBalancer; import io.grpc.LoadBalancer;
@ -198,18 +198,20 @@ final class CdsLoadBalancer extends LoadBalancer {
xdsClient, newUpdate.getClusterName(), newUpdate.getEdsServiceName(), xdsClient, newUpdate.getClusterName(), newUpdate.getEdsServiceName(),
newUpdate.getLbPolicy(), newUpdate.getLrsServerName() != null); newUpdate.getLbPolicy(), newUpdate.getLrsServerName() != null);
} }
// FIXME(chengyuanzhang): handle error correctly to avoid being unnecessarily fragile.
checkArgument( checkArgument(
newUpdate.getLbPolicy().equals("round_robin"), "can only support round_robin policy"); newUpdate.getLbPolicy().equals("round_robin"), "can only support round_robin policy");
LoadBalancerProvider endpointPickingPolicyProvider =
LoadBalancerProvider lbProvider = lbRegistry.getProvider(newUpdate.getLbPolicy()); lbRegistry.getProvider(newUpdate.getLbPolicy());
Object lbConfig = LoadBalancerProvider localityPickingPolicyProvider =
lbProvider.parseLoadBalancingPolicyConfig(ImmutableMap.<String, Object>of()).getConfig(); lbRegistry.getProvider(WEIGHTED_TARGET_POLICY_NAME); // hardcode to weighted-target
final EdsConfig edsConfig = final EdsConfig edsConfig =
new EdsConfig( new EdsConfig(
/* clusterName = */ newUpdate.getClusterName(), /* clusterName = */ newUpdate.getClusterName(),
/* edsServiceName = */ newUpdate.getEdsServiceName(), /* edsServiceName = */ newUpdate.getEdsServiceName(),
/* lrsServerName = */ newUpdate.getLrsServerName(), /* lrsServerName = */ newUpdate.getLrsServerName(),
new PolicySelection(lbProvider, ImmutableMap.<String, Object>of(), lbConfig)); new PolicySelection(localityPickingPolicyProvider, null, null /* by EDS policy */),
new PolicySelection(endpointPickingPolicyProvider, null, null));
if (isXdsSecurityEnabled()) { if (isXdsSecurityEnabled()) {
updateSslContextProviderSupplier(newUpdate.getUpstreamTlsContext()); updateSslContextProviderSupplier(newUpdate.getUpstreamTlsContext());
} }

View File

@ -17,7 +17,6 @@
package io.grpc.xds; package io.grpc.xds;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.xds.XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
@ -25,7 +24,6 @@ import io.grpc.Internal;
import io.grpc.LoadBalancer; import io.grpc.LoadBalancer;
import io.grpc.LoadBalancer.Helper; import io.grpc.LoadBalancer.Helper;
import io.grpc.LoadBalancerProvider; import io.grpc.LoadBalancerProvider;
import io.grpc.LoadBalancerRegistry;
import io.grpc.NameResolver.ConfigOrError; import io.grpc.NameResolver.ConfigOrError;
import io.grpc.internal.ServiceConfigUtil.PolicySelection; import io.grpc.internal.ServiceConfigUtil.PolicySelection;
import java.util.Map; import java.util.Map;
@ -74,21 +72,6 @@ public class EdsLoadBalancerProvider extends LoadBalancerProvider {
final PolicySelection localityPickingPolicy; final PolicySelection localityPickingPolicy;
final PolicySelection endpointPickingPolicy; final PolicySelection endpointPickingPolicy;
// TODO(chengyuanzhang): delete me.
EdsConfig(
String clusterName,
@Nullable String edsServiceName,
@Nullable String lrsServerName,
PolicySelection endpointPickingPolicy) {
this.clusterName = checkNotNull(clusterName, "clusterName");
this.edsServiceName = edsServiceName;
this.lrsServerName = lrsServerName;
this.endpointPickingPolicy = checkNotNull(endpointPickingPolicy, "endpointPickingPolicy");
LoadBalancerProvider provider =
LoadBalancerRegistry.getDefaultRegistry().getProvider(WEIGHTED_TARGET_POLICY_NAME);
localityPickingPolicy = new PolicySelection(provider, null, null);
}
EdsConfig( EdsConfig(
String clusterName, String clusterName,
@Nullable String edsServiceName, @Nullable String edsServiceName,

View File

@ -89,6 +89,7 @@ public class CdsLoadBalancerTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
LoadBalancerRegistry registry = new LoadBalancerRegistry(); LoadBalancerRegistry registry = new LoadBalancerRegistry();
registry.register(new FakeLoadBalancerProvider(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME));
registry.register(new FakeLoadBalancerProvider(XdsLbPolicies.EDS_POLICY_NAME)); registry.register(new FakeLoadBalancerProvider(XdsLbPolicies.EDS_POLICY_NAME));
registry.register(new FakeLoadBalancerProvider("round_robin")); registry.register(new FakeLoadBalancerProvider("round_robin"));
ObjectPool<XdsClient> xdsClientPool = new ObjectPool<XdsClient>() { ObjectPool<XdsClient> xdsClientPool = new ObjectPool<XdsClient>() {
@ -135,6 +136,8 @@ public class CdsLoadBalancerTest {
assertThat(edsConfig.clusterName).isEqualTo(CLUSTER); assertThat(edsConfig.clusterName).isEqualTo(CLUSTER);
assertThat(edsConfig.edsServiceName).isNull(); assertThat(edsConfig.edsServiceName).isNull();
assertThat(edsConfig.lrsServerName).isNull(); assertThat(edsConfig.lrsServerName).isNull();
assertThat(edsConfig.localityPickingPolicy.getProvider().getPolicyName())
.isEqualTo(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME); // hardcoded to weighted-target
assertThat(edsConfig.endpointPickingPolicy.getProvider().getPolicyName()) assertThat(edsConfig.endpointPickingPolicy.getProvider().getPolicyName())
.isEqualTo("round_robin"); .isEqualTo("round_robin");
} }
@ -174,6 +177,8 @@ public class CdsLoadBalancerTest {
assertThat(edsConfig.clusterName).isEqualTo(CLUSTER); assertThat(edsConfig.clusterName).isEqualTo(CLUSTER);
assertThat(edsConfig.edsServiceName).isNull(); assertThat(edsConfig.edsServiceName).isNull();
assertThat(edsConfig.lrsServerName).isNull(); assertThat(edsConfig.lrsServerName).isNull();
assertThat(edsConfig.localityPickingPolicy.getProvider().getPolicyName())
.isEqualTo(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME); // hardcoded to weighted-target
assertThat(edsConfig.endpointPickingPolicy.getProvider().getPolicyName()) assertThat(edsConfig.endpointPickingPolicy.getProvider().getPolicyName())
.isEqualTo("round_robin"); .isEqualTo("round_robin");
@ -185,6 +190,8 @@ public class CdsLoadBalancerTest {
assertThat(edsConfig.clusterName).isEqualTo(CLUSTER); assertThat(edsConfig.clusterName).isEqualTo(CLUSTER);
assertThat(edsConfig.edsServiceName).isEqualTo(edsService); assertThat(edsConfig.edsServiceName).isEqualTo(edsService);
assertThat(edsConfig.lrsServerName).isEqualTo(loadReportServer); assertThat(edsConfig.lrsServerName).isEqualTo(loadReportServer);
assertThat(edsConfig.localityPickingPolicy.getProvider().getPolicyName())
.isEqualTo(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME); // hardcoded to weighted-target
assertThat(edsConfig.endpointPickingPolicy.getProvider().getPolicyName()) assertThat(edsConfig.endpointPickingPolicy.getProvider().getPolicyName())
.isEqualTo("round_robin"); .isEqualTo("round_robin");
} }

View File

@ -143,6 +143,8 @@ public class EdsLoadBalancerTest {
} }
}; };
private final PolicySelection fakeLocalityPickingPolicy =
new PolicySelection(mock(LoadBalancerProvider.class), null, null);
private final PolicySelection fakeEndpointPickingPolicy = private final PolicySelection fakeEndpointPickingPolicy =
new PolicySelection(mock(LoadBalancerProvider.class), null, new Object()); new PolicySelection(mock(LoadBalancerProvider.class), null, new Object());
@ -272,7 +274,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void handleNameResolutionErrorBeforeAndAfterEdsWorkding() { public void handleNameResolutionErrorBeforeAndAfterEdsWorkding() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
// handleResolutionError() before receiving any endpoint update. // handleResolutionError() before receiving any endpoint update.
edsLb.handleNameResolutionError(Status.DATA_LOSS.withDescription("fake status")); edsLb.handleNameResolutionError(Status.DATA_LOSS.withDescription("fake status"));
@ -300,7 +302,8 @@ public class EdsLoadBalancerTest {
public void handleEdsServiceNameChange() { public void handleEdsServiceNameChange() {
assertThat(childHelpers).isEmpty(); assertThat(childHelpers).isEmpty();
deliverResolvedAddresses("edsServiceName1", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName1", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
ClusterLoadAssignment clusterLoadAssignment = ClusterLoadAssignment clusterLoadAssignment =
buildClusterLoadAssignmentV2("edsServiceName1", buildClusterLoadAssignmentV2("edsServiceName1",
ImmutableList.of( ImmutableList.of(
@ -320,7 +323,8 @@ public class EdsLoadBalancerTest {
assertLatestConnectivityState(CONNECTING); assertLatestConnectivityState(CONNECTING);
// Change edsServicename to edsServiceName2. // Change edsServicename to edsServiceName2.
deliverResolvedAddresses("edsServiceName2", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName2", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
// The old balancer was not READY, so it will be shutdown immediately. // The old balancer was not READY, so it will be shutdown immediately.
verify(childBalancer1).shutdown(); verify(childBalancer1).shutdown();
@ -350,7 +354,8 @@ public class EdsLoadBalancerTest {
assertLatestSubchannelPicker(subchannel2); assertLatestSubchannelPicker(subchannel2);
// Change edsServiceName to edsServiceName3. // Change edsServiceName to edsServiceName3.
deliverResolvedAddresses("edsServiceName3", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName3", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
clusterLoadAssignment = clusterLoadAssignment =
buildClusterLoadAssignmentV2("edsServiceName3", buildClusterLoadAssignmentV2("edsServiceName3",
ImmutableList.of( ImmutableList.of(
@ -376,7 +381,8 @@ public class EdsLoadBalancerTest {
assertLatestConnectivityState(CONNECTING); assertLatestConnectivityState(CONNECTING);
// Change edsServiceName to edsServiceName4. // Change edsServiceName to edsServiceName4.
deliverResolvedAddresses("edsServiceName4", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName4", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
verify(childBalancer3).shutdown(); verify(childBalancer3).shutdown();
clusterLoadAssignment = clusterLoadAssignment =
@ -404,7 +410,8 @@ public class EdsLoadBalancerTest {
assertLatestSubchannelPicker(subchannel4); assertLatestSubchannelPicker(subchannel4);
// Change edsServiceName to edsServiceName5. // Change edsServiceName to edsServiceName5.
deliverResolvedAddresses("edsServiceName5", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName5", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
clusterLoadAssignment = clusterLoadAssignment =
buildClusterLoadAssignmentV2("edsServiceName5", buildClusterLoadAssignmentV2("edsServiceName5",
ImmutableList.of( ImmutableList.of(
@ -439,7 +446,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void edsResourceUpdate_allDrop() { public void edsResourceUpdate_allDrop() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
ClusterLoadAssignment clusterLoadAssignment = buildClusterLoadAssignmentV2( ClusterLoadAssignment clusterLoadAssignment = buildClusterLoadAssignmentV2(
CLUSTER_NAME, CLUSTER_NAME,
@ -488,7 +495,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void edsResourceUpdate_localityAssignmentChange() { public void edsResourceUpdate_localityAssignmentChange() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
LbEndpoint endpoint11 = buildLbEndpointV2("addr11.example.com", 8011, HEALTHY, 11); LbEndpoint endpoint11 = buildLbEndpointV2("addr11.example.com", 8011, HEALTHY, 11);
LbEndpoint endpoint12 = buildLbEndpointV2("addr12.example.com", 8012, HEALTHY, 12); LbEndpoint endpoint12 = buildLbEndpointV2("addr12.example.com", 8012, HEALTHY, 12);
@ -575,7 +582,8 @@ public class EdsLoadBalancerTest {
edsLb = edsLb =
new EdsLoadBalancer(helper, lbRegistry, localityStoreFactory, bootstrapper, channelFactory); new EdsLoadBalancer(helper, lbRegistry, localityStoreFactory, bootstrapper, channelFactory);
deliverResolvedAddresses("edsServiceName1", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName1", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
assertThat(localityStores).hasSize(1); assertThat(localityStores).hasSize(1);
LocalityStore localityStore = localityStores.peekLast(); LocalityStore localityStore = localityStores.peekLast();
@ -612,7 +620,8 @@ public class EdsLoadBalancerTest {
verify(localityStore).updateLocalityStore(endpointUpdate.getLocalityLbEndpointsMap()); verify(localityStore).updateLocalityStore(endpointUpdate.getLocalityLbEndpointsMap());
// Change cluster name. // Change cluster name.
deliverResolvedAddresses("edsServiceName2", null, fakeEndpointPickingPolicy); deliverResolvedAddresses("edsServiceName2", null, fakeLocalityPickingPolicy,
fakeEndpointPickingPolicy);
assertThat(localityStores).hasSize(2); assertThat(localityStores).hasSize(2);
localityStore = localityStores.peekLast(); localityStore = localityStores.peekLast();
@ -635,7 +644,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void edsResourceNotExist() { public void edsResourceNotExist() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
// Forwarding 20 seconds so that the xds client will deem EDS resource not available. // Forwarding 20 seconds so that the xds client will deem EDS resource not available.
fakeClock.forwardTime(20, TimeUnit.SECONDS); fakeClock.forwardTime(20, TimeUnit.SECONDS);
@ -649,7 +658,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void edsResourceRemoved() { public void edsResourceRemoved() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
ClusterLoadAssignment clusterLoadAssignment = ClusterLoadAssignment clusterLoadAssignment =
buildClusterLoadAssignmentV2(CLUSTER_NAME, buildClusterLoadAssignmentV2(CLUSTER_NAME,
ImmutableList.of( ImmutableList.of(
@ -695,7 +704,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void transientError_noPreviousEndpointUpdateReceived() { public void transientError_noPreviousEndpointUpdateReceived() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
// Forwarding 20 seconds so that the xds client will deem EDS resource not available. // Forwarding 20 seconds so that the xds client will deem EDS resource not available.
fakeClock.forwardTime(20, TimeUnit.SECONDS); fakeClock.forwardTime(20, TimeUnit.SECONDS);
@ -704,7 +713,7 @@ public class EdsLoadBalancerTest {
@Test @Test
public void transientError_withPreviousEndpointUpdateReceived() { public void transientError_withPreviousEndpointUpdateReceived() {
deliverResolvedAddresses(null, null, fakeEndpointPickingPolicy); deliverResolvedAddresses(null, null, fakeLocalityPickingPolicy, fakeEndpointPickingPolicy);
// Endpoint update received. // Endpoint update received.
ClusterLoadAssignment clusterLoadAssignment = ClusterLoadAssignment clusterLoadAssignment =
buildClusterLoadAssignmentV2(CLUSTER_NAME, buildClusterLoadAssignmentV2(CLUSTER_NAME,
@ -751,9 +760,11 @@ public class EdsLoadBalancerTest {
private void deliverResolvedAddresses( private void deliverResolvedAddresses(
@Nullable String edsServiceName, @Nullable String edsServiceName,
@Nullable String lrsServerName, @Nullable String lrsServerName,
PolicySelection locaityPickingPolicy,
PolicySelection endpointPickingPolicy) { PolicySelection endpointPickingPolicy) {
EdsConfig config = EdsConfig config =
new EdsConfig(CLUSTER_NAME, edsServiceName, lrsServerName, endpointPickingPolicy); new EdsConfig(CLUSTER_NAME, edsServiceName, lrsServerName, locaityPickingPolicy,
endpointPickingPolicy);
ResolvedAddresses.Builder resolvedAddressBuilder = ResolvedAddresses.newBuilder() ResolvedAddresses.Builder resolvedAddressBuilder = ResolvedAddresses.newBuilder()
.setAddresses(ImmutableList.<EquivalentAddressGroup>of()) .setAddresses(ImmutableList.<EquivalentAddressGroup>of())
.setLoadBalancingPolicyConfig(config); .setLoadBalancingPolicyConfig(config);