Google Cloud Bigtable C++ Client  1.1.0
A C++ Client Library for Google Cloud Bigtable
client_options.cc
Go to the documentation of this file.
1 // Copyright 2017 Google Inc.
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 
14 #include "google/cloud/bigtable/client_options.h"
15 #include "google/cloud/bigtable/internal/client_options_defaults.h"
16 #include "google/cloud/internal/build_info.h"
17 #include "google/cloud/internal/getenv.h"
18 #include <thread>
19 
20 namespace {
21 std::shared_ptr<grpc::ChannelCredentials> BigtableDefaultCredentials() {
22  auto emulator = google::cloud::internal::GetEnv("BIGTABLE_EMULATOR_HOST");
23  if (emulator.has_value()) {
24  return grpc::InsecureChannelCredentials();
25  }
26  return grpc::GoogleDefaultCredentials();
27 }
28 } // anonymous namespace
29 
30 namespace google {
31 namespace cloud {
32 namespace bigtable {
33 inline namespace BIGTABLE_CLIENT_NS {
34 inline std::size_t CalculateDefaultConnectionPoolSize() {
35  // For batter resource utilization and greater throughput, it is recommended
36  // to calculate the default pool size based on cores(CPU) available. However,
37  // as per C++11 documentation `std::thread::hardware_concurrency()` cannot be
38  // fully rely upon, it is only a hint and the value can be 0 if it is not
39  // well defined or not computable. Apart from CPU count, multiple channels can
40  // be opened for each CPU to increase throughput.
41  std::size_t cpu_count = std::thread::hardware_concurrency();
42  return (cpu_count > 0) ? cpu_count * BIGTABLE_CLIENT_DEFAULT_CHANNELS_PER_CPU
43  : BIGTABLE_CLIENT_DEFAULT_CONNECTION_POOL_SIZE;
44 }
45 
46 ClientOptions::ClientOptions(std::shared_ptr<grpc::ChannelCredentials> creds)
48  connection_pool_size_(CalculateDefaultConnectionPoolSize()),
49  data_endpoint_("bigtable.googleapis.com"),
50  admin_endpoint_("bigtableadmin.googleapis.com"),
51  instance_admin_endpoint_("bigtableadmin.googleapis.com") {
52  static std::string const kUserAgentPrefix = UserAgentPrefix();
53  channel_arguments_.SetUserAgentPrefix(kUserAgentPrefix);
54  channel_arguments_.SetMaxSendMessageSize(
55  BIGTABLE_CLIENT_DEFAULT_MAX_MESSAGE_LENGTH);
56  channel_arguments_.SetMaxReceiveMessageSize(
57  BIGTABLE_CLIENT_DEFAULT_MAX_MESSAGE_LENGTH);
58 }
59 
61  auto emulator = google::cloud::internal::GetEnv("BIGTABLE_EMULATOR_HOST");
62  if (emulator.has_value()) {
63  data_endpoint_ = *emulator;
64  admin_endpoint_ = *emulator;
65  instance_admin_endpoint_ = *emulator;
66  }
67  auto instance_admin_emulator =
68  google::cloud::internal::GetEnv("BIGTABLE_INSTANCE_ADMIN_EMULATOR_HOST");
69  if (instance_admin_emulator.has_value()) {
70  instance_admin_endpoint_ = *instance_admin_emulator;
71  }
72 }
73 
74 // NOLINTNEXTLINE(readability-identifier-naming)
76  if (size == 0) {
77  connection_pool_size_ = BIGTABLE_CLIENT_DEFAULT_CONNECTION_POOL_SIZE;
78  return *this;
79  }
80  connection_pool_size_ = size;
81  return *this;
82 }
83 
85  std::string agent = "gcloud-cpp/" + version_string() + " " +
86  google::cloud::internal::compiler();
87  return agent;
88 }
89 
90 } // namespace BIGTABLE_CLIENT_NS
91 } // namespace bigtable
92 } // namespace cloud
93 } // namespace google
static std::string UserAgentPrefix()
Return the user agent prefix used by the library.
#define BIGTABLE_CLIENT_NS
Definition: version.h:22
ClientOptions(std::shared_ptr< grpc::ChannelCredentials > creds)
Connect to the production instance of Cloud Bigtable using creds.
ClientOptions()
Initialize the client options with the default credentials.
std::size_t CalculateDefaultConnectionPoolSize()
ClientOptions & set_connection_pool_size(std::size_t size)
Configuration options for the Bigtable Client.
Contains all the Cloud Bigtable C++ client APIs.