Skip to content

feat(storage): record gcp.storage.client.retries for mid-stream and HTTP retries - #15226

Open
nidhiii-27 wants to merge 3 commits into
feat/storage-retries-metric-corefrom
feat/storage-retries-metric-streams-http
Open

nidhiii-27 wants to merge 3 commits into
feat/storage-retries-metric-corefrom
feat/storage-retries-metric-streams-http

Conversation

@nidhiii-27

@nidhiii-27 nidhiii-27 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stacked on #15225.

Extends gcp.storage.client.retries coverage beyond Retrying.DefaultRetrier to mid-stream download retries and google-http-client internal retries.

Changes

  • GapicUnbufferedReadableByteChannel / ApiaryUnbufferedReadableByteChannel: record errors + retries for retried mid-stream ReadObject failures, and errors only 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 an IOException, only when the handler returned true and retries remain) and record retries once 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 records attempts/errors with the same attributes as Retrying, when a metrics context is active. Each failed attempt is counted exactly once; the final attempt is still recorded only by Retrying.
  • Unit tests in GapicUnbufferedReadableByteChannelTest, ApiaryUnbufferedReadableByteChannelTest, and StorageHttpMetricsInterceptorTest.

[Generated-by: AI]

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nidhiii-27
nidhiii-27 requested a review from cpriti-os November 25, 2026 10:24
@nidhiii-27
nidhiii-27 marked this pull request as ready for review November 25, 2026 10:25
@nidhiii-27
nidhiii-27 requested review from a team as code owners November 25, 2026 10:25

@cpriti-os cpriti-os left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial review

startTimeNanos.get(),
endNanos,
response.getStatusCode(),
/* isRetry= */ supportsRetry);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check statusCode >= 400 here so a handled 3xx doesn't record a retry with error.type="OK"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also call metricsContext.recordError(...) in the non-retryable else branches so terminal mid-stream errors are counted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: should we call metricsContext.recordError(...) in the non-retryable else branches for terminal mid-stream errors?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we add a test for an unhandled IOException (supportsRetry == true, no handler) to verify no retry is recorded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we add a test case that hits this IOException branch too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we add a test case that hits this retryable branch too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

…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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Let's assert StorageErrorTypeMapper.STATUS_CONNECTION_ERROR directly here instead of calling mapThrowable(coalesce(ioe)).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-generated Pull Request generated by AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants