|
| 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 | +// http://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_INTERNAL_ASYNC_SAMPLE_ROW_KEYS_H_ |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_ASYNC_SAMPLE_ROW_KEYS_H_ |
| 17 | + |
| 18 | +#include "google/cloud/bigtable/async_operation.h" |
| 19 | +#include "google/cloud/bigtable/bigtable_strong_types.h" |
| 20 | +#include "google/cloud/bigtable/completion_queue.h" |
| 21 | +#include "google/cloud/bigtable/data_client.h" |
| 22 | +#include "google/cloud/bigtable/idempotent_mutation_policy.h" |
| 23 | +#include "google/cloud/bigtable/internal/async_retry_op.h" |
| 24 | +#include "google/cloud/bigtable/internal/bulk_mutator.h" |
| 25 | +#include "google/cloud/bigtable/internal/row_key_sample.h" |
| 26 | +#include "google/cloud/bigtable/table_strong_types.h" |
| 27 | +#include "google/cloud/internal/invoke_result.h" |
| 28 | +#include "google/cloud/internal/make_unique.h" |
| 29 | + |
| 30 | +namespace google { |
| 31 | +namespace cloud { |
| 32 | +namespace bigtable { |
| 33 | +inline namespace BIGTABLE_CLIENT_NS { |
| 34 | +namespace internal { |
| 35 | + |
| 36 | +/** |
| 37 | + * A SampleRowKeys call bound with client, table and app_profile_id. |
| 38 | + * |
| 39 | + * It satisfies the requirements to be used as the `Operation` parameter in |
| 40 | + * `AsyncRetryOp`. |
| 41 | + * |
| 42 | + * It encapsulates calling this RPC and accumulates the result. In case of an |
| 43 | + * error, all partially accumulated data is dropped. |
| 44 | + */ |
| 45 | +class AsyncSampleRowKeys { |
| 46 | + public: |
| 47 | + using Request = google::bigtable::v2::SampleRowKeysRequest; |
| 48 | + using Response = std::vector<RowKeySample>; |
| 49 | + |
| 50 | + AsyncSampleRowKeys(std::shared_ptr<DataClient> client, |
| 51 | + bigtable::AppProfileId const& app_profile_id, |
| 52 | + bigtable::TableId const& table_name); |
| 53 | + |
| 54 | + /** Start the bound aynchronous request. |
| 55 | + * |
| 56 | + * @tparam Functor the type of the function-like object that will receive the |
| 57 | + * results. |
| 58 | + * |
| 59 | + * @tparam valid_callback_type a format parameter, uses `std::enable_if<>` to |
| 60 | + * disable this template if the functor does not match the expected |
| 61 | + * signature. |
| 62 | + * |
| 63 | + * @param cq the completion queue to run the asynchronous operations. |
| 64 | + * |
| 65 | + * @param context the gRPC context used for this request |
| 66 | + * |
| 67 | + * @param callback the functor which will be fired in an unspecified thread |
| 68 | + * once the response stream completes |
| 69 | + */ |
| 70 | + template <typename Functor, |
| 71 | + typename std::enable_if< |
| 72 | + google::cloud::internal::is_invocable<Functor, CompletionQueue&, |
| 73 | + grpc::Status&>::value, |
| 74 | + int>::type valid_callback_type = 0> |
| 75 | + void Start(CompletionQueue& cq, |
| 76 | + std::unique_ptr<grpc::ClientContext>&& context, |
| 77 | + Functor&& callback) { |
| 78 | + cq.MakeUnaryStreamRpc( |
| 79 | + *client_, &DataClient::AsyncSampleRowKeys, request_, std::move(context), |
| 80 | + [this](CompletionQueue&, const grpc::ClientContext&, |
| 81 | + google::bigtable::v2::SampleRowKeysResponse& response) { |
| 82 | + response_.emplace_back(RowKeySample{std::move(response.row_key()), |
| 83 | + response.offset_bytes()}); |
| 84 | + }, |
| 85 | + FinishedCallback<Functor>(*this, std::forward<Functor>(callback))); |
| 86 | + } |
| 87 | + |
| 88 | + Response AccumulatedResult() { return response_; } |
| 89 | + |
| 90 | + private: |
| 91 | + template <typename Functor, |
| 92 | + typename std::enable_if< |
| 93 | + google::cloud::internal::is_invocable<Functor, CompletionQueue&, |
| 94 | + grpc::Status&>::value, |
| 95 | + int>::type valid_callback_type = 0> |
| 96 | + struct FinishedCallback { |
| 97 | + FinishedCallback(AsyncSampleRowKeys& parent, Functor&& callback) |
| 98 | + : parent_(parent), callback_(callback) {} |
| 99 | + |
| 100 | + void operator()(CompletionQueue& cq, grpc::ClientContext& context, |
| 101 | + grpc::Status& status) { |
| 102 | + if (not status.ok()) { |
| 103 | + // The sample must be a consistent sample of the rows in the table. On |
| 104 | + // failure we must forget the previous responses and accumulate only |
| 105 | + // new values. |
| 106 | + parent_.response_ = Response(); |
| 107 | + } |
| 108 | + callback_(cq, status); |
| 109 | + } |
| 110 | + |
| 111 | + // The user of AsyncSampleRowKeys has to make sure that it is not destructed |
| 112 | + // before all callbacks return, so we have a guarantee that this reference |
| 113 | + // is valid for as long as we don't call callback_. |
| 114 | + AsyncSampleRowKeys& parent_; |
| 115 | + Functor callback_; |
| 116 | + }; |
| 117 | + |
| 118 | + private: |
| 119 | + std::shared_ptr<bigtable::DataClient> client_; |
| 120 | + Request request_; |
| 121 | + Response response_; |
| 122 | +}; |
| 123 | + |
| 124 | +/** |
| 125 | + * Perform an `AsyncSampleRowKeys` operation request with retries. |
| 126 | + * |
| 127 | + * @tparam Functor the type of the function-like object that will receive the |
| 128 | + * results. It must satisfy (using C++17 types): |
| 129 | + * static_assert(std::is_invocable_v< |
| 130 | + * Functor, CompletionQueue&, std::vector<RowKeySample>&, |
| 131 | + * grpc::Status&>); |
| 132 | + * |
| 133 | + * @tparam valid_callback_type a format parameter, uses `std::enable_if<>` to |
| 134 | + * disable this template if the functor does not match the expected |
| 135 | + * signature. |
| 136 | + */ |
| 137 | +template <typename Functor, |
| 138 | + typename std::enable_if< |
| 139 | + google::cloud::internal::is_invocable<Functor, CompletionQueue&, |
| 140 | + std::vector<RowKeySample>&, |
| 141 | + grpc::Status&>::value, |
| 142 | + int>::type valid_callback_type = 0> |
| 143 | +class AsyncRetrySampleRowKeys |
| 144 | + : public AsyncRetryOp<ConstantIdempotencyPolicy, Functor, |
| 145 | + AsyncSampleRowKeys> { |
| 146 | + public: |
| 147 | + AsyncRetrySampleRowKeys(char const* error_message, |
| 148 | + std::unique_ptr<RPCRetryPolicy> rpc_retry_policy, |
| 149 | + std::unique_ptr<RPCBackoffPolicy> rpc_backoff_policy, |
| 150 | + MetadataUpdatePolicy metadata_update_policy, |
| 151 | + std::shared_ptr<bigtable::DataClient> client, |
| 152 | + bigtable::AppProfileId const& app_profile_id, |
| 153 | + bigtable::TableId const& table_name, |
| 154 | + Functor&& callback) |
| 155 | + : AsyncRetryOp<ConstantIdempotencyPolicy, Functor, AsyncSampleRowKeys>( |
| 156 | + error_message, std::move(rpc_retry_policy), |
| 157 | + // BulkMutator is idempotent because it keeps track of idempotency |
| 158 | + // of the mutations it holds. |
| 159 | + std::move(rpc_backoff_policy), ConstantIdempotencyPolicy(true), |
| 160 | + std::move(metadata_update_policy), std::forward<Functor>(callback), |
| 161 | + AsyncSampleRowKeys(client, std::move(app_profile_id), |
| 162 | + std::move(table_name))) {} |
| 163 | +}; |
| 164 | + |
| 165 | +} // namespace internal |
| 166 | +} // namespace BIGTABLE_CLIENT_NS |
| 167 | +} // namespace bigtable |
| 168 | +} // namespace cloud |
| 169 | +} // namespace google |
| 170 | + |
| 171 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_INTERNAL_ASYNC_SAMPLE_ROW_KEYS_H_ |
0 commit comments