Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions google/cloud/storage/async/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ class AsyncClient {
* will resume any incomplete ranged reads if the connection(s) are
* interrupted. Use `ResumePolicyOption` and `ResumePolicy` to control this.
*
* @see `storage_experimental::EnableReadHedgingOption` for speculative
* stream-open hedging.
*
* @param bucket_name the name of the bucket that contains the object.
* @param object_name the name of the object to be read.
* @param opts options controlling the behavior of this RPC, for example
Expand All @@ -307,6 +310,9 @@ class AsyncClient {
* will resume any incomplete ranged reads if the connection(s) are
* interrupted. Use `ResumePolicyOption` and `ResumePolicy` to control this.
*
* @see `storage_experimental::EnableReadHedgingOption` for speculative
* stream-open hedging.
*
* @param bucket_name the name of the bucket that contains the object.
* @param object_name the name of the object to be read.
* @param config initial byte ranges to request during connection setup.
Expand All @@ -326,6 +332,9 @@ class AsyncClient {
* will resume any incomplete ranged reads if the connection(s) are
* interrupted. Use `ResumePolicyOption` and `ResumePolicy` to control this.
*
* @see `storage_experimental::EnableReadHedgingOption` for speculative
* stream-open hedging.
*
* @param spec the BidiReadObjectSpec to use when retrieving the
* ObjectDescriptor.
* @param opts options controlling the behavior of this RPC, for example
Expand Down
1 change: 1 addition & 0 deletions google/cloud/storage/google_cloud_cpp_storage.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ google_cloud_cpp_storage_hdrs = [
"internal/hash_validator.h",
"internal/hash_validator_impl.h",
"internal/hash_values.h",
"internal/hedge_limiter.h",
"internal/hedged_object_read_source.h",
"internal/hedged_read_metrics.h",
"internal/hedging_thread_pool.h",
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ add_library(
internal/hash_validator_impl.h
internal/hash_values.cc
internal/hash_values.h
internal/hedge_limiter.h
internal/hedged_object_read_source.cc
internal/hedged_object_read_source.h
internal/hedged_read_metrics.cc
Expand Down Expand Up @@ -462,6 +463,7 @@ if (BUILD_TESTING)
internal/hash_function_impl_test.cc
internal/hash_validator_test.cc
internal/hash_values_test.cc
internal/hedge_limiter_test.cc
internal/hedged_object_read_source_test.cc
internal/hedged_read_metrics_test.cc
internal/hedging_thread_pool_test.cc
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage_grpc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ google_cloud_cpp_storage_grpc_hdrs = [
"internal/async/connection_tracing.h",
"internal/async/default_options.h",
"internal/async/handle_redirect_error.h",
"internal/async/hedged_open_stream.h",
"internal/async/insert_object.h",
"internal/async/multi_stream_manager.h",
"internal/async/object_descriptor_connection_tracing.h",
Expand Down Expand Up @@ -124,6 +125,7 @@ google_cloud_cpp_storage_grpc_srcs = [
"internal/async/connection_tracing.cc",
"internal/async/default_options.cc",
"internal/async/handle_redirect_error.cc",
"internal/async/hedged_open_stream.cc",
"internal/async/insert_object.cc",
"internal/async/multi_stream_manager.cc",
"internal/async/object_descriptor_connection_tracing.cc",
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/storage/google_cloud_cpp_storage_grpc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ add_library(
internal/async/default_options.h
internal/async/handle_redirect_error.cc
internal/async/handle_redirect_error.h
internal/async/hedged_open_stream.cc
internal/async/hedged_open_stream.h
internal/async/insert_object.cc
internal/async/insert_object.h
internal/async/multi_stream_manager.cc
Expand Down Expand Up @@ -426,6 +428,7 @@ set(storage_client_grpc_unit_tests
internal/async/connection_tracing_test.cc
internal/async/default_options_test.cc
internal/async/handle_redirect_error_test.cc
internal/async/hedged_open_stream_test.cc
internal/async/insert_object_test.cc
internal/async/multi_stream_manager_test.cc
internal/async/object_descriptor_connection_tracing_test.cc
Expand Down
79 changes: 62 additions & 17 deletions google/cloud/storage/internal/async/connection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "google/cloud/storage/internal/async/checksum_helpers.h"
#include "google/cloud/storage/internal/async/default_options.h"
#include "google/cloud/storage/internal/async/handle_redirect_error.h"
#include "google/cloud/storage/internal/async/hedged_open_stream.h"
#include "google/cloud/storage/internal/async/insert_object.h"
#include "google/cloud/storage/internal/async/object_descriptor_impl.h"
#include "google/cloud/storage/internal/async/open_object.h"
Expand Down Expand Up @@ -51,6 +52,8 @@
#include "google/cloud/storage/internal/hash_function_impl.h"
#include "google/cloud/storage/internal/hash_validator.h"
#include "google/cloud/storage/internal/hash_validator_impl.h"
#include "google/cloud/storage/internal/hedge_limiter.h"
#include "google/cloud/storage/internal/hedged_read_metrics.h"
#include "google/cloud/storage/internal/retry_logging.h"
#include "google/cloud/storage/internal/storage_stub.h"
#include "google/cloud/storage/internal/storage_stub_factory.h"
Expand All @@ -62,6 +65,8 @@
#include "google/cloud/internal/async_streaming_write_rpc_timeout.h"
#include "google/cloud/internal/make_status.h"
#include <grpcpp/grpcpp.h>
#include <opentelemetry/metrics/provider.h>
#include <chrono>
#include <cstdint>
#include <memory>
#include <set>
Expand Down Expand Up @@ -150,6 +155,9 @@ std::unique_ptr<storage::internal::HashValidator> CreateHashValidator(
/// Whether the retry loop opening a bidi read stream reports its retries.
enum class OpenLogging { kEnabled, kDisabled };

/// Whether the stream-open attempt is part of a hedged `Open()` race.
enum class OpenHedging { kDisabled, kEnabled };

/**
* Creates the factory that opens a bidi read stream.
*
Expand All @@ -175,32 +183,37 @@ OpenStreamFactory MakeOpenStreamFactory(
std::shared_ptr<storage::AsyncRetryPolicy> retry,
std::shared_ptr<storage::BackoffPolicy> backoff,
google::cloud::internal::ImmutableOptions current,
char const* function_name, OpenLogging logging) {
char const* function_name, OpenLogging logging, OpenHedging hedging) {
return OpenStreamFactory(
[stub = std::move(stub), cq = std::move(cq), retry = std::move(retry),
backoff = std::move(backoff), current = std::move(current),
function_name,
logging](google::storage::v2::BidiReadObjectRequest request) {
function_name, logging,
hedging](google::storage::v2::BidiReadObjectRequest request) {
struct DummyRequest {};

std::string resource =
logging == OpenLogging::kEnabled
? RetryLogResource(request.read_object_spec().bucket(),
request.read_object_spec().object())
: std::string{};
auto call = [stub, request = std::move(request)](
auto call = [stub, request = std::move(request), hedging](
CompletionQueue& cq,
std::shared_ptr<grpc::ClientContext> context,
google::cloud::internal::ImmutableOptions options,
DummyRequest const&) mutable {
// Every stream the descriptor opens (the first one, resumes, and
// the extra streams of the multi-stream optimization) goes through
// this factory, so this is where the service learns which client
// features are in play for the read.
// features are in play for the read. `kReadHedging` is set only on
// the initial hedged `Open()` attempts, not on unhedged descriptor
// secondary or resume streams.
std::uint32_t features = 0;
if (options->get<storage::EnableMultiStreamOptimizationOption>()) {
features |= FeatureBit(TrackedFeature::kMultistreamInMrd);
}
if (hedging == OpenHedging::kEnabled) {
features |= FeatureBit(TrackedFeature::kReadHedging);
}
AddFeaturesHeader(*context, features);
auto open = std::make_shared<OpenObject>(
*stub, cq, std::move(context), std::move(options), request);
Expand Down Expand Up @@ -239,7 +252,22 @@ AsyncConnectionImpl::AsyncConnectionImpl(
: cq_(std::move(cq)),
refresh_(std::move(refresh)),
stub_(std::move(stub)),
options_(std::move(options)) {}
options_(std::move(options)) {
if (options_.get<storage_experimental::EnableReadHedgingOption>()) {
double const rate_limit =
options_.get<storage_experimental::ReadHedgeRateLimitOption>();
std::int64_t const max_concurrent =
options_.get<storage_experimental::MaxConcurrentHedgesOption>();
std::int64_t const max_total =
options_.get<storage_experimental::MaxTotalHedgesOption>();
// Allow bursts of up to one second worth of hedges.
hedge_limiter_ = std::make_shared<storage::internal::HedgeLimiter>(
rate_limit, rate_limit, max_concurrent, max_total);
hedged_read_metrics_ =
std::make_shared<storage::internal::HedgedReadMetrics>(
opentelemetry::metrics::Provider::GetMeterProvider());
}
}

future<StatusOr<google::storage::v2::Bucket>> AsyncConnectionImpl::GetBucket(
GetBucketParams p) {
Expand Down Expand Up @@ -327,22 +355,39 @@ AsyncConnectionImpl::Open(OpenParams p) {
// Get the policy factory and immediately create a policy.
auto resume_policy = current->get<storage::ResumePolicyOption>()();

auto retry =
std::shared_ptr<storage::AsyncRetryPolicy>(retry_policy(*current));
auto backoff =
std::shared_ptr<storage::BackoffPolicy>(backoff_policy(*current));
auto const* function_name = __func__;
std::shared_ptr<storage::AsyncRetryPolicy> retry = retry_policy(*current);
std::shared_ptr<storage::BackoffPolicy> backoff = backoff_policy(*current);
char const* function_name = __func__;
// The application is waiting on this open, so its retries are reported. So
// are the descriptor's, with one exception below.
auto factory = MakeOpenStreamFactory(stub_, cq_, retry, backoff, current,
function_name, OpenLogging::kEnabled);
OpenStreamFactory factory =
MakeOpenStreamFactory(stub_, cq_, retry, backoff, current, function_name,
OpenLogging::kEnabled, OpenHedging::kDisabled);

int const max_hedges =
hedge_limiter_ && !hedge_limiter_->IsTotalBudgetSpent() &&
current->get<storage_experimental::EnableReadHedgingOption>()
? current->get<storage_experimental::MaxReadHedgesOption>()
: 0;
future<StatusOr<OpenStreamResult>> pending;
if (max_hedges > 0) {
HedgedOpenParams const params{
current->get<storage_experimental::ReadHedgeDelayOption>(), max_hedges};
OpenStreamFactory hedged_factory = MakeOpenStreamFactory(
stub_, cq_, retry, backoff, current, function_name,
OpenLogging::kEnabled, OpenHedging::kEnabled);
pending = HedgedOpenStream(cq_, std::move(hedged_factory),
std::move(initial_request), retry, params,
hedge_limiter_, hedged_read_metrics_, current);
} else {
pending = factory(std::move(initial_request));
}

// Used only when the descriptor re-opens a stream that ended cleanly: see
// `MakeOpenStreamFactory()` and `ObjectDescriptorImpl`.
auto clean_resume_factory = MakeOpenStreamFactory(
OpenStreamFactory clean_resume_factory = MakeOpenStreamFactory(
stub_, cq_, std::move(retry), std::move(backoff), std::move(current),
function_name, OpenLogging::kDisabled);

auto pending = factory(std::move(initial_request));
function_name, OpenLogging::kDisabled, OpenHedging::kDisabled);
using ReturnType = std::shared_ptr<storage::ObjectDescriptorConnection>;
return pending.then([rp = std::move(resume_policy), fa = std::move(factory),
qfa = std::move(clean_resume_factory),
Expand Down
11 changes: 11 additions & 0 deletions google/cloud/storage/internal/async/connection_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@

namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {
class HedgeLimiter;
class HedgedReadMetrics;
} // namespace internal
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage

namespace storage_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
class GrpcChannelRefresh;
Expand Down Expand Up @@ -148,6 +157,8 @@ class AsyncConnectionImpl
std::shared_ptr<GrpcChannelRefresh> refresh_;
std::shared_ptr<StorageStub> stub_;
Options options_;
std::shared_ptr<storage::internal::HedgeLimiter> hedge_limiter_;
std::shared_ptr<storage::internal::HedgedReadMetrics> hedged_read_metrics_;
google::cloud::internal::InvocationIdGenerator invocation_id_generator_;
};

Expand Down
Loading
Loading