Skip to content

feat(storage): add speculative open hedging to AsyncClient::Open - #16612

Draft
ajayky-os wants to merge 4 commits into
googleapis:mainfrom
ajayky-os:feat/storage-bidi-open-hedging
Draft

ajayky-os wants to merge 4 commits into
googleapis:mainfrom
ajayky-os:feat/storage-bidi-open-hedging

Conversation

@ajayky-os

@ajayky-os ajayky-os commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Note

Stacked PR (2 of 2): Stacked on #16613 (fix(storage): make OpenStream and OpenObject cancellation-safe).

Summary

Extends storage_experimental::EnableReadHedgingOption to storage::AsyncClient::Open() (BidiReadObject) so that slow initial stream-open handshakes are raced against delayed speculative attempts on the gRPC CompletionQueue.

Motivation

Opening a BidiReadObject stream requires a multi-step handshake (Start() -> initial Write() -> first Read() returning object metadata and a BidiReadObjectHandle) and can experience severe tail-latency spikes when an individual subchannel, backend task, or DirectPath/CloudPath route stalls during connection setup or backend routing. Previously, EnableReadHedgingOption only hedged synchronous storage::Client::ReadObject() streams, leaving storage::AsyncClient::Open() vulnerable to single-attempt open stalls until the per-stream stall timeout (DownloadStallTimeoutOption) expired.

Racing the initial Open() handshake against bounded, delayed speculative Open() attempts cuts BidiReadObject open tail latency without duplicating any subsequent ObjectDescriptor::Read() ranged-read payload transfers or spawning dedicated worker thread pools.

