Repository navigation
feat(storage): record gRPC stall.duration and Watchdog TIMEOUT classification #15231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/storage-network-io-metric
Are you sure you want to change the base?
Changes from all commits
f2ffba2
8025b57
06295ea
b09c7b1
a58e88b
9c994f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| import io.grpc.MethodDescriptor; | ||
| import io.grpc.Status; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.Scope; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
|
|
@@ -116,6 +117,7 @@ private void startWithMetrics( | |
| new SimpleForwardingClientCallListener<RespT>(responseListener) { | ||
| private boolean receivedServerHeaders; | ||
| private boolean headerHasUploadId; | ||
| private long lastActivityNanos = startTimeNanos; | ||
|
|
||
| @Override | ||
| public void onHeaders(Metadata headers) { | ||
|
|
@@ -131,10 +133,12 @@ public void onHeaders(Metadata headers) { | |
|
|
||
| @Override | ||
| public void onMessage(@Nullable RespT message) { | ||
| long now = clock.nanoTime(); | ||
| lastActivityNanos = now; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we update |
||
| // The first response message from the server (including metadata-only | ||
| // responses for 0-byte objects) marks Time To First Byte (TTFB). | ||
| if (ttfbRecorded.compareAndSet(false, true)) { | ||
| double ttfbSeconds = (clock.nanoTime() - ttfbStartNanos.get()) / 1_000_000_000.0; | ||
| double ttfbSeconds = (now - ttfbStartNanos.get()) / 1_000_000_000.0; | ||
| String networkPath = detectNetworkPath(getAttributes()); | ||
| metrics.recordTtfb(otelContext, operationName, ttfbSeconds, networkPath); | ||
| } | ||
|
|
@@ -143,14 +147,56 @@ public void onMessage(@Nullable RespT message) { | |
|
|
||
| @Override | ||
| public void onClose(Status status, Metadata trailers) { | ||
| try { | ||
| double durationSeconds = (clock.nanoTime() - startTimeNanos) / 1_000_000_000.0; | ||
| String networkPath = detectNetworkPath(getAttributes()); | ||
| if (metricsCtx != null) { | ||
| metricsCtx.setNetworkPath(networkPath); | ||
| long endTimeNanos = clock.nanoTime(); | ||
| String networkPath = detectNetworkPath(getAttributes()); | ||
| if (metricsCtx != null) { | ||
| metricsCtx.setNetworkPath(networkPath); | ||
| } | ||
| if (status.getCode() != Status.Code.CANCELLED) { | ||
| try { | ||
| recordOnCloseMetrics( | ||
| /* watchdogTimedOut= */ false, endTimeNanos, networkPath, status, trailers); | ||
| } finally { | ||
| super.onClose(status, trailers); | ||
| } | ||
| return; | ||
| } | ||
| boolean[] recordedOnClose = {false}; | ||
| Runnable onWatchdogTimeout = | ||
| () -> { | ||
| if (!recordedOnClose[0]) { | ||
| recordedOnClose[0] = true; | ||
| recordOnCloseMetrics( | ||
| /* watchdogTimedOut= */ true, | ||
| endTimeNanos, | ||
| networkPath, | ||
| status, | ||
| trailers); | ||
| } | ||
| }; | ||
| try (Scope ignore = StorageMetricsContext.enterWatchdogScope(onWatchdogTimeout)) { | ||
| super.onClose(status, trailers); | ||
| } finally { | ||
| if (!recordedOnClose[0]) { | ||
| recordOnCloseMetrics( | ||
| /* watchdogTimedOut= */ false, endTimeNanos, networkPath, status, trailers); | ||
| } | ||
| } | ||
|
nidhiii-27 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| private void recordOnCloseMetrics( | ||
| boolean watchdogTimedOut, | ||
| long endTimeNanos, | ||
| String networkPath, | ||
| Status status, | ||
| Metadata trailers) { | ||
| try { | ||
| double durationSeconds = (endTimeNanos - startTimeNanos) / 1_000_000_000.0; | ||
| String statusCode = status.getCode().name(); | ||
| String errorType = StorageErrorTypeMapper.mapGrpcStatus(status); | ||
| String errorType = | ||
| watchdogTimedOut | ||
| ? StorageErrorTypeMapper.STATUS_TIMEOUT | ||
| : StorageErrorTypeMapper.mapGrpcStatus(status); | ||
|
|
||
| metrics.recordRpcCallDuration( | ||
| otelContext, durationSeconds, rpcMethod, statusCode, networkPath, errorType); | ||
|
|
@@ -177,9 +223,13 @@ public void onClose(Status status, Metadata trailers) { | |
| networkIoTracerFactory.getOutboundBytes(), | ||
| networkIoTracerFactory.getInboundBytes()); | ||
| } | ||
| if (watchdogTimedOut) { | ||
| double stallSeconds = (endTimeNanos - lastActivityNanos) / 1_000_000_000.0; | ||
| metrics.recordStallDuration( | ||
| otelContext, stallSeconds, operationName, rpcMethod, networkPath); | ||
| } | ||
| } finally { | ||
| releaseActive(); | ||
| super.onClose(status, trailers); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,11 +20,15 @@ | |
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import com.google.api.core.ApiClock; | ||
| import com.google.api.core.SettableApiFuture; | ||
| import com.google.api.gax.retrying.ResultRetryAlgorithm; | ||
| import com.google.api.gax.rpc.ApiCallContext; | ||
| import com.google.api.gax.rpc.ResponseObserver; | ||
| import com.google.api.gax.rpc.ServerStreamingCallable; | ||
| import com.google.api.gax.rpc.StreamController; | ||
| import com.google.api.gax.rpc.Watchdog; | ||
| import com.google.api.gax.rpc.WatchdogTimeoutException; | ||
| import com.google.cloud.NoCredentials; | ||
| import com.google.cloud.storage.GrpcUtils.ZeroCopyServerStreamingCallable; | ||
| import com.google.cloud.storage.Retrying.Retrier; | ||
|
|
@@ -35,15 +39,21 @@ | |
| import com.google.storage.v2.ReadObjectResponse; | ||
| import io.grpc.Status; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
| import io.opentelemetry.sdk.metrics.data.LongPointData; | ||
| import io.opentelemetry.sdk.metrics.data.MetricData; | ||
| import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.time.Duration; | ||
| import java.util.Collection; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.junit.Test; | ||
|
|
||
| public final class GapicUnbufferedReadableByteChannelTest { | ||
|
|
@@ -683,6 +693,73 @@ private static void assertMidStreamRetryRecorded( | |
| assertThat(attemptsTotal).isEqualTo(2L); | ||
| } | ||
|
|
||
| @Test | ||
| public void read_watchdogTimeout_notifiesWatchdogScopeSynchronously() throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Could we also test a non-watchdog |
||
| AtomicLong nanos = new AtomicLong(1_000_000_000L); | ||
| ApiClock fakeClock = | ||
| new ApiClock() { | ||
| @Override | ||
| public long nanoTime() { | ||
| return nanos.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public long millisTime() { | ||
| return nanos.get() / 1_000_000L; | ||
| } | ||
| }; | ||
| ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | ||
| AtomicBoolean watchdogNotified = new AtomicBoolean(false); | ||
| try (Watchdog watchdog = Watchdog.createDuration(fakeClock, Duration.ofHours(1), scheduler); | ||
| GapicUnbufferedReadableByteChannel c = | ||
| new GapicUnbufferedReadableByteChannel( | ||
| SettableApiFuture.create(), | ||
| new ZeroCopyServerStreamingCallable<>( | ||
| new ServerStreamingCallable<ReadObjectRequest, ReadObjectResponse>() { | ||
| @Override | ||
| public void call( | ||
| ReadObjectRequest request, | ||
| ResponseObserver<ReadObjectResponse> respond, | ||
| ApiCallContext context) { | ||
| ResponseObserver<ReadObjectResponse> watched = | ||
| watchdog.watchDuration( | ||
| respond, Duration.ofSeconds(1), Duration.ofSeconds(2)); | ||
| watched.onStart( | ||
| new StreamController() { | ||
| @Override | ||
| public void cancel() { | ||
| try (Scope ignore = | ||
| StorageMetricsContext.enterWatchdogScope( | ||
| () -> watchdogNotified.set(true))) { | ||
| watched.onError( | ||
| new CancellationException("cancelled by watchdog")); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void disableAutoInboundFlowControl() {} | ||
|
|
||
| @Override | ||
| public void request(int count) {} | ||
| }); | ||
| nanos.addAndGet(1_500_000_000L); | ||
| watchdog.run(); | ||
| } | ||
| }, | ||
| ResponseContentLifecycleManager.noop()), | ||
| ReadObjectRequest.getDefaultInstance(), | ||
| Hasher.noop(), | ||
| Retrier.attemptOnce(), | ||
| Retrying.neverRetry())) { | ||
| StorageException se = | ||
| assertThrows(StorageException.class, () -> c.read(ByteBuffer.allocate(16))); | ||
| assertThat(se.getCause()).isInstanceOf(WatchdogTimeoutException.class); | ||
| assertThat(watchdogNotified.get()).isTrue(); | ||
| } finally { | ||
| scheduler.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| private static LongPointData findSinglePoint(Collection<MetricData> collected, String name) { | ||
| MetricData metric = | ||
| collected.stream() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.