use of io.grpc.ResolvedServerInfoGroup in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method nameResolutionFailsThenRecoverToDelegate.
@Test
public void nameResolutionFailsThenRecoverToDelegate() {
Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
deliverNameResolutionError(error);
verify(helper).updatePicker(pickerCaptor.capture());
ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
assertSame(error, errorPicker.result.getStatus());
// Recover with a subsequent success
List<ResolvedServerInfoGroup> resolvedServers = createResolvedServerInfoGroupList(false);
Attributes resolutionAttrs = Attributes.newBuilder().set(RESOLUTION_ATTR, "yeah").build();
deliverResolvedAddresses(resolvedServers, resolutionAttrs);
verify(pickFirstBalancerFactory).newLoadBalancer(helper);
verify(pickFirstBalancer).handleResolvedAddresses(eq(resolvedServers), eq(resolutionAttrs));
verifyNoMoreInteractions(roundRobinBalancerFactory);
verifyNoMoreInteractions(roundRobinBalancer);
}
use of io.grpc.ResolvedServerInfoGroup in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method grpclbThenNameResolutionFails.
@Test
public void grpclbThenNameResolutionFails() {
InOrder inOrder = inOrder(helper);
// Go to GRPCLB first
List<ResolvedServerInfoGroup> grpclbResolutionList = createResolvedServerInfoGroupList(true, true);
Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
assertNull(balancer.getDelegate());
verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
assertEquals(1, fakeOobChannels.size());
ManagedChannel oobChannel = fakeOobChannels.poll();
verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
// Let name resolution fail before round-robin list is ready
Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
deliverNameResolutionError(error);
inOrder.verify(helper).updatePicker(pickerCaptor.capture());
ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
assertSame(error, errorPicker.result.getStatus());
assertFalse(oobChannel.isShutdown());
// Simulate receiving LB response
List<ServerEntry> backends = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "TOKEN1"), new ServerEntry("127.0.0.1", 2010, "TOKEN2"));
verify(helper, never()).runSerialized(any(Runnable.class));
lbResponseObserver.onNext(buildInitialResponse());
lbResponseObserver.onNext(buildLbResponse(backends));
verify(helper, times(2)).runSerialized(any(Runnable.class));
inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(0).addr)), any(Attributes.class));
inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(1).addr)), any(Attributes.class));
}
use of io.grpc.ResolvedServerInfoGroup in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method nameResolutionFailsThenRecoverToGrpclb.
@Test
public void nameResolutionFailsThenRecoverToGrpclb() {
Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
deliverNameResolutionError(error);
verify(helper).updatePicker(pickerCaptor.capture());
ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
assertSame(error, errorPicker.result.getStatus());
// Recover with a subsequent success
List<ResolvedServerInfoGroup> resolvedServers = createResolvedServerInfoGroupList(true);
EquivalentAddressGroup eag = resolvedServers.get(0).toEquivalentAddressGroup();
Attributes resolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
deliverResolvedAddresses(resolvedServers, resolutionAttrs);
assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
assertNull(balancer.getDelegate());
verify(helper).createOobChannel(eq(eag), eq(lbAuthority(0)));
verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
verifyNoMoreInteractions(pickFirstBalancerFactory);
verifyNoMoreInteractions(pickFirstBalancer);
verifyNoMoreInteractions(roundRobinBalancerFactory);
verifyNoMoreInteractions(roundRobinBalancer);
}
use of io.grpc.ResolvedServerInfoGroup in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method grpclbBalanerCommErrors.
@Test
public void grpclbBalanerCommErrors() {
InOrder inOrder = inOrder(helper, mockLbService);
// Make the first LB address fail to connect
failingLbAuthorities.add(lbAuthority(0));
List<ResolvedServerInfoGroup> grpclbResolutionList = createResolvedServerInfoGroupList(true, true, true);
Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
// First LB addr fails to connect
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
inOrder.verify(helper).updatePicker(isA(ErrorPicker.class));
assertEquals(2, fakeOobChannels.size());
assertTrue(fakeOobChannels.poll().isShutdown());
// Will move on to second LB addr
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(1).toEquivalentAddressGroup()), eq(lbAuthority(1)));
inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
assertEquals(1, lbRequestObservers.size());
lbRequestObservers.poll();
assertEquals(1, fakeOobChannels.size());
assertFalse(fakeOobChannels.peek().isShutdown());
Status error1 = Status.UNAVAILABLE.withDescription("error1");
// Simulate that the stream on the second LB failed
lbResponseObserver.onError(error1.asException());
assertTrue(fakeOobChannels.poll().isShutdown());
inOrder.verify(helper).updatePicker(pickerCaptor.capture());
ErrorPicker errorPicker = (ErrorPicker) pickerCaptor.getValue();
assertEquals(error1.getCode(), errorPicker.result.getStatus().getCode());
assertTrue(errorPicker.result.getStatus().getDescription().contains(error1.getDescription()));
// Move on to the third LB.
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(2).toEquivalentAddressGroup()), eq(lbAuthority(2)));
inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
lbResponseObserver = lbResponseObserverCaptor.getValue();
assertEquals(1, lbRequestObservers.size());
lbRequestObservers.poll();
assertEquals(1, fakeOobChannels.size());
assertFalse(fakeOobChannels.peek().isShutdown());
// Simulate that the stream on the third LB closed without error. It is treated
// as an error.
lbResponseObserver.onCompleted();
assertTrue(fakeOobChannels.poll().isShutdown());
// Loop back to the first LB addr, which still fails.
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
inOrder.verify(helper).updatePicker(isA(ErrorPicker.class));
assertEquals(2, fakeOobChannels.size());
assertTrue(fakeOobChannels.poll().isShutdown());
// Will move on to second LB addr
inOrder.verify(helper).createOobChannel(eq(grpclbResolutionList.get(1).toEquivalentAddressGroup()), eq(lbAuthority(1)));
inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
lbResponseObserver = lbResponseObserverCaptor.getValue();
assertEquals(1, lbRequestObservers.size());
assertEquals(1, fakeOobChannels.size());
assertFalse(fakeOobChannels.peek().isShutdown());
// Finally it works.
lbResponseObserver.onNext(buildInitialResponse());
List<ServerEntry> backends = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "token001"), new ServerEntry("127.0.0.1", 2010, "token002"));
lbResponseObserver.onNext(buildLbResponse(backends));
inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(0).addr)), any(Attributes.class));
inOrder.verify(helper).createSubchannel(eq(new EquivalentAddressGroup(backends.get(1).addr)), any(Attributes.class));
inOrder.verify(helper).updatePicker(same(GrpclbLoadBalancer.BUFFER_PICKER));
inOrder.verifyNoMoreInteractions();
}
use of io.grpc.ResolvedServerInfoGroup in project grpc-java by grpc.
the class GrpclbLoadBalancer method handleResolvedAddresses.
@Override
public void handleResolvedAddresses(List<ResolvedServerInfoGroup> updatedServers, Attributes attributes) {
LbPolicy newLbPolicy = attributes.get(GrpclbConstants.ATTR_LB_POLICY);
// LB addresses and backend addresses are treated separately
List<LbAddressGroup> newLbAddressGroups = new ArrayList<LbAddressGroup>();
List<ResolvedServerInfoGroup> newBackendServerInfoGroups = new ArrayList<ResolvedServerInfoGroup>();
for (ResolvedServerInfoGroup serverInfoGroup : updatedServers) {
String lbAddrAuthority = serverInfoGroup.getAttributes().get(GrpclbConstants.ATTR_LB_ADDR_AUTHORITY);
EquivalentAddressGroup eag = serverInfoGroup.toEquivalentAddressGroup();
if (lbAddrAuthority != null) {
newLbAddressGroups.add(new LbAddressGroup(eag, lbAddrAuthority));
} else {
newBackendServerInfoGroups.add(serverInfoGroup);
}
}
if (newBackendServerInfoGroups.isEmpty()) {
// handleResolvedAddresses()'s javadoc has guaranteed updatedServers is never empty.
checkState(!newLbAddressGroups.isEmpty(), "No backend address nor LB address. updatedServers=%s", updatedServers);
if (newLbPolicy != LbPolicy.GRPCLB) {
newLbPolicy = LbPolicy.GRPCLB;
logger.log(Level.FINE, "[{0}] Switching to GRPCLB because all addresses are balancers", logId);
}
}
if (newLbPolicy == null) {
logger.log(Level.FINE, "[{0}] New config missing policy. Using PICK_FIRST", logId);
newLbPolicy = LbPolicy.PICK_FIRST;
}
// Switch LB policy if requested
if (newLbPolicy != lbPolicy) {
shutdownDelegate();
shutdownLbComm();
lbAddressGroups = null;
currentLbIndex = 0;
switch(newLbPolicy) {
case PICK_FIRST:
delegate = checkNotNull(pickFirstBalancerFactory.newLoadBalancer(helper), "pickFirstBalancerFactory.newLoadBalancer()");
break;
case ROUND_ROBIN:
delegate = checkNotNull(roundRobinBalancerFactory.newLoadBalancer(helper), "roundRobinBalancerFactory.newLoadBalancer()");
break;
default:
}
}
lbPolicy = newLbPolicy;
// Consume the new addresses
switch(lbPolicy) {
case PICK_FIRST:
case ROUND_ROBIN:
checkNotNull(delegate, "delegate should not be null. newLbPolicy=" + newLbPolicy);
delegate.handleResolvedAddresses(newBackendServerInfoGroups, attributes);
break;
case GRPCLB:
if (newLbAddressGroups.isEmpty()) {
shutdownLbComm();
lbAddressGroups = null;
handleGrpclbError(Status.UNAVAILABLE.withDescription("NameResolver returned no LB address while asking for GRPCLB"));
} else {
// See if the currently used LB server is in the new list.
int newIndexOfCurrentLb = -1;
if (lbAddressGroups != null) {
LbAddressGroup currentLb = lbAddressGroups.get(currentLbIndex);
newIndexOfCurrentLb = newLbAddressGroups.indexOf(currentLb);
}
lbAddressGroups = newLbAddressGroups;
if (newIndexOfCurrentLb == -1) {
shutdownLbComm();
currentLbIndex = 0;
startLbComm();
} else {
// Current LB is still in the list, calibrate index.
currentLbIndex = newIndexOfCurrentLb;
}
}
break;
default:
}
}
Aggregations