Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): backoff policy clones initial state #7696

Merged
merged 2 commits into from
Dec 5, 2021
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@

## v1.35.0 - TBD

### [Common Libraries](https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/README.md)

* Backoff policies are now cloned from their initial state, instead of their
current state. Any accumulated delay will be reset to its initial value in the
clone. The previous behavior was a bug, and thus it has been fixed. ([#7696](https://github.com/googleapis/google-cloud-cpp/pull/7696))

## v1.34.0 - 2021-12

### [BigQuery](https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/bigquery/README.md) [IAM](https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/iam/README.md)
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/bigtable/rpc_backoff_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ ExponentialBackoffPolicy::ExponentialBackoffPolicy(
}

std::unique_ptr<RPCBackoffPolicy> ExponentialBackoffPolicy::clone() const {
return std::unique_ptr<RPCBackoffPolicy>(new ExponentialBackoffPolicy(*this));
return std::unique_ptr<RPCBackoffPolicy>(
new ExponentialBackoffPolicy(initial_delay_, maximum_delay_));
Comment on lines -33 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible follow-up note: It seems to me that copy construction/assignment for these "clone-able" classes is an attractive nuisance (they don't support equality after all), and that we'd be well served trying to delete that capability.

}

void ExponentialBackoffPolicy::Setup(grpc::ClientContext&) const {}
Expand Down
14 changes: 11 additions & 3 deletions google/cloud/bigtable/rpc_backoff_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ class ExponentialBackoffPolicy : public RPCBackoffPolicy {
public:
// NOLINTNEXTLINE(google-explicit-constructor)
ExponentialBackoffPolicy(internal::RPCPolicyParameters defaults);
template <typename DurationT1, typename DurationT2>
ExponentialBackoffPolicy(DurationT1 initial_delay, DurationT2 maximum_delay)
: impl_(initial_delay / 2, maximum_delay, 2.0) {}
template <typename Rep1, typename Period1, typename Rep2, typename Period2>
ExponentialBackoffPolicy(std::chrono::duration<Rep1, Period1> initial_delay,
std::chrono::duration<Rep2, Period2> maximum_delay)
: initial_delay_(std::chrono::duration_cast<std::chrono::microseconds>(
initial_delay)),
maximum_delay_(std::chrono::duration_cast<std::chrono::microseconds>(
maximum_delay)),
impl_(initial_delay_ / 2, maximum_delay_, 2.0) {}

std::unique_ptr<RPCBackoffPolicy> clone() const override;
void Setup(grpc::ClientContext& context) const override;
Expand All @@ -99,6 +104,9 @@ class ExponentialBackoffPolicy : public RPCBackoffPolicy {
std::chrono::milliseconds OnCompletion(grpc::Status const& status) override;

private:
std::chrono::microseconds initial_delay_;
std::chrono::microseconds maximum_delay_;

using Impl = ::google::cloud::internal::ExponentialBackoffPolicy;
Impl impl_;
};
Expand Down
4 changes: 4 additions & 0 deletions google/cloud/bigtable/rpc_backoff_policy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ TEST(ExponentialBackoffRetryPolicy, Clone) {

EXPECT_GE(10_ms, tested->OnCompletion(CreateTransientError()));
EXPECT_LE(10_ms, tested->OnCompletion(CreateTransientError()));

// Ensure the initial state of the policy is cloned, not the current state.
tested = tested->clone();
EXPECT_GE(10_ms, tested->OnCompletion(CreateTransientError()));
}

/// @test Test for testing randomness for 2 objects of
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/internal/backoff_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace internal {

std::unique_ptr<BackoffPolicy> ExponentialBackoffPolicy::clone() const {
return absl::make_unique<ExponentialBackoffPolicy>(*this);
return absl::make_unique<ExponentialBackoffPolicy>(initial_delay_,
maximum_delay_, scaling_);
}

std::chrono::milliseconds ExponentialBackoffPolicy::OnCompletion() {
Expand Down
10 changes: 6 additions & 4 deletions google/cloud/internal/backoff_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ class ExponentialBackoffPolicy : public BackoffPolicy {
ExponentialBackoffPolicy(std::chrono::duration<Rep1, Period1> initial_delay,
std::chrono::duration<Rep2, Period2> maximum_delay,
double scaling)
: current_delay_range_(
std::chrono::duration_cast<std::chrono::microseconds>(
2 * initial_delay)),
: initial_delay_(std::chrono::duration_cast<std::chrono::microseconds>(
initial_delay)),
current_delay_range_(2 * initial_delay_),
maximum_delay_(std::chrono::duration_cast<std::chrono::microseconds>(
maximum_delay)),
scaling_(scaling) {
Expand All @@ -143,14 +143,16 @@ class ExponentialBackoffPolicy : public BackoffPolicy {
// know specifically which one is at fault)
// - We want uncorrelated data streams for each copy anyway.
ExponentialBackoffPolicy(ExponentialBackoffPolicy const& rhs) noexcept
: current_delay_range_(rhs.current_delay_range_),
: initial_delay_(rhs.initial_delay_),
current_delay_range_(rhs.current_delay_range_),
maximum_delay_(rhs.maximum_delay_),
scaling_(rhs.scaling_) {}

std::unique_ptr<BackoffPolicy> clone() const override;
std::chrono::milliseconds OnCompletion() override;

private:
std::chrono::microseconds initial_delay_;
std::chrono::microseconds current_delay_range_;
std::chrono::microseconds maximum_delay_;
double scaling_;
Expand Down
6 changes: 6 additions & 0 deletions google/cloud/internal/backoff_policy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ TEST(ExponentialBackoffPolicy, Clone) {
delay = tested->OnCompletion();
EXPECT_LE(ms(25), delay);
EXPECT_GE(ms(50), delay);

// Ensure the initial state of the policy is cloned, not the current state.
tested = tested->clone();
delay = tested->OnCompletion();
EXPECT_LE(ms(10), delay);
EXPECT_GE(ms(20), delay);
}

/// @test Test for testing randomness for 2 objects of
Expand Down