Skip to content

Commit b711471

Browse files
authored
feat(bigtable): add Make(Data,Admin,InstanceAdmin)Client methods that take Options (#7226)
1 parent 659c664 commit b711471

13 files changed

Lines changed: 367 additions & 97 deletions

google/cloud/bigtable/admin_client.cc

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ class DefaultAdminClient : public google::cloud::bigtable::AdminClient {
136136
// Introduce an early `private:` section because this type is used to define
137137
// the public interface, it should not be part of the public interface.
138138
struct AdminTraits {
139-
static std::string const& Endpoint(
140-
google::cloud::bigtable::ClientOptions& options) {
141-
return options.admin_endpoint();
139+
static std::string const& Endpoint(Options const& options) {
140+
return options.get<AdminEndpointOption>();
142141
}
143142
};
144143

@@ -148,8 +147,7 @@ class DefaultAdminClient : public google::cloud::bigtable::AdminClient {
148147
public:
149148
using AdminStubPtr = Impl::StubPtr;
150149

151-
DefaultAdminClient(std::string project,
152-
google::cloud::bigtable::ClientOptions options)
150+
DefaultAdminClient(std::string project, Options options)
153151
: project_(std::move(project)), impl_(std::move(options)) {}
154152

155153
std::string const& project() const override { return project_; }
@@ -438,26 +436,36 @@ class DefaultAdminClient : public google::cloud::bigtable::AdminClient {
438436

439437
private:
440438
google::cloud::BackgroundThreadsFactory BackgroundThreadsFactory() override {
441-
return impl_.Options().background_threads_factory();
439+
return impl_.BackgroundThreadsFactory();
442440
}
443441

444442
std::string project_;
445443
Impl impl_;
446444
};
447445

448-
std::shared_ptr<AdminClient> CreateDefaultAdminClient(
449-
std::string project,
450-
ClientOptions options) { // NOLINT(performance-unnecessary-value-param)
451-
std::shared_ptr<AdminClient> client =
452-
std::make_shared<DefaultAdminClient>(std::move(project), options);
453-
if (options.tracing_enabled("rpc")) {
446+
std::shared_ptr<AdminClient> MakeAdminClient(std::string project,
447+
Options options) {
448+
options = internal::DefaultOptions(std::move(options));
449+
bool tracing_enabled = google::cloud::internal::Contains(
450+
options.get<TracingComponentsOption>(), "rpc");
451+
auto tracing_options = options.get<GrpcTracingOptionsOption>();
452+
453+
std::shared_ptr<AdminClient> client = std::make_shared<DefaultAdminClient>(
454+
std::move(project), std::move(options));
455+
if (tracing_enabled) {
454456
GCP_LOG(INFO) << "Enabled logging for gRPC calls";
455457
client = std::make_shared<internal::LoggingAdminClient>(
456-
std::move(client), options.tracing_options());
458+
std::move(client), std::move(tracing_options));
457459
}
458460
return client;
459461
}
460462

463+
std::shared_ptr<AdminClient> CreateDefaultAdminClient(std::string project,
464+
ClientOptions options) {
465+
return MakeAdminClient(std::move(project),
466+
internal::MakeOptions(std::move(options)));
467+
}
468+
461469
} // namespace BIGTABLE_CLIENT_NS
462470
} // namespace bigtable
463471
} // namespace cloud

google/cloud/bigtable/admin_client.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,16 @@ class AdminClient {
293293
//@}
294294
};
295295

296-
/// Create a new admin client configured via @p options.
296+
/// Create a new table admin client configured via @p options.
297+
std::shared_ptr<AdminClient> MakeAdminClient(std::string project,
298+
Options options = {});
299+
300+
/**
301+
* Create a new table admin client configured via @p options.
302+
*
303+
* @deprecated use the `MakeAdminClient` method which accepts
304+
* `google::cloud::Options` instead.
305+
*/
297306
std::shared_ptr<AdminClient> CreateDefaultAdminClient(std::string project,
298307
ClientOptions options);
299308

google/cloud/bigtable/admin_client_test.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/bigtable/admin_client.h"
16+
#include "google/cloud/bigtable/internal/logging_admin_client.h"
17+
#include "google/cloud/testing_util/scoped_environment.h"
1618
#include <gmock/gmock.h>
1719

1820
namespace google {
@@ -39,6 +41,34 @@ TEST(AdminClientTest, Default) {
3941
EXPECT_NE(channel0.get(), channel1.get());
4042
}
4143

44+
TEST(AdminClientTest, MakeClient) {
45+
auto admin_client =
46+
MakeAdminClient("test-project", Options{}.set<GrpcNumChannelsOption>(1));
47+
ASSERT_TRUE(admin_client);
48+
EXPECT_EQ("test-project", admin_client->project());
49+
50+
auto channel0 = admin_client->Channel();
51+
EXPECT_TRUE(channel0);
52+
53+
auto channel1 = admin_client->Channel();
54+
EXPECT_EQ(channel0.get(), channel1.get());
55+
56+
admin_client->reset();
57+
channel1 = admin_client->Channel();
58+
EXPECT_TRUE(channel1);
59+
EXPECT_NE(channel0.get(), channel1.get());
60+
}
61+
62+
TEST(AdminClientTest, Logging) {
63+
testing_util::ScopedEnvironment env("GOOGLE_CLOUD_CPP_ENABLE_TRACING", "rpc");
64+
65+
auto admin_client = MakeAdminClient("test-project");
66+
ASSERT_TRUE(admin_client);
67+
ASSERT_TRUE(
68+
dynamic_cast<internal::LoggingAdminClient const*>(admin_client.get()))
69+
<< "Should create LoggingAdminClient";
70+
}
71+
4272
} // namespace
4373
} // namespace BIGTABLE_CLIENT_NS
4474
} // namespace bigtable

google/cloud/bigtable/client_options.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ namespace google {
2525
namespace cloud {
2626
namespace bigtable {
2727
inline namespace BIGTABLE_CLIENT_NS {
28+
namespace internal {
29+
Options&& MakeOptions(ClientOptions&& o) {
30+
if (!o.connection_pool_name_.empty()) {
31+
o.opts_
32+
.lookup<GrpcChannelArgumentsOption>()["cbt-c++/connection-pool-name"] =
33+
std::move(o.connection_pool_name_);
34+
}
35+
return std::move(o.opts_);
36+
}
37+
} // namespace internal
2838

2939
ClientOptions::ClientOptions() : ClientOptions(Options{}) {}
3040

google/cloud/bigtable/client_options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ namespace google {
3636
namespace cloud {
3737
namespace bigtable {
3838
inline namespace BIGTABLE_CLIENT_NS {
39+
class ClientOptions;
3940
namespace internal {
4041
struct InstanceAdminTraits;
42+
Options&& MakeOptions(ClientOptions&& o);
4143
} // namespace internal
4244

4345
/**
@@ -465,6 +467,7 @@ class ClientOptions {
465467
private:
466468
friend struct internal::InstanceAdminTraits;
467469
friend struct ClientOptionsTestTraits;
470+
friend Options&& internal::MakeOptions(ClientOptions&&);
468471

469472
/// Return the current endpoint for instance admin RPCs.
470473
std::string const& instance_admin_endpoint() const {

0 commit comments

Comments
 (0)