Search in sources :

Example 96 with EquivalentAddressGroup

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

the class InternalSubchannelTest method createInternalSubchannel.

private void createInternalSubchannel(SocketAddress... addrs) {
    addressGroup = new EquivalentAddressGroup(Arrays.asList(addrs));
    internalSubchannel = new InternalSubchannel(addressGroup, AUTHORITY, USER_AGENT, mockBackoffPolicyProvider, mockTransportFactory, fakeClock.getScheduledExecutorService(), fakeClock.getStopwatchSupplier(), channelExecutor, mockInternalSubchannelCallback);
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup)

Example 97 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup 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)

Example 98 with EquivalentAddressGroup

use of io.grpc.EquivalentAddressGroup in project bookkeeper by apache.

the class TestSimpleNameResolver method testGetServers.

@Test
public void testGetServers() {
    List<InetSocketAddress> servers = createServers(5);
    List<URI> uris = servers.stream().map(addr -> URI.create("bookie://" + addr.getHostName() + ":" + addr.getPort())).collect(Collectors.toList());
    List<EquivalentAddressGroup> resolvedServers = servers.stream().map(addr -> new EquivalentAddressGroup(Lists.newArrayList(addr), Attributes.EMPTY)).collect(Collectors.toList());
    // for the mock
    @SuppressWarnings("unchecked") SimpleNameResolver nameResolver = new SimpleNameResolver("test-name-resolver", mock(Resource.class), uris);
    assertEquals(resolvedServers, nameResolver.getServers());
}
Also used : SimpleNameResolver(org.apache.bookkeeper.common.resolver.SimpleNameResolver) Resource(org.apache.bookkeeper.common.util.SharedResourceManager.Resource) List(java.util.List) Lists(com.google.common.collect.Lists) Attributes(io.grpc.Attributes) Test(org.junit.Test) URI(java.net.URI) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Mockito.mock(org.mockito.Mockito.mock) InetSocketAddress(java.net.InetSocketAddress) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) SimpleNameResolver(org.apache.bookkeeper.common.resolver.SimpleNameResolver) Resource(org.apache.bookkeeper.common.util.SharedResourceManager.Resource) URI(java.net.URI) Test(org.junit.Test)

Example 99 with EquivalentAddressGroup

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

the class ManagedChannelImplTest method hedgingScheduledThenChannelShutdown_hedgeShouldStillHappen_newCallShouldFail.

