Search in sources :

Example 11 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method dropRpcsWithRespectToLbConfigDropCategories.

@Test
public void dropRpcsWithRespectToLbConfigDropCategories() {
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.singletonList(DropOverload.create("throttle", 500_000)), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    EquivalentAddressGroup endpoint = makeAddress("endpoint-addr", locality);
    deliverAddressesAndConfig(Collections.singletonList(endpoint), config);
    when(mockRandom.nextInt(anyInt())).thenReturn(499_999, 999_999, 1_000_000);
    // one leaf balancer
    assertThat(downstreamBalancers).hasSize(1);
    FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(leafBalancer.name).isEqualTo("round_robin");
    assertThat(Iterables.getOnlyElement(leafBalancer.addresses).getAddresses()).isEqualTo(endpoint.getAddresses());
    Subchannel subchannel = leafBalancer.helper.createSubchannel(CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build());
    leafBalancer.deliverSubchannelState(subchannel, ConnectivityState.READY);
    assertThat(currentState).isEqualTo(ConnectivityState.READY);
    PickResult result = currentPicker.pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(result.getStatus().isOk()).isFalse();
    assertThat(result.getStatus().getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(result.getStatus().getDescription()).isEqualTo("Dropped: throttle");
    ClusterStats clusterStats = Iterables.getOnlyElement(loadStatsManager.getClusterStatsReports(CLUSTER));
    assertThat(clusterStats.clusterServiceName()).isEqualTo(EDS_SERVICE_NAME);
    assertThat(Iterables.getOnlyElement(clusterStats.droppedRequestsList()).category()).isEqualTo("throttle");
    assertThat(Iterables.getOnlyElement(clusterStats.droppedRequestsList()).droppedCount()).isEqualTo(1L);
    assertThat(clusterStats.totalDroppedRequests()).isEqualTo(1L);
    // Config update updates drop policies.
    config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.singletonList(DropOverload.create("lb", 1_000_000)), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(Collections.singletonList(endpoint)).setAttributes(Attributes.newBuilder().set(InternalXdsAttributes.XDS_CLIENT_POOL, xdsClientPool).build()).setLoadBalancingPolicyConfig(config).build());
    result = currentPicker.pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(result.getStatus().isOk()).isFalse();
    assertThat(result.getStatus().getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(result.getStatus().getDescription()).isEqualTo("Dropped: lb");
    clusterStats = Iterables.getOnlyElement(loadStatsManager.getClusterStatsReports(CLUSTER));
    assertThat(clusterStats.clusterServiceName()).isEqualTo(EDS_SERVICE_NAME);
    assertThat(Iterables.getOnlyElement(clusterStats.droppedRequestsList()).category()).isEqualTo("lb");
    assertThat(Iterables.getOnlyElement(clusterStats.droppedRequestsList()).droppedCount()).isEqualTo(1L);
    assertThat(clusterStats.totalDroppedRequests()).isEqualTo(1L);
    result = currentPicker.pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(result.getStatus().isOk()).isTrue();
}
Also used : ClusterStats(io.grpc.xds.Stats.ClusterStats) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalancerProvider(io.grpc.LoadBalancerProvider) PickResult(io.grpc.LoadBalancer.PickResult) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 12 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method nameResolutionError_afterChildPolicyInstantiated_propagateToDownstream.

@Test
public void nameResolutionError_afterChildPolicyInstantiated_propagateToDownstream() {
    FakeLoadBalancerProvider weightedTargetProvider = new FakeLoadBalancerProvider(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME);
    Object weightedTargetConfig = new Object();
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    EquivalentAddressGroup endpoint = makeAddress("endpoint-addr", locality);
    deliverAddressesAndConfig(Collections.singletonList(endpoint), config);
    FakeLoadBalancer childBalancer = Iterables.getOnlyElement(downstreamBalancers);
    loadBalancer.handleNameResolutionError(Status.UNAVAILABLE.withDescription("cannot reach server"));
    assertThat(childBalancer.upstreamError.getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(childBalancer.upstreamError.getDescription()).isEqualTo("cannot reach server");
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 13 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterManagerLoadBalancerTest method buildConfig.

private ClusterManagerConfig buildConfig(Map<String, String> childPolicies) {
    Map<String, PolicySelection> childPolicySelections = new LinkedHashMap<>();
    for (String name : childPolicies.keySet()) {
        String childPolicyName = childPolicies.get(name);
        Object childConfig = lbConfigInventory.get(name);
        PolicySelection policy = new PolicySelection(new FakeLoadBalancerProvider(childPolicyName), childConfig);
        childPolicySelections.put(name, policy);
    }
    return new ClusterManagerConfig(childPolicySelections);
}
Also used : ClusterManagerConfig(io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class PriorityLoadBalancerProviderTest method priorityLbConfig_emptyPriorities.

@SuppressWarnings("ExpectedExceptionChecker")
@Test
public void priorityLbConfig_emptyPriorities() {
    Map<String, PriorityChildConfig> childConfigs = ImmutableMap.of("p0", new PriorityChildConfig(new PolicySelection(mock(LoadBalancerProvider.class), null), true));
    List<String> priorities = ImmutableList.of();
    thrown.expect(IllegalArgumentException.class);
    new PriorityLbConfig(childConfigs, priorities);
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Test(org.junit.Test)

Example 15 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class PriorityLoadBalancerTest method typicalPriorityFailOverFlowWithIdleUpdate.

@Test
public void typicalPriorityFailOverFlowWithIdleUpdate() {
    PriorityChildConfig priorityChildConfig0 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityChildConfig priorityChildConfig1 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityChildConfig priorityChildConfig2 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityChildConfig priorityChildConfig3 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityLbConfig priorityLbConfig = new PriorityLbConfig(ImmutableMap.of("p0", priorityChildConfig0, "p1", priorityChildConfig1, "p2", priorityChildConfig2, "p3", priorityChildConfig3), ImmutableList.of("p0", "p1", "p2", "p3"));
    priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(priorityLbConfig).build());
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
    LoadBalancer balancer0 = Iterables.getLast(fooBalancers);
    Helper helper0 = Iterables.getOnlyElement(fooHelpers);
    // p0 gets IDLE.
    helper0.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // p0 fails over to p1 immediately.
    helper0.updateBalancingState(TRANSIENT_FAILURE, new ErrorPicker(Status.ABORTED));
    assertLatestConnectivityState(CONNECTING);
    assertThat(fooBalancers).hasSize(2);
    assertThat(fooHelpers).hasSize(2);
    LoadBalancer balancer1 = Iterables.getLast(fooBalancers);
    // p1 timeout, and fails over to p2
    fakeClock.forwardTime(10, TimeUnit.SECONDS);
    assertLatestConnectivityState(CONNECTING);
    assertThat(fooBalancers).hasSize(3);
    assertThat(fooHelpers).hasSize(3);
    LoadBalancer balancer2 = Iterables.getLast(fooBalancers);
    Helper helper2 = Iterables.getLast(fooHelpers);
    // p2 gets IDLE
    helper2.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // p0 gets back to IDLE
    helper0.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // p2 fails but does not affect overall picker
    helper2.updateBalancingState(TRANSIENT_FAILURE, new ErrorPicker(Status.UNAVAILABLE));
    assertCurrentPickerIsBufferPicker();
    // p0 fails over to p3 immediately since p1 already timeout and p2 already in TRANSIENT_FAILURE.
    helper0.updateBalancingState(TRANSIENT_FAILURE, new ErrorPicker(Status.UNAVAILABLE));
    assertLatestConnectivityState(CONNECTING);
    assertThat(fooBalancers).hasSize(4);
    assertThat(fooHelpers).hasSize(4);
    LoadBalancer balancer3 = Iterables.getLast(fooBalancers);
    Helper helper3 = Iterables.getLast(fooHelpers);
    // p3 timeout then the channel should go to TRANSIENT_FAILURE
    fakeClock.forwardTime(10, TimeUnit.SECONDS);
    assertCurrentPickerReturnsError(Status.Code.UNAVAILABLE, "timeout");
    // p3 fails then the picker should have error status updated
    helper3.updateBalancingState(TRANSIENT_FAILURE, new ErrorPicker(Status.DATA_LOSS.withDescription("foo")));
    assertCurrentPickerReturnsError(Status.Code.DATA_LOSS, "foo");
    // p2 gets back to IDLE
    helper2.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // p0 gets back to IDLE
    helper0.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // p0 fails over to p2 and picker is updated to p2's existing picker.
    helper0.updateBalancingState(TRANSIENT_FAILURE, new ErrorPicker(Status.UNAVAILABLE));
    assertCurrentPickerIsBufferPicker();
    // Deactivate child balancer get deleted.
    fakeClock.forwardTime(15, TimeUnit.MINUTES);
    verify(balancer0, never()).shutdown();
    verify(balancer1, never()).shutdown();
    verify(balancer2, never()).shutdown();
    verify(balancer3).shutdown();
}
Also used : Helper(io.grpc.LoadBalancer.Helper) PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) ErrorPicker(io.grpc.xds.XdsSubchannelPickers.ErrorPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) LoadBalancer(io.grpc.LoadBalancer) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Test(org.junit.Test)

Aggregations

PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)36 Test (org.junit.Test)27 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)20 LoadBalancerProvider (io.grpc.LoadBalancerProvider)13 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)12 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)12 Helper (io.grpc.LoadBalancer.Helper)10 ConfigOrError (io.grpc.NameResolver.ConfigOrError)10 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)10 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)10 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)9 Subchannel (io.grpc.LoadBalancer.Subchannel)8 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)7 PickResult (io.grpc.LoadBalancer.PickResult)6 LoadBalancer (io.grpc.LoadBalancer)5 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 SocketAddress (java.net.SocketAddress)4 Map (java.util.Map)4 Attributes (io.grpc.Attributes)3