Search in sources :

Example 21 with ManagedChannel

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

the class OpenLoopClient method run.

/**
   * Start the open loop client.
   */
public void run() throws Exception {
    if (config == null) {
        return;
    }
    config.channels = 1;
    config.directExecutor = true;
    ManagedChannel ch = config.newChannel();
    SimpleRequest req = config.newRequest();
    LoadGenerationWorker worker = new LoadGenerationWorker(ch, req, config.targetQps, config.duration);
    final long start = System.nanoTime();
    Histogram histogram = worker.call();
    final long end = System.nanoTime();
    printStats(histogram, end - start);
    if (config.histogramFile != null) {
        saveHistogram(histogram, config.histogramFile);
    }
    ch.shutdown();
}
Also used : Utils.saveHistogram(io.grpc.benchmarks.Utils.saveHistogram) AtomicHistogram(org.HdrHistogram.AtomicHistogram) Histogram(org.HdrHistogram.Histogram) ManagedChannel(io.grpc.ManagedChannel) SimpleRequest(io.grpc.benchmarks.proto.Messages.SimpleRequest)

Example 22 with ManagedChannel

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

the class AbstractBenchmark method teardown.

/**
   * Shutdown all the client channels and then shutdown the server.
   */
protected void teardown() throws Exception {
    logger.fine("shutting down channels");
    for (ManagedChannel channel : channels) {
        channel.shutdown();
    }
    logger.fine("shutting down server");
    server.shutdown();
    if (!server.awaitTermination(5, TimeUnit.SECONDS)) {
        logger.warning("Failed to shutdown server");
    }
    logger.fine("server shut down");
    for (ManagedChannel channel : channels) {
        if (!channel.awaitTermination(1, TimeUnit.SECONDS)) {
            logger.warning("Failed to shutdown client");
        }
    }
    logger.fine("channels shut down");
}
Also used : ManagedChannel(io.grpc.ManagedChannel)

Example 23 with ManagedChannel

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

the class AbstractBenchmark method startStreamingCalls.

/**
   * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
   * {@code done.get()} is true. Each completed call will increment the counter by the specified
   * delta which benchmarks can use to measure messages per second or bandwidth.
   */
protected CountDownLatch startStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) {
    final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
    for (final ManagedChannel channel : channels) {
        for (int i = 0; i < callsPerChannel; i++) {
            final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(pingPongMethod, CALL_OPTIONS);
            final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<StreamObserver<ByteBuf>>();
            final AtomicBoolean ignoreMessages = new AtomicBoolean();
            StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(streamingCall, new StreamObserver<ByteBuf>() {

                @Override
                public void onNext(ByteBuf value) {
                    if (done.get()) {
                        if (!ignoreMessages.getAndSet(true)) {
                            requestObserverRef.get().onCompleted();
                        }
                        return;
                    }
                    requestObserverRef.get().onNext(request.slice());
                    if (record.get()) {
                        counter.addAndGet(counterDelta);
                    }
                // request is called automatically because the observer implicitly has auto
                // inbound flow control
                }

                @Override
                public void onError(Throwable t) {
                    logger.log(Level.WARNING, "call error", t);
                    latch.countDown();
                }

                @Override
                public void onCompleted() {
                    latch.countDown();
                }
            });
            requestObserverRef.set(requestObserver);
            requestObserver.onNext(request.slice());
            requestObserver.onNext(request.slice());
        }
    }
    return latch;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedChannel(io.grpc.ManagedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf)

Example 24 with ManagedChannel

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

the class ManagedChannelImplIdlenessTest method oobTransportDoesNotAffectIdleness.

@Test
public void oobTransportDoesNotAffectIdleness() {
    // Start a call, which goes to delayed transport
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, new Metadata());
    // Verify that we have exited the idle mode
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
    verify(mockLoadBalancerFactory).newLoadBalancer(helperCaptor.capture());
    Helper helper = helperCaptor.getValue();
    // Fail the RPC
    SubchannelPicker failingPicker = mock(SubchannelPicker.class);
    when(failingPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withError(Status.UNAVAILABLE));
    helper.updatePicker(failingPicker);
    executor.runDueTasks();
    verify(mockCallListener).onClose(same(Status.UNAVAILABLE), any(Metadata.class));
    // ... so that the channel resets its in-use state
    assertFalse(channel.inUseStateAggregator.isInUse());
    // Now make an RPC on an OOB channel
    ManagedChannel oob = helper.createOobChannel(addressGroupList.get(0), "oobauthority");
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT));
    ClientCall<String, Integer> oobCall = oob.newCall(method, CallOptions.DEFAULT);
    oobCall.start(mockCallListener2, new Metadata());
    verify(mockTransportFactory).newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT));
    MockClientTransportInfo oobTransportInfo = newTransports.poll();
    assertEquals(0, newTransports.size());
    // The OOB transport reports in-use state
    oobTransportInfo.listener.transportInUse(true);
    // But it won't stop the channel from going idle
    verify(mockLoadBalancer, never()).shutdown();
    timer.forwardTime(IDLE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    verify(mockLoadBalancer).shutdown();
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) Metadata(io.grpc.Metadata) ManagedChannel(io.grpc.ManagedChannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Matchers.anyString(org.mockito.Matchers.anyString) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 25 with ManagedChannel

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

the class ManagedChannelImplTest method oobChannelsNoConnectionShutdown.

@Test
public void oobChannelsNoConnectionShutdown() {
    createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
    ManagedChannel oob1 = helper.createOobChannel(addressGroup, "oob1Authority");
    ManagedChannel oob2 = helper.createOobChannel(addressGroup, "oob2Authority");
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    oob1.shutdown();
    assertTrue(oob1.isTerminated());
    assertFalse(channel.isTerminated());
    oob2.shutdown();
    assertTrue(oob2.isTerminated());
    assertTrue(channel.isTerminated());
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), anyString(), anyString());
}
Also used : ManagedChannel(io.grpc.ManagedChannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)35 Test (org.junit.Test)19 StreamObserver (io.grpc.stub.StreamObserver)8 Metadata (io.grpc.Metadata)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Server (io.grpc.Server)5 Status (io.grpc.Status)5 CallStreamObserver (io.grpc.stub.CallStreamObserver)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BeamFnApi (org.apache.beam.fn.v1.BeamFnApi)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ByteString (com.google.protobuf.ByteString)3 Attributes (io.grpc.Attributes)3 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)3 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)3 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)3 ByteBuf (io.netty.buffer.ByteBuf)3 Semaphore (java.util.concurrent.Semaphore)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2