Google Cloud Bigtable C++ Client 2.13.0
A C++ Client Library for Google Cloud Bigtable
Loading...
Searching...
No Matches
polling_policy.h
1// Copyright 2018 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_BIGTABLE_POLLING_POLICY_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_POLLING_POLICY_H
17
18#include "google/cloud/bigtable/rpc_backoff_policy.h"
19#include "google/cloud/bigtable/rpc_retry_policy.h"
20#include "google/cloud/bigtable/version.h"
21#include "google/cloud/grpc_error_delegate.h"
22#include "google/cloud/polling_policy.h"
23#include <grpcpp/grpcpp.h>
24#include <chrono>
25
26namespace google {
27namespace cloud {
28namespace bigtable {
29GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
30/**
31 * Define the interface for providing asynchronous repetitive call rules
32 *
33 */
34class PollingPolicy {
35 public:
36 virtual ~PollingPolicy() = default;
37
38 /**
39 * Return a new copy of this object.
40 *
41 * Typically implemented as
42 * @code
43 * return std::unique_ptr<PollingPolicy>(new Foo(*this));
44 * @endcode
45 */
46 virtual std::unique_ptr<PollingPolicy> clone() const = 0;
47
48 virtual void Setup(grpc::ClientContext& context) = 0;
49
50 /**
51 * Return true if `status` represents a permanent error that cannot be
52 * retried.
53 * TODO(#2344): remove `grpc::Status` version.
54 */
55 virtual bool IsPermanentError(grpc::Status const& status) {
57 }
58
59 /**
60 * Return true if `status` represents a permanent error that cannot be
61 * retried.
62 */
63 virtual bool IsPermanentError(Status const& status) = 0;
64
65 /**
66 * Handle an RPC failure.
67 * TODO(#2344): remove `grpc::Status` version.
68 *
69 * @return true if the RPC operation should be retried.
70 */
71 virtual bool OnFailure(grpc::Status const& status) {
73 }
74
75 /**
76 * Handle an RPC failure.
77 *
78 * @return true if the RPC operation should be retried.
79 */
80 virtual bool OnFailure(Status const& status) = 0;
81
82 /**
83 * Return true if we cannot try again.
84 */
85 virtual bool Exhausted() = 0;
86
87 /**
88 * Return for how long we should wait before trying again.
89 */
90 virtual std::chrono::milliseconds WaitPeriod() = 0;
91};
92
93/**
94 * Construct a polling policy from existing Retry and Backoff policies.
95 *
96 * A polling policy can be built by composing a retry and backoff policy. For
97 * example, to create a polling policy that "retries N times, waiting a fixed
98 * period between retries" you could compose the "try N times" retry policy with
99 * the "wait a fixed period between retries".
100 *
101 * This class makes it easier to create such composed polling policies.
102 *
103 * @tparam Retry the RPC retry strategy used to limit the number or the total
104 * duration of the polling strategy.
105 * @tparam Backoff the RPC backoff strategy used to control how often the
106 * library polls.
107 */
108template <typename Retry = LimitedTimeRetryPolicy,
109 typename Backoff = ExponentialBackoffPolicy>
110class GenericPollingPolicy : public PollingPolicy {
111 static_assert(std::is_base_of<RPCRetryPolicy, Retry>::value,
112 "Retry must derive from RPCRetryPolicy");
113 static_assert(std::is_base_of<RPCBackoffPolicy, Backoff>::value,
114 "Backoff must derive from RPCBackoffPolicy");
115
116 public:
117 explicit GenericPollingPolicy(internal::RPCPolicyParameters defaults)
118 : retry_prototype_(Retry(defaults)),
119 backoff_prototype_(Backoff(defaults)),
120 retry_clone_(retry_prototype_.clone()),
121 backoff_clone_(backoff_prototype_.clone()) {}
122 GenericPollingPolicy(Retry retry, Backoff backoff)
123 : retry_prototype_(std::move(retry)),
124 backoff_prototype_(std::move(backoff)),
125 retry_clone_(retry_prototype_.clone()),
126 backoff_clone_(backoff_prototype_.clone()) {}
127
128 std::unique_ptr<PollingPolicy> clone() const override {
129 return std::unique_ptr<PollingPolicy>(
130 new GenericPollingPolicy<Retry, Backoff>(retry_prototype_,
131 backoff_prototype_));
132 }
133
134 void Setup(grpc::ClientContext& context) override {
135 retry_clone_->Setup(context);
136 backoff_clone_->Setup(context);
137 }
138
139 bool IsPermanentError(Status const& status) override {
141 }
142
143 bool OnFailure(Status const& status) override {
144 return retry_clone_->OnFailure(status);
145 }
146
147 bool Exhausted() override { return !OnFailure(Status()); }
148
149 std::chrono::milliseconds WaitPeriod() override {
150 return backoff_clone_->OnCompletion(grpc::Status::OK);
151 }
152
153 private:
154 Retry retry_prototype_;
155 Backoff backoff_prototype_;
156
157 std::unique_ptr<RPCRetryPolicy> retry_clone_;
158 std::unique_ptr<RPCBackoffPolicy> backoff_clone_;
159};
160
161std::unique_ptr<PollingPolicy> DefaultPollingPolicy(
162 internal::RPCPolicyParameters defaults);
163
164GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
165} // namespace bigtable
166namespace bigtable_internal {
167GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
168
169std::unique_ptr<PollingPolicy> MakeCommonPollingPolicy(
170 std::unique_ptr<bigtable::PollingPolicy> policy);
171
172GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
173} // namespace bigtable_internal
174} // namespace cloud
175} // namespace google
176
177#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_POLLING_POLICY_H
Implement a simple exponential backoff policy.
Definition: rpc_backoff_policy.h:83
Construct a polling policy from existing Retry and Backoff policies.
Definition: polling_policy.h:110
bool OnFailure(Status const &status) override
Handle an RPC failure.
Definition: polling_policy.h:143
void Setup(grpc::ClientContext &context) override
Definition: polling_policy.h:134
std::chrono::milliseconds WaitPeriod() override
Return for how long we should wait before trying again.
Definition: polling_policy.h:149
std::unique_ptr< PollingPolicy > clone() const override
Return a new copy of this object.
Definition: polling_policy.h:128
bool Exhausted() override
Return true if we cannot try again.
Definition: polling_policy.h:147
GenericPollingPolicy(internal::RPCPolicyParameters defaults)
Definition: polling_policy.h:117
GenericPollingPolicy(Retry retry, Backoff backoff)
Definition: polling_policy.h:122
bool IsPermanentError(Status const &status) override
Return true if status represents a permanent error that cannot be retried.
Definition: polling_policy.h:139
Implement a simple "keep trying for this time" retry policy.
Definition: rpc_retry_policy.h:153
Define the interface for providing asynchronous repetitive call rules.
Definition: polling_policy.h:34
virtual bool OnFailure(Status const &status)=0
Handle an RPC failure.
virtual bool Exhausted()=0
Return true if we cannot try again.
virtual bool IsPermanentError(Status const &status)=0
Return true if status represents a permanent error that cannot be retried.
virtual bool IsPermanentError(grpc::Status const &status)
Return true if status represents a permanent error that cannot be retried.
Definition: polling_policy.h:55
virtual std::unique_ptr< PollingPolicy > clone() const =0
Return a new copy of this object.
virtual bool OnFailure(grpc::Status const &status)
Handle an RPC failure.
Definition: polling_policy.h:71
virtual std::chrono::milliseconds WaitPeriod()=0
Return for how long we should wait before trying again.
virtual void Setup(grpc::ClientContext &context)=0
Define the interface for controlling how the Bigtable client backsoff from failed RPC operations.
Definition: rpc_backoff_policy.h:44
virtual std::chrono::milliseconds OnCompletion(grpc::Status const &s)=0
virtual void Setup(grpc::ClientContext &context) const =0
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(Status const &status)=0
Handle an RPC failure.
static bool IsPermanentFailure(Status const &status)
Definition: rpc_retry_policy.h:108
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< PollingPolicy > DefaultPollingPolicy(internal::RPCPolicyParameters defaults)
google::cloud::Status MakeStatusFromRpcError(grpc::Status const &status)