2323#include " google/cloud/storage/internal/async/checksum_helpers.h"
2424#include " google/cloud/storage/internal/async/default_options.h"
2525#include " google/cloud/storage/internal/async/handle_redirect_error.h"
26+ #include " google/cloud/storage/internal/async/hedged_open_stream.h"
2627#include " google/cloud/storage/internal/async/insert_object.h"
2728#include " google/cloud/storage/internal/async/object_descriptor_impl.h"
2829#include " google/cloud/storage/internal/async/open_object.h"
5152#include " google/cloud/storage/internal/hash_function_impl.h"
5253#include " google/cloud/storage/internal/hash_validator.h"
5354#include " google/cloud/storage/internal/hash_validator_impl.h"
55+ #include " google/cloud/storage/internal/hedge_limiter.h"
56+ #include " google/cloud/storage/internal/hedged_read_metrics.h"
5457#include " google/cloud/storage/internal/retry_logging.h"
5558#include " google/cloud/storage/internal/storage_stub.h"
5659#include " google/cloud/storage/internal/storage_stub_factory.h"
6265#include " google/cloud/internal/async_streaming_write_rpc_timeout.h"
6366#include " google/cloud/internal/make_status.h"
6467#include < grpcpp/grpcpp.h>
68+ #include < opentelemetry/metrics/provider.h>
69+ #include < chrono>
6570#include < cstdint>
6671#include < memory>
6772#include < set>
@@ -150,6 +155,9 @@ std::unique_ptr<storage::internal::HashValidator> CreateHashValidator(
150155// / Whether the retry loop opening a bidi read stream reports its retries.
151156enum class OpenLogging { kEnabled , kDisabled };
152157
158+ // / Whether the stream-open attempt is part of a hedged `Open()` race.
159+ enum class OpenHedging { kDisabled , kEnabled };
160+
153161/* *
154162 * Creates the factory that opens a bidi read stream.
155163 *
@@ -175,32 +183,37 @@ OpenStreamFactory MakeOpenStreamFactory(
175183 std::shared_ptr<storage::AsyncRetryPolicy> retry,
176184 std::shared_ptr<storage::BackoffPolicy> backoff,
177185 google::cloud::internal::ImmutableOptions current,
178- char const * function_name, OpenLogging logging) {
186+ char const * function_name, OpenLogging logging, OpenHedging hedging ) {
179187 return OpenStreamFactory (
180188 [stub = std::move (stub), cq = std::move (cq), retry = std::move (retry),
181189 backoff = std::move (backoff), current = std::move (current),
182- function_name,
183- logging ](google::storage::v2::BidiReadObjectRequest request) {
190+ function_name, logging,
191+ hedging ](google::storage::v2::BidiReadObjectRequest request) {
184192 struct DummyRequest {};
185193
186194 std::string resource =
187195 logging == OpenLogging::kEnabled
188196 ? RetryLogResource (request.read_object_spec ().bucket (),
189197 request.read_object_spec ().object ())
190198 : std::string{};
191- auto call = [stub, request = std::move (request)](
199+ auto call = [stub, request = std::move (request), hedging ](
192200 CompletionQueue& cq,
193201 std::shared_ptr<grpc::ClientContext> context,
194202 google::cloud::internal::ImmutableOptions options,
195203 DummyRequest const &) mutable {
196204 // Every stream the descriptor opens (the first one, resumes, and
197205 // the extra streams of the multi-stream optimization) goes through
198206 // this factory, so this is where the service learns which client
199- // features are in play for the read.
207+ // features are in play for the read. `kReadHedging` is set only on
208+ // the initial hedged `Open()` attempts, not on unhedged descriptor
209+ // secondary or resume streams.
200210 std::uint32_t features = 0 ;
201211 if (options->get <storage::EnableMultiStreamOptimizationOption>()) {
202212 features |= FeatureBit (TrackedFeature::kMultistreamInMrd );
203213 }
214+ if (hedging == OpenHedging::kEnabled ) {
215+ features |= FeatureBit (TrackedFeature::kReadHedging );
216+ }
204217 AddFeaturesHeader (*context, features);
205218 auto open = std::make_shared<OpenObject>(
206219 *stub, cq, std::move (context), std::move (options), request);
@@ -239,7 +252,22 @@ AsyncConnectionImpl::AsyncConnectionImpl(
239252 : cq_(std::move(cq)),
240253 refresh_ (std::move(refresh)),
241254 stub_(std::move(stub)),
242- options_(std::move(options)) {}
255+ options_(std::move(options)) {
256+ if (options_.get <storage_experimental::EnableReadHedgingOption>()) {
257+ double const rate_limit =
258+ options_.get <storage_experimental::ReadHedgeRateLimitOption>();
259+ std::int64_t const max_concurrent =
260+ options_.get <storage_experimental::MaxConcurrentHedgesOption>();
261+ std::int64_t const max_total =
262+ options_.get <storage_experimental::MaxTotalHedgesOption>();
263+ // Allow bursts of up to one second worth of hedges.
264+ hedge_limiter_ = std::make_shared<storage::internal::HedgeLimiter>(
265+ rate_limit, rate_limit, max_concurrent, max_total);
266+ hedged_read_metrics_ =
267+ std::make_shared<storage::internal::HedgedReadMetrics>(
268+ opentelemetry::metrics::Provider::GetMeterProvider ());
269+ }
270+ }
243271
244272future<StatusOr<google::storage::v2::Bucket>> AsyncConnectionImpl::GetBucket (
245273 GetBucketParams p) {
@@ -327,22 +355,39 @@ AsyncConnectionImpl::Open(OpenParams p) {
327355 // Get the policy factory and immediately create a policy.
328356 auto resume_policy = current->get <storage::ResumePolicyOption>()();
329357
330- auto retry =
331- std::shared_ptr<storage::AsyncRetryPolicy>(retry_policy (*current));
332- auto backoff =
333- std::shared_ptr<storage::BackoffPolicy>(backoff_policy (*current));
334- auto const * function_name = __func__;
358+ std::shared_ptr<storage::AsyncRetryPolicy> retry = retry_policy (*current);
359+ std::shared_ptr<storage::BackoffPolicy> backoff = backoff_policy (*current);
360+ char const * function_name = __func__;
335361 // The application is waiting on this open, so its retries are reported. So
336362 // are the descriptor's, with one exception below.
337- auto factory = MakeOpenStreamFactory (stub_, cq_, retry, backoff, current,
338- function_name, OpenLogging::kEnabled );
363+ OpenStreamFactory factory =
364+ MakeOpenStreamFactory (stub_, cq_, retry, backoff, current, function_name,
365+ OpenLogging::kEnabled , OpenHedging::kDisabled );
366+
367+ int const max_hedges =
368+ hedge_limiter_ && !hedge_limiter_->IsTotalBudgetSpent () &&
369+ current->get <storage_experimental::EnableReadHedgingOption>()
370+ ? current->get <storage_experimental::MaxReadHedgesOption>()
371+ : 0 ;
372+ future<StatusOr<OpenStreamResult>> pending;
373+ if (max_hedges > 0 ) {
374+ HedgedOpenParams const params{
375+ current->get <storage_experimental::ReadHedgeDelayOption>(), max_hedges};
376+ OpenStreamFactory hedged_factory = MakeOpenStreamFactory (
377+ stub_, cq_, retry, backoff, current, function_name,
378+ OpenLogging::kEnabled , OpenHedging::kEnabled );
379+ pending = HedgedOpenStream (cq_, std::move (hedged_factory),
380+ std::move (initial_request), retry, params,
381+ hedge_limiter_, hedged_read_metrics_, current);
382+ } else {
383+ pending = factory (std::move (initial_request));
384+ }
385+
339386 // Used only when the descriptor re-opens a stream that ended cleanly: see
340387 // `MakeOpenStreamFactory()` and `ObjectDescriptorImpl`.
341- auto clean_resume_factory = MakeOpenStreamFactory (
388+ OpenStreamFactory clean_resume_factory = MakeOpenStreamFactory (
342389 stub_, cq_, std::move (retry), std::move (backoff), std::move (current),
343- function_name, OpenLogging::kDisabled );
344-
345- auto pending = factory (std::move (initial_request));
390+ function_name, OpenLogging::kDisabled , OpenHedging::kDisabled );
346391 using ReturnType = std::shared_ptr<storage::ObjectDescriptorConnection>;
347392 return pending.then ([rp = std::move (resume_policy), fa = std::move (factory),
348393 qfa = std::move (clean_resume_factory),
0 commit comments