use of 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 io.grpc.ManagedChannel in project startup-os by google.
the class Client method main.
public static void main(String[] args) throws Exception {
Flags.parseCurrentPackage(args);
SslContext sslContext = GrpcSslContexts.forClient().trustManager(new File(certificateFile.get())).build();
ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", GRPC_PORT).sslContext(sslContext).build();
GrpcAuthTestGrpc.GrpcAuthTestBlockingStub stub = GrpcAuthTestGrpc.newBlockingStub(channel).withInterceptors(new ClientAuthInterceptor(token.get()));
logger.at(Level.INFO).log("Calling server to increment %d", n.get());
Protos.Response resp = stub.getNextNumber(Protos.Request.newBuilder().setNumber(n.get()).build());
logger.at(Level.INFO).log("Got %d in response", resp.getNumber());
}
use of io.grpc.ManagedChannel in project grpc-java by grpc.
the class GrpclbLoadBalancerTest method switchPolicy.
@SuppressWarnings("unchecked")
@Test
public void switchPolicy() {
// Go to GRPCLB first
List<ResolvedServerInfoGroup> grpclbResolutionList = createResolvedServerInfoGroupList(true, false, true);
Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.GRPCLB).build();
deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
assertNull(balancer.getDelegate());
verify(helper).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
assertEquals(1, fakeOobChannels.size());
ManagedChannel oobChannel = fakeOobChannels.poll();
verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
// Switch to PICK_FIRST
List<ResolvedServerInfoGroup> pickFirstResolutionList = createResolvedServerInfoGroupList(true, false, true);
Attributes pickFirstResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.PICK_FIRST).build();
verify(pickFirstBalancerFactory, never()).newLoadBalancer(any(Helper.class));
assertEquals(1, lbRequestObservers.size());
StreamObserver<LoadBalanceRequest> lbRequestObserver = lbRequestObservers.poll();
verify(lbRequestObserver, never()).onCompleted();
assertFalse(oobChannel.isShutdown());
deliverResolvedAddresses(pickFirstResolutionList, pickFirstResolutionAttrs);
verify(pickFirstBalancerFactory).newLoadBalancer(same(helper));
// Only non-LB addresses are passed to the delegate
verify(pickFirstBalancer).handleResolvedAddresses(eq(Arrays.asList(pickFirstResolutionList.get(1))), same(pickFirstResolutionAttrs));
assertSame(LbPolicy.PICK_FIRST, balancer.getLbPolicy());
assertSame(pickFirstBalancer, balancer.getDelegate());
// GRPCLB connection is closed
verify(lbRequestObserver).onCompleted();
assertTrue(oobChannel.isShutdown());
// Switch to ROUND_ROBIN
List<ResolvedServerInfoGroup> roundRobinResolutionList = createResolvedServerInfoGroupList(true, false, false);
Attributes roundRobinResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.ROUND_ROBIN).build();
verify(roundRobinBalancerFactory, never()).newLoadBalancer(any(Helper.class));
deliverResolvedAddresses(roundRobinResolutionList, roundRobinResolutionAttrs);
verify(roundRobinBalancerFactory).newLoadBalancer(same(helper));
// Only non-LB addresses are passed to the delegate
verify(roundRobinBalancer).handleResolvedAddresses(eq(roundRobinResolutionList.subList(1, 3)), same(roundRobinResolutionAttrs));
assertSame(LbPolicy.ROUND_ROBIN, balancer.getLbPolicy());
assertSame(roundRobinBalancer, balancer.getDelegate());
// Special case: if all addresses are loadbalancers, use GRPCLB no matter what the NameResolver
// says.
grpclbResolutionList = createResolvedServerInfoGroupList(true, true, true);
grpclbResolutionAttrs = Attributes.newBuilder().set(GrpclbConstants.ATTR_LB_POLICY, LbPolicy.PICK_FIRST).build();
deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
assertSame(LbPolicy.GRPCLB, balancer.getLbPolicy());
assertNull(balancer.getDelegate());
verify(helper, times(2)).createOobChannel(eq(grpclbResolutionList.get(0).toEquivalentAddressGroup()), eq(lbAuthority(0)));
verify(helper, times(2)).createOobChannel(any(EquivalentAddressGroup.class), any(String.class));
assertEquals(1, fakeOobChannels.size());
oobChannel = fakeOobChannels.poll();
verify(mockLbService, times(2)).balanceLoad(lbResponseObserverCaptor.capture());
// Special case: PICK_FIRST is the default
pickFirstResolutionList = createResolvedServerInfoGroupList(true, false, false);
pickFirstResolutionAttrs = Attributes.EMPTY;
verify(pickFirstBalancerFactory).newLoadBalancer(any(Helper.class));
assertFalse(oobChannel.isShutdown());
deliverResolvedAddresses(pickFirstResolutionList, pickFirstResolutionAttrs);
verify(pickFirstBalancerFactory, times(2)).newLoadBalancer(same(helper));
// Only non-LB addresses are passed to the delegate
verify(pickFirstBalancer).handleResolvedAddresses(eq(pickFirstResolutionList.subList(1, 3)), same(pickFirstResolutionAttrs));
assertSame(LbPolicy.PICK_FIRST, balancer.getLbPolicy());
assertSame(pickFirstBalancer, balancer.getDelegate());
// GRPCLB connection is closed
assertTrue(oobChannel.isShutdown());
}
use of io.grpc.ManagedChannel in project beam by apache.
the class ManagedChannelFactoryTest method testDefaultChannel.
@Test
public void testDefaultChannel() {
ApiServiceDescriptor apiServiceDescriptor = ApiServiceDescriptor.newBuilder().setUrl("localhost:123").build();
ManagedChannel channel = ManagedChannelFactory.from(PipelineOptionsFactory.create()).forDescriptor(apiServiceDescriptor);
assertEquals("localhost:123", channel.authority());
channel.shutdownNow();
}
use of io.grpc.ManagedChannel in project beam by apache.
the class ManagedChannelFactoryTest method testEpollDomainSocketChannel.
@Test
public void testEpollDomainSocketChannel() throws Exception {
assumeTrue(io.netty.channel.epoll.Epoll.isAvailable());
ApiServiceDescriptor apiServiceDescriptor = ApiServiceDescriptor.newBuilder().setUrl("unix://" + tmpFolder.newFile().getAbsolutePath()).build();
ManagedChannel channel = ManagedChannelFactory.from(PipelineOptionsFactory.fromArgs(new String[] { "--experiments=beam_fn_api_epoll" }).create()).forDescriptor(apiServiceDescriptor);
assertEquals(apiServiceDescriptor.getUrl().substring("unix://".length()), channel.authority());
channel.shutdownNow();
}
Aggregations