Search in sources :

Example 6 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method recordLoadStats.

@Test
public void recordLoadStats() {
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    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 leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    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()).isTrue();
    ClientStreamTracer streamTracer1 = result.getStreamTracerFactory().newClientStreamTracer(ClientStreamTracer.StreamInfo.newBuilder().build(), // first RPC call
    new Metadata());
    ClientStreamTracer streamTracer2 = result.getStreamTracerFactory().newClientStreamTracer(ClientStreamTracer.StreamInfo.newBuilder().build(), // second RPC call
    new Metadata());
    ClientStreamTracer streamTracer3 = result.getStreamTracerFactory().newClientStreamTracer(ClientStreamTracer.StreamInfo.newBuilder().build(), // third RPC call
    new Metadata());
    streamTracer1.streamClosed(Status.OK);
    streamTracer2.streamClosed(Status.UNAVAILABLE);
    ClusterStats clusterStats = Iterables.getOnlyElement(loadStatsManager.getClusterStatsReports(CLUSTER));
    UpstreamLocalityStats localityStats = Iterables.getOnlyElement(clusterStats.upstreamLocalityStatsList());
    assertThat(localityStats.locality()).isEqualTo(locality);
    assertThat(localityStats.totalIssuedRequests()).isEqualTo(3L);
    assertThat(localityStats.totalSuccessfulRequests()).isEqualTo(1L);
    assertThat(localityStats.totalErrorRequests()).isEqualTo(1L);
    assertThat(localityStats.totalRequestsInProgress()).isEqualTo(1L);
    streamTracer3.streamClosed(Status.OK);
    // stats recorder released
    subchannel.shutdown();
    clusterStats = Iterables.getOnlyElement(loadStatsManager.getClusterStatsReports(CLUSTER));
    // Locality load is reported for one last time in case of loads occurred since the previous
    // load report.
    localityStats = Iterables.getOnlyElement(clusterStats.upstreamLocalityStatsList());
    assertThat(localityStats.locality()).isEqualTo(locality);
    assertThat(localityStats.totalIssuedRequests()).isEqualTo(0L);
    assertThat(localityStats.totalSuccessfulRequests()).isEqualTo(1L);
    assertThat(localityStats.totalErrorRequests()).isEqualTo(0L);
    assertThat(localityStats.totalRequestsInProgress()).isEqualTo(0L);
    clusterStats = Iterables.getOnlyElement(loadStatsManager.getClusterStatsReports(CLUSTER));
    // no longer reported
    assertThat(clusterStats.upstreamLocalityStatsList()).isEmpty();
}
Also used : UpstreamLocalityStats(io.grpc.xds.Stats.UpstreamLocalityStats) ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) ClusterStats(io.grpc.xds.Stats.ClusterStats) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalancerProvider(io.grpc.LoadBalancerProvider) PickResult(io.grpc.LoadBalancer.PickResult) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 7 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider 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 8 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.

the class ClusterImplLoadBalancerProviderTest method provided.

@Test
public void provided() {
    LoadBalancerProvider provider = LoadBalancerRegistry.getDefaultRegistry().getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
    assertThat(provider).isInstanceOf(ClusterImplLoadBalancerProvider.class);
}
Also used : LoadBalancerProvider(io.grpc.LoadBalancerProvider) Test(org.junit.Test)

Example 9 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.

the class ClusterImplLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    when(helper.getAuthority()).thenReturn("api.google.com");
    LoadBalancerProvider provider = new ClusterImplLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(ClusterImplLoadBalancer.class);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) LoadBalancerProvider(io.grpc.LoadBalancerProvider) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 10 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.

the class ClusterManagerLoadBalancerProviderTest method registered.

@Test
public void registered() {
    LoadBalancerProvider provider = LoadBalancerRegistry.getDefaultRegistry().getProvider("cluster_manager_experimental");
    assertThat(provider).isInstanceOf(ClusterManagerLoadBalancerProvider.class);
}
Also used : LoadBalancerProvider(io.grpc.LoadBalancerProvider) Test(org.junit.Test)

Aggregations

LoadBalancerProvider (io.grpc.LoadBalancerProvider)25 Test (org.junit.Test)17 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)11 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)9 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)8 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)8 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)6 Subchannel (io.grpc.LoadBalancer.Subchannel)6 Helper (io.grpc.LoadBalancer.Helper)5 LoadBalancer (io.grpc.LoadBalancer)4 PickResult (io.grpc.LoadBalancer.PickResult)4 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 Metadata (io.grpc.Metadata)3 ConfigOrError (io.grpc.NameResolver.ConfigOrError)3 Map (java.util.Map)3 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 SynchronizationContext (io.grpc.SynchronizationContext)2 ClusterManagerConfig (io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig)2