Google Cloud Bigtable C++ Client 2.13.0
A C++ Client Library for Google Cloud Bigtable
Loading...
Searching...
No Matches
rpc_retry_policy.h
1// Copyright 2017 Google Inc.
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_BIGTABLE_RPC_RETRY_POLICY_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_RPC_RETRY_POLICY_H
17
18#include "google/cloud/bigtable/internal/rpc_policy_parameters.h"
19#include "google/cloud/bigtable/version.h"
20#include "google/cloud/grpc_error_delegate.h"
21#include "google/cloud/internal/retry_policy.h"
22#include "google/cloud/status.h"
23#include <grpcpp/grpcpp.h>
24#include <memory>
25
26namespace google {
27namespace cloud {
28namespace bigtable_internal {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30
31template <typename T>
32class CommonRetryPolicy;
33
34GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
35} // namespace bigtable_internal
36namespace bigtable {
37GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
38namespace internal {
39/// An adapter to use `grpc::Status` with the `google::cloud::*Policies`.
40struct SafeGrpcRetry {
41 static inline bool IsOk(Status const& status) { return status.ok(); }
42 static inline bool IsTransientFailure(Status const& status) {
43 auto const code = status.code();
44 return code == StatusCode::kAborted || code == StatusCode::kUnavailable ||
45 google::cloud::internal::IsTransientInternalError(status);
46 }
47 static inline bool IsPermanentFailure(Status const& status) {
48 return !IsOk(status) && !IsTransientFailure(status);
49 }
50
51 // TODO(#2344) - remove ::grpc::Status version.
52 static inline bool IsOk(grpc::Status const& status) { return status.ok(); }
53 static inline bool IsTransientFailure(grpc::Status const& status) {
54 return IsTransientFailure(MakeStatusFromRpcError(status));
55 }
56 static inline bool IsPermanentFailure(grpc::Status const& status) {
57 return !IsOk(status) && !IsTransientFailure(status);
58 }
59};
60} // namespace internal
61
62/**
63 * Define the interface for controlling how the Bigtable client
64 * retries RPC operations.
65 *
66 * The C++ client for Bigtable needs to hide partial and temporary
67 * failures from the application. However, we need to give the users
68 * enough flexibility to control how many attempts are made to reissue
69 * operations, how often these attempts are executed, and how to
70 * signal that an error has occurred.
71 *
72 * The application provides an instance of this class when the Table
73 * (or TableAdmin) object is created. This instance serves as a
74 * prototype to create new RPCRetryPolicy objects of the same
75 * (dynamic) type and with the same initial state.
76 *
77 */
78class RPCRetryPolicy {
79 public:
80 using RetryableTraits = internal::SafeGrpcRetry;
81
82 virtual ~RPCRetryPolicy() = default;
83
84 /**
85 * Return a new copy of this object.
86 *
87 * Typically implemented as
88 * @code
89 * return std::unique_ptr<RPCRetryPolicy>(new Foo(*this));
90 * @endcode
91 */
92 virtual std::unique_ptr<RPCRetryPolicy> clone() const = 0;
93
94 /**
95 * Update the ClientContext for the next call.
96 */
97 virtual void Setup(grpc::ClientContext& context) const = 0;
98
99 /**
100 * Handle an RPC failure.
101 *
102 * @return true if the RPC operation should be retried.
103 */
104 virtual bool OnFailure(Status const& status) = 0;
105 // TODO(#2344) - remove ::grpc::Status version.
106 virtual bool OnFailure(grpc::Status const& status) = 0;
107
108 static bool IsPermanentFailure(Status const& status) {
109 return internal::SafeGrpcRetry::IsPermanentFailure(status);
110 }
111 // TODO(#2344) - remove ::grpc::Status version.
112 static bool IsPermanentFailure(grpc::Status const& status) {
113 return internal::SafeGrpcRetry::IsPermanentFailure(status);
114 }
115
116 virtual bool IsExhausted() const { return exhausted_; }
117
118 private:
119 template <typename T>
120 friend class bigtable_internal::CommonRetryPolicy;
121
122 bool exhausted_ = false;
123};
124
125/// Return an instance of the default RPCRetryPolicy.
127 internal::RPCPolicyParameters defaults);
128
129/**
130 * Implement a simple "count errors and then stop" retry policy.
131 */
133 public:
134 explicit LimitedErrorCountRetryPolicy(int maximum_failures)
135 : impl_(maximum_failures) {}
136
137 std::unique_ptr<RPCRetryPolicy> clone() const override;
138 void Setup(grpc::ClientContext& context) const override;
139 bool OnFailure(Status const& status) override;
140 // TODO(#2344) - remove ::grpc::Status version.
141 bool OnFailure(grpc::Status const& status) override;
142 bool IsExhausted() const override;
143
144 private:
145 using Impl = ::google::cloud::internal::LimitedErrorCountRetryPolicy<
146 internal::SafeGrpcRetry>;
147 Impl impl_;
148};
149
150/**
151 * Implement a simple "keep trying for this time" retry policy.
152 */
154 public:
155 explicit LimitedTimeRetryPolicy(internal::RPCPolicyParameters defaults);
156 template <typename DurationT>
157 explicit LimitedTimeRetryPolicy(DurationT maximum_duration)
158 : impl_(maximum_duration) {}
159
160 std::unique_ptr<RPCRetryPolicy> clone() const override;
161 void Setup(grpc::ClientContext& context) const override;
162 bool OnFailure(Status const& status) override;
163 // TODO(#2344) - remove ::grpc::Status version.
164 bool OnFailure(grpc::Status const& status) override;
165 bool IsExhausted() const override;
166
167 private:
168 using Impl = ::google::cloud::internal::LimitedTimeRetryPolicy<
169 internal::SafeGrpcRetry>;
170 Impl impl_;
171};
172
173GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
174} // namespace bigtable
175namespace bigtable_internal {
176GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
177
178template <typename ReturnType>
179class CommonRetryPolicy : public ReturnType {
180 public:
181 explicit CommonRetryPolicy(std::unique_ptr<bigtable::RPCRetryPolicy> impl)
182 : impl_(std::move(impl)) {}
183 ~CommonRetryPolicy() override = default;
184
185 std::unique_ptr<ReturnType> clone() const override {
186 return std::make_unique<CommonRetryPolicy>(impl_->clone());
187 }
188 bool OnFailure(Status const& s) override {
189 auto retry = impl_->OnFailure(s);
190 if (!retry && !IsPermanentFailure(s)) impl_->exhausted_ = true;
191 return retry;
192 }
193 bool IsExhausted() const override { return impl_->IsExhausted(); }
194 bool IsPermanentFailure(Status const& s) const override {
196 }
197 void OnFailureImpl() override {}
198
199 private:
200 std::unique_ptr<bigtable::RPCRetryPolicy> impl_;
201};
202
203template <typename ReturnType>
204std::unique_ptr<ReturnType> MakeCommonRetryPolicy(
205 std::unique_ptr<bigtable::RPCRetryPolicy> policy) {
206 return std::make_unique<CommonRetryPolicy<ReturnType>>(std::move(policy));
207}
208
209GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
210} // namespace bigtable_internal
211} // namespace cloud
212} // namespace google
213
214#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_RPC_RETRY_POLICY_H
StatusCode code() const
Implement a simple "count errors and then stop" retry policy.
Definition: rpc_retry_policy.h:132
void Setup(grpc::ClientContext &context) const override
Update the ClientContext for the next call.
bool OnFailure(Status const &status) override
Handle an RPC failure.
std::unique_ptr< RPCRetryPolicy > clone() const override
Return a new copy of this object.
LimitedErrorCountRetryPolicy(int maximum_failures)
Definition: rpc_retry_policy.h:134
bool OnFailure(grpc::Status const &status) override
Implement a simple "keep trying for this time" retry policy.
Definition: rpc_retry_policy.h:153
LimitedTimeRetryPolicy(DurationT maximum_duration)
Definition: rpc_retry_policy.h:157
bool OnFailure(Status const &status) override
Handle an RPC failure.
LimitedTimeRetryPolicy(internal::RPCPolicyParameters defaults)
std::unique_ptr< RPCRetryPolicy > clone() const override
Return a new copy of this object.
bool OnFailure(grpc::Status const &status) override
void Setup(grpc::ClientContext &context) const override
Update the ClientContext for the next call.
Define the interface for controlling how the Bigtable client retries RPC operations.
Definition: rpc_retry_policy.h:78
virtual bool OnFailure(grpc::Status const &status)=0
virtual bool OnFailure(Status const &status)=0
Handle an RPC failure.
static bool IsPermanentFailure(grpc::Status const &status)
Definition: rpc_retry_policy.h:112
virtual bool IsExhausted() const
Definition: rpc_retry_policy.h:116
static bool IsPermanentFailure(Status const &status)
Definition: rpc_retry_policy.h:108
virtual std::unique_ptr< RPCRetryPolicy > clone() const =0
Return a new copy of this object.
virtual void Setup(grpc::ClientContext &context) const =0
Update the ClientContext for the next call.
Contains all the Cloud Bigtable C++ client APIs.
Definition: admin_client.h:28
std::unique_ptr< RPCRetryPolicy > DefaultRPCRetryPolicy(internal::RPCPolicyParameters defaults)
Return an instance of the default RPCRetryPolicy.
google::cloud::Status MakeStatusFromRpcError(grpc::Status const &status)