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

feat(common): retry loops signal via error info #12313

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ using ::google::cloud::testing_util::AsyncSequencer;
using ::google::cloud::testing_util::IsProtoEqual;
using ::google::cloud::testing_util::StatusIs;
using ::google::protobuf::TextFormat;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Contains;
using ::testing::ContainsRegex;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
using ::testing::Pair;
using ::testing::Return;

std::shared_ptr<GoldenThingAdminConnection> CreateTestingConnection(
Expand Down Expand Up @@ -1234,10 +1236,12 @@ TEST(GoldenThingAdminConnectionTest, AsyncGetDatabaseTooManyFailures) {
auto fut = conn->AsyncGetDatabase(dbase);
ASSERT_EQ(std::future_status::ready, fut.wait_for(std::chrono::seconds(10)));
auto db = fut.get();
ASSERT_THAT(db, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry policy exhausted"),
HasSubstr("AsyncGetDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(db,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
auto const& metadata = db.status().error_info().metadata();
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.function", _)));
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.reason",
"retry-policy-exhausted")));
}

TEST(GoldenThingAdminConnectionTest, AsyncGetDatabaseCancel) {
Expand Down Expand Up @@ -1266,10 +1270,8 @@ TEST(GoldenThingAdminConnectionTest, AsyncGetDatabaseCancel) {
fut.cancel();
EXPECT_TRUE(cancel_completed.get());
auto db = fut.get();
ASSERT_THAT(db, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry loop cancelled"),
HasSubstr("AsyncGetDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(db,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
}

TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseSuccess) {
Expand Down Expand Up @@ -1318,10 +1320,8 @@ TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseFailure) {
auto fut = conn->AsyncDropDatabase(request);
ASSERT_EQ(std::future_status::ready, fut.wait_for(std::chrono::seconds(10)));
auto status = fut.get();
ASSERT_THAT(status, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry policy exhausted"),
HasSubstr("AsyncDropDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(status,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
}

TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseCancel) {
Expand Down Expand Up @@ -1356,10 +1356,11 @@ TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseCancel) {
fut.cancel();
EXPECT_TRUE(cancel_completed.get());
auto status = fut.get();
ASSERT_THAT(status, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry loop cancelled"),
HasSubstr("AsyncDropDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(status,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
auto const& metadata = status.error_info().metadata();
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.function", _)));
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.reason", "cancelled")));
}

TEST(GoldenThingAdminConnectionTest, CheckExpectedOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ using ::google::cloud::testing_util::AsyncSequencer;
using ::google::cloud::testing_util::IsProtoEqual;
using ::google::cloud::testing_util::StatusIs;
using ::google::protobuf::TextFormat;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Contains;
using ::testing::ContainsRegex;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::Pair;
using ::testing::Return;

std::shared_ptr<GoldenThingAdminConnection> CreateTestingConnection(
Expand Down Expand Up @@ -1254,10 +1256,12 @@ TEST(GoldenThingAdminConnectionTest, AsyncGetDatabaseTooManyFailures) {
auto fut = conn->AsyncGetDatabase(dbase);
ASSERT_EQ(std::future_status::ready, fut.wait_for(std::chrono::seconds(10)));
auto db = fut.get();
ASSERT_THAT(db, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry policy exhausted"),
HasSubstr("AsyncGetDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(db,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
auto const& metadata = db.status().error_info().metadata();
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.function", _)));
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.reason",
"retry-policy-exhausted")));
}

TEST(GoldenThingAdminConnectionTest, AsyncGetDatabaseCancel) {
Expand Down Expand Up @@ -1286,10 +1290,11 @@ TEST(GoldenThingAdminConnectionTest, AsyncGetDatabaseCancel) {
fut.cancel();
EXPECT_TRUE(cancel_completed.get());
auto db = fut.get();
ASSERT_THAT(db, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry loop cancelled"),
HasSubstr("AsyncGetDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(db,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
auto const& metadata = db.status().error_info().metadata();
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.function", _)));
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.reason", "cancelled")));
}

TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseSuccess) {
Expand Down Expand Up @@ -1338,10 +1343,12 @@ TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseFailure) {
auto fut = conn->AsyncDropDatabase(request);
ASSERT_EQ(std::future_status::ready, fut.wait_for(std::chrono::seconds(10)));
auto status = fut.get();
ASSERT_THAT(status, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry policy exhausted"),
HasSubstr("AsyncDropDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(status,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
auto const& metadata = status.error_info().metadata();
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.function", _)));
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.reason",
"retry-policy-exhausted")));
}

TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseCancel) {
Expand Down Expand Up @@ -1376,10 +1383,11 @@ TEST(GoldenThingAdminConnectionTest, AsyncDropDatabaseCancel) {
fut.cancel();
EXPECT_TRUE(cancel_completed.get());
auto status = fut.get();
ASSERT_THAT(status, StatusIs(StatusCode::kDeadlineExceeded,
AllOf(HasSubstr("Retry loop cancelled"),
HasSubstr("AsyncDropDatabase"),
HasSubstr("try again"))));
ASSERT_THAT(status,
StatusIs(StatusCode::kDeadlineExceeded, HasSubstr("try again")));
auto const& metadata = status.error_info().metadata();
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.function", _)));
EXPECT_THAT(metadata, Contains(Pair("gcloud-cpp.retry.reason", "cancelled")));
}

TEST(GoldenThingAdminConnectionTest, CheckExpectedOptions) {
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/accesscontextmanager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ if (BUILD_TESTING)
set_tests_properties(
accesscontextmanager_quickstart
PROPERTIES LABELS "integration-test;quickstart" PASS_REGULAR_EXPRESSION
"Permanent error in.*ListAccessLevels:")
"Permanent error.*gcloud-cpp.retry.function=ListAccessLevels"
)
endif ()

# Get the destination directories based on the GNU recommendations.
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/apigeeconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ if (BUILD_TESTING)
set_tests_properties(
apigeeconnect_quickstart
PROPERTIES LABELS "integration-test;quickstart" PASS_REGULAR_EXPRESSION
"Permanent error in.*ListConnections:")
"Permanent error.*gcloud-cpp.retry.function=ListConnections")
endif ()

# Get the destination directories based on the GNU recommendations.
Expand Down
7 changes: 5 additions & 2 deletions google/cloud/assuredworkloads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ if (BUILD_TESTING)
set_tests_properties(
assuredworkloads_quickstart
PROPERTIES
LABELS "integration-test;quickstart" ENVIRONMENT
LABELS
"integration-test;quickstart"
ENVIRONMENT
GOOGLE_CLOUD_CPP_USER_PROJECT=$ENV{GOOGLE_CLOUD_CPP_USER_PROJECT}
PASS_REGULAR_EXPRESSION "Permanent error in.*ListWorkloads:")
PASS_REGULAR_EXPRESSION
"Permanent error.*gcloud-cpp.retry.function=ListWorkloads")
endif ()

# Get the destination directories based on the GNU recommendations.
Expand Down
25 changes: 12 additions & 13 deletions google/cloud/internal/async_rest_retry_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ class AsyncRestRetryLoopImpl
void StartAttempt() {
auto self = this->shared_from_this();
if (retry_policy_->IsExhausted()) {
return SetDone(internal::RetryLoopError("Retry policy exhausted in",
location_, last_status_));
return SetDone(
internal::RetryLoopPolicyExhaustedError(last_status_, location_));
}
auto state = StartOperation();
if (state.cancelled) return;
Expand All @@ -259,16 +259,16 @@ class AsyncRestRetryLoopImpl
// Some kind of failure, first verify that it is retryable.
last_status_ = internal::GetResultStatus(std::move(result));
if (idempotency_ == Idempotency::kNonIdempotent) {
return SetDone(internal::RetryLoopError(
"Error in non-idempotent operation", location_, last_status_));
return SetDone(
internal::RetryLoopNonIdempotentError(last_status_, location_));
}
if (!retry_policy_->OnFailure(last_status_)) {
if (retry_policy_->IsPermanentFailure(last_status_)) {
return SetDone(internal::RetryLoopError("Permanent error in", location_,
last_status_));
return SetDone(
internal::RetryLoopPermanentError(last_status_, location_));
}
return SetDone(internal::RetryLoopError("Retry policy exhausted in",
location_, last_status_));
return SetDone(
internal::RetryLoopPolicyExhaustedError(last_status_, location_));
}
StartBackoff();
}
Expand All @@ -280,8 +280,8 @@ class AsyncRestRetryLoopImpl
if (state.cancelled) return;
if (!tp) {
// Some kind of error in the CompletionQueue, probably shutting down.
return SetDone(internal::RetryLoopError("Timer failure in", location_,
std::move(tp).status()));
return SetDone(
internal::RetryLoopCancelled(std::move(tp).status(), location_));
}
StartAttempt();
}
Expand All @@ -308,8 +308,7 @@ class AsyncRestRetryLoopImpl
if (done_) return State{true, 0};
done_ = true;
lk.unlock();
result_.set_value(internal::RetryLoopError("Retry loop cancelled",
location_, last_status_));
result_.set_value(internal::RetryLoopCancelled(last_status_, location_));
return State{true, 0};
}

Expand All @@ -330,7 +329,7 @@ class AsyncRestRetryLoopImpl
Request request_;
char const* location_ = "unknown";
internal::CallContext call_context_;
Status last_status_ = Status(StatusCode::kUnknown, "Retry policy exhausted");
Status last_status_;
promise<T> result_;

// Only the following variables require synchronization, as they coordinate
Expand Down
Loading