inprocess: Support tracing message sizes guarded by flag (#11629)

This commit is contained in:
MV Shiva 2024-10-24 01:22:41 +05:30 committed by GitHub
parent 62f409810d
commit b65cbf5081
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 92 additions and 55 deletions

View File

@ -96,6 +96,9 @@ public abstract class AbstractTransportTest {
private static final int TIMEOUT_MS = 5000; private static final int TIMEOUT_MS = 5000;
protected static final String GRPC_EXPERIMENTAL_SUPPORT_TRACING_MESSAGE_SIZES =
"GRPC_EXPERIMENTAL_SUPPORT_TRACING_MESSAGE_SIZES";
private static final Attributes.Key<String> ADDITIONAL_TRANSPORT_ATTR_KEY = private static final Attributes.Key<String> ADDITIONAL_TRANSPORT_ATTR_KEY =
Attributes.Key.create("additional-attr"); Attributes.Key.create("additional-attr");
@ -238,6 +241,13 @@ public abstract class AbstractTransportTest {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
/**
* Returns true if env var is set.
*/
protected static boolean isEnabledSupportTracingMessageSizes() {
return GrpcUtil.getFlag(GRPC_EXPERIMENTAL_SUPPORT_TRACING_MESSAGE_SIZES, false);
}
/** /**
* Returns the current time, for tests that rely on the clock. * Returns the current time, for tests that rely on the clock.
*/ */
@ -850,16 +860,21 @@ public abstract class AbstractTransportTest {
message.close(); message.close();
assertThat(clientStreamTracer1.nextOutboundEvent()) assertThat(clientStreamTracer1.nextOutboundEvent())
.matches("outboundMessageSent\\(0, -?[0-9]+, -?[0-9]+\\)"); .matches("outboundMessageSent\\(0, -?[0-9]+, -?[0-9]+\\)");
assertThat(clientStreamTracer1.getOutboundWireSize()).isGreaterThan(0L); if (isEnabledSupportTracingMessageSizes()) {
assertThat(clientStreamTracer1.getOutboundUncompressedSize()).isGreaterThan(0L); assertThat(clientStreamTracer1.getOutboundWireSize()).isGreaterThan(0L);
assertThat(clientStreamTracer1.getOutboundUncompressedSize()).isGreaterThan(0L);
}
assertThat(serverStreamTracer1.nextInboundEvent()).isEqualTo("inboundMessage(0)"); assertThat(serverStreamTracer1.nextInboundEvent()).isEqualTo("inboundMessage(0)");
assertNull("no additional message expected", serverStreamListener.messageQueue.poll()); assertNull("no additional message expected", serverStreamListener.messageQueue.poll());
clientStream.halfClose(); clientStream.halfClose();
assertTrue(serverStreamListener.awaitHalfClosed(TIMEOUT_MS, TimeUnit.MILLISECONDS)); assertTrue(serverStreamListener.awaitHalfClosed(TIMEOUT_MS, TimeUnit.MILLISECONDS));
assertThat(serverStreamTracer1.getInboundWireSize()).isGreaterThan(0L); if (isEnabledSupportTracingMessageSizes()) {
assertThat(serverStreamTracer1.getInboundUncompressedSize()).isGreaterThan(0L); assertThat(serverStreamTracer1.getInboundWireSize()).isGreaterThan(0L);
assertThat(serverStreamTracer1.getInboundUncompressedSize()).isGreaterThan(0L);
}
assertThat(serverStreamTracer1.nextInboundEvent()) assertThat(serverStreamTracer1.nextInboundEvent())
.matches("inboundMessageRead\\(0, -?[0-9]+, -?[0-9]+\\)"); .matches("inboundMessageRead\\(0, -?[0-9]+, -?[0-9]+\\)");
@ -890,15 +905,19 @@ public abstract class AbstractTransportTest {
assertNotNull("message expected", message); assertNotNull("message expected", message);
assertThat(serverStreamTracer1.nextOutboundEvent()) assertThat(serverStreamTracer1.nextOutboundEvent())
.matches("outboundMessageSent\\(0, -?[0-9]+, -?[0-9]+\\)"); .matches("outboundMessageSent\\(0, -?[0-9]+, -?[0-9]+\\)");
assertThat(serverStreamTracer1.getOutboundWireSize()).isGreaterThan(0L); if (isEnabledSupportTracingMessageSizes()) {
assertThat(serverStreamTracer1.getOutboundUncompressedSize()).isGreaterThan(0L); assertThat(serverStreamTracer1.getOutboundWireSize()).isGreaterThan(0L);
assertThat(serverStreamTracer1.getOutboundUncompressedSize()).isGreaterThan(0L);
}
assertTrue(clientStreamTracer1.getInboundHeaders()); assertTrue(clientStreamTracer1.getInboundHeaders());
assertThat(clientStreamTracer1.nextInboundEvent()).isEqualTo("inboundMessage(0)"); assertThat(clientStreamTracer1.nextInboundEvent()).isEqualTo("inboundMessage(0)");
assertEquals("Hi. Who are you?", methodDescriptor.parseResponse(message)); assertEquals("Hi. Who are you?", methodDescriptor.parseResponse(message));
assertThat(clientStreamTracer1.nextInboundEvent()) assertThat(clientStreamTracer1.nextInboundEvent())
.matches("inboundMessageRead\\(0, -?[0-9]+, -?[0-9]+\\)"); .matches("inboundMessageRead\\(0, -?[0-9]+, -?[0-9]+\\)");
assertThat(clientStreamTracer1.getInboundWireSize()).isGreaterThan(0L); if (isEnabledSupportTracingMessageSizes()) {
assertThat(clientStreamTracer1.getInboundUncompressedSize()).isGreaterThan(0L); assertThat(clientStreamTracer1.getInboundWireSize()).isGreaterThan(0L);
assertThat(clientStreamTracer1.getInboundUncompressedSize()).isGreaterThan(0L);
}
message.close(); message.close();
assertNull("no additional message expected", clientStreamListener.messageQueue.poll()); assertNull("no additional message expected", clientStreamListener.messageQueue.poll());
@ -1258,10 +1277,12 @@ public abstract class AbstractTransportTest {
serverStream.close(Status.OK, new Metadata()); serverStream.close(Status.OK, new Metadata());
assertTrue(clientStreamTracer1.getOutboundHeaders()); assertTrue(clientStreamTracer1.getOutboundHeaders());
assertTrue(clientStreamTracer1.getInboundHeaders()); assertTrue(clientStreamTracer1.getInboundHeaders());
assertThat(clientStreamTracer1.getInboundWireSize()).isGreaterThan(0L); if (isEnabledSupportTracingMessageSizes()) {
assertThat(clientStreamTracer1.getInboundUncompressedSize()).isGreaterThan(0L); assertThat(clientStreamTracer1.getInboundWireSize()).isGreaterThan(0L);
assertThat(serverStreamTracer1.getOutboundWireSize()).isGreaterThan(0L); assertThat(clientStreamTracer1.getInboundUncompressedSize()).isGreaterThan(0L);
assertThat(serverStreamTracer1.getOutboundUncompressedSize()).isGreaterThan(0L); assertThat(serverStreamTracer1.getOutboundWireSize()).isGreaterThan(0L);
assertThat(serverStreamTracer1.getOutboundUncompressedSize()).isGreaterThan(0L);
}
assertNull(clientStreamTracer1.getInboundTrailers()); assertNull(clientStreamTracer1.getInboundTrailers());
assertSame(status, clientStreamTracer1.getStatus()); assertSame(status, clientStreamTracer1.getStatus());
// There is a race between client cancelling and server closing. The final status seen by the // There is a race between client cancelling and server closing. The final status seen by the

View File

@ -18,6 +18,7 @@ package io.grpc.inprocess;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.inprocess.InProcessTransport.isEnabledSupportTracingMessageSizes;
import com.google.errorprone.annotations.DoNotCall; import com.google.errorprone.annotations.DoNotCall;
import io.grpc.ChannelCredentials; import io.grpc.ChannelCredentials;
@ -118,6 +119,9 @@ public final class InProcessChannelBuilder extends
managedChannelImplBuilder.setStatsRecordStartedRpcs(false); managedChannelImplBuilder.setStatsRecordStartedRpcs(false);
managedChannelImplBuilder.setStatsRecordFinishedRpcs(false); managedChannelImplBuilder.setStatsRecordFinishedRpcs(false);
managedChannelImplBuilder.setStatsRecordRetryMetrics(false); managedChannelImplBuilder.setStatsRecordRetryMetrics(false);
if (!isEnabledSupportTracingMessageSizes) {
managedChannelImplBuilder.disableRetry();
}
} }
@Internal @Internal

View File

@ -82,6 +82,8 @@ import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe @ThreadSafe
final class InProcessTransport implements ServerTransport, ConnectionClientTransport { final class InProcessTransport implements ServerTransport, ConnectionClientTransport {
private static final Logger log = Logger.getLogger(InProcessTransport.class.getName()); private static final Logger log = Logger.getLogger(InProcessTransport.class.getName());
static boolean isEnabledSupportTracingMessageSizes =
GrpcUtil.getFlag("GRPC_EXPERIMENTAL_SUPPORT_TRACING_MESSAGE_SIZES", false);
private final InternalLogId logId; private final InternalLogId logId;
private final SocketAddress address; private final SocketAddress address;
@ -485,22 +487,25 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
@Override @Override
public void writeMessage(InputStream message) { public void writeMessage(InputStream message) {
long messageLength; long messageLength = 0;
try { if (isEnabledSupportTracingMessageSizes) {
if (assumedMessageSize != -1) { try {
messageLength = assumedMessageSize; if (assumedMessageSize != -1) {
} else if (message instanceof KnownLength || message instanceof ByteArrayInputStream) { messageLength = assumedMessageSize;
messageLength = message.available(); } else if (message instanceof KnownLength || message instanceof ByteArrayInputStream) {
} else { messageLength = message.available();
InputStream oldMessage = message; } else {
byte[] payload = ByteStreams.toByteArray(message); InputStream oldMessage = message;
messageLength = payload.length; byte[] payload = ByteStreams.toByteArray(message);
message = new ByteArrayInputStream(payload); messageLength = payload.length;
oldMessage.close(); message = new ByteArrayInputStream(payload);
oldMessage.close();
}
} catch (Exception e) {
throw new RuntimeException("Error processing the message length", e);
} }
} catch (Exception e) {
throw new RuntimeException("Error processing the message length", e);
} }
synchronized (this) { synchronized (this) {
if (closed) { if (closed) {
return; return;
@ -509,11 +514,13 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
statsTraceCtx.outboundMessageSent(outboundSeqNo, -1, -1); statsTraceCtx.outboundMessageSent(outboundSeqNo, -1, -1);
clientStream.statsTraceCtx.inboundMessage(outboundSeqNo); clientStream.statsTraceCtx.inboundMessage(outboundSeqNo);
clientStream.statsTraceCtx.inboundMessageRead(outboundSeqNo, -1, -1); clientStream.statsTraceCtx.inboundMessageRead(outboundSeqNo, -1, -1);
statsTraceCtx.outboundUncompressedSize(messageLength); if (isEnabledSupportTracingMessageSizes) {
statsTraceCtx.outboundWireSize(messageLength); statsTraceCtx.outboundUncompressedSize(messageLength);
// messageLength should be same at receiver's end as no actual wire is involved. statsTraceCtx.outboundWireSize(messageLength);
clientStream.statsTraceCtx.inboundUncompressedSize(messageLength); // messageLength should be same at receiver's end as no actual wire is involved.
clientStream.statsTraceCtx.inboundWireSize(messageLength); clientStream.statsTraceCtx.inboundUncompressedSize(messageLength);
clientStream.statsTraceCtx.inboundWireSize(messageLength);
}
outboundSeqNo++; outboundSeqNo++;
StreamListener.MessageProducer producer = new SingleMessageProducer(message); StreamListener.MessageProducer producer = new SingleMessageProducer(message);
if (clientRequested > 0) { if (clientRequested > 0) {
@ -523,7 +530,6 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
clientReceiveQueue.add(producer); clientReceiveQueue.add(producer);
} }
} }
syncContext.drain(); syncContext.drain();
} }
@ -777,21 +783,23 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
@Override @Override
public void writeMessage(InputStream message) { public void writeMessage(InputStream message) {
long messageLength; long messageLength = 0;
try { if (isEnabledSupportTracingMessageSizes) {
if (assumedMessageSize != -1) { try {
messageLength = assumedMessageSize; if (assumedMessageSize != -1) {
} else if (message instanceof KnownLength || message instanceof ByteArrayInputStream) { messageLength = assumedMessageSize;
messageLength = message.available(); } else if (message instanceof KnownLength || message instanceof ByteArrayInputStream) {
} else { messageLength = message.available();
InputStream oldMessage = message; } else {
byte[] payload = ByteStreams.toByteArray(message); InputStream oldMessage = message;
messageLength = payload.length; byte[] payload = ByteStreams.toByteArray(message);
message = new ByteArrayInputStream(payload); messageLength = payload.length;
oldMessage.close(); message = new ByteArrayInputStream(payload);
oldMessage.close();
}
} catch (Exception e) {
throw new RuntimeException("Error processing the message length", e);
} }
} catch (Exception e) {
throw new RuntimeException("Error processing the message length", e);
} }
synchronized (this) { synchronized (this) {
if (closed) { if (closed) {
@ -801,11 +809,13 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
statsTraceCtx.outboundMessageSent(outboundSeqNo, -1, -1); statsTraceCtx.outboundMessageSent(outboundSeqNo, -1, -1);
serverStream.statsTraceCtx.inboundMessage(outboundSeqNo); serverStream.statsTraceCtx.inboundMessage(outboundSeqNo);
serverStream.statsTraceCtx.inboundMessageRead(outboundSeqNo, -1, -1); serverStream.statsTraceCtx.inboundMessageRead(outboundSeqNo, -1, -1);
statsTraceCtx.outboundUncompressedSize(messageLength); if (isEnabledSupportTracingMessageSizes) {
statsTraceCtx.outboundWireSize(messageLength); statsTraceCtx.outboundUncompressedSize(messageLength);
// messageLength should be same at receiver's end as no actual wire is involved. statsTraceCtx.outboundWireSize(messageLength);
serverStream.statsTraceCtx.inboundUncompressedSize(messageLength); // messageLength should be same at receiver's end as no actual wire is involved.
serverStream.statsTraceCtx.inboundWireSize(messageLength); serverStream.statsTraceCtx.inboundUncompressedSize(messageLength);
serverStream.statsTraceCtx.inboundWireSize(messageLength);
}
outboundSeqNo++; outboundSeqNo++;
StreamListener.MessageProducer producer = new SingleMessageProducer(message); StreamListener.MessageProducer producer = new SingleMessageProducer(message);
if (serverRequested > 0) { if (serverRequested > 0) {

View File

@ -234,9 +234,11 @@ public class InProcessTransportTest extends AbstractTransportTest {
private void assertAssumedMessageSize( private void assertAssumedMessageSize(
TestStreamTracer streamTracerSender, TestStreamTracer streamTracerReceiver) { TestStreamTracer streamTracerSender, TestStreamTracer streamTracerReceiver) {
Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerSender.getOutboundWireSize()); if (isEnabledSupportTracingMessageSizes()) {
Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerSender.getOutboundUncompressedSize()); Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerSender.getOutboundWireSize());
Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerReceiver.getInboundWireSize()); Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerSender.getOutboundUncompressedSize());
Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerReceiver.getInboundUncompressedSize()); Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerReceiver.getInboundWireSize());
Assert.assertEquals(TEST_MESSAGE_LENGTH, streamTracerReceiver.getInboundUncompressedSize());
}
} }
} }