Search in sources :

Example 41 with EquivalentAddressGroup

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

the class XdsSdsClientServerTest method getBlockingStub.

private SimpleServiceGrpc.SimpleServiceBlockingStub getBlockingStub(final UpstreamTlsContext upstreamTlsContext, String overrideAuthority) throws URISyntaxException {
    ManagedChannelBuilder<?> channelBuilder = Grpc.newChannelBuilder("sdstest://localhost:" + port, XdsChannelCredentials.create(InsecureChannelCredentials.create()));
    if (overrideAuthority != null) {
        channelBuilder = channelBuilder.overrideAuthority(overrideAuthority);
    }
    InetSocketAddress socketAddress = new InetSocketAddress(Inet4Address.getLoopbackAddress(), port);
    tlsContextManagerForClient = new TlsContextManagerImpl(bootstrapInfoForClient);
    Attributes attrs = (upstreamTlsContext != null) ? Attributes.newBuilder().set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER, new SslContextProviderSupplier(upstreamTlsContext, tlsContextManagerForClient)).build() : Attributes.EMPTY;
    fakeNameResolverFactory.setServers(ImmutableList.of(new EquivalentAddressGroup(socketAddress, attrs)));
    return SimpleServiceGrpc.newBlockingStub(cleanupRule.register(channelBuilder.build()));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) TlsContextManagerImpl(io.grpc.xds.internal.sds.TlsContextManagerImpl) SslContextProviderSupplier(io.grpc.xds.internal.sds.SslContextProviderSupplier)

Example 42 with EquivalentAddressGroup

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

the class AddressFilterTest method filterAddresses.

@Test
public void filterAddresses() {
    Attributes.Key<String> key1 = Attributes.Key.create("key1");
    Attributes attributes1 = Attributes.newBuilder().set(key1, "value1").build();
    EquivalentAddressGroup eag0 = new EquivalentAddressGroup(new InetSocketAddress(8000));
    EquivalentAddressGroup eag1 = new EquivalentAddressGroup(new InetSocketAddress(8001), attributes1);
    EquivalentAddressGroup eag2 = new EquivalentAddressGroup(new InetSocketAddress(8002));
    EquivalentAddressGroup eag3 = new EquivalentAddressGroup(Arrays.<SocketAddress>asList(new InetSocketAddress(8003), new InetSocketAddress(8083)));
    eag0 = AddressFilter.setPathFilter(eag0, Arrays.asList("A", "C"));
    eag1 = AddressFilter.setPathFilter(eag1, Arrays.asList("A", "B"));
    eag2 = AddressFilter.setPathFilter(eag2, Arrays.asList("D", "C"));
    eag3 = AddressFilter.setPathFilter(eag3, Arrays.asList("A", "B"));
    List<EquivalentAddressGroup> addresses = AddressFilter.filter(Arrays.asList(eag0, eag1, eag2, eag3), "A");
    assertThat(addresses).hasSize(3);
    addresses = AddressFilter.filter(addresses, "B");
    assertThat(addresses).hasSize(2);
    EquivalentAddressGroup filteredAddress0 = addresses.get(0);
    EquivalentAddressGroup filteredAddress1 = addresses.get(1);
    assertThat(filteredAddress0.getAddresses()).containsExactlyElementsIn(eag1.getAddresses());
    assertThat(filteredAddress0.getAttributes().get(key1)).isEqualTo("value1");
    assertThat(filteredAddress1.getAddresses()).containsExactlyElementsIn(eag3.getAddresses());
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress) Attributes(io.grpc.Attributes) Test(org.junit.Test)

Example 43 with EquivalentAddressGroup

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

the class GrpclbLoadBalancerTest method createResolvedBackendAddresses.

private static List<EquivalentAddressGroup> createResolvedBackendAddresses(int n) {
    List<EquivalentAddressGroup> list = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        SocketAddress addr = new FakeSocketAddress("fake-address-" + i);
        list.add(new EquivalentAddressGroup(addr));
    }
    return list;
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ArrayList(java.util.ArrayList) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 44 with EquivalentAddressGroup

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

the class GrpclbLoadBalancerTest method subtestGrpclbFallbackConnectionLost.

