Search in sources :

Example 86 with Channel

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

the class ArtifactStagingServiceTest method setUp.

@Before
public void setUp() throws Exception {
    stagingDir = tempFolder.newFolder("staging").toPath();
    stagingService = new ArtifactStagingService(ArtifactStagingService.beamFilesystemArtifactDestinationProvider(stagingDir.toString()));
    retrievalService = new ArtifactRetrievalService(TEST_BUFFER_SIZE);
    grpcCleanup.register(InProcessServerBuilder.forName("server").directExecutor().addService(stagingService).addService(retrievalService).build().start());
    ManagedChannel channel = grpcCleanup.register(InProcessChannelBuilder.forName("server").build());
    stagingStub = ArtifactStagingServiceGrpc.newStub(channel);
    retrievalBlockingStub = ArtifactRetrievalServiceGrpc.newBlockingStub(channel);
}
Also used : ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) Before(org.junit.Before)

Example 87 with Channel

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

the class GrpcContextHeaderAccessorProviderTest method testWorkerIdOnConnect.

@SuppressWarnings("unchecked")
@Test
public void testWorkerIdOnConnect() throws Exception {
    final String worker1 = "worker1";
    CompletableFuture<String> workerId = new CompletableFuture<>();
    Consumer<StreamObserver<Elements>> consumer = elementsStreamObserver -> {
        workerId.complete(GrpcContextHeaderAccessorProvider.getHeaderAccessor().getSdkWorkerId());
        elementsStreamObserver.onCompleted();
    };
    TestDataService testService = new TestDataService(Mockito.mock(StreamObserver.class), consumer);
    ApiServiceDescriptor serviceDescriptor = ApiServiceDescriptor.newBuilder().setUrl("testServer").build();
    cleanupRule.register(InProcessServerFactory.create().create(ImmutableList.of(testService), serviceDescriptor));
    final Metadata.Key<String> workerIdKey = Metadata.Key.of("worker_id", Metadata.ASCII_STRING_MARSHALLER);
    Channel channel = cleanupRule.register(InProcessChannelBuilder.forName(serviceDescriptor.getUrl()).intercept(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new SimpleForwardingClientCall<ReqT, RespT>(call) {

                @Override
                public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    headers.put(workerIdKey, worker1);
                    super.start(responseListener, headers);
                }
            };
        }
    }).build());
    BeamFnDataGrpc.BeamFnDataStub stub = BeamFnDataGrpc.newStub(channel);
    stub.data(Mockito.mock(StreamObserver.class)).onCompleted();
    Assert.assertEquals(worker1, workerId.get());
}
Also used : StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) ClientInterceptor(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor) Elements(org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements) RunWith(org.junit.runner.RunWith) Channel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Channel) CompletableFuture(java.util.concurrent.CompletableFuture) BeamFnDataGrpc(org.apache.beam.model.fnexecution.v1.BeamFnDataGrpc) GrpcContextHeaderAccessorProvider(org.apache.beam.sdk.fn.server.GrpcContextHeaderAccessorProvider) InProcessChannelBuilder(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.inprocess.InProcessChannelBuilder) MethodDescriptor(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor) ClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientCall) ApiServiceDescriptor(org.apache.beam.model.pipeline.v1.Endpoints.ApiServiceDescriptor) InProcessServerFactory(org.apache.beam.sdk.fn.server.InProcessServerFactory) Metadata(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) BeamFnApi(org.apache.beam.model.fnexecution.v1.BeamFnApi) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CallOptions(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions) GrpcCleanupRule(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.testing.GrpcCleanupRule) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) SimpleForwardingClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Assert(org.junit.Assert) ApiServiceDescriptor(org.apache.beam.model.pipeline.v1.Endpoints.ApiServiceDescriptor) Channel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Channel) Metadata(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata) CallOptions(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions) CompletableFuture(java.util.concurrent.CompletableFuture) ClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientCall) SimpleForwardingClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientInterceptor(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor) BeamFnDataGrpc(org.apache.beam.model.fnexecution.v1.BeamFnDataGrpc) Test(org.junit.Test)

Example 88 with Channel

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

the class ArtifactRetrievalServiceTest method setUp.

@Before
public void setUp() throws Exception {
    retrievalService = new ArtifactRetrievalService(TEST_BUFFER_SIZE);
    grpcCleanup.register(InProcessServerBuilder.forName("server").directExecutor().addService(retrievalService).build().start());
    ManagedChannel channel = grpcCleanup.register(InProcessChannelBuilder.forName("server").build());
    retrievalBlockingStub = ArtifactRetrievalServiceGrpc.newBlockingStub(channel);
    stagingDir = tempFolder.newFolder("staging").toPath();
}
Also used : ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) Before(org.junit.Before)