@Test
public void hedgingScheduledThenChannelShutdown_hedgeShouldStillHappen_newCallShouldFail() {
    Map<String, Object> hedgingPolicy = new HashMap<>();
    hedgingPolicy.put("maxAttempts", 3D);
    hedgingPolicy.put("hedgingDelay", "10s");
    hedgingPolicy.put("nonFatalStatusCodes", Arrays.<Object>asList("UNAVAILABLE"));
    Map<String, Object> methodConfig = new HashMap<>();
    Map<String, Object> name = new HashMap<>();
    name.put("service", "service");
    methodConfig.put("name", Arrays.<Object>asList(name));
    methodConfig.put("hedgingPolicy", hedgingPolicy);
    Map<String, Object> rawServiceConfig = new HashMap<>();
    rawServiceConfig.put("methodConfig", Arrays.<Object>asList(methodConfig));
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(Collections.singletonList(new EquivalentAddressGroup(socketAddress))).build();
    ManagedChannelServiceConfig managedChannelServiceConfig = createManagedChannelServiceConfig(rawServiceConfig, null);
    nameResolverFactory.nextConfigOrError.set(ConfigOrError.fromConfig(managedChannelServiceConfig));
    channelBuilder.nameResolverFactory(nameResolverFactory);
    channelBuilder.executor(MoreExecutors.directExecutor());
    channelBuilder.enableRetry();
    requestConnection = false;
    createChannel();
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, new Metadata());
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
    verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
    helper = helperCaptor.getValue();
    verify(mockLoadBalancer).handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(nameResolverFactory.servers).build());
    // simulating request connection and then transport ready after resolved address
    Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    requestConnectionSafely(helper, subchannel);
    MockClientTransportInfo transportInfo = transports.poll();
    ConnectionClientTransport mockTransport = transportInfo.transport;
    ClientStream mockStream = mock(ClientStream.class);
    ClientStream mockStream2 = mock(ClientStream.class);
    when(mockTransport.newStream(same(method), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any())).thenReturn(mockStream).thenReturn(mockStream2);
    transportInfo.listener.transportReady();
    updateBalancingStateSafely(helper, READY, mockPicker);
    ArgumentCaptor<ClientStreamListener> streamListenerCaptor = ArgumentCaptor.forClass(ClientStreamListener.class);
    verify(mockStream).start(streamListenerCaptor.capture());
    // in hedging delay backoff
    timer.forwardTime(5, TimeUnit.SECONDS);
    assertThat(timer.numPendingTasks()).isEqualTo(1);
    // first hedge fails
    streamListenerCaptor.getValue().closed(Status.UNAVAILABLE, PROCESSED, new Metadata());
    verify(mockStream2, never()).start(any(ClientStreamListener.class));
    // shutdown during backoff period
    channel.shutdown();
    assertThat(timer.numPendingTasks()).isEqualTo(1);
    verify(mockCallListener, never()).onClose(any(Status.class), any(Metadata.class));
    ClientCall<String, Integer> call2 = channel.newCall(method, CallOptions.DEFAULT);
    call2.start(mockCallListener2, new Metadata());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(mockCallListener2).onClose(statusCaptor.capture(), any(Metadata.class));
    assertSame(Status.Code.UNAVAILABLE, statusCaptor.getValue().getCode());
    assertEquals("Channel shutdown invoked", statusCaptor.getValue().getDescription());
    // backoff ends
    timer.forwardTime(5, TimeUnit.SECONDS);
    assertThat(timer.numPendingTasks()).isEqualTo(1);
    verify(mockStream2).start(streamListenerCaptor.capture());
    verify(mockLoadBalancer, never()).shutdown();
    assertFalse("channel.isTerminated() is expected to be false but was true", channel.isTerminated());
    streamListenerCaptor.getValue().closed(Status.INTERNAL, PROCESSED, new Metadata());
    assertThat(timer.numPendingTasks()).isEqualTo(0);
    verify(mockLoadBalancer).shutdown();
    // simulating the shutdown of load balancer triggers the shutdown of subchannel
    shutdownSafely(helper, subchannel);
    // simulating transport shutdown & terminated
    transportInfo.listener.transportShutdown(Status.INTERNAL);
    transportInfo.listener.transportTerminated();
    assertTrue("channel.isTerminated() is expected to be true but was false", channel.isTerminated());
}
Also used : Status(io.grpc.Status) HashMap(java.util.HashMap) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) Metadata(io.grpc.Metadata) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 100 with EquivalentAddressGroup

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

the class ManagedChannelImplTest method subchannelConnectionBroken_ignoreRefreshNameResolutionCheck_noRefresh.

@Test
public void subchannelConnectionBroken_ignoreRefreshNameResolutionCheck_noRefresh() {
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(Collections.singletonList(new EquivalentAddressGroup(socketAddress))).build();
    channelBuilder.nameResolverFactory(nameResolverFactory);
    createChannel();
    FakeNameResolverFactory.FakeNameResolver resolver = Iterables.getOnlyElement(nameResolverFactory.resolvers);
    assertThat(resolver.refreshCalled).isEqualTo(0);
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
    verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
    helper = helperCaptor.getValue();
    helper.ignoreRefreshNameResolutionCheck();
    Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    InternalSubchannel internalSubchannel = (InternalSubchannel) subchannel.getInternalSubchannel();
    internalSubchannel.obtainActiveTransport();
    MockClientTransportInfo transportInfo = transports.poll();
    // Break subchannel connection
    transportInfo.listener.transportShutdown(Status.UNAVAILABLE.withDescription("unreachable"));
    assertThat(logs).isEmpty();
    assertThat(resolver.refreshCalled).isEqualTo(0);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) 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