Search in sources :

Example 1 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project core-java by SpineEventEngine.

the class CommandRouterOnErrorShould method createRouter.

/**
     * Creates a router with mocked {@code CommandBus} which always calls
     * {@link StreamObserver#onError(Throwable) StreamObserver.onError()} when
     * {@link CommandBus#post(Command, StreamObserver) CommandBus.post()} is invoked.
     */
@Override
CommandRouter createRouter(CommandBus ignored, Message sourceMessage, CommandContext commandContext) {
    final CommandBus mockBus = mock(CommandBus.class);
    doAnswer(new Answer() {

        // is OK for Answer
        @SuppressWarnings("ReturnOfNull")
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            final StreamObserver<Response> observer = invocation.getArgument(1);
            observer.onError(new RuntimeException("simulate error"));
            return null;
        }
    }).when(mockBus).post(any(Command.class), ArgumentMatchers.<StreamObserver<Response>>any());
    return new CommandRouter(mockBus, sourceMessage, commandContext);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Response(io.spine.base.Response) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CommandRouter(io.spine.server.procman.CommandRouter) Command(io.spine.base.Command) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommandBus(io.spine.server.commandbus.CommandBus)

Example 2 with StreamObserver

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

the class FlowFileIngestService method send.

/**
 * Handle receipt of a FlowFileRequest and route it to the appropriate process relationship.
 *
 * @param request          the flowfile request
 * @param responseObserver the mechanism by which to reply to the client
 */
