Skip to content

Commit 4be7490

Browse files
committed
feat(storage): add speculative open hedging to AsyncClient::Open
1 parent c639a79 commit 4be7490

5 files changed

Lines changed: 471 additions & 35 deletions

File tree

‎google/cloud/storage/async/client.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ class AsyncClient {
285285
* will resume any incomplete ranged reads if the connection(s) are
286286
* interrupted. Use `ResumePolicyOption` and `ResumePolicy` to control this.
287287
*
288+
* @see `storage_experimental::EnableReadHedgingOption` for speculative
289+
* stream-open hedging.
290+
*
288291
* @param bucket_name the name of the bucket that contains the object.
289292
* @param object_name the name of the object to be read.
290293
* @param opts options controlling the behavior of this RPC, for example
@@ -307,6 +310,9 @@ class AsyncClient {
307310
* will resume any incomplete ranged reads if the connection(s) are
308311
* interrupted. Use `ResumePolicyOption` and `ResumePolicy` to control this.
309312
*
313+
* @see `storage_experimental::EnableReadHedgingOption` for speculative
314+
* stream-open hedging.
315+
*
310316
* @param bucket_name the name of the bucket that contains the object.
311317
* @param object_name the name of the object to be read.
312318
* @param config initial byte ranges to request during connection setup.
@@ -326,6 +332,9 @@ class AsyncClient {
326332
* will resume any incomplete ranged reads if the connection(s) are
327333
* interrupted. Use `ResumePolicyOption` and `ResumePolicy` to control this.
328334
*
335+
* @see `storage_experimental::EnableReadHedgingOption` for speculative
336+
* stream-open hedging.
337+
*
329338
* @param spec the BidiReadObjectSpec to use when retrieving the
330339
* ObjectDescriptor.
331340
* @param opts options controlling the behavior of this RPC, for example

‎google/cloud/storage/internal/async/connection_impl.cc‎

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
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"
@@ -51,6 +52,8 @@
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"
@@ -62,6 +65,8 @@
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.
151156
enum 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

244272
future<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),

‎google/cloud/storage/internal/async/connection_impl.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939

4040
namespace google {
4141
namespace cloud {
42+
namespace storage {
43+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
44+
namespace internal {
45+
class HedgeLimiter;
46+
class HedgedReadMetrics;
47+
} // namespace internal
48+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
49+
} // namespace storage
50+
4251
namespace storage_internal {
4352
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
4453
class GrpcChannelRefresh;
@@ -148,6 +157,8 @@ class AsyncConnectionImpl
148157
std::shared_ptr<GrpcChannelRefresh> refresh_;
149158
std::shared_ptr<StorageStub> stub_;
150159
Options options_;
160+
std::shared_ptr<storage::internal::HedgeLimiter> hedge_limiter_;
161+
std::shared_ptr<storage::internal::HedgedReadMetrics> hedged_read_metrics_;
151162
google::cloud::internal::InvocationIdGenerator invocation_id_generator_;
152163
};
153164

0 commit comments

Comments
 (0)