Search in sources :

Example 81 with ManagedChannel

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

the class GrpclbLoadBalancerTest method grpclbUpdatedAddresses_avoidsReconnect.

@Test
public void grpclbUpdatedAddresses_avoidsReconnect() {
    List<EquivalentAddressGroup> backendList = createResolvedBackendAddresses(1);
    List<EquivalentAddressGroup> grpclbBalancerList = createResolvedBalancerAddresses(1);
    deliverResolvedAddresses(backendList, grpclbBalancerList);
    verify(helper).createOobChannel(eq(xattr(grpclbBalancerList)), eq(lbAuthority(0) + NO_USE_AUTHORITY_SUFFIX));
    ManagedChannel oobChannel = fakeOobChannels.poll();
    assertEquals(1, lbRequestObservers.size());
    List<EquivalentAddressGroup> backendList2 = createResolvedBackendAddresses(1);
    List<EquivalentAddressGroup> grpclbBalancerList2 = createResolvedBalancerAddresses(2);
    deliverResolvedAddresses(backendList2, grpclbBalancerList2);
    verify(helper).updateOobChannelAddresses(eq(oobChannel), eq(xattr(grpclbBalancerList2)));
    // No additional RPC
    assertEquals(1, lbRequestObservers.size());
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 82 with ManagedChannel

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

the class Http2OkHttpTest method hostnameVerifierWithBadHostname.

@Test
public void hostnameVerifierWithBadHostname() throws Exception {
    int port = ((InetSocketAddress) getListenAddress()).getPort();
    ManagedChannel channel = createChannelBuilderPreCredentialsApi().overrideAuthority(GrpcUtil.authorityFromHostAndPort(BAD_HOSTNAME, port)).hostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }).build();
    TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(channel);
    blockingStub.emptyCall(Empty.getDefaultInstance());
    channel.shutdown();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLSession(javax.net.ssl.SSLSession) ManagedChannel(io.grpc.ManagedChannel) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Example 83 with ManagedChannel

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

the class StressTestClient method runStressTest.

@VisibleForTesting
void runStressTest() throws Exception {
    Preconditions.checkState(!shutdown, "client was shutdown.");
    if (testCaseWeightPairs.isEmpty()) {
        return;
    }
    int numChannels = addresses.size() * channelsPerServer;
    int numThreads = numChannels * stubsPerChannel;
    threadpool = MoreExecutors.listeningDecorator(newFixedThreadPool(numThreads));
    int serverIdx = -1;
    for (InetSocketAddress address : addresses) {
        serverIdx++;
        for (int i = 0; i < channelsPerServer; i++) {
            ManagedChannel channel = createChannel(address);
            channels.add(channel);
            for (int j = 0; j < stubsPerChannel; j++) {
                String gaugeName = String.format("/stress_test/server_%d/channel_%d/stub_%d/qps", serverIdx, i, j);
                Worker worker = new Worker(channel, testCaseWeightPairs, durationSecs, gaugeName);
                workerFutures.add(threadpool.submit(worker));
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ManagedChannel(io.grpc.ManagedChannel) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 84 with ManagedChannel

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

the class StressTestClient method shutdown.

@VisibleForTesting
void shutdown() {
    if (shutdown) {
        return;
    }
    shutdown = true;
    for (ManagedChannel ch : channels) {
        try {
            ch.shutdownNow();
            ch.awaitTermination(1, SECONDS);
        } catch (Throwable t) {
            log.log(Level.WARNING, "Error shutting down channel!", t);
        }
    }
    try {
        metricsServer.shutdownNow();
    } catch (Throwable t) {
        log.log(Level.WARNING, "Error shutting down metrics service!", t);
    }
    try {
        if (threadpool != null) {
            threadpool.shutdownNow();
        }
    } catch (Throwable t) {
        log.log(Level.WARNING, "Error shutting down threadpool.", t);
    }
}
Also used : ManagedChannel(io.grpc.ManagedChannel) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 85 with ManagedChannel

use of io.grpc.ManagedChannel 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));
}
Also used : Status(io.grpc.Status) ServingStatus(io.grpc.health.v1.HealthCheckResponse.ServingStatus) SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) Server(io.grpc.Server) LoadBalancer(io.grpc.LoadBalancer) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Before(org.junit.Before)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)164 Test (org.junit.Test)92 CountDownLatch (java.util.concurrent.CountDownLatch)26 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)26 ArrayList (java.util.ArrayList)20 Metadata (io.grpc.Metadata)18 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 ExecutorService (java.util.concurrent.ExecutorService)13 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)11 StreamObserver (io.grpc.stub.StreamObserver)10 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)10 CallOptions (io.grpc.CallOptions)9 Subchannel (io.grpc.LoadBalancer.Subchannel)9 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)9 Endpoints (org.apache.beam.model.pipeline.v1.Endpoints)9