Example 89 with Channel

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

the class GrpcLoggingServiceTest method testMultipleClientsFailingIsHandledGracefullyByServer.

@Test
public void testMultipleClientsFailingIsHandledGracefullyByServer() throws Exception {
    ConcurrentLinkedQueue<BeamFnApi.LogEntry> logs = new ConcurrentLinkedQueue<>();
    GrpcLoggingService service = GrpcLoggingService.forWriter(new CollectionAppendingLogWriter(logs));
    try (GrpcFnServer<GrpcLoggingService> server = GrpcFnServer.allocatePortAndCreateFor(service, InProcessServerFactory.create())) {
        CountDownLatch waitForTermination = new CountDownLatch(3);
        final BlockingQueue<StreamObserver<LogEntry.List>> outboundObservers = new LinkedBlockingQueue<>();
        Collection<Callable<Void>> tasks = new ArrayList<>();
        for (int i = 1; i <= 3; ++i) {
            final int instructionId = i;
            tasks.add(() -> {
                ManagedChannel channel = InProcessChannelBuilder.forName(server.getApiServiceDescriptor().getUrl()).build();
                StreamObserver<LogEntry.List> outboundObserver = BeamFnLoggingGrpc.newStub(channel).logging(TestStreams.withOnNext(messageDiscarder).withOnError(new CountDown(waitForTermination)).build());
                outboundObserver.onNext(createLogsWithIds(instructionId, -instructionId));
                outboundObservers.add(outboundObserver);
                return null;
            });
        }
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.invokeAll(tasks);
        for (int i = 1; i <= 3; ++i) {
            outboundObservers.take().onError(new RuntimeException("Client " + i));
        }
        waitForTermination.await();
    }
}
Also used : StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) ArrayList(java.util.ArrayList) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) LogEntry(org.apache.beam.model.fnexecution.v1.BeamFnApi.LogEntry) Test(org.junit.Test)

Example 90 with Channel

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

the class GrpcDataServiceTest method testMessageReceivedBySingleClientWhenThereAreMultipleClients.

@Test
public void testMessageReceivedBySingleClientWhenThereAreMultipleClients() throws Exception {
    final LinkedBlockingQueue<Elements> clientInboundElements = new LinkedBlockingQueue<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    final CountDownLatch waitForInboundElements = new CountDownLatch(1);
    GrpcDataService service = GrpcDataService.create(PipelineOptionsFactory.create(), Executors.newCachedThreadPool(), OutboundObserverFactory.serverDirect());
    try (GrpcFnServer<GrpcDataService> server = GrpcFnServer.allocatePortAndCreateFor(service, InProcessServerFactory.create())) {
        Collection<Future<Void>> clientFutures = new ArrayList<>();
        for (int i = 0; i < 3; ++i) {
            clientFutures.add(executorService.submit(() -> {
                ManagedChannel channel = InProcessChannelBuilder.forName(server.getApiServiceDescriptor().getUrl()).directExecutor().build();
                StreamObserver<Elements> outboundObserver = BeamFnDataGrpc.newStub(channel).data(TestStreams.withOnNext(clientInboundElements::add).build());
                waitForInboundElements.await();
                outboundObserver.onCompleted();
                return null;
            }));
        }
        for (int i = 0; i < 3; ++i) {
            CloseableFnDataReceiver<WindowedValue<String>> consumer = service.send(LogicalEndpoint.data(Integer.toString(i), TRANSFORM_ID), CODER);
            consumer.accept(WindowedValue.valueInGlobalWindow("A" + i));
            consumer.accept(WindowedValue.valueInGlobalWindow("B" + i));
            consumer.accept(WindowedValue.valueInGlobalWindow("C" + i));
            consumer.close();
        }
        waitForInboundElements.countDown();
        for (Future<Void> clientFuture : clientFutures) {
            clientFuture.get();
        }
        assertThat(clientInboundElements, containsInAnyOrder(elementsWithData("0"), elementsWithData("1"), elementsWithData("2")));
    }
}
Also used : StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) ArrayList(java.util.ArrayList) Elements(org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) LogicalEndpoint(org.apache.beam.sdk.fn.data.LogicalEndpoint) WindowedValue(org.apache.beam.sdk.util.WindowedValue) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) 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