use of io.grpc.EquivalentAddressGroup in project grpc-java by grpc.
the class GrpclbState method useFallbackBackends.
/**
* Populate backend servers to be used from the fallback backends.
*/
private void useFallbackBackends() {
usingFallbackBackends = true;
logger.log(ChannelLogLevel.INFO, "[grpclb-<{0}>] Using fallback backends", serviceName);
List<DropEntry> newDropList = new ArrayList<>();
List<BackendAddressGroup> newBackendAddrList = new ArrayList<>();
for (EquivalentAddressGroup eag : fallbackBackendList) {
newDropList.add(null);
newBackendAddrList.add(new BackendAddressGroup(eag, null));
}
updateServerList(newDropList, newBackendAddrList, null);
}
use of io.grpc.EquivalentAddressGroup in project grpc-java by grpc.
the class HealthCheckingLoadBalancerFactoryTest method setup.
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
for (int i = 0; i < NUM_SUBCHANNELS; i++) {
HealthImpl healthImpl = new HealthImpl();
healthImpls[i] = healthImpl;
Server server = InProcessServerBuilder.forName("health-check-test-" + i).addService(healthImpl).directExecutor().build().start();
servers[i] = server;
ManagedChannel channel = InProcessChannelBuilder.forName("health-check-test-" + i).directExecutor().build();
channels[i] = channel;
EquivalentAddressGroup eag = new EquivalentAddressGroup(new FakeSocketAddress("address-" + i));
eags[i] = eag;
List<EquivalentAddressGroup> eagList = Arrays.asList(eag);
eagLists[i] = eagList;
mockStateListeners[i] = mock(SubchannelStateListener.class);
}
resolvedAddressList = Arrays.asList(eags);
when(backoffPolicyProvider.get()).thenReturn(backoffPolicy1, backoffPolicy2);
when(backoffPolicy1.nextBackoffNanos()).thenReturn(11L, 21L, 31L);
when(backoffPolicy2.nextBackoffNanos()).thenReturn(12L, 22L, 32L);
hcLbFactory = new HealthCheckingLoadBalancerFactory(origLbFactory, backoffPolicyProvider, clock.getStopwatchSupplier());
hcLb = hcLbFactory.newLoadBalancer(origHelper);
// Make sure all calls into the hcLb is from the syncContext
hcLbEventDelivery = new LoadBalancer() {
// Per LoadBalancer API, no more callbacks will be called after shutdown() is called.
boolean shutdown;
@Override
public void handleResolvedAddresses(final ResolvedAddresses resolvedAddresses) {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!shutdown) {
hcLb.handleResolvedAddresses(resolvedAddresses);
}
}
});
}
@Override
public void handleNameResolutionError(Status error) {
throw new AssertionError("Not supposed to be called");
}
@Override
public void shutdown() {
syncContext.execute(new Runnable() {
@Override
public void run() {
if (!shutdown) {
shutdown = true;
hcLb.shutdown();
}
}
});
}
};
verify(origLbFactory).newLoadBalancer(any(Helper.class));
}
use of io.grpc.EquivalentAddressGroup 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);
}
use of io.grpc.EquivalentAddressGroup 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]));
}
use of io.grpc.EquivalentAddressGroup 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);
}
}
Aggregations