Search in sources :

Example 91 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project toolkit by googleapis.

the class Pubsub method grpc.

private static void grpc(final Settings settings) throws Exception {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).build();
    final Semaphore semaphore = new Semaphore(settings.numWorkers());
    Metadata header = new Metadata();
    header.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
    final PublisherFutureStub stub = MetadataUtils.attachHeaders(PublisherGrpc.newFutureStub(channel), header);
    final AtomicLong resetTime = new AtomicLong();
    final AtomicLong numCalls = new AtomicLong();
    final AtomicLong numErrs = new AtomicLong();
    long endTime = System.nanoTime() + settings.warmDurNano() + settings.targetDurNano();
    Thread resetter = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(settings.warmDurNano() / MILLION);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            numCalls.set(0);
            numErrs.set(0);
            resetTime.set(System.nanoTime());
        }
    });
    resetter.start();
    while (System.nanoTime() < endTime) {
        semaphore.acquire(1);
        Futures.addCallback(stub.getTopic(GetTopicRequest.newBuilder().setTopic(TOPIC_NAME_STRING).build()), new FutureCallback<Topic>() {

            @Override
            public void onSuccess(Topic topic) {
                if (!topic.getName().equals(TOPIC_NAME_STRING)) {
                    numErrs.incrementAndGet();
                }
                both();
            }

            @Override
            public void onFailure(Throwable t) {
                numErrs.incrementAndGet();
                both();
            }

            void both() {
                numCalls.incrementAndGet();
                semaphore.release(1);
            }
        });
    }
    long nCalls = numCalls.get();
    long nErrs = numErrs.get();
    long runDurNano = System.nanoTime() - resetTime.get();
    System.out.println("errors: " + nErrs);
    System.out.println("calls: " + nCalls);
    System.out.println("time per call (ns): " + (runDurNano / nCalls));
    System.out.println("QPS: " + (nCalls * BILLION / runDurNano));
    channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Metadata(io.grpc.Metadata) Semaphore(java.util.concurrent.Semaphore) PublisherFutureStub(com.google.pubsub.v1.PublisherGrpc.PublisherFutureStub) AtomicLong(java.util.concurrent.atomic.AtomicLong) ManagedChannel(io.grpc.ManagedChannel) Topic(com.google.pubsub.v1.Topic) File(java.io.File)

Example 92 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Create the channel
    ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).usePlaintext(true).build();
    // Get a stub to use for interacting with the remote service
    PingPongServiceGrpc.PingPongServiceVertxStub stub = PingPongServiceGrpc.newVertxStub(channel);
    // Make a request
    Messages.SimpleRequest request = Messages.SimpleRequest.newBuilder().setFillUsername(true).build();
    // Call the remote service
    stub.unaryCall(request, ar -> {
        if (ar.succeeded()) {
            System.out.println("My username is: " + ar.result().getUsername());
        } else {
            System.out.println("Coult not reach server " + ar.cause().getMessage());
        }
    });
}
Also used : Messages(io.vertx.example.grpc.Messages) ManagedChannel(io.grpc.ManagedChannel) PingPongServiceGrpc(io.vertx.example.grpc.PingPongServiceGrpc)

Example 93 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).usePlaintext(true).build();
    stub = RouteGuideGrpc.newVertxStub(channel);
    List<Feature> features = Util.parseFeatures(Util.getDefaultFeaturesFile());
    // Looking for a valid feature
    getFeature(409146138, -746188906);
    // Feature missing.
    getFeature(0, 0);
    // Looking for features between 40, -75 and 42, -73.
    listFeatures(400000000, -750000000, 420000000, -730000000);
    // Record a few randomly selected points from the features file.
    recordRoute(features, 10);
    routeChat();
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Feature(io.grpc.examples.routeguide.Feature)

Example 94 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).useSsl(options -> options.setSsl(true).setUseAlpn(true).setTrustStoreOptions(new JksOptions().setPath("tls/client-truststore.jks").setPassword("wibble"))).build();
    GreeterGrpc.GreeterVertxStub stub = GreeterGrpc.newVertxStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName("Julien").build();
    stub.sayHello(request, asyncResponse -> {
        if (asyncResponse.succeeded()) {
            System.out.println("Succeeded " + asyncResponse.result().getMessage());
        } else {
            asyncResponse.cause().printStackTrace();
        }
    });
}
Also used : ManagedChannel(io.grpc.ManagedChannel) AbstractVerticle(io.vertx.core.AbstractVerticle) JksOptions(io.vertx.core.net.JksOptions) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Runner(io.vertx.example.grpc.util.Runner) VertxChannelBuilder(io.vertx.grpc.VertxChannelBuilder) JksOptions(io.vertx.core.net.JksOptions) HelloRequest(io.grpc.examples.helloworld.HelloRequest) ManagedChannel(io.grpc.ManagedChannel) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc)

Example 95 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Create the channel
    ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).usePlaintext(true).build();
    // Get a stub to use for interacting with the remote service
    ConsumerServiceGrpc.ConsumerServiceVertxStub stub = ConsumerServiceGrpc.newVertxStub(channel);
    // Make a request
    Messages.StreamingOutputCallRequest request = Messages.StreamingOutputCallRequest.newBuilder().build();
    // Call the remote service
    stub.streamingOutputCall(request, stream -> {
        stream.handler(response -> {
            System.out.println(new String(response.getPayload().toByteArray(), Charset.forName("UTF-8")));
        }).endHandler(v -> {
            System.out.println("Response has ended.");
        });
    });
}
Also used : Messages(io.vertx.example.grpc.Messages) Charset(java.nio.charset.Charset) ManagedChannel(io.grpc.ManagedChannel) AbstractVerticle(io.vertx.core.AbstractVerticle) ConsumerServiceGrpc(io.vertx.example.grpc.ConsumerServiceGrpc) Runner(io.vertx.example.util.Runner) VertxChannelBuilder(io.vertx.grpc.VertxChannelBuilder) Messages(io.vertx.example.grpc.Messages) ManagedChannel(io.grpc.ManagedChannel) ConsumerServiceGrpc(io.vertx.example.grpc.ConsumerServiceGrpc)

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