use of io.grpc.ManagedChannel in project grpc-java by grpc.
the class OpenLoopClient method run.
/**
* Start the open loop client.
*/
public void run() throws Exception {
if (config == null) {
return;
}
config.channels = 1;
config.directExecutor = true;
ManagedChannel ch = config.newChannel();
SimpleRequest req = config.newRequest();
LoadGenerationWorker worker = new LoadGenerationWorker(ch, req, config.targetQps, config.duration);
final long start = System.nanoTime();
Histogram histogram = worker.call();
final long end = System.nanoTime();
printStats(histogram, end - start);
if (config.histogramFile != null) {
saveHistogram(histogram, config.histogramFile);
}
ch.shutdown();
}
use of io.grpc.ManagedChannel in project grpc-java by grpc.
the class AbstractBenchmark method teardown.
/**
* Shutdown all the client channels and then shutdown the server.
*/
protected void teardown() throws Exception {
logger.fine("shutting down channels");
for (ManagedChannel channel : channels) {
channel.shutdown();
}
logger.fine("shutting down server");
server.shutdown();
if (!server.awaitTermination(5, TimeUnit.SECONDS)) {
logger.warning("Failed to shutdown server");
}
logger.fine("server shut down");
for (ManagedChannel channel : channels) {
if (!channel.awaitTermination(1, TimeUnit.SECONDS)) {
logger.warning("Failed to shutdown client");
}
}
logger.fine("channels shut down");
}
use of io.grpc.ManagedChannel in project grpc-java by grpc.
the class AbstractBenchmark method startStreamingCalls.
/**
* Start a continuously executing set of duplex streaming ping-pong calls that will terminate when
* {@code done.get()} is true. Each completed call will increment the counter by the specified
* delta which benchmarks can use to measure messages per second or bandwidth.
*/
protected CountDownLatch startStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) {
final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length);
for (final ManagedChannel channel : channels) {
for (int i = 0; i < callsPerChannel; i++) {
final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(pingPongMethod, CALL_OPTIONS);
final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<StreamObserver<ByteBuf>>();
final AtomicBoolean ignoreMessages = new AtomicBoolean();
StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall(streamingCall, new StreamObserver<ByteBuf>() {
@Override
public void onNext(ByteBuf value) {
if (done.get()) {
if (!ignoreMessages.getAndSet(true)) {
requestObserverRef.get().onCompleted();
}
return;
}
requestObserverRef.get().onNext(request.slice());
if (record.get()) {
counter.addAndGet(counterDelta);
}
// request is called automatically because the observer implicitly has auto
// inbound flow control
}
@Override
public void onError(Throwable t) {
logger.log(Level.WARNING, "call error", t);
latch.countDown();
}
@Override
public void onCompleted() {
latch.countDown();
}
});
requestObserverRef.set(requestObserver);
requestObserver.onNext(request.slice());
requestObserver.onNext(request.slice());
}
}
return latch;
}
use of io.grpc.ManagedChannel in project grpc-java by grpc.
the class ManagedChannelImplIdlenessTest method oobTransportDoesNotAffectIdleness.
@Test
public void oobTransportDoesNotAffectIdleness() {
// Start a call, which goes to delayed transport
ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
call.start(mockCallListener, new Metadata());
// Verify that we have exited the idle mode
ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
verify(mockLoadBalancerFactory).newLoadBalancer(helperCaptor.capture());
Helper helper = helperCaptor.getValue();
// Fail the RPC
SubchannelPicker failingPicker = mock(SubchannelPicker.class);
when(failingPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withError(Status.UNAVAILABLE));
helper.updatePicker(failingPicker);
executor.runDueTasks();
verify(mockCallListener).onClose(same(Status.UNAVAILABLE), any(Metadata.class));
// ... so that the channel resets its in-use state
assertFalse(channel.inUseStateAggregator.isInUse());
// Now make an RPC on an OOB channel
ManagedChannel oob = helper.createOobChannel(addressGroupList.get(0), "oobauthority");
verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT));
ClientCall<String, Integer> oobCall = oob.newCall(method, CallOptions.DEFAULT);
oobCall.start(mockCallListener2, new Metadata());
verify(mockTransportFactory).newClientTransport(any(SocketAddress.class), same("oobauthority"), same(USER_AGENT));
MockClientTransportInfo oobTransportInfo = newTransports.poll();
assertEquals(0, newTransports.size());
// The OOB transport reports in-use state
oobTransportInfo.listener.transportInUse(true);
// But it won't stop the channel from going idle
verify(mockLoadBalancer, never()).shutdown();
timer.forwardTime(IDLE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
verify(mockLoadBalancer).shutdown();
}
use of io.grpc.ManagedChannel in project grpc-java by grpc.
the class ManagedChannelImplTest method oobChannelsNoConnectionShutdown.
@Test
public void oobChannelsNoConnectionShutdown() {
createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
ManagedChannel oob1 = helper.createOobChannel(addressGroup, "oob1Authority");
ManagedChannel oob2 = helper.createOobChannel(addressGroup, "oob2Authority");
channel.shutdown();
verify(mockLoadBalancer).shutdown();
oob1.shutdown();
assertTrue(oob1.isTerminated());
assertFalse(channel.isTerminated());
oob2.shutdown();
assertTrue(oob2.isTerminated());
assertTrue(channel.isTerminated());
verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), anyString(), anyString());
}
Aggregations