Skip to content

refactor(storage): extract HedgeLimiter from HedgingThreadPool - #16715

Open
ajayky-os wants to merge 1 commit into
googleapis:mainfrom
ajayky-os:refactor/storage-extract-hedge-limiter
Open

ajayky-os wants to merge 1 commit into
googleapis:mainfrom
ajayky-os:refactor/storage-extract-hedge-limiter

Conversation

@ajayky-os

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

Copy link
Copy Markdown
Contributor

Summary

Extracts the thread-safe three-gate hedge budget limiter (MaxConcurrentHedgesOption, ReadHedgeRateLimitOption, and MaxTotalHedgesOption) from HedgingThreadPool into a standalone storage::internal::HedgeLimiter header (google/cloud/storage/internal/hedge_limiter.h).

Motivation

Currently, hedge rate, concurrency, and lifetime budget enforcement is embedded inside HedgingThreadPool, which spawns dedicated worker threads for synchronous storage::Client::ReadObject() hedging. Extracting HedgeLimiter as a pure, thread-pool-free component allows asynchronous CompletionQueue-driven hedging (storage::AsyncClient::Open(), #16716 and #16612) to reuse the exact same three-gate limiter without allocating unused background thread pools.

Changes

  • google/cloud/storage/internal/hedge_limiter.h:
    • Extracts HedgeLimiter (TryAcquireHedgeToken(), ReleaseHedgeSlot(), IsTotalBudgetSpent()) with identical gate ordering and rollback semantics.
  • google/cloud/storage/internal/hedging_thread_pool.h:
    • Composes HedgeLimiter limiter_ inside HedgingThreadPool and delegates TryAcquireHedgeToken(), ReleaseHedgeSlot(), and IsTotalBudgetSpent().
  • Tests & Build Lists (hedge_limiter_test.cc, hedging_thread_pool_test.cc, google_cloud_cpp_storage.{cmake,bzl}, storage_client_unit_tests.bzl):
    • Moves the 9 pure limiter unit and contention tests to hedge_limiter_test.cc (testing HedgeLimiter directly without spawning worker threads per test) and retains a delegation test (DelegatesToHedgeLimiter) on HedgingThreadPool.

@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 refactors the hedging logic by extracting the rate, concurrency, and lifetime limiting responsibilities from HedgingThreadPool into a new, dedicated HedgeLimiter class, simplifying the thread pool implementation and adding targeted unit tests. The review feedback recommends templating HedgeLimiter on a Clock type to allow injecting a mock clock, which avoids flaky sleep-based unit tests. Additionally, it suggests explicitly including <mutex> in hedging_thread_pool.h to comply with the "Include What You Use" style guide rule.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.16770% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.79%. Comparing base (4a2bfb0) to head (6d8a063).

Files with missing lines Patch % Lines
google/cloud/storage/internal/hedge_limiter.h 85.45% 8 Missing ⚠️
...oogle/cloud/storage/internal/hedge_limiter_test.cc 97.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16715   +/-   ##
=======================================
  Coverage   92.78%   92.79%           
=======================================
  Files        2277     2279    +2     
  Lines      228147   228185   +38     
=======================================
+ Hits       211696   211740   +44     
+ Misses      16451    16445    -6     

☔ 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 refactor/storage-extract-hedge-limiter branch from bfee084 to 6d8a063 Compare November 26, 2026 14:03

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