Search in sources :

Example 6 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class ClusterResolverLoadBalancerTest method handleEdsResource_ignoreLocalitiesWithNoHealthyEndpoints.

@Test
public void handleEdsResource_ignoreLocalitiesWithNoHealthyEndpoints() {
    ClusterResolverConfig config = new ClusterResolverConfig(Collections.singletonList(edsDiscoveryMechanism1), roundRobin);
    deliverLbConfig(config);
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr-1");
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr-2");
    LocalityLbEndpoints localityLbEndpoints1 = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint1, 100, false)), 10, /* localityWeight */
    1);
    LocalityLbEndpoints localityLbEndpoints2 = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint2, 100, true)), 10, /* localityWeight */
    1);
    String priority = CLUSTER1 + "[priority1]";
    xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME1, ImmutableMap.of(locality1, localityLbEndpoints1, locality2, localityLbEndpoints2));
    FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
    PriorityLbConfig priorityLbConfig = (PriorityLbConfig) childBalancer.config;
    PriorityChildConfig priorityChildConfig = priorityLbConfig.childConfigs.get(priority);
    ClusterImplConfig clusterImplConfig = (ClusterImplConfig) priorityChildConfig.policySelection.getConfig();
    WeightedTargetConfig weightedTargetConfig = (WeightedTargetConfig) clusterImplConfig.childPolicy.getConfig();
    assertThat(weightedTargetConfig.targets.keySet()).containsExactly(locality2.toString());
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) ClusterResolverConfig(io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) LocalityLbEndpoints(io.grpc.xds.Endpoints.LocalityLbEndpoints) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 7 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class WeightedTargetLoadBalancerTest method raceBetweenShutdownAndChildLbBalancingStateUpdate.

@Test
public void raceBetweenShutdownAndChildLbBalancingStateUpdate() {
    Map<String, WeightedPolicySelection> targets = ImmutableMap.of("target0", weightedLbConfig0, "target1", weightedLbConfig1);
    weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(targets)).build());
    verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
    // LB shutdown and subchannel state change can happen simultaneously. If shutdown runs first,
    // any further balancing state update should be ignored.
    weightedTargetLb.shutdown();
    Helper weightedChildHelper0 = childHelpers.iterator().next();
    weightedChildHelper0.updateBalancingState(READY, mock(SubchannelPicker.class));
    verifyNoMoreInteractions(helper);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) Test(org.junit.Test)

Example 8 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class WeightedTargetLoadBalancerTest method balancingStateUpdatedFromChildBalancers.

@Test
public void balancingStateUpdatedFromChildBalancers() {
    Map<String, WeightedPolicySelection> targets = ImmutableMap.of(// {foo, 10, config0}
    "target0", weightedLbConfig0, // {bar, 20, config1}
    "target1", weightedLbConfig1, // {bar, 30, config2}
    "target2", weightedLbConfig2, // {foo, 40, config3}
    "target3", weightedLbConfig3);
    weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(targets)).build());
    verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
    // Subchannels to be created for each child balancer.
    final SubchannelPicker[] subchannelPickers = new SubchannelPicker[] { mock(SubchannelPicker.class), mock(SubchannelPicker.class), mock(SubchannelPicker.class), mock(SubchannelPicker.class) };
    final SubchannelPicker[] failurePickers = new SubchannelPicker[] { new ErrorPicker(Status.CANCELLED), new ErrorPicker(Status.ABORTED), new ErrorPicker(Status.DATA_LOSS), new ErrorPicker(Status.DATA_LOSS) };
    ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(null);
    // One child balancer goes to TRANSIENT_FAILURE.
    childHelpers.get(1).updateBalancingState(TRANSIENT_FAILURE, failurePickers[1]);
    verify(helper, never()).updateBalancingState(eq(TRANSIENT_FAILURE), any(SubchannelPicker.class));
    verify(helper, times(2)).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
    // Another child balancer goes to READY.
    childHelpers.get(2).updateBalancingState(READY, subchannelPickers[2]);
    verify(helper).updateBalancingState(eq(READY), pickerCaptor.capture());
    assertThat(pickerCaptor.getValue()).isInstanceOf(WeightedRandomPicker.class);
    WeightedRandomPicker overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
    assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[2], subchannelPickers[2]));
    // Another child balancer goes to READY.
    childHelpers.get(3).updateBalancingState(READY, subchannelPickers[3]);
    verify(helper, times(2)).updateBalancingState(eq(READY), pickerCaptor.capture());
    overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
    assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[2], subchannelPickers[2]), new WeightedChildPicker(weights[3], subchannelPickers[3]));
    // Another child balancer goes to READY.
    childHelpers.get(0).updateBalancingState(READY, subchannelPickers[0]);
    verify(helper, times(3)).updateBalancingState(eq(READY), pickerCaptor.capture());
    overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
    assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[0], subchannelPickers[0]), new WeightedChildPicker(weights[2], subchannelPickers[2]), new WeightedChildPicker(weights[3], subchannelPickers[3]));
    // One of READY child balancers goes to TRANSIENT_FAILURE.
    childHelpers.get(2).updateBalancingState(TRANSIENT_FAILURE, failurePickers[2]);
    verify(helper, times(4)).updateBalancingState(eq(READY), pickerCaptor.capture());
    overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
    assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[0], subchannelPickers[0]), new WeightedChildPicker(weights[3], subchannelPickers[3]));
    // All child balancers go to TRANSIENT_FAILURE.
    childHelpers.get(3).updateBalancingState(TRANSIENT_FAILURE, failurePickers[3]);
    childHelpers.get(0).updateBalancingState(TRANSIENT_FAILURE, failurePickers[0]);
    verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
    overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
    assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[0], failurePickers[0]), new WeightedChildPicker(weights[1], failurePickers[1]), new WeightedChildPicker(weights[2], failurePickers[2]), new WeightedChildPicker(weights[3], failurePickers[3]));
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) ErrorPicker(io.grpc.xds.XdsSubchannelPickers.ErrorPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) WeightedChildPicker(io.grpc.xds.WeightedRandomPicker.WeightedChildPicker) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) Test(org.junit.Test)