@Override
public void send(final org.apache.nifi.processors.grpc.FlowFileRequest request, final StreamObserver<FlowFileReply> responseObserver) {
    final FlowFileReply.Builder replyBuilder = FlowFileReply.newBuilder();
    final String remoteHost = FlowFileIngestServiceInterceptor.REMOTE_HOST_KEY.get();
    final String remoteDN = FlowFileIngestServiceInterceptor.REMOTE_DN_KEY.get();
    // block until we have a session factory (occurs when processor is triggered)
    ProcessSessionFactory sessionFactory = null;
    while (sessionFactory == null) {
        sessionFactory = sessionFactoryReference.get();
        if (sessionFactory == null) {
            try {
                Thread.sleep(10);
            } catch (final InterruptedException e) {
            }
        }
    }
    final ProcessSession session = sessionFactory.createSession();
    // if there's no space available, reject the request.
    final long n = filesReceived.getAndIncrement() % FILES_BEFORE_CHECKING_DESTINATION_SPACE;
    if (n == 0 || !spaceAvailable.get()) {
        if (context.getAvailableRelationships().isEmpty()) {
            spaceAvailable.set(false);
            final String message = "Received request from " + remoteHost + " but no space available; Indicating Service Unavailable";
            if (logger.isDebugEnabled()) {
                logger.debug(message);
            }
            final FlowFileReply reply = replyBuilder.setResponseCode(FlowFileReply.ResponseCode.ERROR).setBody(message).build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
            return;
        } else {
            spaceAvailable.set(true);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Received request from " + remoteHost);
    }
    final long startNanos = System.nanoTime();
    FlowFile flowFile = session.create();
    // push the attributes provided onto the created flowfile
    final Map<String, String> attributes = Maps.newHashMap();
    attributes.putAll(request.getAttributesMap());
    String sourceSystemFlowFileIdentifier = attributes.get(CoreAttributes.UUID.key());
    if (sourceSystemFlowFileIdentifier != null) {
        sourceSystemFlowFileIdentifier = "urn:nifi:" + sourceSystemFlowFileIdentifier;
        // If we receveied a UUID, we want to give the FlowFile a new UUID and register the sending system's
        // identifier as the SourceSystemFlowFileIdentifier field in the Provenance RECEIVE event
        attributes.put(CoreAttributes.UUID.key(), UUID.randomUUID().toString());
    }
    flowFile = session.putAllAttributes(flowFile, attributes);
    final ByteString content = request.getContent();
    final InputStream contentStream = content.newInput();
    // write the provided content to the flowfile
    flowFile = session.write(flowFile, out -> {
        try (final BufferedOutputStream bos = new BufferedOutputStream(out, 65536)) {
            IOUtils.copy(contentStream, bos);
        }
    });
    final long transferNanos = System.nanoTime() - startNanos;
    final long transferMillis = TimeUnit.MILLISECONDS.convert(transferNanos, TimeUnit.NANOSECONDS);
    session.getProvenanceReporter().receive(flowFile, SERVICE_NAME, sourceSystemFlowFileIdentifier, "Remote DN=" + remoteDN, transferMillis);
    flowFile = session.putAttribute(flowFile, ListenGRPC.REMOTE_HOST, remoteHost);
    flowFile = session.putAttribute(flowFile, ListenGRPC.REMOTE_USER_DN, remoteDN);
    // register success
    session.transfer(flowFile, ListenGRPC.REL_SUCCESS);
    session.commit();
    // reply to client
    final FlowFileReply reply = replyBuilder.setResponseCode(FlowFileReply.ResponseCode.SUCCESS).setBody("FlowFile successfully received.").build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessContext(org.apache.nifi.processor.ProcessContext) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessSession(org.apache.nifi.processor.ProcessSession) UUID(java.util.UUID) ComponentLog(org.apache.nifi.logging.ComponentLog) Maps(com.google.common.collect.Maps) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferedOutputStream(java.io.BufferedOutputStream) ByteString(com.google.protobuf.ByteString) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) CoreAttributes(org.apache.nifi.flowfile.attributes.CoreAttributes) InputStream(java.io.InputStream) FlowFile(org.apache.nifi.flowfile.FlowFile) ByteString(com.google.protobuf.ByteString) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project pravega by pravega.

the class ControllerImplLBTest method setup.

@Before
public void setup() throws IOException {
    final int serverPort1 = TestUtils.getAvailableListenPort();
    final int serverPort2 = TestUtils.getAvailableListenPort();
    final int serverPort3 = TestUtils.getAvailableListenPort();
    // Setup fake servers for simulating multiple controllers with discovery info.
    ControllerServiceImplBase fakeServerImpl1 = new ControllerServiceImplBase() {

        @Override
        public void getControllerServerList(ServerRequest request, StreamObserver<ServerResponse> responseObserver) {
            responseObserver.onNext(ServerResponse.newBuilder().addNodeURI(NodeUri.newBuilder().setEndpoint("localhost").setPort(serverPort1).build()).addNodeURI(NodeUri.newBuilder().setEndpoint("localhost").setPort(serverPort2).build()).addNodeURI(NodeUri.newBuilder().setEndpoint("localhost").setPort(serverPort3).build()).build());
            responseObserver.onCompleted();
        }

        @Override
        public void getURI(SegmentId request, StreamObserver<NodeUri> responseObserver) {
            responseObserver.onNext(NodeUri.newBuilder().setEndpoint("localhost1").setPort(1).build());
            responseObserver.onCompleted();
        }
    };
    ControllerServiceImplBase fakeServerImpl2 = new ControllerServiceImplBase() {

        @Override
        public void getControllerServerList(ServerRequest request, StreamObserver<ServerResponse> responseObserver) {
            responseObserver.onNext(ServerResponse.newBuilder().addNodeURI(NodeUri.newBuilder().setEndpoint("localhost").setPort(serverPort1).build()).addNodeURI(NodeUri.newBuilder().setEndpoint("localhost").setPort(serverPort2).build()).addNodeURI(NodeUri.newBuilder().setEndpoint("localhost").setPort(serverPort3).build()).build());
            responseObserver.onCompleted();
        }

        @Override
        public void getURI(SegmentId request, StreamObserver<NodeUri> responseObserver) {
            responseObserver.onNext(NodeUri.newBuilder().setEndpoint("localhost2").setPort(2).build());
            responseObserver.onCompleted();
        }
    };
    ControllerServiceImplBase fakeServerImpl3 = new ControllerServiceImplBase() {

        @Override
        public void getURI(SegmentId request, StreamObserver<NodeUri> responseObserver) {
            responseObserver.onNext(NodeUri.newBuilder().setEndpoint("localhost3").setPort(3).build());
            responseObserver.onCompleted();
        }
    };
    testRPCServer1 = NettyServerBuilder.forPort(serverPort1).addService(fakeServerImpl1).build().start();
    testRPCServer2 = NettyServerBuilder.forPort(serverPort2).addService(fakeServerImpl2).build().start();
    testRPCServer3 = NettyServerBuilder.forPort(serverPort3).addService(fakeServerImpl3).build().start();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ControllerServiceImplBase(io.pravega.controller.stream.api.grpc.v1.ControllerServiceGrpc.ControllerServiceImplBase) SegmentId(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentId) ServerRequest(io.pravega.controller.stream.api.grpc.v1.Controller.ServerRequest) Before(org.junit.Before)

Example 4 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project etcd-java by IBM.

the class WatchTest method testWatch.

@Test
public void testWatch() throws Exception {
    proxy.start();
    try (KvStoreClient directClient = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build();
        KvStoreClient client = EtcdClient.forEndpoint("localhost", 2390).withPlainText().build()) {
        KvClient kvc = client.getKvClient();
        long start = System.currentTimeMillis();
        final BlockingQueue<Object> watchEvents = new LinkedBlockingQueue<>();
        final Object COMPLETED = new Object();
        final StreamObserver<WatchUpdate> observer = new StreamObserver<WatchUpdate>() {

            @Override
            public void onNext(WatchUpdate value) {
                System.out.println(t(start) + "watch event: " + value);
                watchEvents.add(value);
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t(start) + "watch error: " + t);
                watchEvents.add(t);
            }

            @Override
            public void onCompleted() {
                System.out.println(t(start) + "watch completed");
                watchEvents.add(COMPLETED);
            }
        };
        Watch watch = kvc.watch(bs("/watchtest")).asPrefix().start(observer);
        // assertFalse(watch.isDone());
        // test blocking watch at the same time
        WatchIterator watchIterator = kvc.watch(bs("/watchtest")).asPrefix().start();
        assertTrue(watch.get(1, TimeUnit.SECONDS));
        kvc.put(bs("/watchtest/a"), bs("a value")).sync();
        directClient.getKvClient().put(bs("/watchtest/b"), bs("b value")).sync();
        WatchUpdate wu = getNextSkipEmpty(watchEvents);
        assertNotNull(wu);
        assertEquals("event: " + wu, 1, wu.getEvents().size());
        assertEquals(EventType.PUT, wu.getEvents().get(0).getType());
        assertEquals(bs("a value"), wu.getEvents().get(0).getKv().getValue());
        assertEquals(bs("/watchtest/a"), wu.getEvents().get(0).getKv().getKey());
        WatchUpdate wu2 = getNextSkipEmpty(watchIterator);
        assertEquals(EventType.PUT, wu2.getEvents().get(0).getType());
        assertEquals(bs("a value"), wu2.getEvents().get(0).getKv().getValue());
        assertEquals(bs("/watchtest/a"), wu2.getEvents().get(0).getKv().getKey());
        watchEvents.poll(500, TimeUnit.MILLISECONDS);
        watch.close();
        assertEquals(COMPLETED, watchEvents.poll(500, TimeUnit.MILLISECONDS));
        kvc.put(bs("/watchtest/c"), bs("c value")).sync();
        assertNull(watchEvents.poll(500, TimeUnit.MILLISECONDS));
        assertTrue(watchIterator.hasNext());
        assertTrue(watchIterator.hasNext());
        assertEquals(bs("b value"), watchIterator.next().getEvents().get(0).getKv().getValue());
        assertEquals(EventType.PUT, watchIterator.next().getEvents().get(0).getType());
        // fresh new watch
        watch = kvc.watch(ByteString.copyFromUtf8("/watchtest")).asPrefix().start(observer);
        assertTrue(watch.get(1, TimeUnit.SECONDS));
        kvc.batch().put(kvc.put(bs("/watchtest/d"), bs("d value")).asRequest()).delete(kvc.delete(bs("/watchtest/a")).asRequest()).sync();
        wu = getNextSkipEmpty(watchEvents);
        assertEquals(2, wu.getEvents().size());
        assertTrue(wu.getEvents().stream().anyMatch(e -> e.getType() == EventType.DELETE));
        assertTrue(wu.getEvents().stream().anyMatch(e -> e.getType() == EventType.PUT));
        // kill path to server
        proxy.kill();
        Thread.sleep(1000L);
        // while disconnected, put a new key
        directClient.getKvClient().put(bs("/watchtest/e"), bs("e value")).sync();
        Thread.sleep(1000L);
        // reinstate path to server
        proxy.start();
        // watch should be unaffected - next event seen should be the missed one
        wu = (WatchUpdate) watchEvents.poll(3000L, TimeUnit.MILLISECONDS);
        assertEquals(bs("/watchtest/e"), wu.getEvents().get(0).getKv().getKey());
        watch.close();
        // (earlier batch update)
        assertEquals(2, watchIterator.next().getEvents().size());
        assertEquals(bs("/watchtest/e"), watchIterator.next().getEvents().get(0).getKv().getKey());
        watchIterator.close();
        assertNull(watchIterator.next().getHeader());
        assertFalse(watchIterator.hasNext());
        try {
            watchIterator.next();
            fail("should throw NSEE here");
        } catch (NoSuchElementException nsee) {
        }
    } finally {
        proxy.kill();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AfterClass(org.junit.AfterClass) WatchUpdate(com.ibm.etcd.client.kv.WatchUpdate) BeforeClass(org.junit.BeforeClass) Watch(com.ibm.etcd.client.kv.KvClient.Watch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KvClient(com.ibm.etcd.client.kv.KvClient) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) WatchIterator(com.ibm.etcd.client.kv.KvClient.WatchIterator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchCreateException(com.ibm.etcd.client.watch.WatchCreateException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ByteString(com.google.protobuf.ByteString) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Phaser(java.util.concurrent.Phaser) EventType(com.ibm.etcd.api.Event.EventType) KvTest.bs(com.ibm.etcd.client.KvTest.bs) Assert(org.junit.Assert) NoSuchElementException(java.util.NoSuchElementException) PutResponse(com.ibm.etcd.api.PutResponse) KvTest.t(com.ibm.etcd.client.KvTest.t) WatchUpdate(com.ibm.etcd.client.kv.WatchUpdate) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Watch(com.ibm.etcd.client.kv.KvClient.Watch) KvClient(com.ibm.etcd.client.kv.KvClient) NoSuchElementException(java.util.NoSuchElementException) WatchIterator(com.ibm.etcd.client.kv.KvClient.WatchIterator) Test(org.junit.Test)

Example 5 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project bookkeeper by apache.

the class TestLocationClientImpl method testLocateStorageContainersSucceedAfterRetried.

@Test
public void testLocateStorageContainersSucceedAfterRetried() throws Exception {
    serviceRegistry.removeService(locationServiceDefinition);
    final AtomicInteger retries = new AtomicInteger(3);
    StatusRuntimeException statusException = new StatusRuntimeException(Status.INTERNAL);
    StorageContainerServiceImplBase locationServiceWithFailures = new StorageContainerServiceImplBase() {

        @Override
        public void getStorageContainerEndpoint(GetStorageContainerEndpointRequest request, StreamObserver<GetStorageContainerEndpointResponse> responseObserver) {
            if (retries.decrementAndGet() == 0) {
                locationService.getStorageContainerEndpoint(request, responseObserver);
                return;
            }
            responseObserver.onError(statusException);
        }
    };
    serviceRegistry.addService(locationServiceWithFailures.bindService());
    testLocateStorageContainersSuccess();
    assertEquals(0, retries.get());
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StorageContainerServiceImplBase(org.apache.bookkeeper.stream.proto.storage.StorageContainerServiceGrpc.StorageContainerServiceImplBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatusRuntimeException(io.grpc.StatusRuntimeException) GetStorageContainerEndpointRequest(org.apache.bookkeeper.stream.proto.storage.GetStorageContainerEndpointRequest) Test(org.junit.Test)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)130 Test (org.junit.Test)93 CountDownLatch (java.util.concurrent.CountDownLatch)50 ArrayList (java.util.ArrayList)47 AtomicReference (java.util.concurrent.atomic.AtomicReference)38 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)27 StatusRuntimeException (io.grpc.StatusRuntimeException)26 Status (io.grpc.Status)20 List (java.util.List)18 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)18 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)18 CompletableFuture (java.util.concurrent.CompletableFuture)17 ExecutorService (java.util.concurrent.ExecutorService)16 SegmentId (io.pravega.controller.stream.api.grpc.v1.Controller.SegmentId)14 ServerRequest (io.pravega.controller.stream.api.grpc.v1.Controller.ServerRequest)14 VisibleForTesting (com.google.common.annotations.VisibleForTesting)12 Strings (com.google.common.base.Strings)12 Throwables (com.google.common.base.Throwables)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 AuthHandler (io.pravega.auth.AuthHandler)12