/src/rocksdb/utilities/transactions/pessimistic_transaction.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root directory). |
5 | | |
6 | | #include "utilities/transactions/pessimistic_transaction.h" |
7 | | |
8 | | #include <map> |
9 | | #include <set> |
10 | | #include <string> |
11 | | #include <vector> |
12 | | |
13 | | #include "db/column_family.h" |
14 | | #include "db/db_impl/db_impl.h" |
15 | | #include "logging/logging.h" |
16 | | #include "rocksdb/comparator.h" |
17 | | #include "rocksdb/db.h" |
18 | | #include "rocksdb/snapshot.h" |
19 | | #include "rocksdb/status.h" |
20 | | #include "rocksdb/utilities/transaction_db.h" |
21 | | #include "test_util/sync_point.h" |
22 | | #include "util/cast_util.h" |
23 | | #include "util/string_util.h" |
24 | | #include "utilities/transactions/pessimistic_transaction_db.h" |
25 | | #include "utilities/transactions/transaction_util.h" |
26 | | #include "utilities/write_batch_with_index/write_batch_with_index_internal.h" |
27 | | |
28 | | namespace ROCKSDB_NAMESPACE { |
29 | | |
30 | | struct WriteOptions; |
31 | | |
32 | | std::atomic<TransactionID> PessimisticTransaction::txn_id_counter_(1); |
33 | | |
34 | 0 | TransactionID PessimisticTransaction::GenTxnID() { |
35 | 0 | return txn_id_counter_.fetch_add(1); |
36 | 0 | } |
37 | | |
38 | | PessimisticTransaction::PessimisticTransaction( |
39 | | TransactionDB* txn_db, const WriteOptions& write_options, |
40 | | const TransactionOptions& txn_options, const bool init) |
41 | 0 | : TransactionBaseImpl( |
42 | 0 | txn_db->GetRootDB(), write_options, |
43 | 0 | static_cast_with_check<PessimisticTransactionDB>(txn_db) |
44 | 0 | ->GetLockTrackerFactory()), |
45 | 0 | txn_db_impl_(nullptr), |
46 | 0 | expiration_time_(0), |
47 | 0 | txn_id_(0), |
48 | 0 | waiting_cf_id_(0), |
49 | 0 | waiting_key_(nullptr), |
50 | 0 | lock_timeout_(0), |
51 | 0 | deadlock_detect_(false), |
52 | 0 | deadlock_detect_depth_(0), |
53 | 0 | skip_concurrency_control_(false) { |
54 | 0 | txn_db_impl_ = static_cast_with_check<PessimisticTransactionDB>(txn_db); |
55 | 0 | db_impl_ = static_cast_with_check<DBImpl>(db_); |
56 | 0 | if (init) { |
57 | 0 | Initialize(txn_options); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | void PessimisticTransaction::Initialize(const TransactionOptions& txn_options) { |
62 | | // Range lock manager uses address of transaction object as TXNID |
63 | 0 | const TransactionDBOptions& db_options = txn_db_impl_->GetTxnDBOptions(); |
64 | 0 | if (db_options.lock_mgr_handle && |
65 | 0 | db_options.lock_mgr_handle->getLockManager()->IsRangeLockSupported()) { |
66 | 0 | txn_id_ = reinterpret_cast<TransactionID>(this); |
67 | 0 | } else { |
68 | 0 | txn_id_ = GenTxnID(); |
69 | 0 | } |
70 | |
|
71 | 0 | txn_state_ = STARTED; |
72 | |
|
73 | 0 | deadlock_detect_ = txn_options.deadlock_detect; |
74 | 0 | deadlock_detect_depth_ = txn_options.deadlock_detect_depth; |
75 | 0 | write_batch_.SetMaxBytes(txn_options.max_write_batch_size); |
76 | 0 | write_batch_.GetWriteBatch()->SetTrackTimestampSize( |
77 | 0 | txn_options.write_batch_track_timestamp_size); |
78 | 0 | skip_concurrency_control_ = txn_options.skip_concurrency_control; |
79 | |
|
80 | 0 | lock_timeout_ = txn_options.lock_timeout * 1000; |
81 | 0 | if (lock_timeout_ < 0) { |
82 | | // Lock timeout not set, use default |
83 | 0 | lock_timeout_ = |
84 | 0 | txn_db_impl_->GetTxnDBOptions().transaction_lock_timeout * 1000; |
85 | 0 | } |
86 | | |
87 | | // deadlock timeout should be lower than lock timeout |
88 | 0 | deadlock_timeout_us_ = |
89 | 0 | std::min(txn_options.deadlock_timeout_us, lock_timeout_); |
90 | |
|
91 | 0 | if (txn_options.expiration >= 0) { |
92 | 0 | expiration_time_ = start_time_ + txn_options.expiration * 1000; |
93 | 0 | } else { |
94 | 0 | expiration_time_ = 0; |
95 | 0 | } |
96 | |
|
97 | 0 | if (txn_options.set_snapshot) { |
98 | 0 | SetSnapshot(); |
99 | 0 | } |
100 | |
|
101 | 0 | if (expiration_time_ > 0) { |
102 | 0 | txn_db_impl_->InsertExpirableTransaction(txn_id_, this); |
103 | 0 | } |
104 | 0 | use_only_the_last_commit_time_batch_for_recovery_ = |
105 | 0 | txn_options.use_only_the_last_commit_time_batch_for_recovery; |
106 | 0 | skip_prepare_ = txn_options.skip_prepare; |
107 | |
|
108 | 0 | read_timestamp_ = kMaxTxnTimestamp; |
109 | 0 | commit_timestamp_ = kMaxTxnTimestamp; |
110 | |
|
111 | 0 | if (txn_options.commit_bypass_memtable) { |
112 | | // No need to optimize for empty transction |
113 | 0 | commit_bypass_memtable_threshold_ = 1; |
114 | 0 | } else { |
115 | 0 | commit_bypass_memtable_threshold_ = |
116 | 0 | txn_options.large_txn_commit_optimize_threshold; |
117 | 0 | } |
118 | |
|
119 | 0 | commit_bypass_memtable_byte_threshold_ = |
120 | 0 | txn_options.large_txn_commit_optimize_byte_threshold; |
121 | 0 | } |
122 | | |
123 | 0 | PessimisticTransaction::~PessimisticTransaction() { |
124 | 0 | txn_db_impl_->UnLock(this, *tracked_locks_); |
125 | 0 | if (expiration_time_ > 0) { |
126 | 0 | txn_db_impl_->RemoveExpirableTransaction(txn_id_); |
127 | 0 | } |
128 | 0 | if (!name_.empty() && txn_state_ != COMMITTED) { |
129 | 0 | txn_db_impl_->UnregisterTransaction(this); |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | 0 | void PessimisticTransaction::Clear() { |
134 | 0 | txn_db_impl_->UnLock(this, *tracked_locks_); |
135 | 0 | TransactionBaseImpl::Clear(); |
136 | 0 | } |
137 | | |
138 | | void PessimisticTransaction::Reinitialize( |
139 | | TransactionDB* txn_db, const WriteOptions& write_options, |
140 | 0 | const TransactionOptions& txn_options) { |
141 | 0 | if (!name_.empty() && txn_state_ != COMMITTED) { |
142 | 0 | txn_db_impl_->UnregisterTransaction(this); |
143 | 0 | } |
144 | 0 | TransactionBaseImpl::Reinitialize(txn_db->GetRootDB(), write_options); |
145 | 0 | Initialize(txn_options); |
146 | 0 | } |
147 | | |
148 | 0 | bool PessimisticTransaction::IsExpired() const { |
149 | 0 | if (expiration_time_ > 0) { |
150 | 0 | if (dbimpl_->GetSystemClock()->NowMicros() >= expiration_time_) { |
151 | | // Transaction is expired. |
152 | 0 | return true; |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | return false; |
157 | 0 | } |
158 | | |
159 | | WriteCommittedTxn::WriteCommittedTxn(TransactionDB* txn_db, |
160 | | const WriteOptions& write_options, |
161 | | const TransactionOptions& txn_options) |
162 | 0 | : PessimisticTransaction(txn_db, write_options, txn_options) {} |
163 | | |
164 | | Status WriteCommittedTxn::GetForUpdate(const ReadOptions& read_options, |
165 | | ColumnFamilyHandle* column_family, |
166 | | const Slice& key, std::string* value, |
167 | 0 | bool exclusive, const bool do_validate) { |
168 | 0 | return GetForUpdateImpl(read_options, column_family, key, value, exclusive, |
169 | 0 | do_validate); |
170 | 0 | } |
171 | | |
172 | | Status WriteCommittedTxn::GetForUpdate(const ReadOptions& read_options, |
173 | | ColumnFamilyHandle* column_family, |
174 | | const Slice& key, |
175 | | PinnableSlice* pinnable_val, |
176 | 0 | bool exclusive, const bool do_validate) { |
177 | 0 | return GetForUpdateImpl(read_options, column_family, key, pinnable_val, |
178 | 0 | exclusive, do_validate); |
179 | 0 | } |
180 | | |
181 | | template <typename TValue> |
182 | | inline Status WriteCommittedTxn::GetForUpdateImpl( |
183 | | const ReadOptions& read_options, ColumnFamilyHandle* column_family, |
184 | 0 | const Slice& key, TValue* value, bool exclusive, const bool do_validate) { |
185 | 0 | if (read_options.io_activity != Env::IOActivity::kUnknown) { |
186 | 0 | return Status::InvalidArgument( |
187 | 0 | "Cannot call GetForUpdate with `ReadOptions::io_activity` != " |
188 | 0 | "`Env::IOActivity::kUnknown`"); |
189 | 0 | } |
190 | 0 | column_family = |
191 | 0 | column_family ? column_family : db_impl_->DefaultColumnFamily(); |
192 | 0 | assert(column_family); |
193 | 0 | if (!read_options.timestamp) { |
194 | 0 | const Comparator* const ucmp = column_family->GetComparator(); |
195 | 0 | assert(ucmp); |
196 | 0 | size_t ts_sz = ucmp->timestamp_size(); |
197 | 0 | if (0 == ts_sz) { |
198 | 0 | return TransactionBaseImpl::GetForUpdate(read_options, column_family, key, |
199 | 0 | value, exclusive, do_validate); |
200 | 0 | } |
201 | 0 | } else { |
202 | 0 | Status s = |
203 | 0 | db_impl_->FailIfTsMismatchCf(column_family, *(read_options.timestamp)); |
204 | 0 | if (!s.ok()) { |
205 | 0 | return s; |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | 0 | Status s = SanityCheckReadTimestamp(do_validate); |
210 | 0 | if (!s.ok()) { |
211 | 0 | return s; |
212 | 0 | } |
213 | | |
214 | 0 | if (!read_options.timestamp) { |
215 | 0 | ReadOptions read_opts_copy = read_options; |
216 | 0 | char ts_buf[sizeof(kMaxTxnTimestamp)]; |
217 | 0 | EncodeFixed64(ts_buf, read_timestamp_); |
218 | 0 | Slice ts(ts_buf, sizeof(ts_buf)); |
219 | 0 | read_opts_copy.timestamp = &ts; |
220 | 0 | return TransactionBaseImpl::GetForUpdate(read_opts_copy, column_family, key, |
221 | 0 | value, exclusive, do_validate); |
222 | 0 | } |
223 | 0 | assert(read_options.timestamp); |
224 | 0 | const char* const ts_buf = read_options.timestamp->data(); |
225 | 0 | assert(read_options.timestamp->size() == sizeof(kMaxTxnTimestamp)); |
226 | 0 | TxnTimestamp ts = DecodeFixed64(ts_buf); |
227 | 0 | if (ts != read_timestamp_) { |
228 | 0 | return Status::InvalidArgument("Must read from the same read_timestamp"); |
229 | 0 | } |
230 | 0 | return TransactionBaseImpl::GetForUpdate(read_options, column_family, key, |
231 | 0 | value, exclusive, do_validate); |
232 | 0 | } Unexecuted instantiation: rocksdb::Status rocksdb::WriteCommittedTxn::GetForUpdateImpl<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(rocksdb::ReadOptions const&, rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, bool, bool) Unexecuted instantiation: rocksdb::Status rocksdb::WriteCommittedTxn::GetForUpdateImpl<rocksdb::PinnableSlice>(rocksdb::ReadOptions const&, rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::PinnableSlice*, bool, bool) |
233 | | |
234 | | Status WriteCommittedTxn::GetEntityForUpdate(const ReadOptions& read_options, |
235 | | ColumnFamilyHandle* column_family, |
236 | | const Slice& key, |
237 | | PinnableWideColumns* columns, |
238 | 0 | bool exclusive, bool do_validate) { |
239 | 0 | if (!column_family) { |
240 | 0 | return Status::InvalidArgument( |
241 | 0 | "Cannot call GetEntityForUpdate without a column family handle"); |
242 | 0 | } |
243 | | |
244 | 0 | const Comparator* const ucmp = column_family->GetComparator(); |
245 | 0 | assert(ucmp); |
246 | 0 | const size_t ts_sz = ucmp->timestamp_size(); |
247 | |
|
248 | 0 | if (ts_sz == 0) { |
249 | 0 | return TransactionBaseImpl::GetEntityForUpdate( |
250 | 0 | read_options, column_family, key, columns, exclusive, do_validate); |
251 | 0 | } |
252 | | |
253 | 0 | assert(ts_sz > 0); |
254 | 0 | Status s = SanityCheckReadTimestamp(do_validate); |
255 | 0 | if (!s.ok()) { |
256 | 0 | return s; |
257 | 0 | } |
258 | | |
259 | 0 | std::string ts_buf; |
260 | 0 | PutFixed64(&ts_buf, read_timestamp_); |
261 | 0 | Slice ts(ts_buf); |
262 | |
|
263 | 0 | if (!read_options.timestamp) { |
264 | 0 | ReadOptions read_options_copy = read_options; |
265 | 0 | read_options_copy.timestamp = &ts; |
266 | |
|
267 | 0 | return TransactionBaseImpl::GetEntityForUpdate( |
268 | 0 | read_options_copy, column_family, key, columns, exclusive, do_validate); |
269 | 0 | } |
270 | | |
271 | 0 | assert(read_options.timestamp); |
272 | 0 | if (*read_options.timestamp != ts) { |
273 | 0 | return Status::InvalidArgument("Must read from the same read timestamp"); |
274 | 0 | } |
275 | | |
276 | 0 | return TransactionBaseImpl::GetEntityForUpdate( |
277 | 0 | read_options, column_family, key, columns, exclusive, do_validate); |
278 | 0 | } |
279 | | |
280 | 0 | Status WriteCommittedTxn::SanityCheckReadTimestamp(bool do_validate) { |
281 | 0 | bool enable_udt_validation = |
282 | 0 | txn_db_impl_->GetTxnDBOptions().enable_udt_validation; |
283 | 0 | if (!enable_udt_validation) { |
284 | 0 | if (kMaxTxnTimestamp != read_timestamp_) { |
285 | 0 | return Status::InvalidArgument( |
286 | 0 | "read_timestamp is set but timestamp validation is disabled for the " |
287 | 0 | "DB"); |
288 | 0 | } |
289 | 0 | } else { |
290 | 0 | if (!do_validate) { |
291 | 0 | if (kMaxTxnTimestamp != read_timestamp_) { |
292 | 0 | return Status::InvalidArgument( |
293 | 0 | "If do_validate is false then GetForUpdate with read_timestamp is " |
294 | 0 | "not " |
295 | 0 | "defined."); |
296 | 0 | } |
297 | 0 | } else { |
298 | 0 | if (kMaxTxnTimestamp == read_timestamp_) { |
299 | 0 | return Status::InvalidArgument( |
300 | 0 | "read_timestamp must be set for validation"); |
301 | 0 | } |
302 | 0 | } |
303 | 0 | } |
304 | 0 | return Status::OK(); |
305 | 0 | } |
306 | | |
307 | | Status WriteCommittedTxn::PutEntityImpl(ColumnFamilyHandle* column_family, |
308 | | const Slice& key, |
309 | | const WideColumns& columns, |
310 | 0 | bool do_validate, bool assume_tracked) { |
311 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
312 | 0 | [column_family, &key, &columns, this]() { |
313 | 0 | Status s = GetBatchForWrite()->PutEntity(column_family, key, |
314 | 0 | columns); |
315 | 0 | if (s.ok()) { |
316 | 0 | ++num_put_entities_; |
317 | 0 | } |
318 | 0 | return s; |
319 | 0 | }); |
320 | 0 | } |
321 | | |
322 | | Status WriteCommittedTxn::Put(ColumnFamilyHandle* column_family, |
323 | | const Slice& key, const Slice& value, |
324 | 0 | const bool assume_tracked) { |
325 | 0 | const bool do_validate = !assume_tracked; |
326 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
327 | 0 | [column_family, &key, &value, this]() { |
328 | 0 | Status s = |
329 | 0 | GetBatchForWrite()->Put(column_family, key, value); |
330 | 0 | if (s.ok()) { |
331 | 0 | ++num_puts_; |
332 | 0 | } |
333 | 0 | return s; |
334 | 0 | }); |
335 | 0 | } |
336 | | |
337 | | Status WriteCommittedTxn::Put(ColumnFamilyHandle* column_family, |
338 | | const SliceParts& key, const SliceParts& value, |
339 | 0 | const bool assume_tracked) { |
340 | 0 | const bool do_validate = !assume_tracked; |
341 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
342 | 0 | [column_family, &key, &value, this]() { |
343 | 0 | Status s = |
344 | 0 | GetBatchForWrite()->Put(column_family, key, value); |
345 | 0 | if (s.ok()) { |
346 | 0 | ++num_puts_; |
347 | 0 | } |
348 | 0 | return s; |
349 | 0 | }); |
350 | 0 | } |
351 | | |
352 | | Status WriteCommittedTxn::PutUntracked(ColumnFamilyHandle* column_family, |
353 | 0 | const Slice& key, const Slice& value) { |
354 | 0 | return Operate( |
355 | 0 | column_family, key, /*do_validate=*/false, |
356 | 0 | /*assume_tracked=*/false, [column_family, &key, &value, this]() { |
357 | 0 | Status s = GetBatchForWrite()->Put(column_family, key, value); |
358 | 0 | if (s.ok()) { |
359 | 0 | ++num_puts_; |
360 | 0 | } |
361 | 0 | return s; |
362 | 0 | }); |
363 | 0 | } |
364 | | |
365 | | Status WriteCommittedTxn::PutUntracked(ColumnFamilyHandle* column_family, |
366 | | const SliceParts& key, |
367 | 0 | const SliceParts& value) { |
368 | 0 | return Operate( |
369 | 0 | column_family, key, /*do_validate=*/false, |
370 | 0 | /*assume_tracked=*/false, [column_family, &key, &value, this]() { |
371 | 0 | Status s = GetBatchForWrite()->Put(column_family, key, value); |
372 | 0 | if (s.ok()) { |
373 | 0 | ++num_puts_; |
374 | 0 | } |
375 | 0 | return s; |
376 | 0 | }); |
377 | 0 | } |
378 | | |
379 | | Status WriteCommittedTxn::Delete(ColumnFamilyHandle* column_family, |
380 | 0 | const Slice& key, const bool assume_tracked) { |
381 | 0 | const bool do_validate = !assume_tracked; |
382 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
383 | 0 | [column_family, &key, this]() { |
384 | 0 | Status s = GetBatchForWrite()->Delete(column_family, key); |
385 | 0 | if (s.ok()) { |
386 | 0 | ++num_deletes_; |
387 | 0 | } |
388 | 0 | return s; |
389 | 0 | }); |
390 | 0 | } |
391 | | |
392 | | Status WriteCommittedTxn::Delete(ColumnFamilyHandle* column_family, |
393 | | const SliceParts& key, |
394 | 0 | const bool assume_tracked) { |
395 | 0 | const bool do_validate = !assume_tracked; |
396 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
397 | 0 | [column_family, &key, this]() { |
398 | 0 | Status s = GetBatchForWrite()->Delete(column_family, key); |
399 | 0 | if (s.ok()) { |
400 | 0 | ++num_deletes_; |
401 | 0 | } |
402 | 0 | return s; |
403 | 0 | }); |
404 | 0 | } |
405 | | |
406 | | Status WriteCommittedTxn::DeleteUntracked(ColumnFamilyHandle* column_family, |
407 | 0 | const Slice& key) { |
408 | 0 | return Operate(column_family, key, /*do_validate=*/false, |
409 | 0 | /*assume_tracked=*/false, [column_family, &key, this]() { |
410 | 0 | Status s = GetBatchForWrite()->Delete(column_family, key); |
411 | 0 | if (s.ok()) { |
412 | 0 | ++num_deletes_; |
413 | 0 | } |
414 | 0 | return s; |
415 | 0 | }); |
416 | 0 | } |
417 | | |
418 | | Status WriteCommittedTxn::DeleteUntracked(ColumnFamilyHandle* column_family, |
419 | 0 | const SliceParts& key) { |
420 | 0 | return Operate(column_family, key, /*do_validate=*/false, |
421 | 0 | /*assume_tracked=*/false, [column_family, &key, this]() { |
422 | 0 | Status s = GetBatchForWrite()->Delete(column_family, key); |
423 | 0 | if (s.ok()) { |
424 | 0 | ++num_deletes_; |
425 | 0 | } |
426 | 0 | return s; |
427 | 0 | }); |
428 | 0 | } |
429 | | |
430 | | Status WriteCommittedTxn::SingleDelete(ColumnFamilyHandle* column_family, |
431 | | const Slice& key, |
432 | 0 | const bool assume_tracked) { |
433 | 0 | const bool do_validate = !assume_tracked; |
434 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
435 | 0 | [column_family, &key, this]() { |
436 | 0 | Status s = |
437 | 0 | GetBatchForWrite()->SingleDelete(column_family, key); |
438 | 0 | if (s.ok()) { |
439 | 0 | ++num_deletes_; |
440 | 0 | } |
441 | 0 | return s; |
442 | 0 | }); |
443 | 0 | } |
444 | | |
445 | | Status WriteCommittedTxn::SingleDelete(ColumnFamilyHandle* column_family, |
446 | | const SliceParts& key, |
447 | 0 | const bool assume_tracked) { |
448 | 0 | const bool do_validate = !assume_tracked; |
449 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
450 | 0 | [column_family, &key, this]() { |
451 | 0 | Status s = |
452 | 0 | GetBatchForWrite()->SingleDelete(column_family, key); |
453 | 0 | if (s.ok()) { |
454 | 0 | ++num_deletes_; |
455 | 0 | } |
456 | 0 | return s; |
457 | 0 | }); |
458 | 0 | } |
459 | | |
460 | | Status WriteCommittedTxn::SingleDeleteUntracked( |
461 | 0 | ColumnFamilyHandle* column_family, const Slice& key) { |
462 | 0 | return Operate(column_family, key, /*do_validate=*/false, |
463 | 0 | /*assume_tracked=*/false, [column_family, &key, this]() { |
464 | 0 | Status s = |
465 | 0 | GetBatchForWrite()->SingleDelete(column_family, key); |
466 | 0 | if (s.ok()) { |
467 | 0 | ++num_deletes_; |
468 | 0 | } |
469 | 0 | return s; |
470 | 0 | }); |
471 | 0 | } |
472 | | |
473 | | Status WriteCommittedTxn::Merge(ColumnFamilyHandle* column_family, |
474 | | const Slice& key, const Slice& value, |
475 | 0 | const bool assume_tracked) { |
476 | 0 | const bool do_validate = !assume_tracked; |
477 | 0 | return Operate(column_family, key, do_validate, assume_tracked, |
478 | 0 | [column_family, &key, &value, this]() { |
479 | 0 | Status s = |
480 | 0 | GetBatchForWrite()->Merge(column_family, key, value); |
481 | 0 | if (s.ok()) { |
482 | 0 | ++num_merges_; |
483 | 0 | } |
484 | 0 | return s; |
485 | 0 | }); |
486 | 0 | } |
487 | | |
488 | | template <typename TKey, typename TOperation> |
489 | | Status WriteCommittedTxn::Operate(ColumnFamilyHandle* column_family, |
490 | | const TKey& key, const bool do_validate, |
491 | | const bool assume_tracked, |
492 | 0 | TOperation&& operation) { |
493 | 0 | Status s; |
494 | 0 | if constexpr (std::is_same_v<Slice, TKey>) { |
495 | 0 | s = TryLock(column_family, key, /*read_only=*/false, /*exclusive=*/true, |
496 | 0 | do_validate, assume_tracked); |
497 | 0 | } else if constexpr (std::is_same_v<SliceParts, TKey>) { |
498 | 0 | std::string key_buf; |
499 | 0 | Slice contiguous_key(key, &key_buf); |
500 | 0 | s = TryLock(column_family, contiguous_key, /*read_only=*/false, |
501 | 0 | /*exclusive=*/true, do_validate, assume_tracked); |
502 | 0 | } |
503 | 0 | if (!s.ok()) { |
504 | 0 | return s; |
505 | 0 | } |
506 | 0 | column_family = |
507 | 0 | column_family ? column_family : db_impl_->DefaultColumnFamily(); |
508 | 0 | assert(column_family); |
509 | 0 | const Comparator* const ucmp = column_family->GetComparator(); |
510 | 0 | assert(ucmp); |
511 | 0 | size_t ts_sz = ucmp->timestamp_size(); |
512 | 0 | if (ts_sz > 0) { |
513 | 0 | assert(ts_sz == sizeof(TxnTimestamp)); |
514 | 0 | if (!IndexingEnabled()) { |
515 | 0 | cfs_with_ts_tracked_when_indexing_disabled_.insert( |
516 | 0 | column_family->GetID()); |
517 | 0 | } |
518 | 0 | } |
519 | 0 | return operation(); |
520 | 0 | } Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::PutEntityImpl(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::vector<rocksdb::WideColumn, std::__1::allocator<rocksdb::WideColumn> > const&, bool, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::PutEntityImpl(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, std::__1::vector<rocksdb::WideColumn, std::__1::allocator<rocksdb::WideColumn> > const&, bool, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::Put(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::Slice const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::Put(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::Slice const&, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::SliceParts, rocksdb::WriteCommittedTxn::Put(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, rocksdb::SliceParts const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool, bool, rocksdb::WriteCommittedTxn::Put(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, rocksdb::SliceParts const&, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::PutUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::Slice const&)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::PutUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::Slice const&)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::SliceParts, rocksdb::WriteCommittedTxn::PutUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, rocksdb::SliceParts const&)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool, bool, rocksdb::WriteCommittedTxn::PutUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, rocksdb::SliceParts const&)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::Delete(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::Delete(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::SliceParts, rocksdb::WriteCommittedTxn::Delete(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool, bool, rocksdb::WriteCommittedTxn::Delete(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::DeleteUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::DeleteUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::SliceParts, rocksdb::WriteCommittedTxn::DeleteUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool, bool, rocksdb::WriteCommittedTxn::DeleteUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::SingleDelete(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::SingleDelete(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::SliceParts, rocksdb::WriteCommittedTxn::SingleDelete(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool, bool, rocksdb::WriteCommittedTxn::SingleDelete(rocksdb::ColumnFamilyHandle*, rocksdb::SliceParts const&, bool)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::SingleDeleteUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::SingleDeleteUntracked(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&)::$_0&&) Unexecuted instantiation: pessimistic_transaction.cc:rocksdb::Status rocksdb::WriteCommittedTxn::Operate<rocksdb::Slice, rocksdb::WriteCommittedTxn::Merge(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::Slice const&, bool)::$_0>(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, bool, bool, rocksdb::WriteCommittedTxn::Merge(rocksdb::ColumnFamilyHandle*, rocksdb::Slice const&, rocksdb::Slice const&, bool)::$_0&&) |
521 | | |
522 | 0 | Status WriteCommittedTxn::SetReadTimestampForValidation(TxnTimestamp ts) { |
523 | 0 | if (read_timestamp_ < kMaxTxnTimestamp && ts < read_timestamp_) { |
524 | 0 | return Status::InvalidArgument( |
525 | 0 | "Cannot decrease read timestamp for validation"); |
526 | 0 | } |
527 | 0 | read_timestamp_ = ts; |
528 | 0 | return Status::OK(); |
529 | 0 | } |
530 | | |
531 | 0 | Status WriteCommittedTxn::SetCommitTimestamp(TxnTimestamp ts) { |
532 | 0 | if (txn_db_impl_->GetTxnDBOptions().enable_udt_validation && |
533 | 0 | read_timestamp_ < kMaxTxnTimestamp && ts <= read_timestamp_) { |
534 | 0 | return Status::InvalidArgument( |
535 | 0 | "Cannot commit at timestamp smaller than or equal to read timestamp"); |
536 | 0 | } |
537 | 0 | commit_timestamp_ = ts; |
538 | 0 | return Status::OK(); |
539 | 0 | } |
540 | | |
541 | 0 | Status PessimisticTransaction::CommitBatch(WriteBatch* batch) { |
542 | 0 | if (batch && WriteBatchInternal::HasKeyWithTimestamp(*batch)) { |
543 | | // CommitBatch() needs to lock the keys in the batch. |
544 | | // However, the application also needs to specify the timestamp for the |
545 | | // keys in batch before calling this API. |
546 | | // This means timestamp order may violate the order of locking, thus |
547 | | // violate the sequence number order for the same user key. |
548 | | // Therefore, we disallow this operation for now. |
549 | 0 | return Status::NotSupported( |
550 | 0 | "Batch to commit includes timestamp assigned before locking"); |
551 | 0 | } |
552 | | |
553 | 0 | std::unique_ptr<LockTracker> keys_to_unlock(lock_tracker_factory_.Create()); |
554 | 0 | Status s = LockBatch(batch, keys_to_unlock.get()); |
555 | |
|
556 | 0 | if (!s.ok()) { |
557 | 0 | return s; |
558 | 0 | } |
559 | | |
560 | 0 | bool can_commit = false; |
561 | |
|
562 | 0 | if (IsExpired()) { |
563 | 0 | s = Status::Expired(); |
564 | 0 | } else if (expiration_time_ > 0) { |
565 | 0 | TransactionState expected = STARTED; |
566 | 0 | can_commit = std::atomic_compare_exchange_strong(&txn_state_, &expected, |
567 | 0 | AWAITING_COMMIT); |
568 | 0 | } else if (txn_state_ == STARTED) { |
569 | | // lock stealing is not a concern |
570 | 0 | can_commit = true; |
571 | 0 | } |
572 | |
|
573 | 0 | if (can_commit) { |
574 | 0 | txn_state_.store(AWAITING_COMMIT); |
575 | 0 | s = CommitBatchInternal(batch); |
576 | 0 | if (s.ok()) { |
577 | 0 | txn_state_.store(COMMITTED); |
578 | 0 | } |
579 | 0 | } else if (txn_state_ == LOCKS_STOLEN) { |
580 | 0 | s = Status::Expired(); |
581 | 0 | } else { |
582 | 0 | s = Status::InvalidArgument("Transaction is not in state for commit."); |
583 | 0 | } |
584 | |
|
585 | 0 | txn_db_impl_->UnLock(this, *keys_to_unlock); |
586 | |
|
587 | 0 | return s; |
588 | 0 | } |
589 | | |
590 | 0 | Status PessimisticTransaction::Prepare() { |
591 | 0 | if (name_.empty()) { |
592 | 0 | return Status::InvalidArgument( |
593 | 0 | "Cannot prepare a transaction that has not been named."); |
594 | 0 | } |
595 | | |
596 | 0 | if (IsExpired()) { |
597 | 0 | return Status::Expired(); |
598 | 0 | } |
599 | | |
600 | 0 | Status s; |
601 | 0 | bool can_prepare = false; |
602 | |
|
603 | 0 | if (expiration_time_ > 0) { |
604 | | // must concern ourselves with expiraton and/or lock stealing |
605 | | // need to compare/exchange bc locks could be stolen under us here |
606 | 0 | TransactionState expected = STARTED; |
607 | 0 | can_prepare = std::atomic_compare_exchange_strong(&txn_state_, &expected, |
608 | 0 | AWAITING_PREPARE); |
609 | 0 | } else if (txn_state_ == STARTED) { |
610 | | // expiration and lock stealing is not possible |
611 | 0 | txn_state_.store(AWAITING_PREPARE); |
612 | 0 | can_prepare = true; |
613 | 0 | } |
614 | |
|
615 | 0 | if (can_prepare) { |
616 | | // transaction can't expire after preparation |
617 | 0 | expiration_time_ = 0; |
618 | 0 | assert(log_number_ == 0 || |
619 | 0 | txn_db_impl_->GetTxnDBOptions().write_policy == WRITE_UNPREPARED); |
620 | |
|
621 | 0 | s = PrepareInternal(); |
622 | 0 | if (s.ok()) { |
623 | 0 | txn_state_.store(PREPARED); |
624 | 0 | } |
625 | 0 | } else if (txn_state_ == LOCKS_STOLEN) { |
626 | 0 | s = Status::Expired(); |
627 | 0 | } else if (txn_state_ == PREPARED) { |
628 | 0 | s = Status::InvalidArgument("Transaction has already been prepared."); |
629 | 0 | } else if (txn_state_ == COMMITTED) { |
630 | 0 | s = Status::InvalidArgument("Transaction has already been committed."); |
631 | 0 | } else if (txn_state_ == ROLLEDBACK) { |
632 | 0 | s = Status::InvalidArgument("Transaction has already been rolledback."); |
633 | 0 | } else { |
634 | 0 | s = Status::InvalidArgument("Transaction is not in state for commit."); |
635 | 0 | } |
636 | |
|
637 | 0 | return s; |
638 | 0 | } |
639 | | |
640 | 0 | Status WriteCommittedTxn::PrepareInternal() { |
641 | 0 | WriteOptions write_options = write_options_; |
642 | 0 | write_options.disableWAL = false; |
643 | 0 | auto s = WriteBatchInternal::MarkEndPrepare(GetWriteBatch()->GetWriteBatch(), |
644 | 0 | name_); |
645 | 0 | assert(s.ok()); |
646 | 0 | class MarkLogCallback : public PreReleaseCallback { |
647 | 0 | public: |
648 | 0 | MarkLogCallback(DBImpl* db, bool two_write_queues) |
649 | 0 | : db_(db), two_write_queues_(two_write_queues) { |
650 | 0 | (void)two_write_queues_; // to silence unused private field warning |
651 | 0 | } |
652 | 0 | Status Callback(SequenceNumber, bool is_mem_disabled, uint64_t log_number, |
653 | 0 | size_t /*index*/, size_t /*total*/) override { |
654 | 0 | #ifdef NDEBUG |
655 | 0 | (void)is_mem_disabled; |
656 | 0 | #endif |
657 | 0 | assert(log_number != 0); |
658 | 0 | assert(!two_write_queues_ || is_mem_disabled); // implies the 2nd queue |
659 | 0 | db_->logs_with_prep_tracker()->MarkLogAsContainingPrepSection(log_number); |
660 | 0 | return Status::OK(); |
661 | 0 | } |
662 | |
|
663 | 0 | private: |
664 | 0 | DBImpl* db_; |
665 | 0 | bool two_write_queues_; |
666 | 0 | } mark_log_callback(db_impl_, |
667 | 0 | db_impl_->immutable_db_options().two_write_queues); |
668 | |
|
669 | 0 | WriteCallback* const kNoWriteCallback = nullptr; |
670 | 0 | const uint64_t kRefNoLog = 0; |
671 | 0 | const bool kDisableMemtable = true; |
672 | 0 | SequenceNumber* const KIgnoreSeqUsed = nullptr; |
673 | 0 | const size_t kNoBatchCount = 0; |
674 | 0 | s = db_impl_->WriteImpl(write_options, GetWriteBatch()->GetWriteBatch(), |
675 | 0 | kNoWriteCallback, /*user_write_cb=*/nullptr, |
676 | 0 | &log_number_, kRefNoLog, kDisableMemtable, |
677 | 0 | KIgnoreSeqUsed, kNoBatchCount, &mark_log_callback); |
678 | 0 | return s; |
679 | 0 | } |
680 | | |
681 | 0 | Status PessimisticTransaction::Commit() { |
682 | 0 | bool commit_without_prepare = false; |
683 | 0 | bool commit_prepared = false; |
684 | |
|
685 | 0 | if (IsExpired()) { |
686 | 0 | return Status::Expired(); |
687 | 0 | } |
688 | | |
689 | 0 | if (expiration_time_ > 0) { |
690 | | // we must atomicaly compare and exchange the state here because at |
691 | | // this state in the transaction it is possible for another thread |
692 | | // to change our state out from under us in the even that we expire and have |
693 | | // our locks stolen. In this case the only valid state is STARTED because |
694 | | // a state of PREPARED would have a cleared expiration_time_. |
695 | 0 | TransactionState expected = STARTED; |
696 | 0 | commit_without_prepare = std::atomic_compare_exchange_strong( |
697 | 0 | &txn_state_, &expected, AWAITING_COMMIT); |
698 | 0 | TEST_SYNC_POINT("TransactionTest::ExpirableTransactionDataRace:1"); |
699 | 0 | } else if (txn_state_ == PREPARED) { |
700 | | // expiration and lock stealing is not a concern |
701 | 0 | commit_prepared = true; |
702 | 0 | } else if (txn_state_ == STARTED) { |
703 | | // expiration and lock stealing is not a concern |
704 | 0 | if (skip_prepare_) { |
705 | 0 | commit_without_prepare = true; |
706 | 0 | } else { |
707 | 0 | return Status::TxnNotPrepared(); |
708 | 0 | } |
709 | 0 | } |
710 | | |
711 | 0 | Status s; |
712 | 0 | if (commit_without_prepare) { |
713 | 0 | assert(!commit_prepared); |
714 | 0 | if (WriteBatchInternal::Count(GetCommitTimeWriteBatch()) > 0) { |
715 | 0 | s = Status::InvalidArgument( |
716 | 0 | "Commit-time batch contains values that will not be committed."); |
717 | 0 | } else { |
718 | 0 | txn_state_.store(AWAITING_COMMIT); |
719 | 0 | if (log_number_ > 0) { |
720 | 0 | dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed( |
721 | 0 | log_number_); |
722 | 0 | } |
723 | 0 | s = CommitWithoutPrepareInternal(); |
724 | 0 | if (!name_.empty()) { |
725 | 0 | txn_db_impl_->UnregisterTransaction(this); |
726 | 0 | } |
727 | 0 | Clear(); |
728 | 0 | if (s.ok()) { |
729 | 0 | txn_state_.store(COMMITTED); |
730 | 0 | } |
731 | 0 | } |
732 | 0 | } else if (commit_prepared) { |
733 | 0 | txn_state_.store(AWAITING_COMMIT); |
734 | |
|
735 | 0 | s = CommitInternal(); |
736 | |
|
737 | 0 | if (!s.ok()) { |
738 | 0 | ROCKS_LOG_WARN(db_impl_->immutable_db_options().info_log, |
739 | 0 | "Commit write failed"); |
740 | | // Keep the transaction rollbackable after the commit marker write fails. |
741 | 0 | txn_state_.store(PREPARED); |
742 | 0 | return s; |
743 | 0 | } |
744 | | |
745 | | // FindObsoleteFiles must now look to the memtables |
746 | | // to determine what prep logs must be kept around, |
747 | | // not the prep section heap. |
748 | 0 | assert(log_number_ > 0); |
749 | 0 | dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed( |
750 | 0 | log_number_); |
751 | 0 | txn_db_impl_->UnregisterTransaction(this); |
752 | |
|
753 | 0 | Clear(); |
754 | 0 | txn_state_.store(COMMITTED); |
755 | 0 | } else if (txn_state_ == LOCKS_STOLEN) { |
756 | 0 | s = Status::Expired(); |
757 | 0 | } else if (txn_state_ == COMMITTED) { |
758 | 0 | s = Status::InvalidArgument("Transaction has already been committed."); |
759 | 0 | } else if (txn_state_ == ROLLEDBACK) { |
760 | 0 | s = Status::InvalidArgument("Transaction has already been rolledback."); |
761 | 0 | } else { |
762 | 0 | s = Status::InvalidArgument("Transaction is not in state for commit."); |
763 | 0 | } |
764 | | |
765 | 0 | return s; |
766 | 0 | } |
767 | | |
768 | 0 | Status WriteCommittedTxn::CommitWithoutPrepareInternal() { |
769 | 0 | WriteBatchWithIndex* wbwi = GetWriteBatch(); |
770 | 0 | assert(wbwi); |
771 | 0 | WriteBatch* wb = wbwi->GetWriteBatch(); |
772 | 0 | assert(wb); |
773 | |
|
774 | 0 | const bool needs_ts = WriteBatchInternal::HasKeyWithTimestamp(*wb); |
775 | 0 | if (needs_ts && commit_timestamp_ == kMaxTxnTimestamp) { |
776 | 0 | return Status::InvalidArgument("Must assign a commit timestamp"); |
777 | 0 | } |
778 | | |
779 | 0 | if (needs_ts) { |
780 | 0 | assert(commit_timestamp_ != kMaxTxnTimestamp); |
781 | 0 | char commit_ts_buf[sizeof(kMaxTxnTimestamp)]; |
782 | 0 | EncodeFixed64(commit_ts_buf, commit_timestamp_); |
783 | 0 | Slice commit_ts(commit_ts_buf, sizeof(commit_ts_buf)); |
784 | |
|
785 | 0 | Status s = wb->UpdateTimestamps( |
786 | 0 | commit_ts, [wb, wbwi, this](uint32_t cf) -> size_t { |
787 | | // First search through timestamp info kept inside the WriteBatch |
788 | | // in case some writes bypassed the Transaction's write APIs. |
789 | 0 | auto cf_id_to_ts_sz = wb->GetColumnFamilyToTimestampSize(); |
790 | 0 | auto iter = cf_id_to_ts_sz.find(cf); |
791 | 0 | if (iter != cf_id_to_ts_sz.end()) { |
792 | 0 | size_t ts_sz = iter->second; |
793 | 0 | return ts_sz; |
794 | 0 | } |
795 | 0 | auto cf_iter = cfs_with_ts_tracked_when_indexing_disabled_.find(cf); |
796 | 0 | if (cf_iter != cfs_with_ts_tracked_when_indexing_disabled_.end()) { |
797 | 0 | return sizeof(kMaxTxnTimestamp); |
798 | 0 | } |
799 | 0 | const Comparator* ucmp = |
800 | 0 | WriteBatchWithIndexInternal::GetUserComparator(*wbwi, cf); |
801 | 0 | return ucmp ? ucmp->timestamp_size() |
802 | 0 | : std::numeric_limits<size_t>::max(); |
803 | 0 | }); |
804 | 0 | if (!s.ok()) { |
805 | 0 | return s; |
806 | 0 | } |
807 | 0 | } |
808 | | |
809 | 0 | uint64_t seq_used = kMaxSequenceNumber; |
810 | 0 | SnapshotCreationCallback snapshot_creation_cb(db_impl_, commit_timestamp_, |
811 | 0 | snapshot_notifier_, snapshot_); |
812 | 0 | PostMemTableCallback* post_mem_cb = nullptr; |
813 | 0 | if (snapshot_needed_) { |
814 | 0 | if (commit_timestamp_ == kMaxTxnTimestamp) { |
815 | 0 | return Status::InvalidArgument("Must set transaction commit timestamp"); |
816 | 0 | } else { |
817 | 0 | post_mem_cb = &snapshot_creation_cb; |
818 | 0 | } |
819 | 0 | } |
820 | 0 | auto s = db_impl_->WriteImpl( |
821 | 0 | write_options_, wb, |
822 | 0 | /*callback*/ nullptr, /*user_write_cb=*/nullptr, /*wal_used*/ nullptr, |
823 | 0 | /*log_ref*/ 0, /*disable_memtable*/ false, &seq_used, /*batch_cnt=*/0, |
824 | 0 | /*pre_release_callback=*/nullptr, post_mem_cb); |
825 | 0 | assert(!s.ok() || seq_used != kMaxSequenceNumber); |
826 | 0 | if (s.ok()) { |
827 | 0 | SetId(seq_used); |
828 | 0 | } |
829 | 0 | return s; |
830 | 0 | } |
831 | | |
832 | 0 | Status WriteCommittedTxn::CommitBatchInternal(WriteBatch* batch, size_t) { |
833 | 0 | uint64_t seq_used = kMaxSequenceNumber; |
834 | 0 | auto s = db_impl_->WriteImpl(write_options_, batch, /*callback*/ nullptr, |
835 | 0 | /*user_write_cb=*/nullptr, |
836 | 0 | /*wal_used*/ nullptr, /*log_ref*/ 0, |
837 | 0 | /*disable_memtable*/ false, &seq_used); |
838 | 0 | assert(!s.ok() || seq_used != kMaxSequenceNumber); |
839 | 0 | if (s.ok()) { |
840 | 0 | SetId(seq_used); |
841 | 0 | } |
842 | 0 | return s; |
843 | 0 | } |
844 | | |
845 | 0 | Status WriteCommittedTxn::CommitInternal() { |
846 | 0 | WriteBatchWithIndex* wbwi = GetWriteBatch(); |
847 | 0 | assert(wbwi); |
848 | 0 | WriteBatch* wb = wbwi->GetWriteBatch(); |
849 | 0 | assert(wb); |
850 | |
|
851 | 0 | const bool needs_ts = WriteBatchInternal::HasKeyWithTimestamp(*wb); |
852 | 0 | if (needs_ts && commit_timestamp_ == kMaxTxnTimestamp) { |
853 | 0 | return Status::InvalidArgument("Must assign a commit timestamp"); |
854 | 0 | } |
855 | | // We take the commit-time batch and append the Commit marker. |
856 | | // The Memtable will ignore the Commit marker in non-recovery mode |
857 | 0 | WriteBatch* working_batch = GetCommitTimeWriteBatch(); |
858 | |
|
859 | 0 | Status s; |
860 | 0 | if (!needs_ts) { |
861 | 0 | s = WriteBatchInternal::MarkCommit(working_batch, name_); |
862 | 0 | } else { |
863 | 0 | assert(!commit_bypass_memtable_threshold_); |
864 | 0 | assert(!commit_bypass_memtable_byte_threshold_); |
865 | 0 | assert(commit_timestamp_ != kMaxTxnTimestamp); |
866 | 0 | char commit_ts_buf[sizeof(kMaxTxnTimestamp)]; |
867 | 0 | EncodeFixed64(commit_ts_buf, commit_timestamp_); |
868 | 0 | Slice commit_ts(commit_ts_buf, sizeof(commit_ts_buf)); |
869 | 0 | s = WriteBatchInternal::MarkCommitWithTimestamp(working_batch, name_, |
870 | 0 | commit_ts); |
871 | 0 | if (s.ok()) { |
872 | 0 | s = wb->UpdateTimestamps( |
873 | 0 | commit_ts, [wb, wbwi, this](uint32_t cf) -> size_t { |
874 | | // first search through timestamp info kept inside the WriteBatch |
875 | | // in case some writes bypassed the Transaction's write APIs. |
876 | 0 | auto cf_id_to_ts_sz = wb->GetColumnFamilyToTimestampSize(); |
877 | 0 | auto iter = cf_id_to_ts_sz.find(cf); |
878 | 0 | if (iter != cf_id_to_ts_sz.end()) { |
879 | 0 | return iter->second; |
880 | 0 | } |
881 | 0 | if (cfs_with_ts_tracked_when_indexing_disabled_.find(cf) != |
882 | 0 | cfs_with_ts_tracked_when_indexing_disabled_.end()) { |
883 | 0 | return sizeof(kMaxTxnTimestamp); |
884 | 0 | } |
885 | 0 | const Comparator* ucmp = |
886 | 0 | WriteBatchWithIndexInternal::GetUserComparator(*wbwi, cf); |
887 | 0 | return ucmp ? ucmp->timestamp_size() |
888 | 0 | : std::numeric_limits<size_t>::max(); |
889 | 0 | }); |
890 | 0 | } |
891 | 0 | } |
892 | |
|
893 | 0 | if (!s.ok()) { |
894 | 0 | return s; |
895 | 0 | } |
896 | | |
897 | | // any operations appended to this working_batch will be ignored from WAL |
898 | 0 | working_batch->MarkWalTerminationPoint(); |
899 | |
|
900 | 0 | uint32_t wb_count = wb->Count(); |
901 | 0 | RecordInHistogram(db_impl_->immutable_db_options_.stats, |
902 | 0 | NUM_OP_PER_TRANSACTION, wb_count); |
903 | 0 | bool bypass_memtable = false; |
904 | 0 | if (!needs_ts) { |
905 | 0 | if (commit_bypass_memtable_threshold_ && |
906 | 0 | wb_count >= commit_bypass_memtable_threshold_) { |
907 | 0 | if (wbwi->GetWBWIOpCount() != wb_count) { |
908 | 0 | ROCKS_LOG_WARN( |
909 | 0 | db_impl_->immutable_db_options().info_log, |
910 | 0 | "Transaction %s qualifies for commit optimization due to update " |
911 | 0 | "count. However, it will commit normally due to wbwi and wb record " |
912 | 0 | "count mismatch. Some updates were added directly to the " |
913 | 0 | "transaction's underlying write batch.", |
914 | 0 | GetName().c_str()); |
915 | 0 | } else { |
916 | 0 | bypass_memtable = true; |
917 | 0 | } |
918 | 0 | } else if (commit_bypass_memtable_byte_threshold_ && |
919 | 0 | wb->GetDataSize() >= commit_bypass_memtable_byte_threshold_) { |
920 | 0 | if (wbwi->GetWBWIOpCount() != wb_count) { |
921 | 0 | ROCKS_LOG_WARN( |
922 | 0 | db_impl_->immutable_db_options().info_log, |
923 | 0 | "Transaction %s qualifies for commit optimization due to write " |
924 | 0 | "batch size. However, it will commit normally due to wbwi and wb " |
925 | 0 | "record count mismatch. Some updates were added directly to the " |
926 | 0 | "transaction's underlying write batch.", |
927 | 0 | GetName().c_str()); |
928 | 0 | } else { |
929 | 0 | bypass_memtable = true; |
930 | 0 | } |
931 | 0 | } |
932 | 0 | } |
933 | | // Blob direct write must go through the normal WriteBatch path in |
934 | | // DBImpl::WriteImpl() so large values can be transformed into BlobIndex |
935 | | // entries. The bypass-memtable optimization commits through WBWI ingestion, |
936 | | // which skips that transformation and does not support kTypeBlobIndex. |
937 | 0 | if (bypass_memtable && db_impl_->HasAnyBlobDirectWriteColumnFamily()) { |
938 | 0 | bypass_memtable = false; |
939 | 0 | } |
940 | 0 | if (!bypass_memtable) { |
941 | | // insert prepared batch into Memtable only skipping WAL. |
942 | | // Memtable will ignore BeginPrepare/EndPrepare markers |
943 | | // in non recovery mode and simply insert the values |
944 | 0 | s = WriteBatchInternal::Append(working_batch, wb); |
945 | 0 | assert(s.ok()); |
946 | 0 | } |
947 | |
|
948 | 0 | uint64_t seq_used = kMaxSequenceNumber; |
949 | 0 | SnapshotCreationCallback snapshot_creation_cb(db_impl_, commit_timestamp_, |
950 | 0 | snapshot_notifier_, snapshot_); |
951 | 0 | PostMemTableCallback* post_mem_cb = nullptr; |
952 | 0 | if (snapshot_needed_) { |
953 | 0 | if (commit_timestamp_ == kMaxTxnTimestamp) { |
954 | 0 | s = Status::InvalidArgument("Must set transaction commit timestamp"); |
955 | 0 | return s; |
956 | 0 | } else { |
957 | 0 | post_mem_cb = &snapshot_creation_cb; |
958 | 0 | } |
959 | 0 | } |
960 | 0 | assert(log_number_ > 0); |
961 | 0 | TEST_SYNC_POINT_CALLBACK("WriteCommittedTxn::CommitInternal:bypass_memtable", |
962 | 0 | static_cast<void*>(&bypass_memtable)); |
963 | 0 | if (bypass_memtable) { |
964 | | // Used for differentiating commiting WBWI vs directly ingesting WBWI |
965 | | // see (IngestWriteBatchWithIndex()) |
966 | 0 | assert(working_batch->HasCommit()); |
967 | 0 | s = db_impl_->WriteImpl( |
968 | 0 | write_options_, working_batch, /*callback*/ nullptr, |
969 | 0 | /*user_write_cb=*/nullptr, |
970 | 0 | /*wal_used*/ nullptr, /*log_ref*/ log_number_, |
971 | 0 | /*disable_memtable*/ false, &seq_used, |
972 | 0 | /*batch_cnt=*/0, /*pre_release_callback=*/nullptr, post_mem_cb, |
973 | | /*wbwi=*/ |
974 | 0 | std::make_shared<WriteBatchWithIndex>(std::move(write_batch_))); |
975 | | // Reset write_batch_ since it's accessed in transaction clean up and |
976 | | // might be used for transaction reuse. |
977 | 0 | write_batch_ = WriteBatchWithIndex(cmp_, 0, true, 0, |
978 | 0 | write_options_.protection_bytes_per_key); |
979 | 0 | } else { |
980 | 0 | s = db_impl_->WriteImpl(write_options_, working_batch, /*callback*/ nullptr, |
981 | 0 | /*user_write_cb=*/nullptr, |
982 | 0 | /*wal_used*/ nullptr, /*log_ref*/ log_number_, |
983 | 0 | /*disable_memtable*/ false, &seq_used, |
984 | 0 | /*batch_cnt=*/0, /*pre_release_callback=*/nullptr, |
985 | 0 | post_mem_cb); |
986 | 0 | } |
987 | 0 | assert(!s.ok() || seq_used != kMaxSequenceNumber); |
988 | 0 | if (s.ok()) { |
989 | 0 | SetId(seq_used); |
990 | 0 | } |
991 | 0 | return s; |
992 | 0 | } |
993 | | |
994 | 0 | Status PessimisticTransaction::Rollback() { |
995 | 0 | Status s; |
996 | 0 | if (txn_state_ == PREPARED) { |
997 | 0 | txn_state_.store(AWAITING_ROLLBACK); |
998 | |
|
999 | 0 | s = RollbackInternal(); |
1000 | |
|
1001 | 0 | if (s.ok()) { |
1002 | | // we do not need to keep our prepared section around |
1003 | 0 | assert(log_number_ > 0); |
1004 | 0 | dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed( |
1005 | 0 | log_number_); |
1006 | 0 | Clear(); |
1007 | 0 | txn_state_.store(ROLLEDBACK); |
1008 | 0 | } else { |
1009 | | // Rollback writes can fail under retryable IO errors. Preserve the state |
1010 | | // so callers can retry after error recovery. |
1011 | 0 | txn_state_.store(PREPARED); |
1012 | 0 | } |
1013 | 0 | } else if (txn_state_ == STARTED) { |
1014 | 0 | if (log_number_ > 0) { |
1015 | 0 | assert(txn_db_impl_->GetTxnDBOptions().write_policy == WRITE_UNPREPARED); |
1016 | 0 | assert(GetId() > 0); |
1017 | 0 | s = RollbackInternal(); |
1018 | |
|
1019 | 0 | if (s.ok()) { |
1020 | 0 | dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed( |
1021 | 0 | log_number_); |
1022 | 0 | } |
1023 | 0 | } |
1024 | | // prepare couldn't have taken place |
1025 | 0 | Clear(); |
1026 | 0 | } else if (txn_state_ == COMMITTED) { |
1027 | 0 | s = Status::InvalidArgument("This transaction has already been committed."); |
1028 | 0 | } else { |
1029 | 0 | s = Status::InvalidArgument( |
1030 | 0 | "Two phase transaction is not in state for rollback."); |
1031 | 0 | } |
1032 | |
|
1033 | 0 | return s; |
1034 | 0 | } |
1035 | | |
1036 | 0 | Status WriteCommittedTxn::RollbackInternal() { |
1037 | 0 | WriteBatch rollback_marker; |
1038 | 0 | auto s = WriteBatchInternal::MarkRollback(&rollback_marker, name_); |
1039 | 0 | assert(s.ok()); |
1040 | 0 | s = db_impl_->WriteImpl(write_options_, &rollback_marker); |
1041 | 0 | return s; |
1042 | 0 | } |
1043 | | |
1044 | 0 | Status PessimisticTransaction::RollbackToSavePoint() { |
1045 | 0 | if (txn_state_ != STARTED) { |
1046 | 0 | return Status::InvalidArgument("Transaction is beyond state for rollback."); |
1047 | 0 | } |
1048 | | |
1049 | 0 | if (save_points_ != nullptr && !save_points_->empty()) { |
1050 | | // Unlock any keys locked since last transaction |
1051 | 0 | auto& save_point_tracker = *save_points_->top().new_locks_; |
1052 | 0 | std::unique_ptr<LockTracker> t( |
1053 | 0 | tracked_locks_->GetTrackedLocksSinceSavePoint(save_point_tracker)); |
1054 | 0 | if (t) { |
1055 | 0 | txn_db_impl_->UnLock(this, *t); |
1056 | 0 | } |
1057 | 0 | } |
1058 | |
|
1059 | 0 | return TransactionBaseImpl::RollbackToSavePoint(); |
1060 | 0 | } |
1061 | | |
1062 | | // Lock all keys in this batch. |
1063 | | // On success, caller should unlock keys_to_unlock |
1064 | | Status PessimisticTransaction::LockBatch(WriteBatch* batch, |
1065 | 0 | LockTracker* keys_to_unlock) { |
1066 | 0 | if (!batch) { |
1067 | 0 | return Status::InvalidArgument("batch is nullptr"); |
1068 | 0 | } |
1069 | | |
1070 | 0 | class Handler : public WriteBatch::Handler { |
1071 | 0 | public: |
1072 | | // Sorted map of column_family_id to sorted set of keys. |
1073 | | // Since LockBatch() always locks keys in sorted order, it cannot deadlock |
1074 | | // with itself. We're not using a comparator here since it doesn't matter |
1075 | | // what the sorting is as long as it's consistent. |
1076 | 0 | std::map<uint32_t, std::set<std::string>> keys_; |
1077 | |
|
1078 | 0 | Handler() = default; |
1079 | |
|
1080 | 0 | void RecordKey(uint32_t column_family_id, const Slice& key) { |
1081 | 0 | auto& cfh_keys = keys_[column_family_id]; |
1082 | 0 | cfh_keys.insert(key.ToString()); |
1083 | 0 | } |
1084 | |
|
1085 | 0 | Status PutCF(uint32_t column_family_id, const Slice& key, |
1086 | 0 | const Slice& /* unused */) override { |
1087 | 0 | RecordKey(column_family_id, key); |
1088 | 0 | return Status::OK(); |
1089 | 0 | } |
1090 | 0 | Status PutEntityCF(uint32_t column_family_id, const Slice& key, |
1091 | 0 | const Slice& /* unused */) override { |
1092 | 0 | RecordKey(column_family_id, key); |
1093 | 0 | return Status::OK(); |
1094 | 0 | } |
1095 | 0 | Status MergeCF(uint32_t column_family_id, const Slice& key, |
1096 | 0 | const Slice& /* unused */) override { |
1097 | 0 | RecordKey(column_family_id, key); |
1098 | 0 | return Status::OK(); |
1099 | 0 | } |
1100 | 0 | Status DeleteCF(uint32_t column_family_id, const Slice& key) override { |
1101 | 0 | RecordKey(column_family_id, key); |
1102 | 0 | return Status::OK(); |
1103 | 0 | } |
1104 | 0 | }; |
1105 | | |
1106 | | // Iterating on this handler will add all keys in this batch into keys |
1107 | 0 | Handler handler; |
1108 | 0 | Status s = batch->Iterate(&handler); |
1109 | 0 | if (!s.ok()) { |
1110 | 0 | return s; |
1111 | 0 | } |
1112 | | |
1113 | | // Attempt to lock all keys |
1114 | 0 | for (const auto& cf_iter : handler.keys_) { |
1115 | 0 | uint32_t cfh_id = cf_iter.first; |
1116 | 0 | auto& cfh_keys = cf_iter.second; |
1117 | |
|
1118 | 0 | for (const auto& key_iter : cfh_keys) { |
1119 | 0 | const std::string& key = key_iter; |
1120 | |
|
1121 | 0 | s = txn_db_impl_->TryLock(this, cfh_id, key, true /* exclusive */); |
1122 | 0 | if (!s.ok()) { |
1123 | 0 | break; |
1124 | 0 | } |
1125 | 0 | PointLockRequest r; |
1126 | 0 | r.column_family_id = cfh_id; |
1127 | 0 | r.key = key; |
1128 | 0 | r.seq = kMaxSequenceNumber; |
1129 | 0 | r.read_only = false; |
1130 | 0 | r.exclusive = true; |
1131 | 0 | keys_to_unlock->Track(r); |
1132 | 0 | } |
1133 | |
|
1134 | 0 | if (!s.ok()) { |
1135 | 0 | break; |
1136 | 0 | } |
1137 | 0 | } |
1138 | |
|
1139 | 0 | if (!s.ok()) { |
1140 | 0 | txn_db_impl_->UnLock(this, *keys_to_unlock); |
1141 | 0 | } |
1142 | |
|
1143 | 0 | return s; |
1144 | 0 | } |
1145 | | |
1146 | | // Attempt to lock this key. |
1147 | | // Returns OK if the key has been successfully locked. Non-ok, otherwise. |
1148 | | // If check_shapshot is true and this transaction has a snapshot set, |
1149 | | // this key will only be locked if there have been no writes to this key since |
1150 | | // the snapshot time. |
1151 | | Status PessimisticTransaction::TryLock(ColumnFamilyHandle* column_family, |
1152 | | const Slice& key, bool read_only, |
1153 | | bool exclusive, const bool do_validate, |
1154 | 0 | const bool assume_tracked) { |
1155 | 0 | assert(!assume_tracked || !do_validate); |
1156 | 0 | Status s; |
1157 | 0 | if (UNLIKELY(skip_concurrency_control_)) { |
1158 | 0 | return s; |
1159 | 0 | } |
1160 | 0 | uint32_t cfh_id = GetColumnFamilyID(column_family); |
1161 | 0 | std::string key_str = key.ToString(); |
1162 | |
|
1163 | 0 | PointLockStatus status; |
1164 | 0 | bool lock_upgrade; |
1165 | 0 | bool previously_locked; |
1166 | 0 | if (tracked_locks_->IsPointLockSupported()) { |
1167 | 0 | status = tracked_locks_->GetPointLockStatus(cfh_id, key_str); |
1168 | 0 | previously_locked = status.locked; |
1169 | 0 | lock_upgrade = previously_locked && exclusive && !status.exclusive; |
1170 | 0 | } else { |
1171 | | // If the record is tracked, we can assume it was locked, too. |
1172 | 0 | previously_locked = assume_tracked; |
1173 | 0 | status.locked = false; |
1174 | 0 | lock_upgrade = false; |
1175 | 0 | } |
1176 | | |
1177 | | // Lock this key if this transactions hasn't already locked it or we require |
1178 | | // an upgrade. |
1179 | 0 | if (!previously_locked || lock_upgrade) { |
1180 | 0 | s = txn_db_impl_->TryLock(this, cfh_id, key_str, exclusive); |
1181 | 0 | } |
1182 | |
|
1183 | 0 | const ColumnFamilyHandle* const cfh = |
1184 | 0 | column_family ? column_family : db_impl_->DefaultColumnFamily(); |
1185 | 0 | assert(cfh); |
1186 | 0 | const Comparator* const ucmp = cfh->GetComparator(); |
1187 | 0 | assert(ucmp); |
1188 | 0 | size_t ts_sz = ucmp->timestamp_size(); |
1189 | |
|
1190 | 0 | SetSnapshotIfNeeded(); |
1191 | | |
1192 | | // Even though we do not care about doing conflict checking for this write, |
1193 | | // we still need to take a lock to make sure we do not cause a conflict with |
1194 | | // some other write. However, we do not need to check if there have been |
1195 | | // any writes since this transaction's snapshot. |
1196 | | // TODO(agiardullo): could optimize by supporting shared txn locks in the |
1197 | | // future. |
1198 | 0 | SequenceNumber tracked_at_seq = |
1199 | 0 | status.locked ? status.seq : kMaxSequenceNumber; |
1200 | 0 | if (!do_validate || (snapshot_ == nullptr && |
1201 | 0 | (0 == ts_sz || kMaxTxnTimestamp == read_timestamp_))) { |
1202 | 0 | if (assume_tracked && !previously_locked && |
1203 | 0 | tracked_locks_->IsPointLockSupported()) { |
1204 | 0 | s = Status::InvalidArgument( |
1205 | 0 | "assume_tracked is set but it is not tracked yet"); |
1206 | 0 | } |
1207 | | // Need to remember the earliest sequence number that we know that this |
1208 | | // key has not been modified after. This is useful if this same |
1209 | | // transaction later tries to lock this key again. |
1210 | 0 | if (tracked_at_seq == kMaxSequenceNumber) { |
1211 | | // Since we haven't checked a snapshot, we only know this key has not |
1212 | | // been modified since after we locked it. |
1213 | | // Note: when last_seq_same_as_publish_seq_==false this is less than the |
1214 | | // latest allocated seq but it is ok since i) this is just a heuristic |
1215 | | // used only as a hint to avoid actual check for conflicts, ii) this would |
1216 | | // cause a false positive only if the snapthot is taken right after the |
1217 | | // lock, which would be an unusual sequence. |
1218 | 0 | tracked_at_seq = db_->GetLatestSequenceNumber(); |
1219 | 0 | } |
1220 | 0 | } else if (s.ok()) { |
1221 | | // If a snapshot is set, we need to make sure the key hasn't been modified |
1222 | | // since the snapshot. This must be done after we locked the key. |
1223 | | // If we already have validated an earilier snapshot it must has been |
1224 | | // reflected in tracked_at_seq and ValidateSnapshot will return OK. |
1225 | 0 | s = ValidateSnapshot(column_family, key, &tracked_at_seq); |
1226 | |
|
1227 | 0 | if (!s.ok()) { |
1228 | | // Failed to validate key |
1229 | | // Unlock key we just locked |
1230 | 0 | if (lock_upgrade) { |
1231 | 0 | s = txn_db_impl_->TryLock(this, cfh_id, key_str, false /* exclusive */); |
1232 | 0 | assert(s.ok()); |
1233 | 0 | } else if (!previously_locked) { |
1234 | 0 | txn_db_impl_->UnLock(this, cfh_id, key.ToString()); |
1235 | 0 | } |
1236 | 0 | } |
1237 | 0 | } |
1238 | |
|
1239 | 0 | if (s.ok()) { |
1240 | | // We must track all the locked keys so that we can unlock them later. If |
1241 | | // the key is already locked, this func will update some stats on the |
1242 | | // tracked key. It could also update the tracked_at_seq if it is lower |
1243 | | // than the existing tracked key seq. These stats are necessary for |
1244 | | // RollbackToSavePoint to determine whether a key can be safely removed |
1245 | | // from tracked_keys_. Removal can only be done if a key was only locked |
1246 | | // during the current savepoint. |
1247 | | // |
1248 | | // Recall that if assume_tracked is true, we assume that TrackKey has been |
1249 | | // called previously since the last savepoint, with the same exclusive |
1250 | | // setting, and at a lower sequence number, so skipping here should be |
1251 | | // safe. |
1252 | 0 | if (!assume_tracked) { |
1253 | 0 | TrackKey(cfh_id, key_str, tracked_at_seq, read_only, exclusive); |
1254 | 0 | } else { |
1255 | | #ifndef NDEBUG |
1256 | | if (tracked_locks_->IsPointLockSupported()) { |
1257 | | PointLockStatus lock_status = |
1258 | | tracked_locks_->GetPointLockStatus(cfh_id, key_str); |
1259 | | assert(lock_status.locked); |
1260 | | assert(lock_status.seq <= tracked_at_seq); |
1261 | | assert(lock_status.exclusive == exclusive); |
1262 | | } |
1263 | | #endif |
1264 | 0 | } |
1265 | 0 | } |
1266 | |
|
1267 | 0 | return s; |
1268 | 0 | } |
1269 | | |
1270 | | Status PessimisticTransaction::GetRangeLock(ColumnFamilyHandle* column_family, |
1271 | | const Endpoint& start_endp, |
1272 | 0 | const Endpoint& end_endp) { |
1273 | 0 | ColumnFamilyHandle* cfh = |
1274 | 0 | column_family ? column_family : db_impl_->DefaultColumnFamily(); |
1275 | 0 | uint32_t cfh_id = GetColumnFamilyID(cfh); |
1276 | |
|
1277 | 0 | Status s = txn_db_impl_->TryRangeLock(this, cfh_id, start_endp, end_endp); |
1278 | |
|
1279 | 0 | if (s.ok()) { |
1280 | 0 | RangeLockRequest req{cfh_id, start_endp, end_endp}; |
1281 | 0 | tracked_locks_->Track(req); |
1282 | 0 | } |
1283 | 0 | return s; |
1284 | 0 | } |
1285 | | |
1286 | | // Return OK() if this key has not been modified more recently than the |
1287 | | // transaction snapshot_. |
1288 | | // tracked_at_seq is the global seq at which we either locked the key or already |
1289 | | // have done ValidateSnapshot. |
1290 | | Status PessimisticTransaction::ValidateSnapshot( |
1291 | | ColumnFamilyHandle* column_family, const Slice& key, |
1292 | 0 | SequenceNumber* tracked_at_seq) { |
1293 | 0 | assert(snapshot_ || read_timestamp_ < kMaxTxnTimestamp); |
1294 | |
|
1295 | 0 | SequenceNumber snap_seq = 0; |
1296 | 0 | if (snapshot_) { |
1297 | 0 | snap_seq = snapshot_->GetSequenceNumber(); |
1298 | 0 | if (*tracked_at_seq <= snap_seq) { |
1299 | | // If the key has been previous validated (or locked) at a sequence number |
1300 | | // earlier than the current snapshot's sequence number, we already know it |
1301 | | // has not been modified aftter snap_seq either. |
1302 | 0 | return Status::OK(); |
1303 | 0 | } |
1304 | 0 | } else { |
1305 | 0 | snap_seq = db_impl_->GetLatestSequenceNumber(); |
1306 | 0 | } |
1307 | | |
1308 | | // Otherwise we have either |
1309 | | // 1: tracked_at_seq == kMaxSequenceNumber, i.e., first time tracking the key |
1310 | | // 2: snap_seq < tracked_at_seq: last time we lock the key was via |
1311 | | // do_validate=false which means we had skipped ValidateSnapshot. In both |
1312 | | // cases we should do ValidateSnapshot now. |
1313 | | |
1314 | 0 | *tracked_at_seq = snap_seq; |
1315 | |
|
1316 | 0 | ColumnFamilyHandle* cfh = |
1317 | 0 | column_family ? column_family : db_impl_->DefaultColumnFamily(); |
1318 | |
|
1319 | 0 | assert(cfh); |
1320 | 0 | const Comparator* const ucmp = cfh->GetComparator(); |
1321 | 0 | assert(ucmp); |
1322 | 0 | size_t ts_sz = ucmp->timestamp_size(); |
1323 | 0 | std::string ts_buf; |
1324 | 0 | if (ts_sz > 0 && read_timestamp_ < kMaxTxnTimestamp) { |
1325 | 0 | assert(ts_sz == sizeof(read_timestamp_)); |
1326 | 0 | PutFixed64(&ts_buf, read_timestamp_); |
1327 | 0 | } |
1328 | |
|
1329 | 0 | return TransactionUtil::CheckKeyForConflicts( |
1330 | 0 | db_impl_, cfh, key.ToString(), snap_seq, ts_sz == 0 ? nullptr : &ts_buf, |
1331 | 0 | false /* cache_only */, |
1332 | 0 | /* snap_checker */ nullptr, |
1333 | 0 | /* min_uncommitted */ kMaxSequenceNumber, |
1334 | 0 | txn_db_impl_->GetTxnDBOptions().enable_udt_validation); |
1335 | 0 | } |
1336 | | |
1337 | 0 | bool PessimisticTransaction::TryStealingLocks() { |
1338 | 0 | assert(IsExpired()); |
1339 | 0 | TransactionState expected = STARTED; |
1340 | 0 | return std::atomic_compare_exchange_strong(&txn_state_, &expected, |
1341 | 0 | LOCKS_STOLEN); |
1342 | 0 | } |
1343 | | |
1344 | | void PessimisticTransaction::UnlockGetForUpdate( |
1345 | 0 | ColumnFamilyHandle* column_family, const Slice& key) { |
1346 | 0 | txn_db_impl_->UnLock(this, GetColumnFamilyID(column_family), key.ToString()); |
1347 | 0 | } |
1348 | | |
1349 | 0 | Status PessimisticTransaction::SetName(const TransactionName& name) { |
1350 | 0 | Status s; |
1351 | 0 | if (txn_state_ == STARTED) { |
1352 | 0 | if (name_.length()) { |
1353 | 0 | s = Status::InvalidArgument("Transaction has already been named."); |
1354 | 0 | } else if (name.length() < 1 || name.length() > 512) { |
1355 | 0 | s = Status::InvalidArgument( |
1356 | 0 | "Transaction name length must be between 1 and 512 chars."); |
1357 | 0 | } else { |
1358 | 0 | name_ = name; |
1359 | 0 | s = txn_db_impl_->RegisterTransaction(this); |
1360 | 0 | if (!s.ok()) { |
1361 | 0 | name_.clear(); |
1362 | 0 | } |
1363 | 0 | } |
1364 | 0 | } else { |
1365 | 0 | s = Status::InvalidArgument("Transaction is beyond state for naming."); |
1366 | 0 | } |
1367 | 0 | return s; |
1368 | 0 | } |
1369 | | |
1370 | | Status PessimisticTransaction::CollapseKey(const ReadOptions& options, |
1371 | | const Slice& key, |
1372 | 0 | ColumnFamilyHandle* column_family) { |
1373 | 0 | auto* cfh = column_family ? column_family : db_impl_->DefaultColumnFamily(); |
1374 | 0 | std::string value; |
1375 | 0 | const auto status = GetForUpdate(options, cfh, key, &value, true, true); |
1376 | 0 | if (!status.ok()) { |
1377 | 0 | return status; |
1378 | 0 | } |
1379 | 0 | return Put(column_family, key, value); |
1380 | 0 | } |
1381 | | |
1382 | | } // namespace ROCKSDB_NAMESPACE |