Search in sources :

Example 6 with ResolvedServerInfoGroup

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);
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 7 with ResolvedServerInfoGroup

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));
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) InOrder(org.mockito.InOrder) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 8 with ResolvedServerInfoGroup

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);
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 9 with ResolvedServerInfoGroup

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();
}
Also used : Status(io.grpc.Status) ErrorPicker(io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker) InOrder(org.mockito.InOrder) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) Test(org.junit.Test)

Example 10 with ResolvedServerInfoGroup

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:
    }
}
Also used : LbPolicy(io.grpc.grpclb.GrpclbConstants.LbPolicy) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ArrayList(java.util.ArrayList) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup)

Aggregations

ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)16 Test (org.junit.Test)13 Attributes (io.grpc.Attributes)12 Status (io.grpc.Status)9 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 InOrder (org.mockito.InOrder)6 Subchannel (io.grpc.LoadBalancer.Subchannel)5 ResolvedServerInfo (io.grpc.ResolvedServerInfo)5 ErrorPicker (io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker)5 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 SocketAddress (java.net.SocketAddress)4 Helper (io.grpc.LoadBalancer.Helper)3 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)3 ManagedChannel (io.grpc.ManagedChannel)3 Metadata (io.grpc.Metadata)3 MethodDescriptor (io.grpc.MethodDescriptor)3 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)3 Matchers.anyString (org.mockito.Matchers.anyString)3 CallOptions (io.grpc.CallOptions)2 ArrayList (java.util.ArrayList)2