Google Cloud Spanner C++ Client  1.42.0
A C++ Client Library for Google Cloud Spanner
retry_policy.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RETRY_POLICY_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RETRY_POLICY_H
17 
18 #include "google/cloud/spanner/internal/status_utils.h"
19 #include "google/cloud/spanner/version.h"
20 #include "google/cloud/internal/retry_policy.h"
21 #include "google/cloud/status.h"
22 #include "absl/strings/match.h"
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner_internal {
28 
29 /// Define the gRPC status code semantics for retrying requests.
30 struct SafeGrpcRetry {
31  static inline bool IsOk(google::cloud::Status const& status) {
32  return status.ok();
33  }
34  static inline bool IsTransientFailure(google::cloud::Status const& status) {
35  return status.code() == StatusCode::kUnavailable ||
37  internal::IsTransientInternalError(status);
38  }
39  static inline bool IsPermanentFailure(google::cloud::Status const& status) {
40  return !IsOk(status) && !IsTransientFailure(status);
41  }
42 };
43 
44 /// Define the gRPC status code semantics for rerunning transactions.
45 struct SafeTransactionRerun {
46  static inline bool IsOk(google::cloud::Status const& status) {
47  return status.ok();
48  }
49  static inline bool IsTransientFailure(google::cloud::Status const& status) {
50  return status.code() == StatusCode::kAborted || IsSessionNotFound(status);
51  }
52  static inline bool IsPermanentFailure(google::cloud::Status const& status) {
53  return !IsOk(status) && !IsTransientFailure(status);
54  }
55 };
57 } // namespace spanner_internal
58 
59 namespace spanner {
61 
62 /// The base class for retry policies.
63 using RetryPolicy = ::google::cloud::internal::TraitBasedRetryPolicy<
64  spanner_internal::SafeGrpcRetry>;
65 
66 /// A retry policy that limits based on time.
67 using LimitedTimeRetryPolicy =
68  ::google::cloud::internal::LimitedTimeRetryPolicy<
69  spanner_internal::SafeGrpcRetry>;
70 
71 /// A retry policy that limits the number of times a request can fail.
72 using LimitedErrorCountRetryPolicy =
73  google::cloud::internal::LimitedErrorCountRetryPolicy<
74  spanner_internal::SafeGrpcRetry>;
75 
76 /// The base class for transaction rerun policies.
77 using TransactionRerunPolicy = ::google::cloud::internal::TraitBasedRetryPolicy<
78  spanner_internal::SafeTransactionRerun>;
79 
80 /// A transaction rerun policy that limits the duration of the rerun loop.
81 using LimitedTimeTransactionRerunPolicy =
82  google::cloud::internal::LimitedTimeRetryPolicy<
83  spanner_internal::SafeTransactionRerun>;
84 
85 /// A transaction rerun policy that limits the number of failures.
86 using LimitedErrorCountTransactionRerunPolicy =
87  google::cloud::internal::LimitedErrorCountRetryPolicy<
88  spanner_internal::SafeTransactionRerun>;
89 
91 } // namespace spanner
92 } // namespace cloud
93 } // namespace google
94 
95 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_RETRY_POLICY_H