Override spanner_admin::DatabaseAdminClient Retry Policies
This shows how to override the retry policies for spanner_admin::DatabaseAdminClient
:
namespace spanner = ::google::cloud::spanner;
[](std::string const& project_id, std::string const& instance_id) {
// A database admin client is controlled by three policies. The retry
// policy determines for how long the client will retry transient failures.
auto retry_policy =
google::cloud::spanner_admin::DatabaseAdminLimitedTimeRetryPolicy(
/*maximum_duration=*/std::chrono::minutes(25))
.clone();
// The backoff policy controls how long does the client waits to retry after
// a transient failure. Here we configure a truncated exponential backoff
// with jitter:
// https://en.wikipedia.org/wiki/Exponential_backoff
// https://cloud.google.com/storage/docs/exponential-backoff
auto backoff_policy = spanner::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::seconds(2),
/*maximum_delay=*/std::chrono::minutes(10),
/*scaling=*/2.0)
.clone();
// The polling policy controls how the client waits for long-running
// operations (such as creating new instances). `GenericPollingPolicy<>`
// combines existing policies. In this case, keep polling until the
// operation completes (with success or error) or 45 minutes, whichever
// happens first. Initially pause for 10 seconds between polling requests,
// increasing the pause by a factor of 4 until it becomes 2 minutes.
auto polling_policy =
spanner::GenericPollingPolicy<>(
spanner::LimitedTimeRetryPolicy(
/*maximum_duration=*/std::chrono::minutes(45)),
spanner::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::seconds(10),
/*maximum_delay=*/std::chrono::minutes(2),
/*scaling=*/4.0))
.clone();
auto client = google::cloud::spanner_admin::DatabaseAdminClient(
google::cloud::spanner_admin::MakeDatabaseAdminConnection(),
google::cloud::Options{}
.set<google::cloud::spanner_admin::DatabaseAdminRetryPolicyOption>(
std::move(retry_policy))
.set<
google::cloud::spanner_admin::DatabaseAdminBackoffPolicyOption>(
std::move(backoff_policy))
.set<
google::cloud::spanner_admin::DatabaseAdminPollingPolicyOption>(
std::move(polling_policy)));
// Use the client as usual.
spanner::Instance instance(project_id, instance_id);
std::cout << "Available databases for instance " << instance << "\n";
for (auto& db : client.ListDatabases(instance.FullName())) {
if (!db) throw std::move(db).status();
std::cout << db->name() << "\n";
}
std::cout << "End of available databases\n";
}
This shows how to create a custom idempotency policy:
class CustomIdempotencyPolicy : public google::cloud::spanner_admin::
DatabaseAdminConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<
google::cloud::spanner_admin::DatabaseAdminConnectionIdempotencyPolicy>
clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};