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);
}
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();
}
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();
}
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();
}
}
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());
}
Aggregations