-
Notifications
You must be signed in to change notification settings - Fork 442
impl(bigtable): add RandomTwoLeastUsed stub decorator #16049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
scotthart
merged 1 commit into
googleapis:main
from
scotthart:bigtable_random_two_decorator
Mar 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
343 changes: 343 additions & 0 deletions
343
google/cloud/bigtable/internal/bigtable_random_two_least_used_decorator.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,343 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "google/cloud/bigtable/internal/bigtable_random_two_least_used_decorator.h" | ||
| #include "google/cloud/internal/async_streaming_read_rpc.h" | ||
| #include "google/cloud/internal/streaming_read_rpc.h" | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <utility> | ||
|
|
||
| namespace google { | ||
| namespace cloud { | ||
| namespace bigtable_internal { | ||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
| namespace { | ||
|
|
||
| template <typename T> | ||
| class StreamingReadRpcTracking | ||
| : public google::cloud::internal::StreamingReadRpc<T> { | ||
| public: | ||
| StreamingReadRpcTracking( | ||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc<T>> child, | ||
| std::function<void(void)> on_destruction) | ||
| : child_(std::move(child)), on_destruction_(std::move(on_destruction)) {} | ||
|
|
||
| ~StreamingReadRpcTracking() override { on_destruction_(); } | ||
|
|
||
| void Cancel() override { child_->Cancel(); } | ||
| std::optional<Status> Read(T* response) override { | ||
| return child_->Read(response); | ||
| } | ||
| RpcMetadata GetRequestMetadata() const override { | ||
| return child_->GetRequestMetadata(); | ||
| } | ||
|
|
||
| private: | ||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc<T>> child_; | ||
| std::function<void(void)> on_destruction_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class AsyncStreamingReadRpcTracking | ||
| : public google::cloud::internal::AsyncStreamingReadRpc<T> { | ||
| public: | ||
| AsyncStreamingReadRpcTracking( | ||
| std::unique_ptr<google::cloud::internal::AsyncStreamingReadRpc<T>> child, | ||
| std::function<void(void)> on_destruction) | ||
| : child_(std::move(child)), on_destruction_(std::move(on_destruction)) {} | ||
|
|
||
| ~AsyncStreamingReadRpcTracking() override { on_destruction_(); } | ||
|
|
||
| void Cancel() override { child_->Cancel(); } | ||
| future<bool> Start() override { return child_->Start(); } | ||
| future<std::optional<T>> Read() override { return child_->Read(); } | ||
| future<Status> Finish() override { return child_->Finish(); } | ||
| RpcMetadata GetRequestMetadata() const override { | ||
| return child_->GetRequestMetadata(); | ||
| } | ||
|
|
||
| private: | ||
| std::unique_ptr<google::cloud::internal::AsyncStreamingReadRpc<T>> child_; | ||
| std::function<void(void)> on_destruction_; | ||
| }; | ||
|
|
||
| template <typename Response> | ||
| Response UnaryHelper(std::shared_ptr<DynamicChannelPool<BigtableStub>>& pool, | ||
| std::function<Response(BigtableStub&)> fn) { | ||
| auto child = pool->GetChannelRandomTwoLeastUsed(); | ||
| auto stub = child->AcquireStub(); | ||
| auto result = fn(*stub); | ||
| child->ReleaseStub(); | ||
| return result; | ||
| } | ||
|
|
||
| template <typename Response> | ||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc<Response>> | ||
| StreamingHelper( | ||
| std::shared_ptr<DynamicChannelPool<BigtableStub>>& pool, | ||
| std::function<std::unique_ptr< | ||
| google::cloud::internal::StreamingReadRpc<Response>>(BigtableStub&)> | ||
| fn) { | ||
| auto child = pool->GetChannelRandomTwoLeastUsed(); | ||
| auto stub = child->AcquireStub(); | ||
| auto result = fn(*stub); | ||
| auto release_fn = [weak = child->MakeWeak()] { | ||
| auto child = weak.lock(); | ||
| if (child) child->ReleaseStub(); | ||
| }; | ||
| return std::make_unique<StreamingReadRpcTracking<Response>>( | ||
| std::move(result), std::move(release_fn)); | ||
| } | ||
|
|
||
| template <typename Response> | ||
| std::unique_ptr<google::cloud::internal::AsyncStreamingReadRpc<Response>> | ||
| AsyncStreamingHelper( | ||
| std::shared_ptr<DynamicChannelPool<BigtableStub>>& pool, | ||
| std::function<std::unique_ptr< | ||
| google::cloud::internal::AsyncStreamingReadRpc<Response>>( | ||
| BigtableStub&)> | ||
| fn) { | ||
| auto child = pool->GetChannelRandomTwoLeastUsed(); | ||
| auto stub = child->AcquireStub(); | ||
| auto result = fn(*stub); | ||
| auto release_fn = [weak = child->MakeWeak()] { | ||
| auto child = weak.lock(); | ||
| if (child) child->ReleaseStub(); | ||
| }; | ||
| return std::make_unique<AsyncStreamingReadRpcTracking<Response>>( | ||
| std::move(result), std::move(release_fn)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc< | ||
| google::bigtable::v2::ReadRowsResponse>> | ||
| BigtableRandomTwoLeastUsed::ReadRows( | ||
| std::shared_ptr<grpc::ClientContext> context, Options const& options, | ||
| google::bigtable::v2::ReadRowsRequest const& request) { | ||
| return StreamingHelper<google::bigtable::v2::ReadRowsResponse>( | ||
| pool_, [&, context = std::move(context)](BigtableStub& stub) mutable { | ||
| return stub.ReadRows(std::move(context), options, request); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc< | ||
| google::bigtable::v2::SampleRowKeysResponse>> | ||
| BigtableRandomTwoLeastUsed::SampleRowKeys( | ||
| std::shared_ptr<grpc::ClientContext> context, Options const& options, | ||
| google::bigtable::v2::SampleRowKeysRequest const& request) { | ||
| return StreamingHelper<google::bigtable::v2::SampleRowKeysResponse>( | ||
| pool_, [&, context = std::move(context)](BigtableStub& stub) mutable { | ||
| return stub.SampleRowKeys(std::move(context), options, request); | ||
| }); | ||
| } | ||
|
|
||
| StatusOr<google::bigtable::v2::MutateRowResponse> | ||
| BigtableRandomTwoLeastUsed::MutateRow( | ||
| grpc::ClientContext& context, Options const& options, | ||
| google::bigtable::v2::MutateRowRequest const& request) { | ||
| return UnaryHelper<StatusOr<google::bigtable::v2::MutateRowResponse>>( | ||
| pool_, [&](BigtableStub& stub) { | ||
| return stub.MutateRow(context, options, request); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc< | ||
| google::bigtable::v2::MutateRowsResponse>> | ||
| BigtableRandomTwoLeastUsed::MutateRows( | ||
| std::shared_ptr<grpc::ClientContext> context, Options const& options, | ||
| google::bigtable::v2::MutateRowsRequest const& request) { | ||
| return StreamingHelper<google::bigtable::v2::MutateRowsResponse>( | ||
| pool_, [&, context = std::move(context)](BigtableStub& stub) mutable { | ||
| return stub.MutateRows(std::move(context), options, request); | ||
| }); | ||
| } | ||
|
|
||
| StatusOr<google::bigtable::v2::CheckAndMutateRowResponse> | ||
| BigtableRandomTwoLeastUsed::CheckAndMutateRow( | ||
| grpc::ClientContext& context, Options const& options, | ||
| google::bigtable::v2::CheckAndMutateRowRequest const& request) { | ||
| return UnaryHelper<StatusOr<google::bigtable::v2::CheckAndMutateRowResponse>>( | ||
| pool_, [&](BigtableStub& stub) { | ||
| return stub.CheckAndMutateRow(context, options, request); | ||
| }); | ||
| } | ||
|
|
||
| StatusOr<google::bigtable::v2::PingAndWarmResponse> | ||
| BigtableRandomTwoLeastUsed::PingAndWarm( | ||
| grpc::ClientContext& context, Options const& options, | ||
| google::bigtable::v2::PingAndWarmRequest const& request) { | ||
| return UnaryHelper<StatusOr<google::bigtable::v2::PingAndWarmResponse>>( | ||
| pool_, [&](BigtableStub& stub) { | ||
| return stub.PingAndWarm(context, options, request); | ||
| }); | ||
| } | ||
|
|
||
| StatusOr<google::bigtable::v2::ReadModifyWriteRowResponse> | ||
| BigtableRandomTwoLeastUsed::ReadModifyWriteRow( | ||
| grpc::ClientContext& context, Options const& options, | ||
| google::bigtable::v2::ReadModifyWriteRowRequest const& request) { | ||
| return UnaryHelper< | ||
| StatusOr<google::bigtable::v2::ReadModifyWriteRowResponse>>( | ||
| pool_, [&](BigtableStub& stub) { | ||
| return stub.ReadModifyWriteRow(context, options, request); | ||
| }); | ||
| } | ||
|
|
||
| StatusOr<google::bigtable::v2::PrepareQueryResponse> | ||
| BigtableRandomTwoLeastUsed::PrepareQuery( | ||
| grpc::ClientContext& context, Options const& options, | ||
| google::bigtable::v2::PrepareQueryRequest const& request) { | ||
| return UnaryHelper<StatusOr<google::bigtable::v2::PrepareQueryResponse>>( | ||
| pool_, [&](BigtableStub& stub) { | ||
| return stub.PrepareQuery(context, options, request); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<google::cloud::internal::StreamingReadRpc< | ||
| google::bigtable::v2::ExecuteQueryResponse>> | ||
| BigtableRandomTwoLeastUsed::ExecuteQuery( | ||
| std::shared_ptr<grpc::ClientContext> context, Options const& options, | ||
| google::bigtable::v2::ExecuteQueryRequest const& request) { | ||
| return StreamingHelper<google::bigtable::v2::ExecuteQueryResponse>( | ||
| pool_, [&, context = std::move(context)](BigtableStub& stub) mutable { | ||
| return stub.ExecuteQuery(std::move(context), options, request); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<google::cloud::internal::AsyncStreamingReadRpc< | ||
| google::bigtable::v2::ReadRowsResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncReadRows( | ||
| google::cloud::CompletionQueue const& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::ReadRowsRequest const& request) { | ||
| return AsyncStreamingHelper<google::bigtable::v2::ReadRowsResponse>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncReadRows(cq, std::move(context), std::move(options), | ||
| request); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<google::cloud::internal::AsyncStreamingReadRpc< | ||
| google::bigtable::v2::SampleRowKeysResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncSampleRowKeys( | ||
| google::cloud::CompletionQueue const& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::SampleRowKeysRequest const& request) { | ||
| return AsyncStreamingHelper<google::bigtable::v2::SampleRowKeysResponse>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncSampleRowKeys(cq, std::move(context), | ||
| std::move(options), request); | ||
| }); | ||
| } | ||
|
|
||
| future<StatusOr<google::bigtable::v2::MutateRowResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncMutateRow( | ||
| google::cloud::CompletionQueue& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::MutateRowRequest const& request) { | ||
| return UnaryHelper<future<StatusOr<google::bigtable::v2::MutateRowResponse>>>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncMutateRow(cq, std::move(context), std::move(options), | ||
| request); | ||
| }); | ||
| } | ||
|
|
||
| std::unique_ptr<google::cloud::internal::AsyncStreamingReadRpc< | ||
| google::bigtable::v2::MutateRowsResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncMutateRows( | ||
| google::cloud::CompletionQueue const& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::MutateRowsRequest const& request) { | ||
| return AsyncStreamingHelper<google::bigtable::v2::MutateRowsResponse>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncMutateRows(cq, std::move(context), std::move(options), | ||
| request); | ||
| }); | ||
| } | ||
|
|
||
| future<StatusOr<google::bigtable::v2::CheckAndMutateRowResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncCheckAndMutateRow( | ||
| google::cloud::CompletionQueue& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::CheckAndMutateRowRequest const& request) { | ||
| return UnaryHelper< | ||
| future<StatusOr<google::bigtable::v2::CheckAndMutateRowResponse>>>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncCheckAndMutateRow(cq, std::move(context), | ||
| std::move(options), request); | ||
| }); | ||
| } | ||
|
|
||
| future<StatusOr<google::bigtable::v2::PingAndWarmResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncPingAndWarm( | ||
| google::cloud::CompletionQueue& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::PingAndWarmRequest const& request) { | ||
| return UnaryHelper< | ||
| future<StatusOr<google::bigtable::v2::PingAndWarmResponse>>>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncPingAndWarm(cq, std::move(context), std::move(options), | ||
| request); | ||
| }); | ||
| } | ||
|
|
||
| future<StatusOr<google::bigtable::v2::ReadModifyWriteRowResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncReadModifyWriteRow( | ||
| google::cloud::CompletionQueue& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::ReadModifyWriteRowRequest const& request) { | ||
| return UnaryHelper< | ||
| future<StatusOr<google::bigtable::v2::ReadModifyWriteRowResponse>>>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncReadModifyWriteRow(cq, std::move(context), | ||
| std::move(options), request); | ||
| }); | ||
| } | ||
|
|
||
| future<StatusOr<google::bigtable::v2::PrepareQueryResponse>> | ||
| BigtableRandomTwoLeastUsed::AsyncPrepareQuery( | ||
| google::cloud::CompletionQueue& cq, | ||
| std::shared_ptr<grpc::ClientContext> context, | ||
| google::cloud::internal::ImmutableOptions options, | ||
| google::bigtable::v2::PrepareQueryRequest const& request) { | ||
| return UnaryHelper< | ||
| future<StatusOr<google::bigtable::v2::PrepareQueryResponse>>>( | ||
| pool_, [&, context = std::move(context), | ||
| options = std::move(options)](BigtableStub& stub) mutable { | ||
| return stub.AsyncPrepareQuery(cq, std::move(context), | ||
| std::move(options), request); | ||
| }); | ||
| } | ||
|
|
||
| GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
| } // namespace bigtable_internal | ||
| } // namespace cloud | ||
| } // namespace google | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.