// Fallback outside of the initial timeout, where all connections are lost.
private void subtestGrpclbFallbackConnectionLost(boolean balancerBroken, boolean allSubchannelsBroken) {
    long loadReportIntervalMillis = 1983;
    InOrder inOrder = inOrder(helper, mockLbService, subchannelPool);
    // Create balancer and backend addresses
    List<EquivalentAddressGroup> backendList = createResolvedBackendAddresses(2);
    List<EquivalentAddressGroup> grpclbBalancerList = createResolvedBalancerAddresses(1);
    deliverResolvedAddresses(backendList, grpclbBalancerList);
    inOrder.verify(helper).createOobChannel(eq(xattr(grpclbBalancerList)), eq(lbAuthority(0) + NO_USE_AUTHORITY_SUFFIX));
    // Attempted to connect to balancer
    assertEquals(1, fakeOobChannels.size());
    fakeOobChannels.poll();
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
    assertEquals(1, lbRequestObservers.size());
    StreamObserver<LoadBalanceRequest> lbRequestObserver = lbRequestObservers.poll();
    verify(lbRequestObserver).onNext(eq(LoadBalanceRequest.newBuilder().setInitialRequest(InitialLoadBalanceRequest.newBuilder().setName(SERVICE_AUTHORITY).build()).build()));
    lbResponseObserver.onNext(buildInitialResponse(loadReportIntervalMillis));
    // We don't care if these methods have been run.
    inOrder.verify(helper, atLeast(0)).getSynchronizationContext();
    inOrder.verify(helper, atLeast(0)).getScheduledExecutorService();
    inOrder.verifyNoMoreInteractions();
    // Balancer returns a server list
    List<ServerEntry> serverList = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "token0001"), new ServerEntry("127.0.0.1", 2010, "token0002"));
    lbResponseObserver.onNext(buildInitialResponse());
    lbResponseObserver.onNext(buildLbResponse(serverList));
    List<Subchannel> subchannels = fallbackTestVerifyUseOfBalancerBackendLists(inOrder, serverList);
    // Break connections
    if (balancerBroken) {
        lbResponseObserver.onError(Status.UNAVAILABLE.asException());
        // A new stream to LB is created
        inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
        lbResponseObserver = lbResponseObserverCaptor.getValue();
        assertEquals(1, lbRequestObservers.size());
        lbRequestObserver = lbRequestObservers.poll();
        inOrder.verify(helper).refreshNameResolution();
    }
    if (allSubchannelsBroken) {
        for (Subchannel subchannel : subchannels) {
            // A READY subchannel transits to IDLE when receiving a go-away
            deliverSubchannelState(subchannel, ConnectivityStateInfo.forNonError(IDLE));
            inOrder.verify(helper).refreshNameResolution();
        }
    }
    if (balancerBroken && allSubchannelsBroken) {
        // Going into fallback
        subchannels = fallbackTestVerifyUseOfFallbackBackendLists(inOrder, Arrays.asList(backendList.get(0), backendList.get(1)));
        // connections are lost
        for (Subchannel subchannel : subchannels) {
            deliverSubchannelState(subchannel, ConnectivityStateInfo.forNonError(IDLE));
            inOrder.verify(helper).refreshNameResolution();
        }
        // Exit fallback mode or cancel fallback timer when receiving a new server list from balancer
        List<ServerEntry> serverList2 = Arrays.asList(new ServerEntry("127.0.0.1", 2001, "token0003"), new ServerEntry("127.0.0.1", 2011, "token0004"));
        lbResponseObserver.onNext(buildInitialResponse());
        lbResponseObserver.onNext(buildLbResponse(serverList2));
        fallbackTestVerifyUseOfBalancerBackendLists(inOrder, serverList2);
    }
    assertEquals(0, fakeClock.numPendingTasks(FALLBACK_MODE_TASK_FILTER));
    // No subchannel to fallback backends should have been created if no fallback happened
    if (!(balancerBroken && allSubchannelsBroken)) {
        verify(subchannelPool, never()).takeOrCreateSubchannel(eq(backendList.get(0)), any(Attributes.class));
        verify(subchannelPool, never()).takeOrCreateSubchannel(eq(backendList.get(1)), any(Attributes.class));
    }
}
Also used : InOrder(org.mockito.InOrder) Attributes(io.grpc.Attributes) InitialLoadBalanceResponse(io.grpc.lb.v1.InitialLoadBalanceResponse) LoadBalanceResponse(io.grpc.lb.v1.LoadBalanceResponse) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalanceRequest(io.grpc.lb.v1.LoadBalanceRequest) InitialLoadBalanceRequest(io.grpc.lb.v1.InitialLoadBalanceRequest)

