Search in sources :

Example 96 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();
    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 : HelloRequest(io.grpc.examples.helloworld.HelloRequest) ManagedChannel(io.grpc.ManagedChannel) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc)

Example 97 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.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 98 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.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 99 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.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)

Example 100 with ManagedChannel

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

the class ITListenGRPC method testSecureOneWaySSL.

@Test
public void testSecureOneWaySSL() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, InterruptedException {
    final int randPort = TestGRPCClient.randomPort();
    final Map<String, String> sslProperties = getTruststoreProperties();
    final ManagedChannel channel = TestGRPCClient.buildChannel(HOST, randPort, sslProperties);
    final FlowFileServiceGrpc.FlowFileServiceBlockingStub stub = FlowFileServiceGrpc.newBlockingStub(channel);
    final ListenGRPC listenGRPC = new ListenGRPC();
    final TestRunner runner = TestRunners.newTestRunner(listenGRPC);
    runner.setProperty(ListenGRPC.PROP_SERVICE_PORT, String.valueOf(randPort));
    runner.setProperty(ListenGRPC.PROP_USE_SECURE, "true");
    useSSLContextService(runner, getKeystoreProperties());
    final ProcessContext processContext = runner.getProcessContext();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    try {
        // start the server. The order of the following statements shouldn't matter, because the
        // startServer() method waits for a processSessionFactory to be available to it.
        listenGRPC.startServer(processContext);
        listenGRPC.onTrigger(processContext, processSessionFactory);
        final FlowFileRequest ingestFile = FlowFileRequest.newBuilder().putAttributes("FOO", "BAR").setContent(ByteString.copyFrom("content".getBytes())).build();
        final FlowFileReply reply = stub.send(ingestFile);
        assertThat(reply.getResponseCode(), equalTo(FlowFileReply.ResponseCode.SUCCESS));
        assertThat(reply.getBody(), equalTo("FlowFile successfully received."));
        // known race condition spot: grpc reply vs flowfile transfer
        Thread.sleep(10);
        runner.assertTransferCount(ListenGRPC.REL_SUCCESS, 1);
        final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ListenGRPC.REL_SUCCESS);
        assertThat(successFiles.size(), equalTo(1));
        final MockFlowFile mockFlowFile = successFiles.get(0);
        assertThat(mockFlowFile.getAttribute("FOO"), equalTo("BAR"));
        assertThat(mockFlowFile.getAttribute(ListenGRPC.REMOTE_HOST), equalTo("127.0.0.1"));
        assertThat(mockFlowFile.getAttribute(ListenGRPC.REMOTE_USER_DN), equalTo(FlowFileIngestServiceInterceptor.DEFAULT_FOUND_SUBJECT));
    } finally {
        // stop the server
        listenGRPC.stopServer(processContext);
        channel.shutdown();
    }
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ByteString(com.google.protobuf.ByteString) ProcessContext(org.apache.nifi.processor.ProcessContext) MockFlowFile(org.apache.nifi.util.MockFlowFile) ManagedChannel(io.grpc.ManagedChannel) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) Test(org.junit.Test)

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