Skip to content

Commit ce78c2f

Browse files
authored
impl(v3): add bespoke WaitForConsistency to bigtable table admin (#15917)
1 parent 74e98fc commit ce78c2f

11 files changed

Lines changed: 182 additions & 31 deletions

generator/generator_config.textproto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ service {
588588
{rpc_name: "BigtableTableAdmin.CheckConsistency", idempotency: IDEMPOTENT}
589589
]
590590
omit_repo_metadata: true
591+
bespoke_methods : [
592+
{
593+
name: "WaitForConsistency",
594+
return_type: "future<StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>",
595+
parameters: "(google::bigtable::admin::v2::CheckConsistencyRequest const& request, Options opts = {})"
596+
}
597+
]
591598
}
592599
593600
# Billing

google/cloud/bigtable/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ add_library(
8989
admin/internal/bigtable_table_admin_auth_decorator.h
9090
admin/internal/bigtable_table_admin_connection_impl.cc
9191
admin/internal/bigtable_table_admin_connection_impl.h
92+
admin/internal/bigtable_table_admin_connection_impl_bespoke.cc
9293
admin/internal/bigtable_table_admin_logging_decorator.cc
9394
admin/internal/bigtable_table_admin_logging_decorator.h
9495
admin/internal/bigtable_table_admin_metadata_decorator.cc

google/cloud/bigtable/admin/bigtable_table_admin_client.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,14 @@ BigtableTableAdminClient::AsyncCheckConsistency(
821821
return connection_->AsyncCheckConsistency(request);
822822
}
823823

824+
future<StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
825+
BigtableTableAdminClient::WaitForConsistency(
826+
google::bigtable::admin::v2::CheckConsistencyRequest const& request,
827+
Options opts) {
828+
internal::OptionsSpan span(internal::MergeOptions(std::move(opts), options_));
829+
return connection_->WaitForConsistency(request);
830+
}
831+
824832
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
825833
} // namespace bigtable_admin
826834
} // namespace cloud

google/cloud/bigtable/admin/bigtable_table_admin_client.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,40 @@ class BigtableTableAdminClient {
25812581
google::bigtable::admin::v2::CheckConsistencyRequest const& request,
25822582
Options opts = {});
25832583

2584+
// clang-format off
2585+
///
2586+
/// Polls a table until it is consistent or the RetryPolicy is exhausted based
2587+
/// on a consistency token, that is, if replication has caught up based on the
2588+
/// provided conditions specified in the token and the check request.
2589+
///
2590+
/// @param request Unary RPCs, such as the one wrapped by this
2591+
/// function, receive a single `request` proto message which includes all
2592+
/// the inputs for the RPC. In this case, the proto message is a
2593+
/// [google.bigtable.admin.v2.CheckConsistencyRequest].
2594+
/// Proto messages are converted to C++ classes by Protobuf, using the
2595+
/// [Protobuf mapping rules].
2596+
/// @param opts Optional. Override the class-level options, such as retry and
2597+
/// backoff policies.
2598+
/// @return the result of the RPC. The response message type
2599+
/// ([google.bigtable.admin.v2.CheckConsistencyResponse])
2600+
/// is mapped to a C++ class using the [Protobuf mapping rules].
2601+
/// If the request fails, the [`StatusOr`] contains the error details.
2602+
///
2603+
/// [Protobuf mapping rules]: https://protobuf.dev/reference/cpp/cpp-generated/
2604+
/// [input iterator requirements]: https://en.cppreference.com/w/cpp/named_req/InputIterator
2605+
/// [`std::string`]: https://en.cppreference.com/w/cpp/string/basic_string
2606+
/// [`future`]: @ref google::cloud::future
2607+
/// [`StatusOr`]: @ref google::cloud::StatusOr
2608+
/// [`Status`]: @ref google::cloud::Status
2609+
/// [google.bigtable.admin.v2.CheckConsistencyRequest]: @googleapis_reference_link{google/bigtable/admin/v2/bigtable_table_admin.proto#L909}
2610+
/// [google.bigtable.admin.v2.CheckConsistencyResponse]: @googleapis_reference_link{google/bigtable/admin/v2/bigtable_table_admin.proto#L948}
2611+
///
2612+
// clang-format on
2613+
future<StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
2614+
WaitForConsistency(
2615+
google::bigtable::admin::v2::CheckConsistencyRequest const& request,
2616+
Options opts = {});
2617+
25842618
private:
25852619
std::shared_ptr<BigtableTableAdminConnection> connection_;
25862620
Options options_;

