use of io.grpc.ClientStreamTracer.StreamInfo in project grpc-java by grpc.
the class ClientStreamTracerTest method streamInfo_toBuilder.
@Test
public void streamInfo_toBuilder() {
StreamInfo info1 = StreamInfo.newBuilder().setCallOptions(callOptions).build();
StreamInfo info2 = info1.toBuilder().build();
assertThat(info2.getCallOptions()).isSameInstanceAs(callOptions);
}
use of io.grpc.ClientStreamTracer.StreamInfo in project grpc-java by grpc.
the class RetryTest method statsRecorde_callCancelledBeforeCommit.
@Test
public void statsRecorde_callCancelledBeforeCommit() throws Exception {
startNewServer();
retryPolicy = ImmutableMap.<String, Object>builder().put("maxAttempts", 4D).put("initialBackoff", "10s").put("maxBackoff", "10s").put("backoffMultiplier", 1D).put("retryableStatusCodes", Arrays.<Object>asList("UNAVAILABLE")).build();
createNewChannel();
// We will have streamClosed return at a particular moment that we want.
final CountDownLatch streamClosedLatch = new CountDownLatch(1);
ClientStreamTracer.Factory streamTracerFactory = new ClientStreamTracer.Factory() {
@Override
public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
return new ClientStreamTracer() {
@Override
public void streamClosed(Status status) {
if (status.getCode().equals(Code.CANCELLED)) {
try {
streamClosedLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError("streamClosedLatch interrupted", e);
}
}
}
};
}
};
ClientCall<String, Integer> call = channel.newCall(clientStreamingMethod, CallOptions.DEFAULT.withStreamTracerFactory(streamTracerFactory));
call.start(mockCallListener, new Metadata());
assertRpcStartedRecorded();
fakeClock.forwardTime(5, SECONDS);
String message = "String of length 20.";
call.sendMessage(message);
assertOutboundMessageRecorded();
ServerCall<String, Integer> serverCall = serverCalls.poll(5, SECONDS);
serverCall.request(2);
assertOutboundWireSizeRecorded(message.length());
// trigger retry
serverCall.close(Status.UNAVAILABLE.withDescription("original attempt failed"), new Metadata());
assertRpcStatusRecorded(Code.UNAVAILABLE, 5000, 1);
elapseBackoff(10, SECONDS);
assertRpcStartedRecorded();
assertOutboundMessageRecorded();
serverCall = serverCalls.poll(5, SECONDS);
serverCall.request(2);
assertOutboundWireSizeRecorded(message.length());
fakeClock.forwardTime(7, SECONDS);
// A noop substream will commit.
call.cancel("Cancelled before commit", null);
// The call listener is closed, but the netty substream listener is not yet closed.
verify(mockCallListener, timeout(5000)).onClose(any(Status.class), any(Metadata.class));
// Let the netty substream listener be closed.
streamClosedLatch.countDown();
assertRetryStatsRecorded(1, 0, 10_000);
assertRpcStatusRecorded(Code.CANCELLED, 7_000, 1);
}
use of io.grpc.ClientStreamTracer.StreamInfo in project grpc-java by grpc.
the class GrpcUtil method getTransportFromPickResult.
/**
* Returns a transport out of a PickResult, or {@code null} if the result is "buffer".
*/
@Nullable
static ClientTransport getTransportFromPickResult(PickResult result, boolean isWaitForReady) {
final ClientTransport transport;
Subchannel subchannel = result.getSubchannel();
if (subchannel != null) {
transport = ((TransportProvider) subchannel.getInternalSubchannel()).obtainActiveTransport();
} else {
transport = null;
}
if (transport != null) {
final ClientStreamTracer.Factory streamTracerFactory = result.getStreamTracerFactory();
if (streamTracerFactory == null) {
return transport;
}
return new ClientTransport() {
@Override
public ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers) {
StreamInfo info = StreamInfo.newBuilder().setCallOptions(callOptions).build();
ClientStreamTracer streamTracer = streamTracerFactory.newClientStreamTracer(info, headers);
checkState(tracers[tracers.length - 1] == NOOP_TRACER, "lb tracer already assigned");
tracers[tracers.length - 1] = streamTracer;
return transport.newStream(method, headers, callOptions, tracers);
}
@Override
public void ping(PingCallback callback, Executor executor) {
transport.ping(callback, executor);
}
@Override
public InternalLogId getLogId() {
return transport.getLogId();
}
@Override
public ListenableFuture<SocketStats> getStats() {
return transport.getStats();
}
};
}
if (!result.getStatus().isOk()) {
if (result.isDrop()) {
return new FailingClientTransport(result.getStatus(), RpcProgress.DROPPED);
}
if (!isWaitForReady) {
return new FailingClientTransport(result.getStatus(), RpcProgress.PROCESSED);
}
}
return null;
}
use of io.grpc.ClientStreamTracer.StreamInfo in project grpc-java by grpc.
the class ManagedChannelImplTest method pickerReturnsStreamTracer_delayed.
@Test
public void pickerReturnsStreamTracer_delayed() {
ClientStream mockStream = mock(ClientStream.class);
final ClientStreamTracer tracer1 = new ClientStreamTracer() {
};
final ClientStreamTracer tracer2 = new ClientStreamTracer() {
};
ClientStreamTracer.Factory factory1 = new ClientStreamTracer.Factory() {
@Override
public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
return tracer1;
}
};
ClientStreamTracer.Factory factory2 = new ClientStreamTracer.Factory() {
@Override
public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
return tracer2;
}
};
createChannel();
CallOptions callOptions = CallOptions.DEFAULT.withStreamTracerFactory(factory1);
ClientCall<String, Integer> call = channel.newCall(method, callOptions);
call.start(mockCallListener, new Metadata());
Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
requestConnectionSafely(helper, subchannel);
MockClientTransportInfo transportInfo = transports.poll();
transportInfo.listener.transportReady();
ClientTransport mockTransport = transportInfo.transport;
when(mockTransport.newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any())).thenReturn(mockStream);
when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel, factory2));
updateBalancingStateSafely(helper, READY, mockPicker);
assertEquals(1, executor.runDueTasks());
verify(mockPicker).pickSubchannel(any(PickSubchannelArgs.class));
verify(mockTransport).newStream(same(method), any(Metadata.class), callOptionsCaptor.capture(), tracersCaptor.capture());
assertThat(tracersCaptor.getValue()).isEqualTo(new ClientStreamTracer[] { tracer1, tracer2 });
}
use of io.grpc.ClientStreamTracer.StreamInfo in project grpc-java by grpc.
the class ManagedChannelImplTest method pickerReturnsStreamTracer_noDelay.
@Test
public void pickerReturnsStreamTracer_noDelay() {
ClientStream mockStream = mock(ClientStream.class);
final ClientStreamTracer tracer1 = new ClientStreamTracer() {
};
final ClientStreamTracer tracer2 = new ClientStreamTracer() {
};
ClientStreamTracer.Factory factory1 = new ClientStreamTracer.Factory() {
@Override
public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
return tracer1;
}
};
ClientStreamTracer.Factory factory2 = new ClientStreamTracer.Factory() {
@Override
public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
return tracer2;
}
};
createChannel();
Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
requestConnectionSafely(helper, subchannel);
MockClientTransportInfo transportInfo = transports.poll();
transportInfo.listener.transportReady();
ClientTransport mockTransport = transportInfo.transport;
when(mockTransport.newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any())).thenReturn(mockStream);
when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel, factory2));
updateBalancingStateSafely(helper, READY, mockPicker);
CallOptions callOptions = CallOptions.DEFAULT.withStreamTracerFactory(factory1);
ClientCall<String, Integer> call = channel.newCall(method, callOptions);
call.start(mockCallListener, new Metadata());
verify(mockPicker).pickSubchannel(any(PickSubchannelArgs.class));
verify(mockTransport).newStream(same(method), any(Metadata.class), callOptionsCaptor.capture(), tracersCaptor.capture());
assertThat(tracersCaptor.getValue()).isEqualTo(new ClientStreamTracer[] { tracer1, tracer2 });
}
Aggregations