Search in sources :

Example 81 with Channel

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

the class GrpcWindmillServer method initializeLocalHost.

private synchronized void initializeLocalHost(int port) throws IOException {
    this.logEveryNStreamFailures = 1;
    this.maxBackoff = Duration.millis(500);
    // For local testing use short deadlines.
    this.unaryDeadlineSeconds = 10;
    Channel channel = localhostChannel(port);
    if (streamingEngineEnabled()) {
        this.stubList.add(CloudWindmillServiceV1Alpha1Grpc.newStub(channel));
        this.syncStubList.add(CloudWindmillServiceV1Alpha1Grpc.newBlockingStub(channel));
    } else {
        this.syncApplianceStub = WindmillApplianceGrpc.newBlockingStub(channel);
    }
}
Also used : Channel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Channel)

Example 82 with Channel

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

the class GrpcWindmillServer method remoteChannel.

private Channel remoteChannel(HostAndPort endpoint) throws IOException {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(endpoint.getHost(), endpoint.getPort());
    int timeoutSec = options.getWindmillServiceRpcChannelAliveTimeoutSec();
    if (timeoutSec > 0) {
        builder = builder.keepAliveTime(timeoutSec, TimeUnit.SECONDS).keepAliveTimeout(timeoutSec, TimeUnit.SECONDS).keepAliveWithoutCalls(true);
    }
    return builder.flowControlWindow(10 * 1024 * 1024).maxInboundMessageSize(java.lang.Integer.MAX_VALUE).maxInboundMetadataSize(1024 * 1024).negotiationType(NegotiationType.TLS).sslContext(GrpcSslContexts.forClient().ciphers(null).build()).build();
}
Also used : NettyChannelBuilder(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.netty.NettyChannelBuilder)

Example 83 with Channel

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

the class BeamFnDataGrpcServiceTest method testMessageReceivedByProperClientWhenThereAreMultipleClients.

@Test
public void testMessageReceivedByProperClientWhenThereAreMultipleClients() throws Exception {
    ConcurrentHashMap<String, LinkedBlockingQueue<Elements>> clientInboundElements = new ConcurrentHashMap<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    CountDownLatch waitForInboundElements = new CountDownLatch(1);
    int numberOfClients = 3;
    int numberOfMessages = 3;
    for (int client = 0; client < numberOfClients; ++client) {
        String clientId = Integer.toString(client);
        clientInboundElements.put(clientId, new LinkedBlockingQueue<>());
        executorService.submit(() -> {
            ManagedChannel channel = ManagedChannelFactory.createDefault().withInterceptors(Arrays.asList(AddHarnessIdInterceptor.create(clientId))).forDescriptor(service.getApiServiceDescriptor());
            StreamObserver<BeamFnApi.Elements> outboundObserver = BeamFnDataGrpc.newStub(channel).data(TestStreams.withOnNext(clientInboundElements.get(clientId)::add).build());
            waitForInboundElements.await();
            outboundObserver.onCompleted();
            return null;
        });
    }
    for (int client = 0; client < numberOfClients; ++client) {
        for (int i = 0; i < 3; ++i) {
            String instructionId = client + "-" + i;
            CloseableFnDataReceiver<WindowedValue<String>> consumer = service.getDataService(Integer.toString(client)).send(LogicalEndpoint.data(instructionId, TRANSFORM_ID), CODER);
            consumer.accept(valueInGlobalWindow("A" + instructionId));
            consumer.accept(valueInGlobalWindow("B" + instructionId));
            consumer.accept(valueInGlobalWindow("C" + instructionId));
            consumer.close();
        }
    }
    for (int client = 0; client < numberOfClients; ++client) {
        // Specifically copy the elements to a new list so we perform blocking calls on the queue
        // to ensure the elements arrive.
        ArrayList<BeamFnApi.Elements> copy = new ArrayList<>();
        for (int i = 0; i < numberOfMessages; ++i) {
            copy.add(clientInboundElements.get(Integer.toString(client)).take());
        }
        assertThat(copy, containsInAnyOrder(elementsWithData(client + "-" + 0), elementsWithData(client + "-" + 1), elementsWithData(client + "-" + 2)));
    }
    waitForInboundElements.countDown();
}
Also used : ArrayList(java.util.ArrayList) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Elements(org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements) LogicalEndpoint(org.apache.beam.sdk.fn.data.LogicalEndpoint) WindowedValue(org.apache.beam.sdk.util.WindowedValue) ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 84 with Channel

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

the class BeamFnLoggingServiceTest method testServerCloseHangsUpClients.

