|
| 1 | +// Copyright 2021 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_INTERNAL_ASYNC_LONG_RUNNING_OPERATION_H |
| 16 | +#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_ASYNC_LONG_RUNNING_OPERATION_H |
| 17 | + |
| 18 | +#include "google/cloud/backoff_policy.h" |
| 19 | +#include "google/cloud/completion_queue.h" |
| 20 | +#include "google/cloud/future.h" |
| 21 | +#include "google/cloud/internal/async_polling_loop.h" |
| 22 | +#include "google/cloud/internal/async_retry_loop.h" |
| 23 | +#include "google/cloud/polling_policy.h" |
| 24 | +#include "google/cloud/status_or.h" |
| 25 | +#include "google/cloud/version.h" |
| 26 | +#include "absl/functional/function_ref.h" |
| 27 | +#include <google/longrunning/operations.pb.h> |
| 28 | +#include <grpcpp/grpcpp.h> |
| 29 | +#include <functional> |
| 30 | +#include <memory> |
| 31 | +#include <string> |
| 32 | + |
| 33 | +namespace google { |
| 34 | +namespace cloud { |
| 35 | +inline namespace GOOGLE_CLOUD_CPP_NS { |
| 36 | +namespace internal { |
| 37 | + |
| 38 | +/// Extracts the value (or error) from a completed long-running operation |
| 39 | +Status ExtractOperationResultImpl( |
| 40 | + StatusOr<google::longrunning::Operation> op, |
| 41 | + google::protobuf::Message& result, |
| 42 | + absl::FunctionRef<bool(google::protobuf::Any const&)> validate_any, |
| 43 | + std::string const& location); |
| 44 | + |
| 45 | +/** |
| 46 | + * Extracts the value from a completed long-running operation. |
| 47 | + * |
| 48 | + * This helper is used in `AsyncLongRunningOperation()` to extract the value (or |
| 49 | + * error) from a completed long-running operation. |
| 50 | + */ |
| 51 | +template <typename ReturnType> |
| 52 | +StatusOr<ReturnType> ExtractLongRunningResult( |
| 53 | + StatusOr<google::longrunning::Operation> op, std::string const& location) { |
| 54 | + ReturnType result; |
| 55 | + auto status = ExtractOperationResultImpl( |
| 56 | + std::move(op), result, |
| 57 | + [](google::protobuf::Any const& any) { return any.Is<ReturnType>(); }, |
| 58 | + location); |
| 59 | + if (!status.ok()) return status; |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Asynchronously starts and polls a long-running operation. |
| 65 | + * |
| 66 | + * Long-running operations [aip/151] are used for API methods that take a |
| 67 | + * significant amount of time to complete (think minutes, maybe an hour). The |
| 68 | + * gRPC API returns a "promise" object, represented by the |
| 69 | + * `google::longrunning::Operation` proto, and the application (or client |
| 70 | + * library) should periodically poll this object until it is "done". |
| 71 | + * |
| 72 | + * In the C++ client libraries we represent these long-running operations by |
| 73 | + * a member function that returns `future<StatusOr<ReturnType>>`. This function |
| 74 | + * is a helper to implement these member functions. It first starts the |
| 75 | + * operation using an asynchronous retry loop, and then starts an asynchronous |
| 76 | + * loop to poll the operation until it completes. |
| 77 | + * |
| 78 | + * The promise can complete with an error, which is represented by a |
| 79 | + * `google::cloud::Status` object, or with success and some `ReturnType` value. |
| 80 | + * The application may also configure the "polling policy", which may stop the |
| 81 | + * polling even though the operation has not completed. |
| 82 | + * |
| 83 | + * Library developers would use this function as follows: |
| 84 | + * |
| 85 | + * @code |
| 86 | + * class BarStub { |
| 87 | + * public: |
| 88 | + * virtual future<StatusOr<google::longrunning::Operation>> AsyncFoo( |
| 89 | + * google::cloud::CompletionQueue& cq, |
| 90 | + * std::unique_ptr<grpc::ClientContext> context, |
| 91 | + * FooRequest const& request) = 0; |
| 92 | + * |
| 93 | + * virtual future<StatusOr<google::longrunning::Operation>> AsyncGetOperation( |
| 94 | + * google::cloud::CompletionQueue& cq, |
| 95 | + * std::unique_ptr<grpc::ClientContext> context, |
| 96 | + * google::longrunning::GetOperationRequest const& request) = 0; |
| 97 | + * }; |
| 98 | + * @endcode |
| 99 | + * |
| 100 | + * The corresponding `*ConnectionImpl` class would look as follows: |
| 101 | + * |
| 102 | + * @code |
| 103 | + * class BarConnectionImpl : public BarConnection { |
| 104 | + * public: |
| 105 | + * // Using C++14 for exposition purposes. The implementation supports C++11. |
| 106 | + * future<StatusOr<FooResponse>> Foo(FooRequest const& request) override { |
| 107 | + * return google::cloud::internal::AsyncLongRunningOperation( |
| 108 | + * cq_, request, |
| 109 | + * [stub = stub_](auto& cq, auto context, auto const& request) { |
| 110 | + * return stub->AsyncFoo(cq, std::move(context), request); |
| 111 | + * }, |
| 112 | + * [stub = stub_](auto& cq, auto context, auto const& request) { |
| 113 | + * return stub->AsyncGetOperation(cq, std::move(context), request); |
| 114 | + * }, |
| 115 | + * retry_policy_->clone(), backoff_policy_->clone(), |
| 116 | + * IdempotencyPolicy::kIdempotent, |
| 117 | + * polling_policy_->clone(), |
| 118 | + * __func__ // for debugging |
| 119 | + * ); |
| 120 | + * } |
| 121 | + * |
| 122 | + * private: |
| 123 | + * google::cloud::CompletionQueue cq_; |
| 124 | + * std::shared_ptr<BarStub> stub_; |
| 125 | + * }; |
| 126 | + * @endcode |
| 127 | + * |
| 128 | + * [aip/151]: https://google.aip.dev/151 |
| 129 | + */ |
| 130 | +template <typename ReturnType, typename RequestType, typename StartFunctor, |
| 131 | + typename RetryPolicyType> |
| 132 | +future<StatusOr<ReturnType>> AsyncLongRunningOperation( |
| 133 | + google::cloud::CompletionQueue cq, RequestType&& request, |
| 134 | + StartFunctor&& start, AsyncPollLongRunningOperation poll, |
| 135 | + std::unique_ptr<RetryPolicyType> retry_policy, |
| 136 | + std::unique_ptr<BackoffPolicy> backoff_policy, Idempotency idempotent, |
| 137 | + std::unique_ptr<PollingPolicy> polling_policy, char const* location) { |
| 138 | + auto operation = |
| 139 | + AsyncRetryLoop(std::move(retry_policy), std::move(backoff_policy), |
| 140 | + idempotent, cq, std::forward<StartFunctor>(start), |
| 141 | + std::forward<RequestType>(request), location); |
| 142 | + struct MoveCapture { |
| 143 | + google::cloud::CompletionQueue cq; |
| 144 | + AsyncPollLongRunningOperation poll; |
| 145 | + std::unique_ptr<PollingPolicy> polling_policy; |
| 146 | + std::string location; |
| 147 | + |
| 148 | + future<StatusOr<ReturnType>> operator()( |
| 149 | + future<StatusOr<google::longrunning::Operation>> f) { |
| 150 | + auto op = f.get(); |
| 151 | + if (!op) { |
| 152 | + return make_ready_future(StatusOr<ReturnType>(std::move(op).status())); |
| 153 | + } |
| 154 | + auto loc = this->location; |
| 155 | + return AsyncPollingLoop(std::move(cq), *std::move(op), std::move(poll), |
| 156 | + std::move(polling_policy), std::move(location)) |
| 157 | + .then([loc](future<StatusOr<google::longrunning::Operation>> g) { |
| 158 | + return ExtractLongRunningResult<ReturnType>(g.get(), loc); |
| 159 | + }); |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + return operation.then(MoveCapture{std::move(cq), std::move(poll), |
| 164 | + std::move(polling_policy), |
| 165 | + std::string{location}}); |
| 166 | +} |
| 167 | + |
| 168 | +} // namespace internal |
| 169 | +} // namespace GOOGLE_CLOUD_CPP_NS |
| 170 | +} // namespace cloud |
| 171 | +} // namespace google |
| 172 | + |
| 173 | +#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_ASYNC_LONG_RUNNING_OPERATION_H |
0 commit comments