Search in sources :

Example 96 with ManagedChannel

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");
}
Also used : ServiceVersionClientServiceGrpc(alluxio.grpc.ServiceVersionClientServiceGrpc) ManagedChannel(io.grpc.ManagedChannel) RpcClient(alluxio.hub.common.RpcClient)

Example 97 with ManagedChannel

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());
}
Also used : ManagedChannel(io.grpc.ManagedChannel) File(java.io.File) SslContext(io.netty.handler.ssl.SslContext)

Example 98 with ManagedChannel

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());
}
Also used : Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Attributes(io.grpc.Attributes) ManagedChannel(io.grpc.ManagedChannel) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 99 with ManagedChannel

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();
}
Also used : ApiServiceDescriptor(org.apache.beam.fn.v1.BeamFnApi.ApiServiceDescriptor) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 100 with ManagedChannel

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();
}
Also used : ApiServiceDescriptor(org.apache.beam.fn.v1.BeamFnApi.ApiServiceDescriptor) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Aggregations

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