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