@Test
public void testServerCloseHangsUpClients() throws Exception {
    LinkedBlockingQueue<BeamFnApi.LogEntry> logs = new LinkedBlockingQueue<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    Collection<Future<Void>> futures = new ArrayList<>();
    try (BeamFnLoggingService service = new BeamFnLoggingService(findOpenPort(), logs::add, ServerStreamObserverFactory.fromOptions(PipelineOptionsFactory.create())::from, GrpcContextHeaderAccessorProvider.getHeaderAccessor())) {
        server = ServerFactory.createDefault().create(Arrays.asList(service), service.getApiServiceDescriptor());
        for (int i = 1; i <= 3; ++i) {
            long instructionId = i;
            futures.add(executorService.submit(() -> {
                CountDownLatch waitForServerHangup = new CountDownLatch(1);
                ManagedChannel channel = ManagedChannelFactory.createDefault().withInterceptors(Arrays.asList(AddHarnessIdInterceptor.create(WORKER_ID + instructionId))).forDescriptor(service.getApiServiceDescriptor());
                StreamObserver<BeamFnApi.LogEntry.List> outboundObserver = BeamFnLoggingGrpc.newStub(channel).logging(TestStreams.withOnNext(BeamFnLoggingServiceTest::discardMessage).withOnCompleted(waitForServerHangup::countDown).build());
                outboundObserver.onNext(createLogsWithIds(instructionId));
                waitForServerHangup.await();
                return null;
            }));
        }
        // Wait till each client has sent their message showing that they have connected.
        for (int i = 1; i <= 3; ++i) {
            logs.take();
        }
        service.close();
        server.shutdownNow();
    }
    for (Future<Void> future : futures) {
        future.get();
    }
}
Also used : StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) Test(org.junit.Test)

Example 85 with Channel

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

the class BeamFnLoggingServiceTest method testMultipleClientsSuccessfullyProcessed.

@Test
public void testMultipleClientsSuccessfullyProcessed() throws Exception {
    ConcurrentLinkedQueue<BeamFnApi.LogEntry> logs = new ConcurrentLinkedQueue<>();
    try (BeamFnLoggingService service = new BeamFnLoggingService(findOpenPort(), logs::add, ServerStreamObserverFactory.fromOptions(PipelineOptionsFactory.create())::from, GrpcContextHeaderAccessorProvider.getHeaderAccessor())) {
        server = ServerFactory.createDefault().create(Arrays.asList(service), service.getApiServiceDescriptor());
        Collection<Callable<Void>> tasks = new ArrayList<>();
        for (int i = 1; i <= 3; ++i) {
            int instructionId = i;
            tasks.add(() -> {
                CountDownLatch waitForServerHangup = new CountDownLatch(1);
                ManagedChannel channel = ManagedChannelFactory.createDefault().withInterceptors(Arrays.asList(AddHarnessIdInterceptor.create(WORKER_ID + instructionId))).forDescriptor(service.getApiServiceDescriptor());
                StreamObserver<BeamFnApi.LogEntry.List> outboundObserver = BeamFnLoggingGrpc.newStub(channel).logging(TestStreams.withOnNext(BeamFnLoggingServiceTest::discardMessage).withOnCompleted(waitForServerHangup::countDown).build());
                outboundObserver.onNext(createLogsWithIds(instructionId, -instructionId));
                outboundObserver.onCompleted();
                waitForServerHangup.await();
                return null;
            });
        }
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.invokeAll(tasks);
        assertThat(logs, containsInAnyOrder(createLogWithId(1L), createLogWithId(2L), createLogWithId(3L), createLogWithId(-1L), createLogWithId(-2L), createLogWithId(-3L)));
    }
}
Also used : BeamFnApi(org.apache.beam.model.fnexecution.v1.BeamFnApi) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) ArrayList(java.util.ArrayList) List(org.apache.beam.model.fnexecution.v1.BeamFnApi.LogEntry.List) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Aggregations

Channel (io.grpc.Channel)63 Test (org.junit.Test)55 CallOptions (io.grpc.CallOptions)38 Metadata (io.grpc.Metadata)26 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)25 MethodDescriptor (io.grpc.MethodDescriptor)21 ClientInterceptor (io.grpc.ClientInterceptor)20 ManagedChannel (io.grpc.ManagedChannel)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 ClientCall (io.grpc.ClientCall)14 ArrayList (java.util.ArrayList)14 ExecutorService (java.util.concurrent.ExecutorService)12 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)10 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)10 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)9 Endpoints (org.apache.beam.model.pipeline.v1.Endpoints)9 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)8 SocketAddress (java.net.SocketAddress)8