Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions util/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,6 @@ public Subchannel createSubchannel(CreateSubchannelArgs args) {
&& addressMap.containsKey(addressGroups.get(0).getAddresses().get(0))) {
EndpointTracker tracker = addressMap.get(addressGroups.get(0).getAddresses().get(0));
tracker.addSubchannel(subchannel);

// If this address has already been ejected, we need to immediately eject this Subchannel.
if (tracker.ejectionTimeNanos != null) {
subchannel.eject();
}
}

return subchannel;
Expand All @@ -264,6 +259,7 @@ final class OutlierDetectionSubchannel extends ForwardingSubchannel {
private final Subchannel delegate;
private EndpointTracker endpointTracker;
private boolean ejected;
private boolean started;
private ConnectivityStateInfo lastSubchannelState;

// In the new pick first: created at construction, delegates to health consumer listener;
Expand Down Expand Up @@ -294,6 +290,10 @@ public void start(SubchannelStateListener listener) {
subchannelStateListener = listener;
super.start(new OutlierDetectionSubchannelStateListener(listener));
}
started = true;
if (ejected) {
notifyEjected();
}
}

@Override
Expand Down Expand Up @@ -372,15 +372,21 @@ void clearEndpointTracker() {

void eject() {
ejected = true;
if (started) {
notifyEjected();
}
logger.log(ChannelLogLevel.INFO, "Subchannel ejected: {0}", this);
}

private void notifyEjected() {
subchannelStateListener.onSubchannelState(ConnectivityStateInfo.forTransientFailure(
Status.UNAVAILABLE.withDescription(
"The subchannel has been ejected by outlier detection")));
logger.log(ChannelLogLevel.INFO, "Subchannel ejected: {0}", this);
}

void uneject() {
ejected = false;
if (lastSubchannelState != null) {
if (started && lastSubchannelState != null) {
subchannelStateListener.onSubchannelState(lastSubchannelState);
logger.log(ChannelLogLevel.INFO, "Subchannel unejected: {0}", this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,31 @@ public void childLbRecreatesSubchannels() {
assertThat(addressTracker.getSubchannels()).doesNotContain(trackedSubchannel);
}

@Test
public void ejectedEndpoint_newSubchannelRemainsEjected() {
OutlierDetectionLoadBalancerConfig config = new OutlierDetectionLoadBalancerConfig.Builder()
.setSuccessRateEjection(new SuccessRateEjection.Builder().build())
.setChildConfig(newChildConfig(fakeLbProvider, null)).build();
loadBalancer.acceptResolvedAddresses(buildResolvedAddress(config, servers.get(0)));

EndpointTracker tracker = loadBalancer.endpointTrackerMap.get(
ImmutableSet.copyOf(servers.get(0).getAddresses()));
tracker.ejectSubchannels(fakeClock.getTicker().read());

// A priority change may create a new subchannel for the same endpoint.
SubchannelStateListener childListener = mock(SubchannelStateListener.class);
fakeChildLb.recreateSubchannels(childListener);

OutlierDetectionSubchannel newSubchannel =
(OutlierDetectionSubchannel) fakeChildLb.subchannelList.get(0);
assertThat(tracker.getSubchannels()).containsExactly(newSubchannel);
assertThat(newSubchannel.isEjected()).isTrue();
ArgumentCaptor<ConnectivityStateInfo> stateCaptor =
ArgumentCaptor.forClass(ConnectivityStateInfo.class);
verify(childListener).onSubchannelState(stateCaptor.capture());
assertThat(stateCaptor.getValue().getState()).isEqualTo(TRANSIENT_FAILURE);
}

/**
* Outlier detection first enabled, then removed.
*/
Expand Down Expand Up @@ -1512,12 +1537,16 @@ public void shutdown() {
// Simulates a situation where a load balancer might recreate some of the subchannels it is
// tracking even if acceptResolvedAddresses() has not been called.
void recreateSubchannels() {
recreateSubchannels(mock(SubchannelStateListener.class));
}

void recreateSubchannels(SubchannelStateListener listener) {
List<Subchannel> newSubchannelList = new ArrayList<>(subchannelList.size());
for (Subchannel subchannel : subchannelList) {
Subchannel newSubchannel = helper
.createSubchannel(
CreateSubchannelArgs.newBuilder().setAddresses(subchannel.getAddresses()).build());
newSubchannel.start(mock(SubchannelStateListener.class));
newSubchannel.start(listener);
subchannel.shutdown();
newSubchannelList.add(newSubchannel);
}
Expand Down
75 changes: 37 additions & 38 deletions xds/src/main/java/io/grpc/xds/CdsLoadBalancer2.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static io.grpc.ConnectivityState.CONNECTING;
import static io.grpc.ConnectivityState.TRANSIENT_FAILURE;
import static io.grpc.xds.XdsLbPolicies.CDS_POLICY_NAME;
import static io.grpc.xds.XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME;
import static io.grpc.xds.XdsLbPolicies.PRIORITY_POLICY_NAME;

import com.google.common.collect.ImmutableList;
Expand All @@ -33,7 +34,6 @@
import io.grpc.InternalEquivalentAddressGroup;
import io.grpc.InternalLogId;
import io.grpc.LoadBalancer;
import io.grpc.LoadBalancerProvider;
import io.grpc.LoadBalancerRegistry;
import io.grpc.NameResolver;
import io.grpc.Status;
Expand Down Expand Up @@ -140,12 +140,13 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {

if (clusterConfig.getChildren() instanceof EndpointConfig) {
addBackendServicePickDetailsLabel = true;
CdsUpdate discovery = clusterConfig.getClusterResource();
StatusOr<XdsClusterConfig.EndpointConfig> endpointConfig =
getEndpointConfig(xdsConfig, clusterName);
StatusOr<ClusterResolutionResult> statusOrResult = clusterState.edsUpdateToResult(
clusterName,
clusterConfig.getClusterResource(),
clusterConfig.getClusterResource().lbPolicyConfig(),
discovery,
discovery.lbPolicyConfig(),
endpointConfig);
if (!statusOrResult.hasValue()) {
Status status = Status.UNAVAILABLE
Expand All @@ -161,14 +162,32 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
.withDescription("No usable endpoint from cluster: " + clusterName);
return fail(status);
}
Object gracefulConfig = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
Object priorityPolicyConfig = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
lbRegistry.getProvider(PRIORITY_POLICY_NAME),
new PriorityLbConfig(
Collections.unmodifiableMap(result.priorityChildConfigs),
Collections.unmodifiableList(result.priorities)));
ClusterImplConfig clusterImplLbConfig = new ClusterImplConfig(
clusterName,
discovery.edsServiceName(),
discovery.lrsServerInfo(),
discovery.maxConcurrentRequests(),
result.dropOverloads,
priorityPolicyConfig,
discovery.upstreamTlsContext(),
discovery.filterMetadata(),
discovery.backendMetricPropagation());
Object clusterImplPolicyConfig = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
lbRegistry.getProvider(CLUSTER_IMPL_POLICY_NAME), clusterImplLbConfig);
Object topLevelPolicyConfig = clusterImplPolicyConfig;
if (discovery.outlierDetection() != null) {
topLevelPolicyConfig = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
lbRegistry.getProvider("outlier_detection_experimental"),
buildOutlierDetectionLbConfig(discovery.outlierDetection(), clusterImplPolicyConfig));
}
return delegate.acceptResolvedAddresses(
resolvedAddresses.toBuilder()
.setLoadBalancingPolicyConfig(gracefulConfig)
.setLoadBalancingPolicyConfig(topLevelPolicyConfig)
.setAddresses(Collections.unmodifiableList(addresses))
.setAttributes(resolvedAddresses.getAttributes().toBuilder()
.set(NameResolver.ATTR_BACKEND_SERVICE, clusterName)
Expand Down Expand Up @@ -461,10 +480,11 @@ StatusOr<ClusterResolutionResult> edsUpdateToResult(
sortedPriorityNames.retainAll(prioritizedLocalityWeights.keySet());
Map<String, PriorityChildConfig> priorityChildConfigs =
generatePriorityChildConfigs(
clusterName, discovery, lbConfig, lbRegistry,
prioritizedLocalityWeights, dropOverloads);
lbConfig,
prioritizedLocalityWeights,
discovery.clusterType() == ClusterType.EDS /* ignoreReresolution */);
return StatusOr.fromValue(new ClusterResolutionResult(addresses, priorityChildConfigs,
sortedPriorityNames));
sortedPriorityNames, dropOverloads));
}

/**
Expand Down Expand Up @@ -566,54 +586,33 @@ private static class ClusterResolutionResult {
private final Map<String, PriorityChildConfig> priorityChildConfigs;
// List of priority names ordered in descending priorities.
private final List<String> priorities;
// Drop policies for the cluster.
private final List<DropOverload> dropOverloads;

ClusterResolutionResult(List<EquivalentAddressGroup> addresses,
Map<String, PriorityChildConfig> configs, List<String> priorities) {
Map<String, PriorityChildConfig> configs, List<String> priorities,
List<DropOverload> dropOverloads) {
this.addresses = addresses;
this.priorityChildConfigs = configs;
this.priorities = priorities;
this.dropOverloads = dropOverloads;
}
}

/**
* Generates configs to be used in the priority LB policy for priorities in a cluster.
*
* <p>priority LB -> cluster_impl LB (one per priority) -> (weighted_target LB
* -> round_robin / least_request_experimental (one per locality)) / ring_hash_experimental
* <p>priority LB -> (weighted_target LB -> round_robin / least_request_experimental (one per
* locality)) / ring_hash_experimental
*/
private static Map<String, PriorityChildConfig> generatePriorityChildConfigs(
String clusterName,
CdsUpdate discovery,
Object endpointLbConfig,
LoadBalancerRegistry lbRegistry,
Map<String, Map<Locality, Integer>> prioritizedLocalityWeights,
List<DropOverload> dropOverloads) {
boolean ignoreReresolution) {
Map<String, PriorityChildConfig> configs = new HashMap<>();
for (String priority : prioritizedLocalityWeights.keySet()) {
ClusterImplConfig clusterImplConfig =
new ClusterImplConfig(
clusterName, discovery.edsServiceName(), discovery.lrsServerInfo(),
discovery.maxConcurrentRequests(), dropOverloads, endpointLbConfig,
discovery.upstreamTlsContext(), discovery.filterMetadata(),
discovery.backendMetricPropagation());
LoadBalancerProvider clusterImplLbProvider =
lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
Object priorityChildPolicy = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
clusterImplLbProvider, clusterImplConfig);

// If outlier detection has been configured we wrap the child policy in the outlier detection
// load balancer.
if (discovery.outlierDetection() != null) {
LoadBalancerProvider outlierDetectionProvider = lbRegistry.getProvider(
"outlier_detection_experimental");
priorityChildPolicy = GracefulSwitchLoadBalancer.createLoadBalancingPolicyConfig(
outlierDetectionProvider,
buildOutlierDetectionLbConfig(discovery.outlierDetection(), priorityChildPolicy));
}

boolean isEds = discovery.clusterType() == ClusterType.EDS;
PriorityChildConfig priorityChildConfig =
new PriorityChildConfig(priorityChildPolicy, isEds /* ignoreReresolution */);
new PriorityChildConfig(endpointLbConfig, ignoreReresolution);
configs.put(priority, priorityChildConfig);
}
return configs;
Expand Down
9 changes: 4 additions & 5 deletions xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@
import javax.annotation.Nullable;

/**
* Load balancer for cluster_impl_experimental LB policy. This LB policy is the child LB policy of
* the priority_experimental LB policy and the parent LB policy of the weighted_target_experimental
* LB policy in the xDS load balancing hierarchy. This LB policy applies cluster-level
* configurations to requests sent to the corresponding cluster, such as drop policies, circuit
* breakers.
* Load balancer for cluster_impl_experimental LB policy. For a non-aggregate cluster, this policy
* sits above priority_experimental and below cds_experimental, or outlier_detection_experimental
* when configured. It applies cluster-level configurations, such as drop policies and circuit
* breakers, across all priorities in the cluster.
*/
final class ClusterImplLoadBalancer extends LoadBalancer {

Expand Down
35 changes: 31 additions & 4 deletions xds/src/test/java/io/grpc/xds/CdsLoadBalancer2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import io.grpc.internal.FakeClock;
import io.grpc.testing.GrpcCleanupRule;
import io.grpc.util.GracefulSwitchLoadBalancerAccessor;
import io.grpc.util.OutlierDetectionLoadBalancer.OutlierDetectionLoadBalancerConfig;
import io.grpc.xds.CdsLoadBalancerProvider.CdsConfig;
import io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig;
import io.grpc.xds.client.BootstrapperImpl;
Expand Down Expand Up @@ -262,7 +263,10 @@ public void discoverTopLevelCluster() {
verify(helper, never()).updateBalancingState(eq(ConnectivityState.TRANSIENT_FAILURE), any());
assertThat(childBalancers).hasSize(1);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(PRIORITY_POLICY_NAME);
assertThat(childBalancer.name).isEqualTo("outlier_detection_experimental");
OutlierDetectionLoadBalancerConfig outlierDetectionConfig =
(OutlierDetectionLoadBalancerConfig) childBalancer.config;
getPriorityLbConfig(getClusterImplConfig(outlierDetectionConfig.childConfig));
}

@Test
Expand Down Expand Up @@ -407,7 +411,6 @@ public void nonAggregateCluster_resourceNotExist_returnErrorPicker() {

@Test
public void nonAggregateCluster_resourceUpdate() {
lbRegistry.register(new PriorityLoadBalancerProvider());
Cluster cluster = EDS_CLUSTER.toBuilder()
.setCircuitBreakers(CircuitBreakers.newBuilder()
.addThresholds(CircuitBreakers.Thresholds.newBuilder()
Expand All @@ -420,9 +423,11 @@ public void nonAggregateCluster_resourceUpdate() {
verify(helper, never()).updateBalancingState(eq(ConnectivityState.TRANSIENT_FAILURE), any());
assertThat(childBalancers).hasSize(1);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
ClusterImplConfig childLbConfig = (ClusterImplConfig) childBalancer.config;
assertThat(childLbConfig.cluster).isEqualTo(CLUSTER);
assertThat(childLbConfig.maxConcurrentRequests).isEqualTo(100L);
getPriorityLbConfig(childLbConfig);

cluster = EDS_CLUSTER.toBuilder()
.setCircuitBreakers(CircuitBreakers.newBuilder()
Expand All @@ -436,6 +441,7 @@ public void nonAggregateCluster_resourceUpdate() {
childBalancer = Iterables.getOnlyElement(childBalancers);
childLbConfig = (ClusterImplConfig) childBalancer.config;
assertThat(childLbConfig.maxConcurrentRequests).isEqualTo(200L);
getPriorityLbConfig(childLbConfig);
}

@Test
Expand All @@ -458,15 +464,16 @@ public void nonAggregateCluster_addsBackendServiceAttributeAndPickDetailsLabel()

@Test
public void nonAggregateCluster_resourceRevoked() {
lbRegistry.register(new PriorityLoadBalancerProvider());
controlPlaneService.setXdsConfig(ADS_TYPE_URL_CDS, ImmutableMap.of(CLUSTER, EDS_CLUSTER));
startXdsDepManager();

verify(helper, never()).updateBalancingState(eq(ConnectivityState.TRANSIENT_FAILURE), any());
assertThat(childBalancers).hasSize(1);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
ClusterImplConfig childLbConfig = (ClusterImplConfig) childBalancer.config;
assertThat(childLbConfig.cluster).isEqualTo(CLUSTER);
getPriorityLbConfig(childLbConfig);

controlPlaneService.setXdsConfig(ADS_TYPE_URL_CDS, ImmutableMap.of());

Expand Down Expand Up @@ -496,7 +503,8 @@ public void dynamicCluster() {
verify(helper, never()).updateBalancingState(eq(ConnectivityState.TRANSIENT_FAILURE), any());
assertThat(childBalancers).hasSize(1);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(PRIORITY_POLICY_NAME);
assertThat(childBalancer.name).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
getPriorityLbConfig((ClusterImplConfig) childBalancer.config);

assertThat(this.lastXdsConfig.getClusters()).containsKey(clusterName);
shutdownLoadBalancer();
Expand Down Expand Up @@ -670,8 +678,10 @@ public void aggregateCluster_leafAddsBackendServicePickDetailsLabel() {
startXdsDepManager();

FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
ClusterImplConfig clusterImplConfig = (ClusterImplConfig) childBalancer.config;
assertThat(clusterImplConfig.cluster).isEqualTo(cluster1);
getPriorityLbConfig(clusterImplConfig);
assertThat(childBalancer.attributes.get(NameResolver.ATTR_BACKEND_SERVICE))
.isEqualTo(cluster1);
childBalancer.deliverSubchannelState(PickResult.withNoResult(), ConnectivityState.READY);
Expand Down Expand Up @@ -719,6 +729,7 @@ public void discoverAggregateCluster_testChildCdsLbPolicyParsing() {
.isEqualTo("cluster-01.googleapis.com");
assertThat(cluster1ImplConfig.edsServiceName)
.isEqualTo("backend-service-1.googleapis.com");
getPriorityLbConfig(cluster1ImplConfig);
assertThat(childBalancers.get(0).attributes.get(NameResolver.ATTR_BACKEND_SERVICE))
.isEqualTo(cluster1);
ClusterImplConfig cluster2ImplConfig =
Expand All @@ -727,6 +738,7 @@ public void discoverAggregateCluster_testChildCdsLbPolicyParsing() {
.isEqualTo("cluster-02.googleapis.com");
assertThat(cluster2ImplConfig.edsServiceName)
.isEqualTo("backend-service-1.googleapis.com");
getPriorityLbConfig(cluster2ImplConfig);
assertThat(childBalancers.get(1).attributes.get(NameResolver.ATTR_BACKEND_SERVICE))
.isEqualTo(cluster2);
}
Expand Down Expand Up @@ -929,6 +941,21 @@ private static PickSubchannelArgs newPickSubchannelArgs(PickDetailsConsumer pick
return args;
}

private static ClusterImplConfig getClusterImplConfig(Object childConfig) {
assertThat(GracefulSwitchLoadBalancerAccessor.getChildProvider(childConfig).getPolicyName())
.isEqualTo(CLUSTER_IMPL_POLICY_NAME);
return (ClusterImplConfig) GracefulSwitchLoadBalancerAccessor.getChildConfig(childConfig);
}

private static PriorityLoadBalancerProvider.PriorityLbConfig getPriorityLbConfig(
ClusterImplConfig clusterImplConfig) {
Object childConfig = clusterImplConfig.childConfig;
assertThat(GracefulSwitchLoadBalancerAccessor.getChildProvider(childConfig).getPolicyName())
.isEqualTo(PRIORITY_POLICY_NAME);
return (PriorityLoadBalancerProvider.PriorityLbConfig)
GracefulSwitchLoadBalancerAccessor.getChildConfig(childConfig);
}

private final class FakeLoadBalancerProvider extends LoadBalancerProvider {
private final String policyName;
private final LoadBalancerProvider configParsingDelegate;
Expand Down
Loading