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);
}
}
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;
}
}
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());
}
}
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");
}
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);
}
Aggregations