use of io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig in project grpc-java by grpc.
the class LeastRequestLoadBalancerProviderTest method parseLoadBalancingConfig_valid.
@Test
public void parseLoadBalancingConfig_valid() throws IOException {
String lbConfig = "{\"choiceCount\" : 3}";
ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(parseJsonObject(lbConfig));
assertThat(configOrError.getConfig()).isNotNull();
LeastRequestConfig config = (LeastRequestConfig) configOrError.getConfig();
assertThat(config.choiceCount).isEqualTo(3);
}
use of io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig in project grpc-java by grpc.
the class CdsLoadBalancer2Test method discoverTopLevelLogicalDnsCluster.
@Test
public void discoverTopLevelLogicalDnsCluster() {
CdsUpdate update = CdsUpdate.forLogicalDns(CLUSTER, DNS_HOST_NAME, LRS_SERVER_INFO, 100L, upstreamTlsContext).leastRequestLbPolicy(3).build();
xdsClient.deliverCdsUpdate(CLUSTER, update);
assertThat(childBalancers).hasSize(1);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(CLUSTER_RESOLVER_POLICY_NAME);
ClusterResolverConfig childLbConfig = (ClusterResolverConfig) childBalancer.config;
assertThat(childLbConfig.discoveryMechanisms).hasSize(1);
DiscoveryMechanism instance = Iterables.getOnlyElement(childLbConfig.discoveryMechanisms);
assertDiscoveryMechanism(instance, CLUSTER, DiscoveryMechanism.Type.LOGICAL_DNS, null, DNS_HOST_NAME, LRS_SERVER_INFO, 100L, upstreamTlsContext);
assertThat(childLbConfig.lbPolicy.getProvider().getPolicyName()).isEqualTo("least_request_experimental");
assertThat(((LeastRequestConfig) childLbConfig.lbPolicy.getConfig()).choiceCount).isEqualTo(3);
}
use of io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig in project grpc-java by grpc.
the class LeastRequestLoadBalancerTest method pickerLeastRequest.
@Test
public void pickerLeastRequest() throws Exception {
int choiceCount = 2;
// This should add inFlight counters to all subchannels.
loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).setLoadBalancingPolicyConfig(new LeastRequestConfig(choiceCount)).build());
assertEquals(3, loadBalancer.getSubchannels().size());
List<Subchannel> subchannels = Lists.newArrayList(loadBalancer.getSubchannels());
// Make sure all inFlight counters have started at 0
assertEquals(0, subchannels.get(0).getAttributes().get(IN_FLIGHTS).get());
assertEquals(0, subchannels.get(1).getAttributes().get(IN_FLIGHTS).get());
assertEquals(0, subchannels.get(2).getAttributes().get(IN_FLIGHTS).get());
for (Subchannel sc : subchannels) {
deliverSubchannelState(sc, ConnectivityStateInfo.forNonError(READY));
}
// Capture the active ReadyPicker once all subchannels are READY
verify(mockHelper, times(4)).updateBalancingState(any(ConnectivityState.class), pickerCaptor.capture());
assertThat(pickerCaptor.getValue()).isInstanceOf(ReadyPicker.class);
ReadyPicker picker = (ReadyPicker) pickerCaptor.getValue();
assertThat(picker.getList()).containsExactlyElementsIn(subchannels);
// Make random return 0, then 2 for the sample indexes.
when(mockRandom.nextInt(subchannels.size())).thenReturn(0, 2);
PickResult pickResult1 = picker.pickSubchannel(mockArgs);
verify(mockRandom, times(choiceCount)).nextInt(subchannels.size());
assertEquals(subchannels.get(0), pickResult1.getSubchannel());
// This simulates sending the actual RPC on the picked channel
ClientStreamTracer streamTracer1 = pickResult1.getStreamTracerFactory().newClientStreamTracer(StreamInfo.newBuilder().build(), new Metadata());
streamTracer1.streamCreated(Attributes.EMPTY, new Metadata());
assertEquals(1, pickResult1.getSubchannel().getAttributes().get(IN_FLIGHTS).get());
// For the second pick it should pick the one with lower inFlight.
when(mockRandom.nextInt(subchannels.size())).thenReturn(0, 2);
PickResult pickResult2 = picker.pickSubchannel(mockArgs);
// Since this is the second pick we expect the total random samples to be choiceCount * 2
verify(mockRandom, times(choiceCount * 2)).nextInt(subchannels.size());
assertEquals(subchannels.get(2), pickResult2.getSubchannel());
// For the third pick we unavoidably pick subchannel with index 1.
when(mockRandom.nextInt(subchannels.size())).thenReturn(1, 1);
PickResult pickResult3 = picker.pickSubchannel(mockArgs);
verify(mockRandom, times(choiceCount * 3)).nextInt(subchannels.size());
assertEquals(subchannels.get(1), pickResult3.getSubchannel());
// Finally ensure a finished RPC decreases inFlight
streamTracer1.streamClosed(Status.OK);
assertEquals(0, pickResult1.getSubchannel().getAttributes().get(IN_FLIGHTS).get());
}
use of io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig in project grpc-java by grpc.
the class LeastRequestLoadBalancerTest method pickAfterConfigChange.
@Test
public void pickAfterConfigChange() {
final LeastRequestConfig oldConfig = new LeastRequestConfig(4);
final LeastRequestConfig newConfig = new LeastRequestConfig(6);
final Subchannel readySubchannel = subchannels.values().iterator().next();
loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(affinity).setLoadBalancingPolicyConfig(oldConfig).build());
deliverSubchannelState(readySubchannel, ConnectivityStateInfo.forNonError(READY));
verify(mockHelper, times(3)).createSubchannel(any(CreateSubchannelArgs.class));
verify(mockHelper, times(2)).updateBalancingState(any(ConnectivityState.class), pickerCaptor.capture());
// At this point it should use a ReadyPicker with oldConfig
pickerCaptor.getValue().pickSubchannel(mockArgs);
verify(mockRandom, times(oldConfig.choiceCount)).nextInt(1);
loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(affinity).setLoadBalancingPolicyConfig(newConfig).build());
verify(mockHelper, times(3)).updateBalancingState(any(ConnectivityState.class), pickerCaptor.capture());
// At this point it should use a ReadyPicker with newConfig
pickerCaptor.getValue().pickSubchannel(mockArgs);
verify(mockRandom, times(oldConfig.choiceCount + newConfig.choiceCount)).nextInt(1);
verifyNoMoreInteractions(mockHelper);
}
use of io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig in project grpc-java by grpc.
the class LeastRequestLoadBalancerTest method nameResolutionErrorWithActiveChannels.
@Test
public void nameResolutionErrorWithActiveChannels() throws Exception {
int choiceCount = 8;
final Subchannel readySubchannel = subchannels.values().iterator().next();
loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setLoadBalancingPolicyConfig(new LeastRequestConfig(choiceCount)).setAddresses(servers).setAttributes(affinity).build());
deliverSubchannelState(readySubchannel, ConnectivityStateInfo.forNonError(READY));
loadBalancer.handleNameResolutionError(Status.NOT_FOUND.withDescription("nameResolutionError"));
verify(mockHelper, times(3)).createSubchannel(any(CreateSubchannelArgs.class));
verify(mockHelper, times(2)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());
Iterator<ConnectivityState> stateIterator = stateCaptor.getAllValues().iterator();
assertEquals(CONNECTING, stateIterator.next());
assertEquals(READY, stateIterator.next());
LoadBalancer.PickResult pickResult = pickerCaptor.getValue().pickSubchannel(mockArgs);
verify(mockRandom, times(choiceCount)).nextInt(1);
assertEquals(readySubchannel, pickResult.getSubchannel());
assertEquals(Status.OK.getCode(), pickResult.getStatus().getCode());
LoadBalancer.PickResult pickResult2 = pickerCaptor.getValue().pickSubchannel(mockArgs);
verify(mockRandom, times(choiceCount * 2)).nextInt(1);
assertEquals(readySubchannel, pickResult2.getSubchannel());
verifyNoMoreInteractions(mockHelper);
}
Aggregations