Changes

  • Standalone HedgeLimiter (google/cloud/storage/internal/hedge_limiter.h, hedging_thread_pool.h):
    • Extracts the thread-safe three-gate hedge budget limiter (MaxConcurrentHedgesOption, ReadHedgeRateLimitOption, and MaxTotalHedgesOption) from HedgingThreadPool into hedge_limiter.h as a verbatim delegation (HedgingThreadPool's API, behavior, and existing hedging_thread_pool_test.cc gate/rollback/contention coverage are preserved unchanged) so HedgingThreadPool and AsyncConnectionImpl share identical budget enforcement without coupling AsyncConnectionImpl to synchronous worker thread pools.
  • Speculative Open() Hedging (google/cloud/storage/internal/async/connection_impl.{h,cc}):
    • Forward-declares HedgeLimiter and HedgedReadMetrics in connection_impl.h and initializes both (capacity = rate_limit 1-second burst, matching StorageConnectionImpl, and storage.read_hedging.{hedges_dispatched,hedge_won}) on AsyncConnectionImpl when EnableReadHedgingOption is enabled at client construction time.
    • Implements HedgedOpenOperation using CompletionQueue::MakeRelativeTimer (zero dedicated hedging/read thread pools, {null_promise_t{}} promise initialization, move-only RAII HedgeSlot concurrency release, and kHedgeRetryMinDelay = 10ms contention floor) to race the primary OpenStreamFactory attempt against up to MaxReadHedgesOption speculative attempts spaced by ReadHedgeDelayOption, propagating CallContext across timer and completion callbacks.
    • Cancels losing attempts (via fix(storage): make OpenStream and OpenObject cancellation-safe #16613's OpenObject / OpenStream cancellation path) and pending hedge timers as soon as any attempt wins; aborts stalled sibling attempts immediately on permanent non-kCancelled rejections from any attempt (primary or hedge, since unlike mid-stream sync HedgedObjectReadSource every Open() attempt sends the identical initial request), while allowing in-flight hedges to rescue primaries cancelled by per-stream stall timeouts (kCancelled); and drains active attempts cleanly on caller .cancel().
    • Reports TrackedFeature::kReadHedging (x-goog-storage-cpp-features) strictly on HedgedOpenOperation's initial Open() attempts (OpenHedging::kEnabled) when hedging is active (!IsTotalBudgetSpent()), while keeping subsequent ObjectDescriptor::Read() calls, MSO standby streams, and background stream resumes unhedged and untagged (OpenHedging::kDisabled). Note that any initial ranges passed via ReadRangesOption are carried in initial_request on each hedged Open() attempt.
  • Documentation & Tests (options.h, async/client.h, connection_impl_open_test.cc):
    • Documents AsyncClient::Open() support (including ReadRangesOption behavior) on EnableReadHedgingOption, MaxTotalHedgesOption, ReadHedgeDelayOption, MaxReadHedgesOption, and AsyncClient::Open(), and marks sync-only options (MaximumHedgeBufferOption, ReadThreadPoolSizeOption, HedgingThreadPoolSizeOption) as ignored by AsyncClient::Open().
    • Adds OpenHedgingTest / OpenHedgingRescueTest / OpenHedgingConcurrentCompletionRace covering gating & per-call overrides, fast-primary timer cancellation + unhedged MSO standby, slow-primary hedge win + unhedged background resume, primary win after hedge dispatch, kAborted redirect & stall-timeout (kCancelled) rescues, permanent failure abort from either primary (PrimaryPermanentFailureAbortsInFlightHedge) or hedge (HedgePermanentFailureAbortsStalledPrimary), all-attempts-fail primary error precedence, caller .cancel(), MaxConcurrentHedgesOption / MaxTotalHedgesOption contention & lifetime exhaustion, x-goog-storage-cpp-features kReadHedging scoping (FeatureHeaderSetOnlyOnHedgedOpenAttempts), and multi-threaded completion races.

@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Nov 2, 2026

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

Copy link
Copy Markdown

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 implements experimental request hedging for storage::AsyncClient::Open() stream-open handshakes. It introduces the HedgedOpenOperation class to manage racing the primary stream-open attempt against delayed speculative attempts, and extracts HedgeLimiter from HedgingThreadPool to enforce connection-wide concurrency and rate limits. Additionally, it refactors OpenObject and OpenStream to support robust cancellation during the stream-open handshake, and adds comprehensive unit tests to verify these new behaviors. I have no feedback to provide as there are no review comments.

@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.42382% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.79%. Comparing base (6f1c3c3) to head (c2f8dbb).

Files with missing lines Patch % Lines
google/cloud/storage/internal/hedge_limiter.h 84.00% 8 Missing ⚠️
...torage/internal/async/connection_impl_open_test.cc 98.77% 7 Missing ⚠️
...le/cloud/storage/internal/async/connection_impl.cc 98.73% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16612      +/-   ##
==========================================
+ Coverage   92.78%   92.79%   +0.01%     
==========================================
  Files        2271     2272       +1     
  Lines      227688   228731    +1043     
==========================================
+ Hits       211255   212246     +991     
- Misses      16433    16485      +52     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Extract the connection-wide rate, concurrency, and lifetime hedge
limiter out of HedgingThreadPool into internal/hedge_limiter.h so
AsyncConnectionImpl can share the same rate/concurrency/total limiter
state machine without instantiating a thread pool.
Extends storage_experimental::EnableReadHedgingOption to
storage::AsyncClient::Open() (BidiReadObject) so slow initial stream-open
handshakes race against up to MaxReadHedgesOption delayed speculative
attempts scheduled via CompletionQueue::MakeRelativeTimer without spawning
dedicated read or hedging thread pools.
@ajayky-os
ajayky-os force-pushed the feat/storage-bidi-open-hedging branch from e3c716f to 085a1bc Compare November 25, 2026 08:52
@ajayky-os
ajayky-os force-pushed the feat/storage-bidi-open-hedging branch from 085a1bc to c2f8dbb Compare November 25, 2026 11:35

This branch was successfully deployed

1 active (outdated) deployment
false — e3c716f2 Deployed Nov 2, 2026 by ajayky-os via Save PR ref #12774
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant