Skip to content

Commit 11169c3

Browse files
authored
feat(bigtable): add explicit instance MakeDataConnection (#16191)
1 parent 570927e commit 11169c3

22 files changed

Lines changed: 220 additions & 52 deletions

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ the APIs in these libraries are stable, and are ready for production use.
1616

1717
- [Agent Registry API](/google/cloud/agentregistry/README.md)
1818

19+
### [Bigtable](/google/cloud/bigtable/README.md)
20+
21+
- Explicit instance declaration is now encouraged during client initialization via a new overload of `MakeDataConnection` that takes a `std::vector<InstanceResource>`. Specifying the target instances at client startup enables optimizing connection pooling (pre-warming/priming channels) and telemetry.
22+
23+
```cpp
24+
#include "google/cloud/bigtable/data_connection.h"
25+
26+
namespace cbt = ::google::cloud::bigtable;
27+
28+
auto connection = cbt::MakeDataConnection(
29+
{cbt::InstanceResource(google::cloud::Project("my-project"), "my-instance")},
30+
google::cloud::Options{});
31+
```
32+
1933
## v3.6.0 - 2026-06
2034
2135
### New Libraries

google/cloud/bigtable/benchmarks/benchmark.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,13 @@ void Benchmark::DeleteTable() {
130130
Table Benchmark::MakeTable(Options connection_opts) const {
131131
auto connection_options = MergeOptions(std::move(connection_opts), opts_);
132132
auto table_opts = Options{}.set<AppProfileIdOption>(options_.app_profile_id);
133-
return Table(MakeDataConnection(std::move(connection_options)),
134-
TableResource(options_.project_id, options_.instance_id,
135-
options_.table_id),
136-
std::move(table_opts));
133+
return Table(
134+
MakeDataConnection({InstanceResource(Project(options_.project_id),
135+
options_.instance_id)},
136+
std::move(connection_options)),
137+
TableResource(options_.project_id, options_.instance_id,
138+
options_.table_id),
139+
std::move(table_opts));
137140
}
138141

139142
google::cloud::StatusOr<BenchmarkResult> Benchmark::PopulateTable() {

google/cloud/bigtable/benchmarks/embedded_server_test.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ TEST(EmbeddedServer, TableApply) {
7676
.set<GrpcCredentialOption>(grpc::InsecureChannelCredentials())
7777
.set<EndpointOption>(server->address());
7878

79-
Table table(MakeDataConnection(options),
79+
Table table(MakeDataConnection(
80+
{InstanceResource(Project("fake-project"), "fake-instance")},
81+
std::move(options)),
8082
TableResource("fake-project", "fake-instance", "fake-table"));
8183

8284
SingleRowMutation mutation("row1",
@@ -101,7 +103,9 @@ TEST(EmbeddedServer, TableBulkApply) {
101103
.set<GrpcCredentialOption>(grpc::InsecureChannelCredentials())
102104
.set<EndpointOption>(server->address());
103105

104-
Table table(MakeDataConnection(options),
106+
Table table(MakeDataConnection(
107+
{InstanceResource(Project("fake-project"), "fake-instance")},
108+
std::move(options)),
105109
TableResource("fake-project", "fake-instance", "fake-table"));
106110

107111
BulkMutation bulk;
@@ -128,7 +132,9 @@ TEST(EmbeddedServer, ReadRows1) {
128132
.set<GrpcCredentialOption>(grpc::InsecureChannelCredentials())
129133
.set<EndpointOption>(server->address());
130134

131-
Table table(MakeDataConnection(options),
135+
Table table(MakeDataConnection(
136+
{InstanceResource(Project("fake-project"), "fake-instance")},
137+
std::move(options)),
132138
TableResource("fake-project", "fake-instance", "fake-table"));
133139

134140
EXPECT_EQ(0, server->read_rows_count());
@@ -150,7 +156,9 @@ TEST(EmbeddedServer, ReadRows100) {
150156
.set<GrpcCredentialOption>(grpc::InsecureChannelCredentials())
151157
.set<EndpointOption>(server->address());
152158

153-
Table table(MakeDataConnection(options),
159+
Table table(MakeDataConnection(
160+
{InstanceResource(Project("fake-project"), "fake-instance")},
161+
std::move(options)),
154162
TableResource("fake-project", "fake-instance", "fake-table"));
155163

156164
EXPECT_EQ(0, server->read_rows_count());

google/cloud/bigtable/benchmarks/mutation_batcher_throughput_benchmark.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ int main(int argc, char* argv[]) {
201201

202202
auto table = cbt::Table(
203203
cbt::MakeDataConnection(
204+
{cbt::InstanceResource(google::cloud::Project(options->project_id),
205+
options->instance_id)},
204206
Options{}.set<google::cloud::GrpcBackgroundThreadPoolSizeOption>(
205207
options->max_batches)),
206208
cbt::TableResource(options->project_id, options->instance_id, *table_id));

google/cloud/bigtable/data_connection.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "google/cloud/internal/disable_deprecation_warnings.inc"
1516
#include "google/cloud/bigtable/data_connection.h"
1617
#include "google/cloud/bigtable/internal/bigtable_stub_factory.h"
1718
#include "google/cloud/bigtable/internal/data_connection_impl.h"
@@ -174,6 +175,13 @@ bigtable::RowStream DataConnection::ExecuteQuery(bigtable::ExecuteQueryParams) {
174175
Status(StatusCode::kUnimplemented, "not implemented")));
175176
}
176177

178+
std::shared_ptr<DataConnection> MakeDataConnection(
179+
std::vector<InstanceResource> instances, Options options) {
180+
options.set<experimental::InstanceChannelAffinityOption>(
181+
std::move(instances));
182+
return MakeDataConnection(std::move(options));
183+
}
184+
177185
std::shared_ptr<DataConnection> MakeDataConnection(Options options) {
178186
google::cloud::internal::CheckExpectedOptions<
179187
AppProfileIdOption, CommonOptionList, GrpcOptionList,

google/cloud/bigtable/data_connection.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_DATA_CONNECTION_H
1717

1818
#include "google/cloud/bigtable/filters.h"
19+
#include "google/cloud/bigtable/instance_resource.h"
1920
#include "google/cloud/bigtable/internal/bigtable_stub.h"
2021
#include "google/cloud/bigtable/mutation_branch.h"
2122
#include "google/cloud/bigtable/mutations.h"
@@ -33,6 +34,7 @@
3334
#include "google/cloud/stream_range.h"
3435
#include "google/cloud/version.h"
3536
#include <memory>
37+
#include <vector>
3638

3739
namespace google {
3840
namespace cloud {
@@ -168,6 +170,10 @@ class DataConnection {
168170
* Returns a `DataConnection` object that can be used for interacting with
169171
* the Cloud Bigtable Data API.
170172
*
173+
* Calling this function with a list of target @p instances automatically
174+
* enables dynamic channel pooling for those instances, allowing the connection
175+
* pools to scale dynamically based on outstanding RPC load.
176+
*
171177
* The returned connection object should not be used directly; instead it
172178
* should be given to a `Table` instance, and methods should be invoked on
173179
* `Table`.
@@ -186,9 +192,27 @@ class DataConnection {
186192
* `GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes` in the environment and unexpected
187193
* options will be logged.
188194
*
195+
* @param instances The target instances to connect to. Specifying target
196+
* instances enables dynamic channel pooling and other connection-level
197+
* optimizations.
198+
* @param options (optional) Configure the `DataConnection` created by this
199+
* function.
200+
*/
201+
std::shared_ptr<DataConnection> MakeDataConnection(
202+
std::vector<InstanceResource> instances, Options options = {});
203+
204+
/**
205+
* Returns a `DataConnection` object that can be used for interacting with
206+
* the Cloud Bigtable Data API.
207+
*
208+
* @deprecated Use the MakeDataConnection overload that accepts a vector of
209+
* instances.
210+
*
189211
* @param options (optional) Configure the `DataConnection` created by this
190212
* function.
191213
*/
214+
GOOGLE_CLOUD_CPP_DEPRECATED(
215+
"Use the MakeDataConnection overload that accepts a vector of instances.")
192216
std::shared_ptr<DataConnection> MakeDataConnection(Options options = {});
193217

194218
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

google/cloud/bigtable/data_connection_test.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "google/cloud/internal/disable_deprecation_warnings.inc"
1516
#include "google/cloud/bigtable/data_connection.h"
1617
#include "google/cloud/bigtable/internal/bigtable_stub_factory.h"
1718
#include "google/cloud/bigtable/options.h"
@@ -100,6 +101,22 @@ TEST(MakeDataConnection, TracingDisabled) {
100101
EXPECT_THAT(span_catcher->GetSpans(), IsEmpty());
101102
}
102103

104+
TEST(MakeDataConnection, WithInstances) {
105+
InstanceResource instance_a{Project("my-project"), "instance-a"};
106+
InstanceResource instance_b{Project("my-project"), "instance-b"};
107+
auto conn = MakeDataConnection(
108+
{instance_a, instance_b},
109+
TestOptions().set<AppProfileIdOption>("user-supplied"));
110+
auto options = conn->options();
111+
EXPECT_TRUE(options.has<DataBackoffPolicyOption>())
112+
<< "Options are not defaulted in MakeDataConnection()";
113+
EXPECT_EQ(options.get<AppProfileIdOption>(), "user-supplied")
114+
<< "User supplied Options are overridden in MakeDataConnection()";
115+
ASSERT_TRUE(options.has<experimental::InstanceChannelAffinityOption>());
116+
EXPECT_THAT(options.get<experimental::InstanceChannelAffinityOption>().size(),
117+
Eq(2));
118+
}
119+
103120
} // namespace
104121
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
105122
} // namespace bigtable

google/cloud/bigtable/examples/bigtable_examples_common.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "google/cloud/internal/disable_deprecation_warnings.inc"
1516
#include "google/cloud/bigtable/examples/bigtable_examples_common.h"
1617
#include "google/cloud/internal/getenv.h"
1718
#include "absl/strings/str_join.h"
@@ -53,7 +54,9 @@ Commands::value_type MakeCommandEntry(std::string const& name,
5354
throw Usage{std::move(os).str()};
5455
}
5556
google::cloud::bigtable::Table table(
56-
google::cloud::bigtable::MakeDataConnection(),
57+
google::cloud::bigtable::MakeDataConnection(
58+
{google::cloud::bigtable::InstanceResource(
59+
google::cloud::Project(argv[0]), argv[1])}),
5760
google::cloud::bigtable::TableResource(argv[0], argv[1], argv[2]));
5861
argv.erase(argv.begin(), argv.begin() + 3);
5962
function(table, argv);
@@ -118,7 +121,9 @@ Commands::value_type MakeCommandEntry(std::string const& name,
118121
throw Usage{std::move(os).str()};
119122
}
120123
google::cloud::bigtable::Table table(
121-
google::cloud::bigtable::MakeDataConnection(),
124+
google::cloud::bigtable::MakeDataConnection(
125+
{google::cloud::bigtable::InstanceResource(
126+
google::cloud::Project(argv[0]), argv[1])}),
122127
google::cloud::bigtable::TableResource(argv[0], argv[1], argv[2]));
123128
google::cloud::CompletionQueue cq;
124129
std::thread t([&cq] { cq.Run(); });

google/cloud/bigtable/examples/bigtable_hello_app_profile.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ void HelloWorldAppProfile(std::vector<std::string> const& argv) {
4343
std::string const& profile_id = argv[3];
4444

4545
// Create an object to access the Cloud Bigtable Data API.
46-
auto connection = cbt::MakeDataConnection();
46+
auto connection = cbt::MakeDataConnection(
47+
{cbt::InstanceResource(google::cloud::Project(project_id), instance_id)});
4748

4849
// Use the default profile to write some data.
4950
cbt::Table write(connection,

google/cloud/bigtable/examples/bigtable_hello_world.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ void BigtableHelloWorld(std::vector<std::string> const& argv) {
5252

5353
//! [connect data]
5454
// Create an object to access the Cloud Bigtable Data API.
55-
cbt::Table table(cbt::MakeDataConnection(),
55+
cbt::Table table(cbt::MakeDataConnection({cbt::InstanceResource(
56+
google::cloud::Project(project_id), instance_id)}),
5657
cbt::TableResource(project_id, instance_id, table_id));
5758
//! [connect data]
5859
//! [connect admin] [END bigtable_hw_connect]

0 commit comments

Comments
 (0)