Skip to content

impl(storage): add HedgedOpenStream coordinator for async Open - #16716

Draft
ajayky-os wants to merge 2 commits into
googleapis:mainfrom
ajayky-os:impl/storage-hedged-open-stream
Draft

ajayky-os wants to merge 2 commits into
googleapis:mainfrom
ajayky-os:impl/storage-hedged-open-stream

Conversation

@ajayky-os

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

Copy link
Copy Markdown
Contributor

Note

Stacked PR (Part 2 of 3 splitting #16612): Stacked on #16715 (refactor(storage): extract HedgeLimiter from HedgingThreadPool).

Summary

Adds the standalone CompletionQueue-driven storage_internal::HedgedOpenStream coordinator (google/cloud/storage/internal/async/hedged_open_stream.{h,cc}) and its unit/concurrency test suite (hedged_open_stream_test.cc), ahead of wiring AsyncConnectionImpl::Open() in #16612.

Design & Concurrency Invariants

  • Single Free-Function Surface (hedged_open_stream.h):
    • Exposes HedgedOpenParams{delay, max_hedges} and HedgedOpenStream(...), keeping HedgedOpenOperation private inside hedged_open_stream.cc.
  • Race Semantics (hedged_open_stream.cc):
    • Races a primary OpenStreamFactory attempt against up to max_hedges delayed speculative attempts scheduled via CompletionQueue::MakeRelativeTimer (with zero dedicated worker threads).
    • First attempt to succeed wins; remaining in-flight attempts and timers are drained under mu_ and cancelled outside mu_ (preventing re-entrant deadlocks), and any late-completing loser's OpenStream is immediately closed via OpenStream::Cancel().
    • Uses std::vector<std::optional<future<void>>> (FinishAttemptLocked) so winner self-extraction avoids self-cancellation while preventing post-completion slot overwrites before future::then marks its output state ready.
    • Aborts stalled sibling attempts immediately on any non-kCancelled permanent failure (retry_policy->IsPermanentFailure(status)), while allowing in-flight hedges to rescue primaries cancelled by per-stream stall timeouts (kCancelled).
    • Gates speculative dispatches through HedgeLimiter (re-arming at max(delay, 10ms) on temporary concurrency/rate denial and stopping once IsTotalBudgetSpent()), releasing concurrency slots via move-only RAII HedgeSlot prior to completion resolution.
  • Unit & Concurrency Tests (hedged_open_stream_test.cc):
    • Exercises HedgedOpenStream directly against an AsyncSequencer-driven OpenStreamFactory and MockCompletionQueueImpl across fast-primary timer cancellation, slow-primary hedge win + late-stream Cancel(), primary win after hedge dispatch, parameterized transient/stall-timeout rescues, parameterized symmetric permanent-error aborts, all-attempts-fail primary error precedence, caller .cancel() (both async and inline inside the cancel callback), 10ms limiter-denial retry + lifetime budget exhaustion, negative parameter clamping, and 25-iteration multi-threaded barrier races.

@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 introduces a new HedgeLimiter class to centralize connection-wide rate, concurrency, and lifetime limits on speculative hedged requests, refactoring HedgingThreadPool to delegate to it. It also implements HedgedOpenStream to race a primary open stream attempt against speculative delayed attempts. Feedback on the changes identifies a potential data race in HedgedOpenStreamConcurrencyTest where workers.emplace_back is called concurrently without synchronization inside the mock factory lambda.

@ajayky-os
ajayky-os force-pushed the impl/storage-hedged-open-stream branch from 2922104 to 929672c Compare November 26, 2026 14:03
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.58247% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.80%. Comparing base (4a2bfb0) to head (957c80c).

Files with missing lines Patch % Lines
.../storage/internal/async/hedged_open_stream_test.cc 96.81% 10 Missing ⚠️
google/cloud/storage/internal/hedge_limiter.h 85.45% 8 Missing ⚠️
...cloud/storage/internal/async/hedged_open_stream.cc 98.48% 3 Missing ⚠️
...oogle/cloud/storage/internal/hedge_limiter_test.cc 98.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16716      +/-   ##
==========================================
+ Coverage   92.78%   92.80%   +0.01%     
==========================================
  Files        2277     2281       +4     
  Lines      228147   228697     +550     
==========================================
+ Hits       211696   212239     +543     
- Misses      16451    16458       +7     

☔ 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.

@ajayky-os
ajayky-os force-pushed the impl/storage-hedged-open-stream branch from 929672c to 957c80c Compare November 26, 2026 14:55

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

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant