15 #include "google/cloud/spanner/client.h"
16 #include "google/cloud/spanner/internal/connection_impl.h"
17 #include "google/cloud/spanner/internal/spanner_stub.h"
18 #include "google/cloud/spanner/internal/status_utils.h"
19 #include "google/cloud/spanner/options.h"
20 #include "google/cloud/spanner/retry_policy.h"
21 #include "google/cloud/spanner/transaction.h"
22 #include "google/cloud/backoff_policy.h"
23 #include "google/cloud/internal/getenv.h"
24 #include "google/cloud/internal/retry_loop.h"
25 #include "google/cloud/log.h"
26 #include <grpcpp/grpcpp.h>
35 std::vector<std::string> columns,
37 return conn_->
Read({spanner_internal::MakeSingleUseTransaction(
42 std::move(read_options),
47 std::string table,
KeySet keys,
48 std::vector<std::string> columns,
50 return conn_->
Read({spanner_internal::MakeSingleUseTransaction(
51 std::move(transaction_options)),
55 std::move(read_options),
60 std::vector<std::string> columns,
62 return conn_->
Read({std::move(transaction),
66 std::move(read_options),
71 return conn_->
Read(spanner_internal::MakeReadParams(read_partition)
);
76 std::vector<std::string> columns,
ReadOptions read_options,
82 std::move(read_options),
89 return conn_->
ExecuteQuery({spanner_internal::MakeSingleUseTransaction(
92 OverlayQueryOptions(opts),
99 return conn_->
ExecuteQuery({spanner_internal::MakeSingleUseTransaction(
100 std::move(transaction_options)),
101 std::move(statement),
102 OverlayQueryOptions(opts),
109 std::move(statement),
110 OverlayQueryOptions(opts),
116 auto params = spanner_internal::MakeSqlParams(partition);
123 return conn_->
ProfileQuery({spanner_internal::MakeSingleUseTransaction(
125 std::move(statement),
126 OverlayQueryOptions(opts),
133 return conn_->
ProfileQuery({spanner_internal::MakeSingleUseTransaction(
134 std::move(transaction_options)),
135 std::move(statement),
136 OverlayQueryOptions(opts),
144 std::move(statement),
145 OverlayQueryOptions(opts),
153 {std::move(transaction), std::move(statement), partition_options}
);
160 std::move(statement),
161 OverlayQueryOptions(opts),
169 std::move(statement),
170 OverlayQueryOptions(opts),
178 std::move(statement),
179 OverlayQueryOptions(opts),
186 internal::CheckExpectedOptions<RequestOptionList>(opts,
__func__);
188 {std::move(transaction), std::move(statements), std::move(opts)}
);
192 std::function<StatusOr<Mutations>(
Transaction)>
const& mutator,
193 std::unique_ptr<TransactionRerunPolicy> rerun_policy,
194 std::unique_ptr<BackoffPolicy> backoff_policy,
197 using RerunnablePolicy = spanner_internal::SafeTransactionRerun;
202 for (
int rerun = 0;; ++rerun) {
203 StatusOr<Mutations> mutations;
204 #if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
207 mutations
= mutator(txn);
208 #if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
218 if (!RerunnablePolicy::IsOk(rb_status)) {
219 GCP_LOG(WARNING) <<
"Rollback() failure in Client::Commit(): "
226 if (RerunnablePolicy::IsOk(status)) {
229 if (!RerunnablePolicy::IsTransientFailure(status)) {
233 if (!RerunnablePolicy::IsTransientFailure(status)) {
235 if (!RerunnablePolicy::IsOk(rb_status)) {
236 GCP_LOG(WARNING) <<
"Rollback() failure in Client::Commit(): "
243 if (!rerun_policy->OnFailure(status)) {
246 if (spanner_internal::IsSessionNotFound(status)) {
248 spanner_internal::Visit(
249 txn, [](spanner_internal::SessionHolder& s,
250 StatusOr<
google::spanner::v1::TransactionSelector>
const&,
251 std::string
const&, std::int64_t) {
261 std::this_thread::sleep_for(backoff_policy->OnCompletion());
266 std::function<StatusOr<Mutations>(
Transaction)>
const& mutator,
268 auto const rerun_maximum_duration = std::chrono::minutes(10);
269 auto default_commit_rerun_policy =
270 LimitedTimeTransactionRerunPolicy(rerun_maximum_duration).clone();
272 auto const backoff_initial_delay = std::chrono::milliseconds(100);
273 auto const backoff_maximum_delay = std::chrono::minutes(5);
274 auto const backoff_scaling = 2.0;
275 auto default_commit_backoff_policy =
276 ExponentialBackoffPolicy(backoff_initial_delay, backoff_maximum_delay,
280 return Commit(mutator
, std::move(default_commit_rerun_policy)
,
281 std::move(default_commit_backoff_policy)
, options
);
293 return conn_->
Commit({std::move(transaction), std::move(mutations), options}
);
297 return conn_->
Rollback({std::move(transaction)}
);
303 {std::move(statement), OverlayQueryOptions(opts)}
);
315 static auto const*
const kOptimizerVersionEnvValue =
316 new auto(
google::
cloud::internal::GetEnv(
"SPANNER_OPTIMIZER_VERSION"));
317 static auto const*
const kOptimizerStatisticsPackageEnvValue =
new auto(
318 google::
cloud::internal::GetEnv(
"SPANNER_OPTIMIZER_STATISTICS_PACKAGE"));
320 return spanner_internal::OverlayQueryOptions(
322 *kOptimizerStatisticsPackageEnvValue);
327 internal::CheckExpectedOptions<
328 CommonOptionList, GrpcOptionList, SessionPoolOptionList,
329 spanner_internal::SessionPoolClockOption, SpannerPolicyOptionList>(
331 opts
= spanner_internal::DefaultOptions(std::move(opts));
333 std::vector<std::shared_ptr<spanner_internal::SpannerStub>> stubs(
336 std::generate(stubs.begin(), stubs.end(), [&id, db, opts] {
337 return spanner_internal::CreateDefaultSpannerStub(db, opts, id++);
339 return std::make_shared<spanner_internal::ConnectionImpl>(
340 std::move(db), std::move(stubs), opts);
344 Database const& db, ConnectionOptions
const& connection_options,
346 auto opts = internal::MergeOptions(
347 internal::MakeOptions(connection_options),
348 spanner_internal::MakeOptions(std::move(session_pool_options)));
353 Database const& db, ConnectionOptions
const& connection_options,
355 std::unique_ptr<RetryPolicy> retry_policy,
356 std::unique_ptr<BackoffPolicy> backoff_policy) {
357 auto opts = internal::MergeOptions(
358 internal::MakeOptions(connection_options),
359 spanner_internal::MakeOptions(std::move(session_pool_options)));
368 namespace spanner_internal {
376 absl::optional<std::string>
const& optimizer_version_env,
377 absl::optional<std::string>
const& optimizer_statistics_package_env) {
385 }
else if (optimizer_version_env.has_value()) {
396 }
else if (optimizer_statistics_package_env.has_value()) {