Google Cloud Bigtable C++ Client  1.40.2
A C++ Client Library for Google Cloud Bigtable
rpc_backoff_policy.h
Go to the documentation of this file.
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_BACKOFF_POLICY_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_RPC_BACKOFF_POLICY_H
17 
18 #include "google/cloud/bigtable/internal/rpc_policy_parameters.h"
19 #include "google/cloud/bigtable/version.h"
20 #include "google/cloud/internal/backoff_policy.h"
21 #include <grpcpp/grpcpp.h>
22 #include <chrono>
23 #include <memory>
24 
25 namespace google {
26 namespace cloud {
27 namespace bigtable {
29 /**
30  * Define the interface for controlling how the Bigtable client
31  * backsoff from failed RPC operations.
32  *
33  * The C++ client for Bigtable needs to hide partial and temporary
34  * failures from the application. However, we need to give the users
35  * enough flexibility to control how many attempts are made to reissue
36  * operations, how often these attempts are executed, and how to
37  * signal that an error has occurred.
38  *
39  * The application provides an instance of this class when the Table
40  * (or TableAdmin) object is created. This instance serves as a
41  * prototype to create new RPCBackoffPolicy objects of the same
42  * (dynamic) type and with the same initial state.
43  */
45  public:
46  virtual ~RPCBackoffPolicy() = default;
47 
48  /**
49  * Return a new copy of this object.
50  *
51  * Typically implemented as
52  * @code
53  * return std::unique_ptr<RPCRetryPolicy>(new Foo(*this));
54  * @endcode
55  */
56  virtual std::unique_ptr<RPCBackoffPolicy> clone() const = 0;
57 
58  /**
59  * Update the ClientContext for the next call.
60  */
61  virtual void Setup(grpc::ClientContext& context) const = 0;
62 
63  /**
64  * Return the delay after an RPC operation has completed.
65  *
66  * @return true the delay before trying the operation again.
67  * @param status the status returned by the last RPC operation.
68  */
69  virtual std::chrono::milliseconds OnCompletion(Status const& status) = 0;
70  // TODO(#2344) - remove ::grpc::Status version.
71  virtual std::chrono::milliseconds OnCompletion(grpc::Status const& s) = 0;
72 
73  std::chrono::milliseconds OnCompletion() { return OnCompletion(Status{}); }
74 };
75 
76 /// Return an instance of the default RPCBackoffPolicy.
78  internal::RPCPolicyParameters defaults);
79 
80 /**
81  * Implement a simple exponential backoff policy.
82  */
84  public:
85  // NOLINTNEXTLINE(google-explicit-constructor)
86  ExponentialBackoffPolicy(internal::RPCPolicyParameters defaults);
87  template <typename Rep1, typename Period1, typename Rep2, typename Period2>
88  ExponentialBackoffPolicy(std::chrono::duration<Rep1, Period1> initial_delay,
89  std::chrono::duration<Rep2, Period2> maximum_delay)
90  : initial_delay_(std::chrono::duration_cast<std::chrono::microseconds>(
91  initial_delay)),
92  maximum_delay_(std::chrono::duration_cast<std::chrono::microseconds>(
93  maximum_delay)),
94  impl_(initial_delay_ / 2, maximum_delay_, 2.0) {}
95 
96  std::unique_ptr<RPCBackoffPolicy> clone() const override;
97  void Setup(grpc::ClientContext& context) const override;
98  std::chrono::milliseconds OnCompletion(Status const& status) override;
99  // TODO(#2344) - remove ::grpc::Status version.
100  std::chrono::milliseconds OnCompletion(grpc::Status const& status) override;
101 
102  private:
103  std::chrono::microseconds initial_delay_;
104  std::chrono::microseconds maximum_delay_;
105 
106  using Impl = ::google::cloud::internal::ExponentialBackoffPolicy;
107  Impl impl_;
108 };
109 
111 } // namespace bigtable
112 namespace bigtable_internal {
114 
115 std::unique_ptr<internal::BackoffPolicy> MakeCommonBackoffPolicy(
116  std::unique_ptr<bigtable::RPCBackoffPolicy> policy);
117 
119 } // namespace bigtable_internal
120 } // namespace cloud
121 } // namespace google
122 
123 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_RPC_BACKOFF_POLICY_H