Search in sources :

Example 11 with MetricsRecord

use of io.grpc.internal.testing.StatsTestUtils.MetricsRecord in project grpc-java by grpc.

the class RetryTest method transparentRetryStatsRecorded.

@Test
public void transparentRetryStatsRecorded() throws Exception {
    startNewServer();
    createNewChannel();
    final AtomicBoolean originalAttemptFailed = new AtomicBoolean();
    class TransparentRetryTriggeringTracer extends ClientStreamTracer {

        @Override
        public void streamCreated(Attributes transportAttrs, Metadata metadata) {
            if (originalAttemptFailed.get()) {
                return;
            }
            // Send GOAWAY from server. The client may either receive GOAWAY or create the underlying
            // netty stream and write headers first, even we await server termination as below.
            // In the latter case, we rerun the test. We can also call localServer.shutdown() to trigger
            // GOAWAY, but it takes a lot longer time to gracefully shut down.
            localServer.shutdownNow();
            try {
                localServer.awaitTermination();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new AssertionError(e);
            }
        }

        @Override
        public void streamClosed(Status status) {
            if (originalAttemptFailed.get()) {
                return;
            }
            originalAttemptFailed.set(true);
            try {
                startNewServer();
                channel.resetConnectBackoff();
            } catch (Exception e) {
                throw new AssertionError("local server can not be restarted", e);
            }
        }
    }
    class TransparentRetryTracerFactory extends ClientStreamTracer.Factory {

        @Override
        public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
            return new TransparentRetryTriggeringTracer();
        }
    }
    CallOptions callOptions = CallOptions.DEFAULT.withWaitForReady().withStreamTracerFactory(new TransparentRetryTracerFactory());
    while (true) {
        ClientCall<String, Integer> call = channel.newCall(clientStreamingMethod, callOptions);
        call.start(mockCallListener, new Metadata());
        // original attempt
        assertRpcStartedRecorded();
        MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
        assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT)).isEqualTo(1);
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
        if (statusTag.asString().equals(Code.UNAVAILABLE.toString())) {
            break;
        } else {
            // Due to race condition, GOAWAY is not received/processed before the stream is closed due
            // to connection error. Rerun the test.
            assertThat(statusTag.asString()).isEqualTo(Code.UNKNOWN.toString());
            assertRetryStatsRecorded(0, 0, 0);
            originalAttemptFailed.set(false);
        }
    }
    // retry attempt
    assertRpcStartedRecorded();
    ServerCall<String, Integer> serverCall = serverCalls.poll(5, SECONDS);
    serverCall.close(Status.INVALID_ARGUMENT, new Metadata());
    assertRpcStatusRecorded(Code.INVALID_ARGUMENT, 0, 0);
    assertRetryStatsRecorded(0, 1, 0);
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord) Attributes(io.grpc.Attributes) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) TagValue(io.opencensus.tags.TagValue) Test(org.junit.Test)

Example 12 with MetricsRecord

use of io.grpc.internal.testing.StatsTestUtils.MetricsRecord in project grpc-java by grpc.

the class RetryTest method assertOutboundMessageRecorded.

private void assertOutboundMessageRecorded() throws Exception {
    MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
    assertThat(record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_SENT_MESSAGES_PER_METHOD)).isEqualTo(1);
}
Also used : MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord)

Example 13 with MetricsRecord

use of io.grpc.internal.testing.StatsTestUtils.MetricsRecord in project grpc-java by grpc.

the class RetryTest method assertInboundMessageRecorded.

private void assertInboundMessageRecorded() throws Exception {
    MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
    assertThat(record.getMetricAsLongOrFail(RpcMeasureConstants.GRPC_CLIENT_RECEIVED_MESSAGES_PER_METHOD)).isEqualTo(1);
}
Also used : MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord)

Example 14 with MetricsRecord

use of io.grpc.internal.testing.StatsTestUtils.MetricsRecord in project grpc-java by grpc.

the class RetryTest method assertRetryStatsRecorded.

private void assertRetryStatsRecorded(int numRetries, int numTransparentRetries, long retryDelayMs) throws Exception {
    MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
    assertThat(record.getMetricAsLongOrFail(RETRIES_PER_CALL)).isEqualTo(numRetries);
    assertThat(record.getMetricAsLongOrFail(TRANSPARENT_RETRIES_PER_CALL)).isEqualTo(numTransparentRetries);
    assertThat(record.getMetricAsLongOrFail(RETRY_DELAY_PER_CALL)).isEqualTo(retryDelayMs);
}
Also used : MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord)

Example 15 with MetricsRecord

use of io.grpc.internal.testing.StatsTestUtils.MetricsRecord in project grpc-java by grpc.

the class RetryTest method assertRpcStatusRecorded.

private void assertRpcStatusRecorded(Status.Code code, long roundtripLatencyMs, long outboundMessages) throws Exception {
    MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
    TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
    assertThat(statusTag.asString()).isEqualTo(code.toString());
    assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT)).isEqualTo(1);
    assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_ROUNDTRIP_LATENCY)).isEqualTo(roundtripLatencyMs);
    assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_REQUEST_COUNT)).isEqualTo(outboundMessages);
}
Also used : MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord) TagValue(io.opencensus.tags.TagValue)

Aggregations

MetricsRecord (io.grpc.internal.testing.StatsTestUtils.MetricsRecord)19 Test (org.junit.Test)6 StatusRuntimeException (io.grpc.StatusRuntimeException)4 StreamingOutputCallRequest (io.grpc.testing.integration.Messages.StreamingOutputCallRequest)3 StreamingOutputCallResponse (io.grpc.testing.integration.Messages.StreamingOutputCallResponse)2 TagValue (io.opencensus.tags.TagValue)2 ByteString (com.google.protobuf.ByteString)1 Attributes (io.grpc.Attributes)1 CallOptions (io.grpc.CallOptions)1 ClientStreamTracer (io.grpc.ClientStreamTracer)1 StreamInfo (io.grpc.ClientStreamTracer.StreamInfo)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 TestClientStreamTracer (io.grpc.internal.testing.TestClientStreamTracer)1 ResponseParameters (io.grpc.testing.integration.Messages.ResponseParameters)1 StreamingInputCallRequest (io.grpc.testing.integration.Messages.StreamingInputCallRequest)1 StreamingInputCallResponse (io.grpc.testing.integration.Messages.StreamingInputCallResponse)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1