Search in sources :

Example 86 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project dubbo by alibaba.

the class JEtcdClientWrapper method newChannel.

/**
 * try get client's shared channel, because all fields is private on jetcd,
 * we must using it by reflect, in the future, jetcd may provider better tools.
 *
 * @param client get channel from current client
 * @return current connection channel
 */
private ManagedChannel newChannel(Client client) {
    try {
        Field connectionField = client.getClass().getDeclaredField("connectionManager");
        ReflectUtils.makeAccessible(connectionField);
        Object connection = connectionField.get(client);
        Method channel = connection.getClass().getDeclaredMethod("getChannel");
        ReflectUtils.makeAccessible(channel);
        return (ManagedChannel) channel.invoke(connection);
    } catch (Exception e) {
        throw new RuntimeException("Failed to obtain connection channel from " + url.getBackupAddress(), e);
    }
}
Also used : Field(java.lang.reflect.Field) ManagedChannel(io.grpc.ManagedChannel) Method(java.lang.reflect.Method) TimeoutException(java.util.concurrent.TimeoutException) EtcdException(io.etcd.jetcd.common.exception.EtcdException) ExecutionException(java.util.concurrent.ExecutionException)

Example 87 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project dubbo by alibaba.

the class JEtcdClientTest method getChannel.

private ManagedChannel getChannel(Client client) {
    try {
        // hack, use reflection to get the shared channel.
        Field connectionField = client.getClass().getDeclaredField("connectionManager");
        ReflectUtils.makeAccessible(connectionField);
        Object connection = connectionField.get(client);
        Method channelMethod = connection.getClass().getDeclaredMethod("getChannel");
        ReflectUtils.makeAccessible(channelMethod);
        ManagedChannel channel = (ManagedChannel) channelMethod.invoke(connection);
        return channel;
    } catch (Exception e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) ManagedChannel(io.grpc.ManagedChannel) Method(java.lang.reflect.Method) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException)

Example 88 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project dubbo by alibaba.

the class JEtcdClientTest method testWatchWithGrpc.

@Test
public void testWatchWithGrpc() {
    String path = "/dubbo/config/test_watch_with_grpc/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch latch = new CountDownLatch(1);
    try (Client client = Client.builder().endpoints(endpoint).build()) {
        ManagedChannel channel = getChannel(client);
        StreamObserver<WatchRequest> observer = WatchGrpc.newStub(channel).watch(new StreamObserver<WatchResponse>() {

            @Override
            public void onNext(WatchResponse response) {
                for (Event event : response.getEventsList()) {
                    Assertions.assertEquals("PUT", event.getType().toString());
                    Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8));
                    Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8));
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        });
        WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder().setKey(ByteString.copyFrom(path, UTF_8));
        observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build());
        // try to modify the key
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        latch.await(5, TimeUnit.SECONDS);
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : WatchRequest(io.etcd.jetcd.api.WatchRequest) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) WatchCreateRequest(io.etcd.jetcd.api.WatchCreateRequest) ManagedChannel(io.grpc.ManagedChannel) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Event(io.etcd.jetcd.api.Event) Client(io.etcd.jetcd.Client) Test(org.junit.jupiter.api.Test)

Example 89 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project alluxio by Alluxio.

the class ManagerProcessMonitor method pingService.

/**
 * Attempts to ping the version service of the RPC server.
 *
 * @param addr The address to connect to
 * @param policy the retry policy
 * @param timeoutMs the timeout to wait for a ping response
 * @throws Exception if the client can't connect
 */
static void pingService(InetSocketAddress addr, RetryPolicy policy, long timeoutMs) throws Exception {
    ManagedChannel channel = NettyChannelBuilder.forAddress(addr).build();
    RpcClient<ServiceVersionClientServiceGrpc.ServiceVersionClientServiceBlockingStub> versionClient = new RpcClient<>(ServerConfiguration.global(), addr, ServiceVersionClientServiceGrpc::newBlockingStub, () -> ExponentialTimeBoundedRetry.builder().withSkipInitialSleep().withMaxDuration(Duration.ofMillis(timeoutMs)).build());
    try {
        while (policy.attempt()) {
            try {
                versionClient.get().withDeadlineAfter(timeoutMs, TimeUnit.MILLISECONDS).getServiceVersion(GetServiceVersionPRequest.newBuilder().setServiceType(ServiceType.UNKNOWN_SERVICE).build());
                return;
            } catch (Throwable t) {
                LOG.info("Failed to reach version service", t);
            }
        }
    } finally {
        channel.shutdown();
        channel.awaitTermination(3, TimeUnit.SECONDS);
    }
    throw new Exception("Failed to reach the version service after " + policy.getAttemptCount() + " attempts");
}
Also used : ServiceVersionClientServiceGrpc(alluxio.grpc.ServiceVersionClientServiceGrpc) ManagedChannel(io.grpc.ManagedChannel) RpcClient(alluxio.hub.common.RpcClient)

Example 90 with ManagedChannel

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

the class Pubsub method gapic.

private static void gapic(final Settings settings) throws Exception {
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    // In real clients, InstantiatingChannelProvider is responsible for adding interceptors.
    // We can't use it here because InstantiatingChannelProvider doesn't have sslContext method.
    // Instead, we imitate HeaderInterceptor.
    ClientInterceptor headerInterceptor = new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new SimpleForwardingClientCall<ReqT, RespT>(call) {

                @Override
                public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    headers.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
                    super.start(responseListener, headers);
                }
            };
        }
    };
    ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).intercept(headerInterceptor).build();
    final Semaphore semaphore = new Semaphore(settings.numWorkers());
    final TopicAdminClient client = TopicAdminClient.create(TopicAdminSettings.defaultBuilder().setChannelProvider(FixedChannelProvider.create(channel)).setExecutorProvider(FixedExecutorProvider.create(executor)).build());
    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(() -> {
        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);
        ApiFutures.addCallback(client.getTopicCallable().futureCall(GetTopicRequest.newBuilder().setTopicWithTopicName(TOPIC_NAME_RESOURCE).build()), new ApiFutureCallback<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));
    client.close();
    channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Semaphore(java.util.concurrent.Semaphore) MethodDescriptor(io.grpc.MethodDescriptor) AtomicLong(java.util.concurrent.atomic.AtomicLong) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ClientInterceptor(io.grpc.ClientInterceptor) ManagedChannel(io.grpc.ManagedChannel) Topic(com.google.pubsub.v1.Topic) File(java.io.File) SimpleForwardingClientCall(io.grpc.ForwardingClientCall.SimpleForwardingClientCall)

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