Example 9 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class WeightedTargetLoadBalancerTest method handleNameResolutionError.

@Test
public void handleNameResolutionError() {
    ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(null);
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
    // Error before any child balancer created.
    weightedTargetLb.handleNameResolutionError(Status.DATA_LOSS);
    verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
    PickResult pickResult = pickerCaptor.getValue().pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(pickResult.getStatus().getCode()).isEqualTo(Status.Code.DATA_LOSS);
    // Child configs updated.
    Map<String, WeightedPolicySelection> targets = ImmutableMap.of(// {foo, 10, config0}
    "target0", weightedLbConfig0, // {bar, 20, config1}
    "target1", weightedLbConfig1, // {bar, 30, config2}
    "target2", weightedLbConfig2, // {foo, 40, config3}
    "target3", weightedLbConfig3);
    weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(targets)).build());
    verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
    // Error after child balancers created.
    weightedTargetLb.handleNameResolutionError(Status.ABORTED);
    for (LoadBalancer childBalancer : childBalancers) {
        verify(childBalancer).handleNameResolutionError(statusCaptor.capture());
        assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.ABORTED);
    }
}
Also used : Status(io.grpc.Status) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PickResult(io.grpc.LoadBalancer.PickResult) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) LoadBalancer(io.grpc.LoadBalancer) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) Test(org.junit.Test)

Example 10 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class ClusterResolverLoadBalancer method generateEdsBasedPriorityChildConfigs.

/**
 * Generates configs to be used in the priority LB policy for priorities in an EDS cluster.
 *
 * <p>priority LB -> cluster_impl LB (one per priority) -> (weighted_target LB
 * -> round_robin / least_request_experimental (one per locality)) / ring_hash_experimental
 */
private static Map<String, PriorityChildConfig> generateEdsBasedPriorityChildConfigs(String cluster, @Nullable String edsServiceName, @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext, PolicySelection endpointLbPolicy, LoadBalancerRegistry lbRegistry, Map<String, Map<Locality, Integer>> prioritizedLocalityWeights, List<DropOverload> dropOverloads) {
    Map<String, PriorityChildConfig> configs = new HashMap<>();
    for (String priority : prioritizedLocalityWeights.keySet()) {
        PolicySelection leafPolicy = endpointLbPolicy;
        // product of each endpoint's weight and the weight of the locality it belongs to.
        if (endpointLbPolicy.getProvider().getPolicyName().equals("round_robin") || endpointLbPolicy.getProvider().getPolicyName().equals("least_request_experimental")) {
            Map<Locality, Integer> localityWeights = prioritizedLocalityWeights.get(priority);
            Map<String, WeightedPolicySelection> targets = new HashMap<>();
            for (Locality locality : localityWeights.keySet()) {
                int weight = localityWeights.get(locality);
                WeightedPolicySelection target = new WeightedPolicySelection(weight, endpointLbPolicy);
                targets.put(localityName(locality), target);
            }
            LoadBalancerProvider weightedTargetLbProvider = lbRegistry.getProvider(WEIGHTED_TARGET_POLICY_NAME);
            WeightedTargetConfig weightedTargetConfig = new WeightedTargetConfig(Collections.unmodifiableMap(targets));
            leafPolicy = new PolicySelection(weightedTargetLbProvider, weightedTargetConfig);
        }
        ClusterImplConfig clusterImplConfig = new ClusterImplConfig(cluster, edsServiceName, lrsServerInfo, maxConcurrentRequests, dropOverloads, leafPolicy, tlsContext);
        LoadBalancerProvider clusterImplLbProvider = lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
        PolicySelection clusterImplPolicy = new PolicySelection(clusterImplLbProvider, clusterImplConfig);
        PriorityChildConfig priorityChildConfig = new PriorityChildConfig(clusterImplPolicy, true);
        configs.put(priority, priorityChildConfig);
    }
    return configs;
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) HashMap(java.util.HashMap) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)

Aggregations

WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)17 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)15 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)13 Test (org.junit.Test)11 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)10 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)9 LoadBalancerProvider (io.grpc.LoadBalancerProvider)8 Subchannel (io.grpc.LoadBalancer.Subchannel)6 PickResult (io.grpc.LoadBalancer.PickResult)5 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)5 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)3 Metadata (io.grpc.Metadata)3 ClusterResolverConfig (io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig)3 LocalityLbEndpoints (io.grpc.xds.Endpoints.LocalityLbEndpoints)3 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)3 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 Helper (io.grpc.LoadBalancer.Helper)2