-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathmutation_batcher.cc
More file actions
281 lines (257 loc) · 10.9 KB
/
mutation_batcher.cc
File metadata and controls
281 lines (257 loc) · 10.9 KB
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
// Copyright 2019 Google LLC
//
// 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/mutation_batcher.h"
#include "google/cloud/bigtable/internal/client_options_defaults.h"
#include "google/cloud/grpc_error_delegate.h"
#include <sstream>
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
// Cloud Bigtable doesn't accept more than this in a single request.
auto constexpr kBigtableMutationLimit = 100000;
auto constexpr kDefaultMutationLimit = 1000;
// Maximum mutations that can be outstanding in the Cloud Bigtable front end.
// NOTE: this is a system-wide limit, but it is only enforced per process.
auto constexpr kBigtableOutstandingMutationLimit = 300000;
// Let's make the default slightly smaller, so that overheads or
// miscalculations don't tip us over.
auto constexpr kDefaultMaxSizePerBatch =
(BIGTABLE_CLIENT_DEFAULT_MAX_MESSAGE_LENGTH * 90LL) / 100LL;
auto constexpr kDefaultMaxBatches = 4;
auto constexpr kDefaultMaxOutstandingSize =
kDefaultMaxSizePerBatch * kDefaultMaxBatches;
MutationBatcher::Options::Options()
: max_mutations_per_batch(kDefaultMutationLimit),
max_size_per_batch(kDefaultMaxSizePerBatch),
max_batches(kDefaultMaxBatches),
max_outstanding_size(kDefaultMaxOutstandingSize),
max_outstanding_mutations(kBigtableOutstandingMutationLimit) {}
MutationBatcher::Options& MutationBatcher::Options::SetMaxMutationsPerBatch(
size_t max_mutations_per_batch_arg) {
max_mutations_per_batch = std::min<std::size_t>(max_mutations_per_batch_arg,
kBigtableMutationLimit);
return *this;
}
MutationBatcher::Options& MutationBatcher::Options::SetMaxOutstandingMutations(
size_t max_outstanding_mutations_arg) {
max_outstanding_mutations = std::min<std::size_t>(
max_outstanding_mutations_arg, kBigtableOutstandingMutationLimit);
return *this;
}
std::pair<future<void>, future<Status>> MutationBatcher::AsyncApply(
CompletionQueue& cq, SingleRowMutation mut) {
AdmissionPromise admission_promise;
CompletionPromise completion_promise;
auto res = std::make_pair(admission_promise.get_future(),
completion_promise.get_future());
PendingSingleRowMutation pending(std::move(mut),
std::move(completion_promise),
std::move(admission_promise));
std::unique_lock<std::mutex> lk(mu_);
grpc::Status mutation_status = IsValid(pending);
if (!mutation_status.ok()) {
lk.unlock();
// Destroy the mutation before satisfying the admission promise so that we
// can limit the memory usage.
pending.mut.Clear();
pending.completion_promise.set_value(
MakeStatusFromRpcError(mutation_status));
// No need to consider no_more_pending_promises because this operation
// didn't lower the number of pending operations.
pending.admission_promise.set_value();
return res;
}
++num_requests_pending_;
if (!CanAppendToBatch(pending)) {
pending_mutations_.push(std::move(pending));
return res;
}
std::vector<AdmissionPromise> admission_promises_to_satisfy;
admission_promises_to_satisfy.emplace_back(
std::move(pending.admission_promise));
Admit(std::move(pending));
FlushIfPossible(cq);
SatisfyPromises(std::move(admission_promises_to_satisfy), lk);
return res;
}
future<void> MutationBatcher::AsyncWaitForNoPendingRequests() {
std::unique_lock<std::mutex> lk(mu_);
if (num_requests_pending_ == 0) {
return make_ready_future();
}
no_more_pending_promises_.emplace_back();
return no_more_pending_promises_.back().get_future();
}
MutationBatcher::PendingSingleRowMutation::PendingSingleRowMutation(
SingleRowMutation mut_arg, CompletionPromise completion_promise,
AdmissionPromise admission_promise)
: mut(std::move(mut_arg)),
completion_promise(std::move(completion_promise)),
admission_promise(std::move(admission_promise)) {
::google::bigtable::v2::MutateRowsRequest::Entry tmp;
mut.MoveTo(&tmp);
// This operation might not be cheap, so let's cache it.
request_size = tmp.ByteSizeLong();
num_mutations = static_cast<std::size_t>(tmp.mutations_size());
mut = SingleRowMutation(std::move(tmp));
}
grpc::Status MutationBatcher::IsValid(PendingSingleRowMutation& mut) const {
// Objects of this class need to be aware of the maximum allowed number of
// mutations in a batch because it should not pack more. If we have this
// knowledge, we might as well simplify everything and not admit larger
// mutations.
auto mutation_limit = (std::min)(options_.max_mutations_per_batch,
options_.max_outstanding_mutations);
if (mut.num_mutations > mutation_limit) {
std::stringstream stream;
stream << "Too many (" << mut.num_mutations
<< ") mutations in a SingleRowMutations request. " << mutation_limit
<< " is the limit.";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, stream.str());
}
if (mut.num_mutations == 0) {
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"Supplied SingleRowMutations has no entries");
}
if (mut.request_size > options_.max_size_per_batch) {
std::stringstream stream;
stream << "Too large (" << mut.request_size
<< " bytes) mutation in a SingleRowMutations request. "
<< options_.max_size_per_batch << " bytes is the limit.";
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, stream.str());
}
return grpc::Status();
}
bool MutationBatcher::HasSpaceFor(PendingSingleRowMutation const& mut) const {
return outstanding_size_ + mut.request_size <=
options_.max_outstanding_size &&
outstanding_mutations_ + mut.num_mutations <=
options_.max_outstanding_mutations &&
cur_batch_->requests_size + mut.request_size <=
options_.max_size_per_batch &&
cur_batch_->num_mutations + mut.num_mutations <=
options_.max_mutations_per_batch;
}
future<std::vector<FailedMutation>> MutationBatcher::AsyncBulkApplyImpl(
Table& table, BulkMutation&& mut) {
return table.AsyncBulkApply(std::move(mut));
}
bool MutationBatcher::FlushIfPossible(CompletionQueue cq) {
if (cur_batch_->num_mutations > 0 &&
num_outstanding_batches_ < options_.max_batches) {
++num_outstanding_batches_;
auto batch = std::make_shared<Batch>();
cur_batch_.swap(batch);
AsyncBulkApplyImpl(table_, std::move(batch->requests))
.then([this, cq,
batch](future<std::vector<FailedMutation>> failed) mutable {
// Calling OnBulkApplyDone here might lead to a deadlock if the
// underlying operation completes very quickly, yielding the outer
// `.then()` call synchronous. The deadlock would occur because the
// mutex is held here and OnBulkApplyDone would try to reacquire it.
cq.RunAsync([this, b = std::move(batch),
f = failed.get()](CompletionQueue& cq) {
OnBulkApplyDone(cq, std::move(*b), std::move(f));
});
});
return true;
}
return false;
}
void MutationBatcher::OnBulkApplyDone(
CompletionQueue cq, MutationBatcher::Batch batch,
std::vector<FailedMutation> const& failed) {
// First process all the failures, marking the mutations as done after
// processing them.
for (auto const& f : failed) {
int const idx = f.original_index();
if (idx < 0 ||
static_cast<std::size_t>(idx) >= batch.mutation_data.size()) {
// This is a bug on the server or the client, either terminate (when
// -fno-exceptions is set) or throw an exception.
std::ostringstream os;
os << "Index " << idx << " is out of range [0,"
<< batch.mutation_data.size() << ")";
google::cloud::internal::ThrowRuntimeError(std::move(os).str());
}
MutationData& data = batch.mutation_data[idx];
data.completion_promise.set_value(f.status());
data.done = true;
}
// Any remaining mutations are treated as successful.
for (auto& data : batch.mutation_data) {
if (!data.done) {
data.completion_promise.set_value(Status());
data.done = true;
}
}
auto const num_mutations = batch.mutation_data.size();
batch.mutation_data.clear();
std::unique_lock<std::mutex> lk(mu_);
outstanding_size_ -= batch.requests_size;
outstanding_mutations_ -= batch.num_mutations;
num_requests_pending_ -= num_mutations;
num_outstanding_batches_--;
SatisfyPromises(TryAdmit(cq), lk); // unlocks the lock
}
std::vector<MutationBatcher::AdmissionPromise> MutationBatcher::TryAdmit(
CompletionQueue& cq) {
// Defer satisfying promises until we release the lock.
std::vector<AdmissionPromise> admission_promises;
do {
while (!pending_mutations_.empty() &&
HasSpaceFor(pending_mutations_.front())) {
auto& mut = pending_mutations_.front();
admission_promises.emplace_back(std::move(mut.admission_promise));
Admit(std::move(mut));
pending_mutations_.pop();
}
} while (FlushIfPossible(cq));
return admission_promises;
}
void MutationBatcher::Admit(PendingSingleRowMutation mut) {
outstanding_size_ += mut.request_size;
outstanding_mutations_ += mut.num_mutations;
cur_batch_->requests_size += mut.request_size;
cur_batch_->num_mutations += mut.num_mutations;
cur_batch_->requests.emplace_back(std::move(mut.mut));
cur_batch_->mutation_data.emplace_back(std::move(mut));
}
void MutationBatcher::SatisfyPromises(
std::vector<AdmissionPromise> admission_promises,
std::unique_lock<std::mutex>& lk) {
std::vector<NoMorePendingPromise> no_more_pending_promises;
if (num_requests_pending_ == 0 && num_outstanding_batches_ == 0) {
// We should wait not only on num_requests_pending_ being zero but also on
// num_outstanding_batches_ because we want to allow the user to kill the
// completion queue after this promise is fulfilled. Otherwise, the user can
// destroy the completion queue while the last batch is still being
// processed - we've had this bug (#2140).
no_more_pending_promises_.swap(no_more_pending_promises);
}
lk.unlock();
// Inform the user that we've admitted these mutations and there might be some
// space in the buffer finally.
for (auto& promise : admission_promises) {
promise.set_value();
}
for (auto& promise : no_more_pending_promises) {
promise.set_value();
}
}
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
} // namespace cloud
} // namespace google