Search in sources :

Example 11 with ManagedChannel

use of io.grpc.ManagedChannel in project beam by apache.

the class BeamFnDataGrpcClientTest method testForInboundConsumer.

@Test
public void testForInboundConsumer() throws Exception {
    CountDownLatch waitForClientToConnect = new CountDownLatch(1);
    Collection<WindowedValue<String>> inboundValuesA = new ConcurrentLinkedQueue<>();
    Collection<WindowedValue<String>> inboundValuesB = new ConcurrentLinkedQueue<>();
    Collection<BeamFnApi.Elements> inboundServerValues = new ConcurrentLinkedQueue<>();
    AtomicReference<StreamObserver<BeamFnApi.Elements>> outboundServerObserver = new AtomicReference<>();
    CallStreamObserver<BeamFnApi.Elements> inboundServerObserver = TestStreams.withOnNext(inboundServerValues::add).build();
    BeamFnApi.ApiServiceDescriptor apiServiceDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setUrl(this.getClass().getName() + "-" + UUID.randomUUID().toString()).build();
    Server server = InProcessServerBuilder.forName(apiServiceDescriptor.getUrl()).addService(new BeamFnDataGrpc.BeamFnDataImplBase() {

        @Override
        public StreamObserver<BeamFnApi.Elements> data(StreamObserver<BeamFnApi.Elements> outboundObserver) {
            outboundServerObserver.set(outboundObserver);
            waitForClientToConnect.countDown();
            return inboundServerObserver;
        }
    }).build();
    server.start();
    try {
        ManagedChannel channel = InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
        BeamFnDataGrpcClient clientFactory = new BeamFnDataGrpcClient(PipelineOptionsFactory.create(), (BeamFnApi.ApiServiceDescriptor descriptor) -> channel, this::createStreamForTest);
        CompletableFuture<Void> readFutureA = clientFactory.forInboundConsumer(apiServiceDescriptor, KEY_A, CODER, inboundValuesA::add);
        waitForClientToConnect.await();
        outboundServerObserver.get().onNext(ELEMENTS_A_1);
        // Purposefully transmit some data before the consumer for B is bound showing that
        // data is not lost
        outboundServerObserver.get().onNext(ELEMENTS_B_1);
        Thread.sleep(100);
        CompletableFuture<Void> readFutureB = clientFactory.forInboundConsumer(apiServiceDescriptor, KEY_B, CODER, inboundValuesB::add);
        // Show that out of order stream completion can occur.
        readFutureB.get();
        assertThat(inboundValuesB, contains(valueInGlobalWindow("JKL"), valueInGlobalWindow("MNO")));
        outboundServerObserver.get().onNext(ELEMENTS_A_2);
        readFutureA.get();
        assertThat(inboundValuesA, contains(valueInGlobalWindow("ABC"), valueInGlobalWindow("DEF"), valueInGlobalWindow("GHI")));
    } finally {
        server.shutdownNow();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) CallStreamObserver(io.grpc.stub.CallStreamObserver) Server(io.grpc.Server) BeamFnApi(org.apache.beam.fn.v1.BeamFnApi) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) WindowedValue(org.apache.beam.sdk.util.WindowedValue) ManagedChannel(io.grpc.ManagedChannel) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 12 with ManagedChannel

use of io.grpc.ManagedChannel in project beam by apache.

the class BeamFnControlClientTest method testDelegation.

