Repository navigation
feat(storage): record gcp.storage.client.retries for mid-stream and HTTP retries - #15226
nidhiii-27 wants to merge 3 commits into
Conversation
…TTP retries [Generated-by: AI]
There was a problem hiding this comment.
Code Review
This pull request introduces tracking and recording of mid-stream retries and error metrics in both Apiary and Gapic unbuffered readable byte channels, as well as HTTP interceptors. It exposes StorageMetricsContext from the Retrier interface and adds corresponding unit tests. One improvement was suggested in ApiaryUnbufferedReadableByteChannel to reuse the translated StorageException when recording mid-stream retries, ensuring more precise error/retry metrics are captured instead of passing the raw IOException.
| startTimeNanos.get(), | ||
| endNanos, | ||
| response.getStatusCode(), | ||
| /* isRetry= */ supportsRetry); |
There was a problem hiding this comment.
Should we also check statusCode >= 400 here so a handled 3xx doesn't record a retry with error.type="OK"?
There was a problem hiding this comment.
Done in 736b10f: a retry is now only recorded for a failed attempt (>= 400 or an IOException) that google-http-client actually retries, so a handled 3xx no longer records one. Added initialize_handled3xxThenRetry_doesNotRecordRetry.
Co-authored by AI Agent
| } | ||
| } | ||
|
|
||
| private void recordMidStreamRetry(Throwable error) { |
There was a problem hiding this comment.
Should we also call metricsContext.recordError(...) in the non-retryable else branches so terminal mid-stream errors are counted?
There was a problem hiding this comment.
Done in 736b10f: both terminal branches (throw ioE and throw new IOException(coalesce(t))) now record gcp.storage.client.errors before rethrowing (no retry). The open path runs outside this try, so it can't double count with Retrying. Added midStreamIoException_notRetryable_recordsErrorWithoutRetry.
Co-authored by AI Agent
| } | ||
| } | ||
|
|
||
| private void recordMidStreamRetry(Throwable error) { |
There was a problem hiding this comment.
Same here: should we call metricsContext.recordError(...) in the non-retryable else branches for terminal mid-stream errors?
There was a problem hiding this comment.
Done in 736b10f: same change on the gRPC path for both terminal branches, using the same coalesce(...) input as the retry branch. Added midStreamError_notRetryable_recordsErrorWithoutRetry.
Co-authored by AI Agent
| } | ||
| boolean handled = | ||
| existingIoHandler != null && existingIoHandler.handleIOException(req, supportsRetry); | ||
| if (supportsRetry && handled && recordedThisAttempt) { |
There was a problem hiding this comment.
Nit: Can we add a test for an unhandled IOException (supportsRetry == true, no handler) to verify no retry is recorded?
There was a problem hiding this comment.
Done in 736b10f: initialize_supportsRetryFalse_doesNotRecordRetry now also covers an IOException with retries left but no IOExceptionHandler (google-http-client rethrows), and asserts no retry.
Co-authored by AI Agent
| if (take instanceof IOException) { | ||
| IOException ioe = (IOException) take; | ||
| if (alg.shouldRetry(ioe, null)) { | ||
| recordMidStreamRetry(StorageException.coalesce(ioe)); |
There was a problem hiding this comment.
Nit: Can we add a test case that hits this IOException branch too?
There was a problem hiding this comment.
Done in 736b10f: added midStreamIoException_retried_recordsRetryAndErrorMetrics (gRPC), which queues an IOException mid-stream that the algorithm retries.
Co-authored by AI Agent
| if (resultRetryAlgorithm.shouldRetry(t, null)) { | ||
| // if our retry algorithm COULD allow a retry, continue the loop and allow trying to | ||
| // open the stream again. | ||
| recordMidStreamRetry(t); |
There was a problem hiding this comment.
Nit: Can we add a test case that hits this retryable branch too?
There was a problem hiding this comment.
Done in 736b10f: added midStreamIoException_directlyRetryable_recordsRetryAndErrorMetrics, where the algorithm accepts the raw exception so the first branch is taken.
Co-authored by AI Agent
| } | ||
|
|
||
| private void recordResponse(HttpRequest request, long startNanos, long endNanos, int statusCode) { | ||
| private void recordResponse( |
There was a problem hiding this comment.
Instead of passing isRetry and checking supportsRetry in both handlers, could we store a per-request AtomicReference<String> pendingRetryErrorType on failure (statusCode >= 400 or IOException) and record the retry once inside setInterceptor when the next attempt actually starts? That drops the boolean flag, deduplicates recordRetry, and avoids 3xx and exhausted-retry edge cases.
There was a problem hiding this comment.
Done in 736b10f, reworked as suggested: each failed attempt that google-http-client will retry (>= 400, or an IOException, when the handler returned true and retries remain) is stored per request, and the retry is recorded exactly once when the next attempt starts in the execute interceptor. This drops the isRetry flag and the duplicate recordRetry calls, and covers the 3xx and exhausted-retry cases. One small difference: the pending value is the failed attempt's HTTP status (0 for an IOException) in an AtomicInteger rather than the error type string, so the same value yields both error.type and the correct http.response.status_code for the attempt recorded in the next comment.
Co-authored by AI Agent
| if (supportsRetry && handled && recordedThisAttempt) { | ||
| StorageMetricsContext ctx = StorageMetricsContext.current(); | ||
| Context otelContext = ctx != null ? ctx.getOtelContext() : Context.current(); | ||
| metrics.recordRetry( |
There was a problem hiding this comment.
When google-http-client retries a 401 or IOException internally, DefaultRetrier never sees the failed attempt, so retries increments while attempts stays at 1 and errors at 0. Should we also record the failed attempt and error here, and add tests covering this, handled 3xx responses, and terminal mid-stream read errors?
There was a problem hiding this comment.
Done in 736b10f: when google-http-client retries internally (e.g. a 401 credential refresh or an IOException), the failed attempt now also records gcp.storage.client.attempts and gcp.storage.client.errors via StorageMetricsContext.recordAttemptFailure, using the same attributes as Retrying (when a metrics context is active). It is recorded from the same next-attempt hook, so each failed attempt is counted exactly once: the final attempt is still recorded only by Retrying, and Retrying-level retries build a new HttpRequest. Tests: 401->200 and IOException->200 now assert the attempt/error points, plus handled 3xx and terminal mid-stream errors (see the other threads).
Co-authored by AI Agent
…ries-metric-streams-http [Generated-by: AI]
…unt terminal mid-stream errors - StorageHttpMetricsInterceptor keeps the status of a failed attempt that google-http-client will retry (>= 400, or no response for an IOException) and records gcp.storage.client.retries once the next attempt starts. This drops the isRetry plumbing and no longer records a retry for a handled 3xx. - The internally retried failed attempt also records gcp.storage.client.attempts and gcp.storage.client.errors via StorageMetricsContext.recordAttemptFailure, since Retrying only observes the final outcome of HttpRequest.execute(). - Apiary/Gapic unbuffered read channels record gcp.storage.client.errors for terminal (non-retryable) mid-stream failures. [Generated-by: AI]
| } | ||
| } | ||
|
|
||
| private void recordMidStreamError(Throwable error) { |
There was a problem hiding this comment.
Should we also call recordMidStreamError in validateCumulativeChecksum so EOF checksum mismatches are counted on gRPC reads too?
| IOException ioe = new IOException("connection reset"); | ||
| assertMidStreamRetryRecorded( | ||
| readWithMidStreamError(ioe, Retrying.alwaysRetry(), true), | ||
| StorageErrorTypeMapper.mapThrowable(StorageException.coalesce(ioe))); |
There was a problem hiding this comment.
Nit: Let's assert StorageErrorTypeMapper.STATUS_CONNECTION_ERROR directly here instead of calling mapThrowable(coalesce(ioe)).
Summary
Stacked on #15225.
Extends
gcp.storage.client.retriescoverage beyondRetrying.DefaultRetrierto mid-stream download retries and google-http-client internal retries.Changes
GapicUnbufferedReadableByteChannel/ApiaryUnbufferedReadableByteChannel: recorderrors+retriesfor retried mid-streamReadObjectfailures, anderrorsonly for terminal (non-retryable) mid-stream failures.StorageHttpMetricsInterceptor: store the status of a failed attempt that google-http-client will retry (>= 400, or 0 for anIOException, only when the handler returned true and retries remain) and recordretriesonce the next attempt starts. A handled 3xx records nothing.StorageMetricsContext.recordAttemptFailure(String errorType, long httpStatusCode): that internally retried failed attempt (e.g. 401 credential refresh,IOException) also recordsattempts/errorswith the same attributes asRetrying, when a metrics context is active. Each failed attempt is counted exactly once; the final attempt is still recorded only byRetrying.GapicUnbufferedReadableByteChannelTest,ApiaryUnbufferedReadableByteChannelTest, andStorageHttpMetricsInterceptorTest.[Generated-by: AI]