Example 45 with EquivalentAddressGroup

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

the class GrpclbLoadBalancerTest method grpclbFallback_allLost_failToFallback.

@Test
public void grpclbFallback_allLost_failToFallback() {
    long loadReportIntervalMillis = 1983;
    InOrder inOrder = inOrder(helper, mockLbService, subchannelPool);
    // Create balancer and (empty) backend addresses
    List<EquivalentAddressGroup> grpclbBalancerList = createResolvedBalancerAddresses(1);
    deliverResolvedAddresses(Collections.<EquivalentAddressGroup>emptyList(), grpclbBalancerList);
    inOrder.verify(helper).createOobChannel(eq(xattr(grpclbBalancerList)), eq(lbAuthority(0) + NO_USE_AUTHORITY_SUFFIX));
    // Attempted to connect to balancer
    assertEquals(1, fakeOobChannels.size());
    fakeOobChannels.poll();
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
    assertEquals(1, lbRequestObservers.size());
    StreamObserver<LoadBalanceRequest> lbRequestObserver = lbRequestObservers.poll();
    verify(lbRequestObserver).onNext(eq(LoadBalanceRequest.newBuilder().setInitialRequest(InitialLoadBalanceRequest.newBuilder().setName(SERVICE_AUTHORITY).build()).build()));
    lbResponseObserver.onNext(buildInitialResponse(loadReportIntervalMillis));
    // We don't care if these methods have been run.
    inOrder.verify(helper, atLeast(0)).getSynchronizationContext();
    inOrder.verify(helper, atLeast(0)).getScheduledExecutorService();
    inOrder.verifyNoMoreInteractions();
    // Balancer returns a server list
    List<ServerEntry> serverList = Arrays.asList(new ServerEntry("127.0.0.1", 2000, "token0001"), new ServerEntry("127.0.0.1", 2010, "token0002"));
    lbResponseObserver.onNext(buildInitialResponse());
    lbResponseObserver.onNext(buildLbResponse(serverList));
    List<Subchannel> subchannels = fallbackTestVerifyUseOfBalancerBackendLists(inOrder, serverList);
    // Break connections
    lbResponseObserver.onError(Status.UNAVAILABLE.asException());
    // A new stream to LB is created
    inOrder.verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
    assertEquals(1, lbRequestObservers.size());
    // Break all subchannel connections
    Status error = Status.UNAUTHENTICATED.withDescription("Permission denied");
    for (Subchannel subchannel : subchannels) {
        deliverSubchannelState(subchannel, ConnectivityStateInfo.forTransientFailure(error));
    }
    // Recycle all subchannels
    for (Subchannel subchannel : subchannels) {
        verify(subchannelPool).returnSubchannel(eq(subchannel), any(ConnectivityStateInfo.class));
    }
    // RPC error status includes errors of subchannels to balancer-provided backends
    inOrder.verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
    PickResult result = pickerCaptor.getValue().pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(result.getStatus().getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(result.getStatus().getDescription()).startsWith(GrpclbState.NO_FALLBACK_BACKENDS_STATUS.getDescription());
    assertThat(result.getStatus().getDescription()).contains(error.getDescription());
}
Also used : Status(io.grpc.Status) InOrder(org.mockito.InOrder) InitialLoadBalanceResponse(io.grpc.lb.v1.InitialLoadBalanceResponse) LoadBalanceResponse(io.grpc.lb.v1.LoadBalanceResponse) ConnectivityStateInfo(io.grpc.ConnectivityStateInfo) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalanceRequest(io.grpc.lb.v1.LoadBalanceRequest) InitialLoadBalanceRequest(io.grpc.lb.v1.InitialLoadBalanceRequest) PickResult(io.grpc.LoadBalancer.PickResult) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Aggregations

EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)173 Test (org.junit.Test)133 Subchannel (io.grpc.LoadBalancer.Subchannel)54 SocketAddress (java.net.SocketAddress)42 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)41 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)39 Status (io.grpc.Status)39 Attributes (io.grpc.Attributes)37 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)33 InOrder (org.mockito.InOrder)31 Helper (io.grpc.LoadBalancer.Helper)30 Metadata (io.grpc.Metadata)29 ArrayList (java.util.ArrayList)26 InetSocketAddress (java.net.InetSocketAddress)24 PickResult (io.grpc.LoadBalancer.PickResult)22 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)20 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)20 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)19 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)19 ClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder)18