google/cloud/bigtable/admin/bigtable_table_admin_connection.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ BigtableTableAdminConnection::AsyncCheckConsistency(
382382
Status(StatusCode::kUnimplemented, "not implemented"));
383383
}
384384

385+
future<StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
386+
BigtableTableAdminConnection::WaitForConsistency(
387+
google::bigtable::admin::v2::CheckConsistencyRequest const&) {
388+
return google::cloud::make_ready_future<
389+
StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>(
390+
Status(StatusCode::kUnimplemented, "not implemented"));
391+
}
392+
385393
std::shared_ptr<BigtableTableAdminConnection> MakeBigtableTableAdminConnection(
386394
Options options) {
387395
internal::CheckExpectedOptions<CommonOptionList, GrpcOptionList,

google/cloud/bigtable/admin/bigtable_table_admin_connection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ class BigtableTableAdminConnection {
359359
StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
360360
AsyncCheckConsistency(
361361
google::bigtable::admin::v2::CheckConsistencyRequest const& request);
362+
363+
virtual future<
364+
StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
365+
WaitForConsistency(
366+
google::bigtable::admin::v2::CheckConsistencyRequest const& request);
362367
};
363368

364369
/**

google/cloud/bigtable/admin/internal/bigtable_table_admin_connection_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ class BigtableTableAdminConnectionImpl
234234
google::bigtable::admin::v2::CheckConsistencyRequest const& request)
235235
override;
236236

237+
future<StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
238+
WaitForConsistency(google::bigtable::admin::v2::CheckConsistencyRequest const&
239+
request) override;
240+
237241
private:
238242
std::unique_ptr<google::cloud::BackgroundThreads> background_;
239243
std::shared_ptr<bigtable_admin_internal::BigtableTableAdminStub> stub_;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2026 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+
#include "google/cloud/bigtable/admin/internal/bigtable_table_admin_connection_impl.h"
16+
#include "google/cloud/common_options.h"
17+
#include "google/cloud/internal/async_retry_loop.h"
18+
#include <memory>
19+
#include <utility>
20+
21+
namespace google {
22+
namespace cloud {
23+
namespace bigtable_admin_internal {
24+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
25+
namespace {
26+
27+
std::unique_ptr<bigtable_admin::BigtableTableAdminRetryPolicy> retry_policy(
28+
Options const& options) {
29+
return options.get<bigtable_admin::BigtableTableAdminRetryPolicyOption>()
30+
->clone();
31+
}
32+
33+
std::unique_ptr<BackoffPolicy> backoff_policy(Options const& options) {
34+
return options.get<bigtable_admin::BigtableTableAdminBackoffPolicyOption>()
35+
->clone();
36+
}
37+
38+
std::unique_ptr<bigtable_admin::BigtableTableAdminConnectionIdempotencyPolicy>
39+
idempotency_policy(Options const& options) {
40+
return options
41+
.get<
42+
bigtable_admin::BigtableTableAdminConnectionIdempotencyPolicyOption>()
43+
->clone();
44+
}
45+
46+
} // namespace
47+
48+
future<StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse>>
49+
BigtableTableAdminConnectionImpl::WaitForConsistency(
50+
google::bigtable::admin::v2::CheckConsistencyRequest const& request) {
51+
auto current = google::cloud::internal::SaveCurrentOptions();
52+
auto request_copy = request;
53+
auto const idempotent =
54+
idempotency_policy(*current)->CheckConsistency(request_copy);
55+
auto retry = retry_policy(*current);
56+
auto backoff = backoff_policy(*current);
57+
auto attempt_predicate =
58+
[](StatusOr<google::bigtable::admin::v2::CheckConsistencyResponse> const&
59+
r) { return r.ok() && r->consistent(); };
60+
return google::cloud::internal::AsyncRetryLoop(
61+
std::move(retry), std::move(backoff), idempotent, background_->cq(),
62+
[stub = stub_](
63+
CompletionQueue& cq, std::shared_ptr<grpc::ClientContext> context,
64+
google::cloud::internal::ImmutableOptions options,
65+
google::bigtable::admin::v2::CheckConsistencyRequest const& request) {
66+
return stub->AsyncCheckConsistency(cq, std::move(context),
67+
std::move(options), request);
68+
},
69+
std::move(current), std::move(request_copy), __func__,
70+
std::move(attempt_predicate));
71+
}
72+
73+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
74+
} // namespace bigtable_admin_internal
75+
} // namespace cloud
76+
} // namespace google

google/cloud/bigtable/examples/table_admin_snippets.cc

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -633,30 +633,24 @@ void WaitForConsistencyCheck(
633633
using ::google::cloud::future;
634634
using ::google::cloud::Status;
635635
using ::google::cloud::StatusOr;
636-
[](cbta::BigtableTableAdminClient admin, std::string const& project_id,
636+
[](cbta::BigtableTableAdminClient, std::string const& project_id,
637637
std::string const& instance_id, std::string const& table_id) {
638+
auto client = cbta::BigtableTableAdminClient(
639+
cbta::MakeBigtableTableAdminConnection());
638640
std::string table_name = cbt::TableName(project_id, instance_id, table_id);
639641
StatusOr<google::bigtable::admin::v2::GenerateConsistencyTokenResponse>
640-
consistency_token = admin.GenerateConsistencyToken(table_name);
642+
consistency_token = client.GenerateConsistencyToken(table_name);
641643
if (!consistency_token) {
642644
throw std::move(consistency_token).status();
643645
}
644-
// Start a thread to perform the background work.
645-
CompletionQueue cq;
646-
std::thread cq_runner([&cq] { cq.Run(); });
647-
648-
std::string token = consistency_token->consistency_token();
649-
future<Status> consistent_future =
650-
cbta::AsyncWaitForConsistency(cq, admin, table_name, token);
651-
652-
// Simplify the example by blocking until the operation is done.
653-
Status status = consistent_future.get();
654-
if (!status.ok()) throw std::runtime_error(status.message());
646+
auto token = consistency_token->consistency_token();
647+
google::bigtable::admin::v2::CheckConsistencyRequest wait_request;
648+
wait_request.set_name(table_name);
649+
wait_request.set_consistency_token(token);
650+
auto consistency_future = client.WaitForConsistency(wait_request);
651+
auto consistency = consistency_future.get();
652+
if (!consistency) throw std::runtime_error(consistency.status().message());
655653
std::cout << "Table is consistent with token " << token << "\n";
656-
657-
// Shutdown the work queue and join the background thread
658-
cq.Shutdown();
659-
cq_runner.join();
660654
}
661655
//! [wait for consistency check]
662656
(std::move(admin), argv.at(0), argv.at(1), argv.at(2));

google/cloud/bigtable/google_cloud_cpp_bigtable.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ google_cloud_cpp_bigtable_srcs = [
163163
"admin/internal/bigtable_instance_admin_tracing_stub.cc",
164164
"admin/internal/bigtable_table_admin_auth_decorator.cc",
165165
"admin/internal/bigtable_table_admin_connection_impl.cc",
166+
"admin/internal/bigtable_table_admin_connection_impl_bespoke.cc",
166167
"admin/internal/bigtable_table_admin_logging_decorator.cc",
167168
"admin/internal/bigtable_table_admin_metadata_decorator.cc",
168169
"admin/internal/bigtable_table_admin_option_defaults.cc",

0 commit comments

Comments
 (0)