Search in sources :

Example 61 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class OrcaOobUtilTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        orcaServiceImps[i] = new OpenRcaServiceImp();
        cleanupRule.register(InProcessServerBuilder.forName("orca-reporting-test-" + i).addService(orcaServiceImps[i]).directExecutor().build().start());
        ManagedChannel channel = cleanupRule.register(InProcessChannelBuilder.forName("orca-reporting-test-" + i).directExecutor().build());
        channels[i] = channel;
        EquivalentAddressGroup eag = new EquivalentAddressGroup(new FakeSocketAddress("address-" + i));
        List<EquivalentAddressGroup> eagList = Arrays.asList(eag);
        eagLists[i] = eagList;
        mockStateListeners[i] = mock(SubchannelStateListener.class);
    }
    when(backoffPolicyProvider.get()).thenReturn(backoffPolicy1, backoffPolicy2);
    when(backoffPolicy1.nextBackoffNanos()).thenReturn(11L, 21L);
    when(backoffPolicy2.nextBackoffNanos()).thenReturn(12L, 22L);
    orcaHelperWrapper = OrcaOobUtil.newOrcaReportingHelperWrapper(origHelper, mockOrcaListener0, backoffPolicyProvider, fakeClock.getStopwatchSupplier());
    parentHelperWrapper = OrcaOobUtil.newOrcaReportingHelperWrapper(origHelper, mockOrcaListener1, backoffPolicyProvider, fakeClock.getStopwatchSupplier());
    childHelperWrapper = OrcaOobUtil.newOrcaReportingHelperWrapper(parentHelperWrapper.asHelper(), mockOrcaListener2, backoffPolicyProvider, fakeClock.getStopwatchSupplier());
}
Also used : SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) Before(org.junit.Before)

Example 62 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class AbstractBenchmark method startFlowControlledStreamingCalls.

/**
 * 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 startFlowControlledStreamingCalls(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(flowControlledStreaming, CALL_OPTIONS);
            final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<>();
            final AtomicBoolean ignoreMessages = new AtomicBoolean();
            StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(streamingCall, new StreamObserver<ByteBuf>() {

                @Override
                public void onNext(ByteBuf value) {
                    StreamObserver<ByteBuf> obs = requestObserverRef.get();
                    if (done.get()) {
                        if (!ignoreMessages.getAndSet(true)) {
                            obs.onCompleted();
                        }
                        return;
                    }
                    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);
            // Add some outstanding requests to ensure the server is filling the connection
            streamingCall.request(5);
            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 63 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class AbstractBenchmark method startUnaryCalls.

/**
 * Start a continuously executing set of unary 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 QPS or bandwidth.
 */
protected void startUnaryCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean done, final long counterDelta) {
    for (final ManagedChannel channel : channels) {
        for (int i = 0; i < callsPerChannel; i++) {
            StreamObserver<ByteBuf> observer = new StreamObserver<ByteBuf>() {

                @Override
                public void onNext(ByteBuf value) {
                    counter.addAndGet(counterDelta);
                }

                @Override
                public void onError(Throwable t) {
                    done.set(true);
                }

                @Override
                public void onCompleted() {
                    if (!done.get()) {
                        ByteBuf slice = request.slice();
                        ClientCalls.asyncUnaryCall(channel.newCall(unaryMethod, CALL_OPTIONS), slice, this);
                    }
                }
            };
            observer.onCompleted();
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ManagedChannel(io.grpc.ManagedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 64 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class HelloWorldClientTls method main.

/**
 * Greet server. If provided, the first element of {@code args} is the name to use in the
 * greeting.
 */
public static void main(String[] args) throws Exception {
    if (args.length < 2 || args.length == 4 || args.length > 5) {
        System.out.println("USAGE: HelloWorldClientTls host port [trustCertCollectionFilePath " + "[clientCertChainFilePath clientPrivateKeyFilePath]]\n  Note: clientCertChainFilePath and " + "clientPrivateKeyFilePath are only needed if mutual auth is desired.");
        System.exit(0);
    }
    // If only defaults are necessary, you can use TlsChannelCredentials.create() instead of
    // interacting with the Builder.
    TlsChannelCredentials.Builder tlsBuilder = TlsChannelCredentials.newBuilder();
    switch(args.length) {
        case 5:
            tlsBuilder.keyManager(new File(args[3]), new File(args[4]));
        // fallthrough
        case 3:
            tlsBuilder.trustManager(new File(args[2]));
        // fallthrough
        default:
    }
    String host = args[0];
    int port = Integer.parseInt(args[1]);
    ManagedChannel channel = Grpc.newChannelBuilderForAddress(host, port, tlsBuilder.build()).overrideAuthority("foo.test.google.fr").build();
    try {
        HelloWorldClientTls client = new HelloWorldClientTls(channel);
        client.greet(host);
    } finally {
        channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
    }
}
Also used : TlsChannelCredentials(io.grpc.TlsChannelCredentials) ManagedChannel(io.grpc.ManagedChannel) File(java.io.File)

Example 65 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class HelloWorldAltsClient method run.

private void run(String[] args) throws InterruptedException {
    parseArgs(args);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    ManagedChannel channel = AltsChannelBuilder.forTarget(serverAddress).executor(executor).build();
    try {
        GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
        HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
        logger.log(Level.INFO, "Got {0}", resp);
    } finally {
        channel.shutdown();
        channel.awaitTermination(1, TimeUnit.SECONDS);
        // Wait until the channel has terminated, since tasks can be queued after the channel is
        // shutdown.
        executor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(io.grpc.ManagedChannel) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)163 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