@Test
public void testDelegation() throws Exception {
    AtomicBoolean clientClosedStream = new AtomicBoolean();
    BlockingQueue<BeamFnApi.InstructionResponse> values = new LinkedBlockingQueue<>();
    BlockingQueue<StreamObserver<BeamFnApi.InstructionRequest>> outboundServerObservers = new LinkedBlockingQueue<>();
    CallStreamObserver<BeamFnApi.InstructionResponse> inboundServerObserver = TestStreams.withOnNext(values::add).withOnCompleted(() -> clientClosedStream.set(true)).build();
    BeamFnApi.ApiServiceDescriptor apiServiceDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setUrl(this.getClass().getName() + "-" + UUID.randomUUID().toString()).build();
    Server server = InProcessServerBuilder.forName(apiServiceDescriptor.getUrl()).addService(new BeamFnControlGrpc.BeamFnControlImplBase() {

        @Override
        public StreamObserver<BeamFnApi.InstructionResponse> control(StreamObserver<BeamFnApi.InstructionRequest> outboundObserver) {
            Uninterruptibles.putUninterruptibly(outboundServerObservers, outboundObserver);
            return inboundServerObserver;
        }
    }).build();
    server.start();
    try {
        ManagedChannel channel = InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
        EnumMap<BeamFnApi.InstructionRequest.RequestCase, ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>> handlers = new EnumMap<>(BeamFnApi.InstructionRequest.RequestCase.class);
        handlers.put(BeamFnApi.InstructionRequest.RequestCase.PROCESS_BUNDLE, new ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>() {

            @Override
            public BeamFnApi.InstructionResponse.Builder apply(BeamFnApi.InstructionRequest value) throws Exception {
                return BeamFnApi.InstructionResponse.newBuilder().setProcessBundle(BeamFnApi.ProcessBundleResponse.getDefaultInstance());
            }
        });
        handlers.put(BeamFnApi.InstructionRequest.RequestCase.REGISTER, new ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>() {

            @Override
            public BeamFnApi.InstructionResponse.Builder apply(BeamFnApi.InstructionRequest value) throws Exception {
                throw FAILURE;
            }
        });
        BeamFnControlClient client = new BeamFnControlClient(apiServiceDescriptor, (BeamFnApi.ApiServiceDescriptor descriptor) -> channel, this::createStreamForTest, handlers);
        // Get the connected client and attempt to send and receive an instruction
        StreamObserver<BeamFnApi.InstructionRequest> outboundServerObserver = outboundServerObservers.take();
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Void> future = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                client.processInstructionRequests(executor);
                return null;
            }
        });
        outboundServerObserver.onNext(SUCCESSFUL_REQUEST);
        assertEquals(SUCCESSFUL_RESPONSE, values.take());
        // Ensure that conversion of an unknown request type is properly converted to a
        // failure response.
        outboundServerObserver.onNext(UNKNOWN_HANDLER_REQUEST);
        assertEquals(UNKNOWN_HANDLER_RESPONSE, values.take());
        // Ensure that all exceptions are caught and translated to failures
        outboundServerObserver.onNext(FAILURE_REQUEST);
        assertEquals(FAILURE_RESPONSE, values.take());
        // Ensure that the server completing the stream translates to the completable future
        // being completed allowing for a successful shutdown of the client.
        outboundServerObserver.onCompleted();
        future.get();
    } finally {
        server.shutdownNow();
    }
}
Also used : Server(io.grpc.Server) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ManagedChannel(io.grpc.ManagedChannel) EnumMap(java.util.EnumMap) StreamObserver(io.grpc.stub.StreamObserver) CallStreamObserver(io.grpc.stub.CallStreamObserver) ThrowingFunction(org.apache.beam.fn.harness.fn.ThrowingFunction) BeamFnApi(org.apache.beam.fn.v1.BeamFnApi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 13 with ManagedChannel

use of io.grpc.ManagedChannel in project beam by apache.

the class ManagedChannelFactoryTest method testEpollHostPortChannel.

@Test
public void testEpollHostPortChannel() {
    assumeTrue(io.netty.channel.epoll.Epoll.isAvailable());
    ApiServiceDescriptor apiServiceDescriptor = ApiServiceDescriptor.newBuilder().setUrl("localhost:123").build();
    ManagedChannel channel = ManagedChannelFactory.from(PipelineOptionsFactory.fromArgs(new String[] { "--experiments=beam_fn_api_epoll" }).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 14 with ManagedChannel

use of io.grpc.ManagedChannel in project google-cloud-java by GoogleCloudPlatform.

the class Subscriber method doStart.

@Override
protected void doStart() {
    logger.log(Level.FINE, "Starting subscriber group.");
    try {
        for (int i = 0; i < numChannels; i++) {
            final ManagedChannel channel = channelProvider.needsExecutor() ? channelProvider.getChannel(executor) : channelProvider.getChannel();
            channels.add(channel);
            if (channelProvider.shouldAutoClose()) {
                closeables.add(new AutoCloseable() {

                    @Override
                    public void close() {
                        channel.shutdown();
                    }
                });
            }
        }
    } catch (IOException e) {
        // doesn't matter what we throw, the Service will just catch it and fail to start.
        throw new IllegalStateException(e);
    }
    // When started, connections submit tasks to the executor.
    // These tasks must finish before the connections can declare themselves running.
    // If we have a single-thread executor and call startPollingConnections from the
    // same executor, it will deadlock: the thread will be stuck waiting for connections
    // to start but cannot start the connections.
    // For this reason, we spawn a dedicated thread. Starting subscriber should be rare.
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                startPollingConnections();
                notifyStarted();
            } catch (Throwable t) {
                notifyFailed(t);
            }
        }
    }).start();
}
Also used : ManagedChannel(io.grpc.ManagedChannel) IOException(java.io.IOException)

Example 15 with ManagedChannel

use of io.grpc.ManagedChannel in project google-cloud-java by GoogleCloudPlatform.

the class SpannerImpl method close.

@Override
public void close() {
    List<ListenableFuture<Void>> closureFutures = null;
    synchronized (this) {
        Preconditions.checkState(!spannerIsClosed, "Cloud Spanner client has been closed");
        spannerIsClosed = true;
        closureFutures = new ArrayList<>();
        for (DatabaseClientImpl dbClient : dbClients.values()) {
            closureFutures.add(dbClient.closeAsync());
        }
        dbClients.clear();
    }
    try {
        Futures.successfulAsList(closureFutures).get();
    } catch (InterruptedException | ExecutionException e) {
        throw SpannerExceptionFactory.newSpannerException(e);
    }
    for (ManagedChannel channel : getOptions().getRpcChannels()) {
        try {
            channel.shutdown();
        } catch (RuntimeException e) {
            logger.log(Level.WARNING, "Failed to close channel", e);
        }
    }
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ManagedChannel(io.grpc.ManagedChannel) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)35 Test (org.junit.Test)19 StreamObserver (io.grpc.stub.StreamObserver)8 Metadata (io.grpc.Metadata)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Server (io.grpc.Server)5 Status (io.grpc.Status)5 CallStreamObserver (io.grpc.stub.CallStreamObserver)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BeamFnApi (org.apache.beam.fn.v1.BeamFnApi)5 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ByteString (com.google.protobuf.ByteString)3 Attributes (io.grpc.Attributes)3 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)3 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)3 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)3 ByteBuf (io.netty.buffer.ByteBuf)3 Semaphore (java.util.concurrent.Semaphore)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2