-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathtable.cc
585 lines (531 loc) · 22.9 KB
/
table.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "google/cloud/bigtable/table.h"
#include "google/cloud/bigtable/internal/bulk_mutator.h"
#include "google/cloud/bigtable/internal/data_connection_impl.h"
#include "google/cloud/bigtable/internal/legacy_async_bulk_apply.h"
#include "google/cloud/bigtable/internal/legacy_async_row_sampler.h"
#include "google/cloud/bigtable/internal/legacy_row_reader.h"
#include "google/cloud/bigtable/internal/unary_client_utils.h"
#include "google/cloud/internal/async_retry_unary_rpc.h"
#include <thread>
#include <type_traits>
namespace btproto = ::google::bigtable::v2;
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
using ::google::cloud::Idempotency;
using ::google::cloud::internal::MergeOptions;
using ::google::cloud::internal::OptionsSpan;
namespace {
template <typename Request>
void SetCommonTableOperationRequest(Request& request,
std::string const& app_profile_id,
std::string const& table_name) {
request.set_app_profile_id(app_profile_id);
request.set_table_name(table_name);
}
} // namespace
using ClientUtils = bigtable::internal::UnaryClientUtils<DataClient>;
static_assert(std::is_copy_assignable<bigtable::Table>::value,
"bigtable::Table must be CopyAssignable");
Status Table::Apply(SingleRowMutation mut, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->Apply(table_name_, std::move(mut));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`.");
}
// Copy the policies in effect for this operation. Many policy classes change
// their state as the operation makes progress (or fails to make progress), so
// we need fresh instances.
auto rpc_policy = clone_rpc_retry_policy();
auto backoff_policy = clone_rpc_backoff_policy();
auto idempotent_policy = clone_idempotent_mutation_policy();
// Build the RPC request, try to minimize copying.
btproto::MutateRowRequest request;
SetCommonTableOperationRequest<btproto::MutateRowRequest>(
request, app_profile_id(), table_name_);
mut.MoveTo(request);
bool const is_idempotent =
std::all_of(request.mutations().begin(), request.mutations().end(),
[&idempotent_policy](btproto::Mutation const& m) {
return idempotent_policy->is_idempotent(m);
});
btproto::MutateRowResponse response;
grpc::Status status;
while (true) {
grpc::ClientContext client_context;
rpc_policy->Setup(client_context);
backoff_policy->Setup(client_context);
metadata_update_policy_.Setup(client_context);
status = client_->MutateRow(&client_context, request, &response);
if (status.ok()) {
return google::cloud::Status{};
}
// It is up to the policy to terminate this loop, it could run
// forever, but that would be a bad policy (pun intended).
if (!rpc_policy->OnFailure(status) || !is_idempotent) {
return MakeStatusFromRpcError(status);
}
auto delay = backoff_policy->OnCompletion(status);
std::this_thread::sleep_for(delay);
}
}
future<Status> Table::AsyncApply(SingleRowMutation mut, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->AsyncApply(table_name_, std::move(mut));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return make_ready_future(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."));
}
google::bigtable::v2::MutateRowRequest request;
SetCommonTableOperationRequest<google::bigtable::v2::MutateRowRequest>(
request, app_profile_id(), table_name_);
mut.MoveTo(request);
// Determine if all the mutations are idempotent. The idempotency of the
// mutations won't change as the retry loop executes, so we can just compute
// it once and use a constant value for the loop.
auto idempotent_mutation_policy = clone_idempotent_mutation_policy();
auto const is_idempotent = std::all_of(
request.mutations().begin(), request.mutations().end(),
[&idempotent_mutation_policy](google::bigtable::v2::Mutation const& m) {
return idempotent_mutation_policy->is_idempotent(m);
});
auto const idempotency =
is_idempotent ? Idempotency::kIdempotent : Idempotency::kNonIdempotent;
auto cq = background_threads_->cq();
auto& client = client_;
auto metadata_update_policy = clone_metadata_update_policy();
return google::cloud::internal::StartRetryAsyncUnaryRpc(
cq, __func__, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
idempotency,
[client, metadata_update_policy](
grpc::ClientContext* context,
google::bigtable::v2::MutateRowRequest const& request,
grpc::CompletionQueue* cq) {
metadata_update_policy.Setup(*context);
return client->AsyncMutateRow(context, request, cq);
},
std::move(request))
.then([](future<StatusOr<google::bigtable::v2::MutateRowResponse>> r) {
return r.get().status();
});
}
std::vector<FailedMutation> Table::BulkApply(BulkMutation mut, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->BulkApply(table_name_, std::move(mut));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return bigtable_internal::MakeFailedMutations(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."),
mut.size());
}
if (mut.empty()) return {};
grpc::Status status;
// Copy the policies in effect for this operation. Many policy classes change
// their state as the operation makes progress (or fails to make progress), so
// we need fresh instances.
auto backoff_policy = clone_rpc_backoff_policy();
auto retry_policy = clone_rpc_retry_policy();
auto idempotent_policy = clone_idempotent_mutation_policy();
bigtable::internal::BulkMutator mutator(app_profile_id(), table_name_,
*idempotent_policy, std::move(mut));
while (true) {
grpc::ClientContext client_context;
backoff_policy->Setup(client_context);
retry_policy->Setup(client_context);
metadata_update_policy_.Setup(client_context);
status = mutator.MakeOneRequest(*client_, client_context);
if (!mutator.HasPendingMutations()) break;
if (!retry_policy->OnFailure(status)) break;
auto delay = backoff_policy->OnCompletion(status);
std::this_thread::sleep_for(delay);
}
return std::move(mutator).OnRetryDone();
}
future<std::vector<FailedMutation>> Table::AsyncBulkApply(BulkMutation mut,
Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->AsyncBulkApply(table_name_, std::move(mut));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return make_ready_future(bigtable_internal::MakeFailedMutations(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."),
mut.size()));
}
auto cq = background_threads_->cq();
auto mutation_policy = clone_idempotent_mutation_policy();
return internal::AsyncRetryBulkApply::Create(
cq, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
*mutation_policy, clone_metadata_update_policy(), client_,
app_profile_id(), table_name_, std::move(mut));
}
RowReader Table::ReadRows(RowSet row_set, Filter filter, Options opts) {
return ReadRows(std::move(row_set), RowReader::NO_ROWS_LIMIT,
std::move(filter), std::move(opts));
}
RowReader Table::ReadRows(RowSet row_set, std::int64_t rows_limit,
Filter filter, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->ReadRows(table_name_, std::move(row_set), rows_limit,
std::move(filter));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return MakeRowReader(
std::make_shared<bigtable_internal::StatusOnlyRowReader>(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`.")));
}
auto impl = std::make_shared<bigtable_internal::LegacyRowReader>(
client_, app_profile_id(), table_name_, std::move(row_set), rows_limit,
std::move(filter), clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
metadata_update_policy_,
std::make_unique<bigtable::internal::ReadRowsParserFactory>());
return bigtable_internal::MakeRowReader(std::move(impl));
}
StatusOr<std::pair<bool, Row>> Table::ReadRow(std::string row_key,
Filter filter, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->ReadRow(table_name_, std::move(row_key),
std::move(filter));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`.");
}
RowSet row_set(std::move(row_key));
std::int64_t const rows_limit = 1;
RowReader reader =
ReadRows(std::move(row_set), rows_limit, std::move(filter));
auto it = reader.begin();
if (it == reader.end()) {
return std::make_pair(false, Row("", {}));
}
if (!*it) {
return it->status();
}
auto result = std::make_pair(true, std::move(**it));
if (++it != reader.end()) {
return Status(
StatusCode::kInternal,
"internal error - RowReader returned more than one row in ReadRow()");
}
return result;
}
StatusOr<MutationBranch> Table::CheckAndMutateRow(
std::string row_key, Filter filter, std::vector<Mutation> true_mutations,
std::vector<Mutation> false_mutations, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->CheckAndMutateRow(
table_name_, std::move(row_key), std::move(filter),
std::move(true_mutations), std::move(false_mutations));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`.");
}
grpc::Status status;
btproto::CheckAndMutateRowRequest request;
request.set_row_key(std::move(row_key));
SetCommonTableOperationRequest<btproto::CheckAndMutateRowRequest>(
request, app_profile_id(), table_name_);
*request.mutable_predicate_filter() = std::move(filter).as_proto();
for (auto& m : true_mutations) {
*request.add_true_mutations() = std::move(m.op);
}
for (auto& m : false_mutations) {
*request.add_false_mutations() = std::move(m.op);
}
auto const idempotency = idempotent_mutation_policy_->is_idempotent(request)
? Idempotency::kIdempotent
: Idempotency::kNonIdempotent;
auto response = ClientUtils::MakeCall(
*client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
metadata_update_policy_, &DataClient::CheckAndMutateRow, request,
"Table::CheckAndMutateRow", status, idempotency);
if (!status.ok()) {
return MakeStatusFromRpcError(status);
}
return response.predicate_matched() ? MutationBranch::kPredicateMatched
: MutationBranch::kPredicateNotMatched;
}
future<StatusOr<MutationBranch>> Table::AsyncCheckAndMutateRow(
std::string row_key, Filter filter, std::vector<Mutation> true_mutations,
std::vector<Mutation> false_mutations, Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->AsyncCheckAndMutateRow(
table_name_, std::move(row_key), std::move(filter),
std::move(true_mutations), std::move(false_mutations));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return make_ready_future<StatusOr<MutationBranch>>(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."));
}
btproto::CheckAndMutateRowRequest request;
request.set_row_key(std::move(row_key));
SetCommonTableOperationRequest<btproto::CheckAndMutateRowRequest>(
request, app_profile_id(), table_name_);
*request.mutable_predicate_filter() = std::move(filter).as_proto();
for (auto& m : true_mutations) {
*request.add_true_mutations() = std::move(m.op);
}
for (auto& m : false_mutations) {
*request.add_false_mutations() = std::move(m.op);
}
auto const idempotency = idempotent_mutation_policy_->is_idempotent(request)
? Idempotency::kIdempotent
: Idempotency::kNonIdempotent;
auto cq = background_threads_->cq();
auto& client = client_;
auto metadata_update_policy = clone_metadata_update_policy();
return google::cloud::internal::StartRetryAsyncUnaryRpc(
cq, __func__, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
idempotency,
[client, metadata_update_policy](
grpc::ClientContext* context,
btproto::CheckAndMutateRowRequest const& request,
grpc::CompletionQueue* cq) {
metadata_update_policy.Setup(*context);
return client->AsyncCheckAndMutateRow(context, request, cq);
},
std::move(request))
.then([](future<StatusOr<btproto::CheckAndMutateRowResponse>> f)
-> StatusOr<MutationBranch> {
auto response = f.get();
if (!response) {
return response.status();
}
return response->predicate_matched()
? MutationBranch::kPredicateMatched
: MutationBranch::kPredicateNotMatched;
});
}
// Call the `google.bigtable.v2.Bigtable.SampleRowKeys` RPC until
// successful. When RPC is finished, this function returns the SampleRowKeys
// as a std::vector<>. If the RPC fails, it will keep retrying until the
// policies in effect tell us to stop. Note that each retry must clear the
// samples otherwise the result is an inconsistent set of sample row keys.
StatusOr<std::vector<bigtable::RowKeySample>> Table::SampleRows(Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->SampleRows(table_name_);
}
if (!google::cloud::internal::IsEmpty(opts)) {
return Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`.");
}
// Copy the policies in effect for this operation.
auto backoff_policy = clone_rpc_backoff_policy();
auto retry_policy = clone_rpc_retry_policy();
std::vector<bigtable::RowKeySample> samples;
// Build the RPC request for SampleRowKeys
btproto::SampleRowKeysRequest request;
btproto::SampleRowKeysResponse response;
SetCommonTableOperationRequest<btproto::SampleRowKeysRequest>(
request, app_profile_id(), table_name_);
while (true) {
grpc::ClientContext client_context;
backoff_policy->Setup(client_context);
retry_policy->Setup(client_context);
clone_metadata_update_policy().Setup(client_context);
auto stream = client_->SampleRowKeys(&client_context, request);
while (stream->Read(&response)) {
bigtable::RowKeySample row_sample;
row_sample.offset_bytes = response.offset_bytes();
row_sample.row_key = std::move(*response.mutable_row_key());
samples.emplace_back(std::move(row_sample));
}
auto status = stream->Finish();
if (status.ok()) {
break;
}
if (!retry_policy->OnFailure(status)) {
return MakeStatusFromRpcError(
status.error_code(),
"Retry policy exhausted: " + status.error_message());
}
samples.clear();
auto delay = backoff_policy->OnCompletion(status);
std::this_thread::sleep_for(delay);
}
return samples;
}
future<StatusOr<std::vector<bigtable::RowKeySample>>> Table::AsyncSampleRows(
Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->AsyncSampleRows(table_name_);
}
if (!google::cloud::internal::IsEmpty(opts)) {
return make_ready_future<StatusOr<std::vector<RowKeySample>>>(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."));
}
auto cq = background_threads_->cq();
return internal::LegacyAsyncRowSampler::Create(
cq, client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
metadata_update_policy_, app_profile_id(), table_name_);
}
StatusOr<Row> Table::ReadModifyWriteRowImpl(
btproto::ReadModifyWriteRowRequest request, Options opts) {
SetCommonTableOperationRequest<
::google::bigtable::v2::ReadModifyWriteRowRequest>(
request, app_profile_id(), table_name_);
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->ReadModifyWriteRow(std::move(request));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`.");
}
grpc::Status status;
auto response = ClientUtils::MakeNonIdempotentCall(
*(client_), clone_rpc_retry_policy(), clone_metadata_update_policy(),
&DataClient::ReadModifyWriteRow, request, "ReadModifyWriteRowRequest",
status);
if (!status.ok()) {
return MakeStatusFromRpcError(status);
}
return bigtable_internal::TransformReadModifyWriteRowResponse(
std::move(response));
}
future<StatusOr<Row>> Table::AsyncReadModifyWriteRowImpl(
::google::bigtable::v2::ReadModifyWriteRowRequest request, Options opts) {
SetCommonTableOperationRequest<
::google::bigtable::v2::ReadModifyWriteRowRequest>(
request, app_profile_id(), table_name_);
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->AsyncReadModifyWriteRow(std::move(request));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return make_ready_future<StatusOr<Row>>(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."));
}
auto cq = background_threads_->cq();
auto& client = client_;
auto metadata_update_policy = clone_metadata_update_policy();
return google::cloud::internal::StartRetryAsyncUnaryRpc(
cq, __func__, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
Idempotency::kNonIdempotent,
[client, metadata_update_policy](
grpc::ClientContext* context,
btproto::ReadModifyWriteRowRequest const& request,
grpc::CompletionQueue* cq) {
metadata_update_policy.Setup(*context);
return client->AsyncReadModifyWriteRow(context, request, cq);
},
std::move(request))
.then([](future<StatusOr<btproto::ReadModifyWriteRowResponse>> fut)
-> StatusOr<Row> {
auto result = fut.get();
if (!result) return std::move(result).status();
return bigtable_internal::TransformReadModifyWriteRowResponse(
*std::move(result));
});
}
future<StatusOr<std::pair<bool, Row>>> Table::AsyncReadRow(std::string row_key,
Filter filter,
Options opts) {
if (connection_) {
OptionsSpan span(MergeOptions(std::move(opts), options_));
return connection_->AsyncReadRow(table_name_, std::move(row_key),
std::move(filter));
}
if (!google::cloud::internal::IsEmpty(opts)) {
return make_ready_future<StatusOr<std::pair<bool, Row>>>(
Status(StatusCode::kInvalidArgument,
"Per-operation options only apply to `Table`s constructed "
"with a `DataConnection`."));
}
class AsyncReadRowHandler {
public:
AsyncReadRowHandler() : row_("", {}) {}
future<StatusOr<std::pair<bool, Row>>> GetFuture() {
return row_promise_.get_future();
}
future<bool> OnRow(Row row) {
// assert(!row_received_);
row_ = std::move(row);
row_received_ = true;
// Don't satisfy the promise before `OnStreamFinished`.
//
// The `CompletionQueue`, which this object holds a reference to, should
// not be shut down before `OnStreamFinished` is called. In order to make
// sure of that, satisying the `promise<>` is deferred until then - the
// user shouldn't shutdown the `CompletionQueue` before this whole
// operation is done.
return make_ready_future(false);
}
void OnStreamFinished(Status status) {
if (row_received_) {
// If we got a row we don't need to care about the stream status.
row_promise_.set_value(std::make_pair(true, std::move(row_)));
return;
}
if (status.ok()) {
row_promise_.set_value(std::make_pair(false, Row("", {})));
} else {
row_promise_.set_value(std::move(status));
}
}
private:
Row row_;
bool row_received_{};
promise<StatusOr<std::pair<bool, Row>>> row_promise_;
};
RowSet row_set(std::move(row_key));
std::int64_t const rows_limit = 1;
auto handler = std::make_shared<AsyncReadRowHandler>();
AsyncReadRows([handler](Row row) { return handler->OnRow(std::move(row)); },
[handler](Status status) {
handler->OnStreamFinished(std::move(status));
},
std::move(row_set), rows_limit, std::move(filter));
return handler->GetFuture();
}
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
} // namespace cloud
} // namespace google