pessimistic_transaction.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. #ifndef ROCKSDB_LITE
  6. #include "utilities/transactions/pessimistic_transaction.h"
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "db/column_family.h"
  12. #include "db/db_impl/db_impl.h"
  13. #include "rocksdb/comparator.h"
  14. #include "rocksdb/db.h"
  15. #include "rocksdb/snapshot.h"
  16. #include "rocksdb/status.h"
  17. #include "rocksdb/utilities/transaction_db.h"
  18. #include "test_util/sync_point.h"
  19. #include "util/cast_util.h"
  20. #include "util/string_util.h"
  21. #include "utilities/transactions/pessimistic_transaction_db.h"
  22. #include "utilities/transactions/transaction_util.h"
  23. namespace ROCKSDB_NAMESPACE {
  24. struct WriteOptions;
  25. std::atomic<TransactionID> PessimisticTransaction::txn_id_counter_(1);
  26. TransactionID PessimisticTransaction::GenTxnID() {
  27. return txn_id_counter_.fetch_add(1);
  28. }
  29. PessimisticTransaction::PessimisticTransaction(
  30. TransactionDB* txn_db, const WriteOptions& write_options,
  31. const TransactionOptions& txn_options, const bool init)
  32. : TransactionBaseImpl(txn_db->GetRootDB(), write_options),
  33. txn_db_impl_(nullptr),
  34. expiration_time_(0),
  35. txn_id_(0),
  36. waiting_cf_id_(0),
  37. waiting_key_(nullptr),
  38. lock_timeout_(0),
  39. deadlock_detect_(false),
  40. deadlock_detect_depth_(0),
  41. skip_concurrency_control_(false) {
  42. txn_db_impl_ =
  43. static_cast_with_check<PessimisticTransactionDB, TransactionDB>(txn_db);
  44. db_impl_ = static_cast_with_check<DBImpl, DB>(db_);
  45. if (init) {
  46. Initialize(txn_options);
  47. }
  48. }
  49. void PessimisticTransaction::Initialize(const TransactionOptions& txn_options) {
  50. txn_id_ = GenTxnID();
  51. txn_state_ = STARTED;
  52. deadlock_detect_ = txn_options.deadlock_detect;
  53. deadlock_detect_depth_ = txn_options.deadlock_detect_depth;
  54. write_batch_.SetMaxBytes(txn_options.max_write_batch_size);
  55. skip_concurrency_control_ = txn_options.skip_concurrency_control;
  56. lock_timeout_ = txn_options.lock_timeout * 1000;
  57. if (lock_timeout_ < 0) {
  58. // Lock timeout not set, use default
  59. lock_timeout_ =
  60. txn_db_impl_->GetTxnDBOptions().transaction_lock_timeout * 1000;
  61. }
  62. if (txn_options.expiration >= 0) {
  63. expiration_time_ = start_time_ + txn_options.expiration * 1000;
  64. } else {
  65. expiration_time_ = 0;
  66. }
  67. if (txn_options.set_snapshot) {
  68. SetSnapshot();
  69. }
  70. if (expiration_time_ > 0) {
  71. txn_db_impl_->InsertExpirableTransaction(txn_id_, this);
  72. }
  73. use_only_the_last_commit_time_batch_for_recovery_ =
  74. txn_options.use_only_the_last_commit_time_batch_for_recovery;
  75. }
  76. PessimisticTransaction::~PessimisticTransaction() {
  77. txn_db_impl_->UnLock(this, &GetTrackedKeys());
  78. if (expiration_time_ > 0) {
  79. txn_db_impl_->RemoveExpirableTransaction(txn_id_);
  80. }
  81. if (!name_.empty() && txn_state_ != COMMITED) {
  82. txn_db_impl_->UnregisterTransaction(this);
  83. }
  84. }
  85. void PessimisticTransaction::Clear() {
  86. txn_db_impl_->UnLock(this, &GetTrackedKeys());
  87. TransactionBaseImpl::Clear();
  88. }
  89. void PessimisticTransaction::Reinitialize(
  90. TransactionDB* txn_db, const WriteOptions& write_options,
  91. const TransactionOptions& txn_options) {
  92. if (!name_.empty() && txn_state_ != COMMITED) {
  93. txn_db_impl_->UnregisterTransaction(this);
  94. }
  95. TransactionBaseImpl::Reinitialize(txn_db->GetRootDB(), write_options);
  96. Initialize(txn_options);
  97. }
  98. bool PessimisticTransaction::IsExpired() const {
  99. if (expiration_time_ > 0) {
  100. if (db_->GetEnv()->NowMicros() >= expiration_time_) {
  101. // Transaction is expired.
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. WriteCommittedTxn::WriteCommittedTxn(TransactionDB* txn_db,
  108. const WriteOptions& write_options,
  109. const TransactionOptions& txn_options)
  110. : PessimisticTransaction(txn_db, write_options, txn_options){};
  111. Status PessimisticTransaction::CommitBatch(WriteBatch* batch) {
  112. TransactionKeyMap keys_to_unlock;
  113. Status s = LockBatch(batch, &keys_to_unlock);
  114. if (!s.ok()) {
  115. return s;
  116. }
  117. bool can_commit = false;
  118. if (IsExpired()) {
  119. s = Status::Expired();
  120. } else if (expiration_time_ > 0) {
  121. TransactionState expected = STARTED;
  122. can_commit = std::atomic_compare_exchange_strong(&txn_state_, &expected,
  123. AWAITING_COMMIT);
  124. } else if (txn_state_ == STARTED) {
  125. // lock stealing is not a concern
  126. can_commit = true;
  127. }
  128. if (can_commit) {
  129. txn_state_.store(AWAITING_COMMIT);
  130. s = CommitBatchInternal(batch);
  131. if (s.ok()) {
  132. txn_state_.store(COMMITED);
  133. }
  134. } else if (txn_state_ == LOCKS_STOLEN) {
  135. s = Status::Expired();
  136. } else {
  137. s = Status::InvalidArgument("Transaction is not in state for commit.");
  138. }
  139. txn_db_impl_->UnLock(this, &keys_to_unlock);
  140. return s;
  141. }
  142. Status PessimisticTransaction::Prepare() {
  143. Status s;
  144. if (name_.empty()) {
  145. return Status::InvalidArgument(
  146. "Cannot prepare a transaction that has not been named.");
  147. }
  148. if (IsExpired()) {
  149. return Status::Expired();
  150. }
  151. bool can_prepare = false;
  152. if (expiration_time_ > 0) {
  153. // must concern ourselves with expiraton and/or lock stealing
  154. // need to compare/exchange bc locks could be stolen under us here
  155. TransactionState expected = STARTED;
  156. can_prepare = std::atomic_compare_exchange_strong(&txn_state_, &expected,
  157. AWAITING_PREPARE);
  158. } else if (txn_state_ == STARTED) {
  159. // expiration and lock stealing is not possible
  160. can_prepare = true;
  161. }
  162. if (can_prepare) {
  163. txn_state_.store(AWAITING_PREPARE);
  164. // transaction can't expire after preparation
  165. expiration_time_ = 0;
  166. assert(log_number_ == 0 ||
  167. txn_db_impl_->GetTxnDBOptions().write_policy == WRITE_UNPREPARED);
  168. s = PrepareInternal();
  169. if (s.ok()) {
  170. txn_state_.store(PREPARED);
  171. }
  172. } else if (txn_state_ == LOCKS_STOLEN) {
  173. s = Status::Expired();
  174. } else if (txn_state_ == PREPARED) {
  175. s = Status::InvalidArgument("Transaction has already been prepared.");
  176. } else if (txn_state_ == COMMITED) {
  177. s = Status::InvalidArgument("Transaction has already been committed.");
  178. } else if (txn_state_ == ROLLEDBACK) {
  179. s = Status::InvalidArgument("Transaction has already been rolledback.");
  180. } else {
  181. s = Status::InvalidArgument("Transaction is not in state for commit.");
  182. }
  183. return s;
  184. }
  185. Status WriteCommittedTxn::PrepareInternal() {
  186. WriteOptions write_options = write_options_;
  187. write_options.disableWAL = false;
  188. WriteBatchInternal::MarkEndPrepare(GetWriteBatch()->GetWriteBatch(), name_);
  189. class MarkLogCallback : public PreReleaseCallback {
  190. public:
  191. MarkLogCallback(DBImpl* db, bool two_write_queues)
  192. : db_(db), two_write_queues_(two_write_queues) {
  193. (void)two_write_queues_; // to silence unused private field warning
  194. }
  195. virtual Status Callback(SequenceNumber, bool is_mem_disabled,
  196. uint64_t log_number, size_t /*index*/,
  197. size_t /*total*/) override {
  198. #ifdef NDEBUG
  199. (void)is_mem_disabled;
  200. #endif
  201. assert(log_number != 0);
  202. assert(!two_write_queues_ || is_mem_disabled); // implies the 2nd queue
  203. db_->logs_with_prep_tracker()->MarkLogAsContainingPrepSection(log_number);
  204. return Status::OK();
  205. }
  206. private:
  207. DBImpl* db_;
  208. bool two_write_queues_;
  209. } mark_log_callback(db_impl_,
  210. db_impl_->immutable_db_options().two_write_queues);
  211. WriteCallback* const kNoWriteCallback = nullptr;
  212. const uint64_t kRefNoLog = 0;
  213. const bool kDisableMemtable = true;
  214. SequenceNumber* const KIgnoreSeqUsed = nullptr;
  215. const size_t kNoBatchCount = 0;
  216. Status s = db_impl_->WriteImpl(
  217. write_options, GetWriteBatch()->GetWriteBatch(), kNoWriteCallback,
  218. &log_number_, kRefNoLog, kDisableMemtable, KIgnoreSeqUsed, kNoBatchCount,
  219. &mark_log_callback);
  220. return s;
  221. }
  222. Status PessimisticTransaction::Commit() {
  223. Status s;
  224. bool commit_without_prepare = false;
  225. bool commit_prepared = false;
  226. if (IsExpired()) {
  227. return Status::Expired();
  228. }
  229. if (expiration_time_ > 0) {
  230. // we must atomicaly compare and exchange the state here because at
  231. // this state in the transaction it is possible for another thread
  232. // to change our state out from under us in the even that we expire and have
  233. // our locks stolen. In this case the only valid state is STARTED because
  234. // a state of PREPARED would have a cleared expiration_time_.
  235. TransactionState expected = STARTED;
  236. commit_without_prepare = std::atomic_compare_exchange_strong(
  237. &txn_state_, &expected, AWAITING_COMMIT);
  238. TEST_SYNC_POINT("TransactionTest::ExpirableTransactionDataRace:1");
  239. } else if (txn_state_ == PREPARED) {
  240. // expiration and lock stealing is not a concern
  241. commit_prepared = true;
  242. } else if (txn_state_ == STARTED) {
  243. // expiration and lock stealing is not a concern
  244. commit_without_prepare = true;
  245. // TODO(myabandeh): what if the user mistakenly forgets prepare? We should
  246. // add an option so that the user explictly express the intention of
  247. // skipping the prepare phase.
  248. }
  249. if (commit_without_prepare) {
  250. assert(!commit_prepared);
  251. if (WriteBatchInternal::Count(GetCommitTimeWriteBatch()) > 0) {
  252. s = Status::InvalidArgument(
  253. "Commit-time batch contains values that will not be committed.");
  254. } else {
  255. txn_state_.store(AWAITING_COMMIT);
  256. if (log_number_ > 0) {
  257. dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed(
  258. log_number_);
  259. }
  260. s = CommitWithoutPrepareInternal();
  261. if (!name_.empty()) {
  262. txn_db_impl_->UnregisterTransaction(this);
  263. }
  264. Clear();
  265. if (s.ok()) {
  266. txn_state_.store(COMMITED);
  267. }
  268. }
  269. } else if (commit_prepared) {
  270. txn_state_.store(AWAITING_COMMIT);
  271. s = CommitInternal();
  272. if (!s.ok()) {
  273. ROCKS_LOG_WARN(db_impl_->immutable_db_options().info_log,
  274. "Commit write failed");
  275. return s;
  276. }
  277. // FindObsoleteFiles must now look to the memtables
  278. // to determine what prep logs must be kept around,
  279. // not the prep section heap.
  280. assert(log_number_ > 0);
  281. dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed(
  282. log_number_);
  283. txn_db_impl_->UnregisterTransaction(this);
  284. Clear();
  285. txn_state_.store(COMMITED);
  286. } else if (txn_state_ == LOCKS_STOLEN) {
  287. s = Status::Expired();
  288. } else if (txn_state_ == COMMITED) {
  289. s = Status::InvalidArgument("Transaction has already been committed.");
  290. } else if (txn_state_ == ROLLEDBACK) {
  291. s = Status::InvalidArgument("Transaction has already been rolledback.");
  292. } else {
  293. s = Status::InvalidArgument("Transaction is not in state for commit.");
  294. }
  295. return s;
  296. }
  297. Status WriteCommittedTxn::CommitWithoutPrepareInternal() {
  298. uint64_t seq_used = kMaxSequenceNumber;
  299. auto s =
  300. db_impl_->WriteImpl(write_options_, GetWriteBatch()->GetWriteBatch(),
  301. /*callback*/ nullptr, /*log_used*/ nullptr,
  302. /*log_ref*/ 0, /*disable_memtable*/ false, &seq_used);
  303. assert(!s.ok() || seq_used != kMaxSequenceNumber);
  304. if (s.ok()) {
  305. SetId(seq_used);
  306. }
  307. return s;
  308. }
  309. Status WriteCommittedTxn::CommitBatchInternal(WriteBatch* batch, size_t) {
  310. uint64_t seq_used = kMaxSequenceNumber;
  311. auto s = db_impl_->WriteImpl(write_options_, batch, /*callback*/ nullptr,
  312. /*log_used*/ nullptr, /*log_ref*/ 0,
  313. /*disable_memtable*/ false, &seq_used);
  314. assert(!s.ok() || seq_used != kMaxSequenceNumber);
  315. if (s.ok()) {
  316. SetId(seq_used);
  317. }
  318. return s;
  319. }
  320. Status WriteCommittedTxn::CommitInternal() {
  321. // We take the commit-time batch and append the Commit marker.
  322. // The Memtable will ignore the Commit marker in non-recovery mode
  323. WriteBatch* working_batch = GetCommitTimeWriteBatch();
  324. WriteBatchInternal::MarkCommit(working_batch, name_);
  325. // any operations appended to this working_batch will be ignored from WAL
  326. working_batch->MarkWalTerminationPoint();
  327. // insert prepared batch into Memtable only skipping WAL.
  328. // Memtable will ignore BeginPrepare/EndPrepare markers
  329. // in non recovery mode and simply insert the values
  330. WriteBatchInternal::Append(working_batch, GetWriteBatch()->GetWriteBatch());
  331. uint64_t seq_used = kMaxSequenceNumber;
  332. auto s =
  333. db_impl_->WriteImpl(write_options_, working_batch, /*callback*/ nullptr,
  334. /*log_used*/ nullptr, /*log_ref*/ log_number_,
  335. /*disable_memtable*/ false, &seq_used);
  336. assert(!s.ok() || seq_used != kMaxSequenceNumber);
  337. if (s.ok()) {
  338. SetId(seq_used);
  339. }
  340. return s;
  341. }
  342. Status PessimisticTransaction::Rollback() {
  343. Status s;
  344. if (txn_state_ == PREPARED) {
  345. txn_state_.store(AWAITING_ROLLBACK);
  346. s = RollbackInternal();
  347. if (s.ok()) {
  348. // we do not need to keep our prepared section around
  349. assert(log_number_ > 0);
  350. dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed(
  351. log_number_);
  352. Clear();
  353. txn_state_.store(ROLLEDBACK);
  354. }
  355. } else if (txn_state_ == STARTED) {
  356. if (log_number_ > 0) {
  357. assert(txn_db_impl_->GetTxnDBOptions().write_policy == WRITE_UNPREPARED);
  358. assert(GetId() > 0);
  359. s = RollbackInternal();
  360. if (s.ok()) {
  361. dbimpl_->logs_with_prep_tracker()->MarkLogAsHavingPrepSectionFlushed(
  362. log_number_);
  363. }
  364. }
  365. // prepare couldn't have taken place
  366. Clear();
  367. } else if (txn_state_ == COMMITED) {
  368. s = Status::InvalidArgument("This transaction has already been committed.");
  369. } else {
  370. s = Status::InvalidArgument(
  371. "Two phase transaction is not in state for rollback.");
  372. }
  373. return s;
  374. }
  375. Status WriteCommittedTxn::RollbackInternal() {
  376. WriteBatch rollback_marker;
  377. WriteBatchInternal::MarkRollback(&rollback_marker, name_);
  378. auto s = db_impl_->WriteImpl(write_options_, &rollback_marker);
  379. return s;
  380. }
  381. Status PessimisticTransaction::RollbackToSavePoint() {
  382. if (txn_state_ != STARTED) {
  383. return Status::InvalidArgument("Transaction is beyond state for rollback.");
  384. }
  385. // Unlock any keys locked since last transaction
  386. const std::unique_ptr<TransactionKeyMap>& keys =
  387. GetTrackedKeysSinceSavePoint();
  388. if (keys) {
  389. txn_db_impl_->UnLock(this, keys.get());
  390. }
  391. return TransactionBaseImpl::RollbackToSavePoint();
  392. }
  393. // Lock all keys in this batch.
  394. // On success, caller should unlock keys_to_unlock
  395. Status PessimisticTransaction::LockBatch(WriteBatch* batch,
  396. TransactionKeyMap* keys_to_unlock) {
  397. class Handler : public WriteBatch::Handler {
  398. public:
  399. // Sorted map of column_family_id to sorted set of keys.
  400. // Since LockBatch() always locks keys in sorted order, it cannot deadlock
  401. // with itself. We're not using a comparator here since it doesn't matter
  402. // what the sorting is as long as it's consistent.
  403. std::map<uint32_t, std::set<std::string>> keys_;
  404. Handler() {}
  405. void RecordKey(uint32_t column_family_id, const Slice& key) {
  406. std::string key_str = key.ToString();
  407. auto& cfh_keys = keys_[column_family_id];
  408. auto iter = cfh_keys.find(key_str);
  409. if (iter == cfh_keys.end()) {
  410. // key not yet seen, store it.
  411. cfh_keys.insert({std::move(key_str)});
  412. }
  413. }
  414. Status PutCF(uint32_t column_family_id, const Slice& key,
  415. const Slice& /* unused */) override {
  416. RecordKey(column_family_id, key);
  417. return Status::OK();
  418. }
  419. Status MergeCF(uint32_t column_family_id, const Slice& key,
  420. const Slice& /* unused */) override {
  421. RecordKey(column_family_id, key);
  422. return Status::OK();
  423. }
  424. Status DeleteCF(uint32_t column_family_id, const Slice& key) override {
  425. RecordKey(column_family_id, key);
  426. return Status::OK();
  427. }
  428. };
  429. // Iterating on this handler will add all keys in this batch into keys
  430. Handler handler;
  431. batch->Iterate(&handler);
  432. Status s;
  433. // Attempt to lock all keys
  434. for (const auto& cf_iter : handler.keys_) {
  435. uint32_t cfh_id = cf_iter.first;
  436. auto& cfh_keys = cf_iter.second;
  437. for (const auto& key_iter : cfh_keys) {
  438. const std::string& key = key_iter;
  439. s = txn_db_impl_->TryLock(this, cfh_id, key, true /* exclusive */);
  440. if (!s.ok()) {
  441. break;
  442. }
  443. TrackKey(keys_to_unlock, cfh_id, std::move(key), kMaxSequenceNumber,
  444. false, true /* exclusive */);
  445. }
  446. if (!s.ok()) {
  447. break;
  448. }
  449. }
  450. if (!s.ok()) {
  451. txn_db_impl_->UnLock(this, keys_to_unlock);
  452. }
  453. return s;
  454. }
  455. // Attempt to lock this key.
  456. // Returns OK if the key has been successfully locked. Non-ok, otherwise.
  457. // If check_shapshot is true and this transaction has a snapshot set,
  458. // this key will only be locked if there have been no writes to this key since
  459. // the snapshot time.
  460. Status PessimisticTransaction::TryLock(ColumnFamilyHandle* column_family,
  461. const Slice& key, bool read_only,
  462. bool exclusive, const bool do_validate,
  463. const bool assume_tracked) {
  464. assert(!assume_tracked || !do_validate);
  465. Status s;
  466. if (UNLIKELY(skip_concurrency_control_)) {
  467. return s;
  468. }
  469. uint32_t cfh_id = GetColumnFamilyID(column_family);
  470. std::string key_str = key.ToString();
  471. bool previously_locked;
  472. bool lock_upgrade = false;
  473. // lock this key if this transactions hasn't already locked it
  474. SequenceNumber tracked_at_seq = kMaxSequenceNumber;
  475. const auto& tracked_keys = GetTrackedKeys();
  476. const auto tracked_keys_cf = tracked_keys.find(cfh_id);
  477. if (tracked_keys_cf == tracked_keys.end()) {
  478. previously_locked = false;
  479. } else {
  480. auto iter = tracked_keys_cf->second.find(key_str);
  481. if (iter == tracked_keys_cf->second.end()) {
  482. previously_locked = false;
  483. } else {
  484. if (!iter->second.exclusive && exclusive) {
  485. lock_upgrade = true;
  486. }
  487. previously_locked = true;
  488. tracked_at_seq = iter->second.seq;
  489. }
  490. }
  491. // Lock this key if this transactions hasn't already locked it or we require
  492. // an upgrade.
  493. if (!previously_locked || lock_upgrade) {
  494. s = txn_db_impl_->TryLock(this, cfh_id, key_str, exclusive);
  495. }
  496. SetSnapshotIfNeeded();
  497. // Even though we do not care about doing conflict checking for this write,
  498. // we still need to take a lock to make sure we do not cause a conflict with
  499. // some other write. However, we do not need to check if there have been
  500. // any writes since this transaction's snapshot.
  501. // TODO(agiardullo): could optimize by supporting shared txn locks in the
  502. // future
  503. if (!do_validate || snapshot_ == nullptr) {
  504. if (assume_tracked && !previously_locked) {
  505. s = Status::InvalidArgument(
  506. "assume_tracked is set but it is not tracked yet");
  507. }
  508. // Need to remember the earliest sequence number that we know that this
  509. // key has not been modified after. This is useful if this same
  510. // transaction
  511. // later tries to lock this key again.
  512. if (tracked_at_seq == kMaxSequenceNumber) {
  513. // Since we haven't checked a snapshot, we only know this key has not
  514. // been modified since after we locked it.
  515. // Note: when last_seq_same_as_publish_seq_==false this is less than the
  516. // latest allocated seq but it is ok since i) this is just a heuristic
  517. // used only as a hint to avoid actual check for conflicts, ii) this would
  518. // cause a false positive only if the snapthot is taken right after the
  519. // lock, which would be an unusual sequence.
  520. tracked_at_seq = db_->GetLatestSequenceNumber();
  521. }
  522. } else {
  523. // If a snapshot is set, we need to make sure the key hasn't been modified
  524. // since the snapshot. This must be done after we locked the key.
  525. // If we already have validated an earilier snapshot it must has been
  526. // reflected in tracked_at_seq and ValidateSnapshot will return OK.
  527. if (s.ok()) {
  528. s = ValidateSnapshot(column_family, key, &tracked_at_seq);
  529. if (!s.ok()) {
  530. // Failed to validate key
  531. if (!previously_locked) {
  532. // Unlock key we just locked
  533. if (lock_upgrade) {
  534. s = txn_db_impl_->TryLock(this, cfh_id, key_str,
  535. false /* exclusive */);
  536. assert(s.ok());
  537. } else {
  538. txn_db_impl_->UnLock(this, cfh_id, key.ToString());
  539. }
  540. }
  541. }
  542. }
  543. }
  544. if (s.ok()) {
  545. // We must track all the locked keys so that we can unlock them later. If
  546. // the key is already locked, this func will update some stats on the
  547. // tracked key. It could also update the tracked_at_seq if it is lower
  548. // than the existing tracked key seq. These stats are necessary for
  549. // RollbackToSavePoint to determine whether a key can be safely removed
  550. // from tracked_keys_. Removal can only be done if a key was only locked
  551. // during the current savepoint.
  552. //
  553. // Recall that if assume_tracked is true, we assume that TrackKey has been
  554. // called previously since the last savepoint, with the same exclusive
  555. // setting, and at a lower sequence number, so skipping here should be
  556. // safe.
  557. if (!assume_tracked) {
  558. TrackKey(cfh_id, key_str, tracked_at_seq, read_only, exclusive);
  559. } else {
  560. #ifndef NDEBUG
  561. assert(tracked_keys_cf->second.count(key_str) > 0);
  562. const auto& info = tracked_keys_cf->second.find(key_str)->second;
  563. assert(info.seq <= tracked_at_seq);
  564. assert(info.exclusive == exclusive);
  565. #endif
  566. }
  567. }
  568. return s;
  569. }
  570. // Return OK() if this key has not been modified more recently than the
  571. // transaction snapshot_.
  572. // tracked_at_seq is the global seq at which we either locked the key or already
  573. // have done ValidateSnapshot.
  574. Status PessimisticTransaction::ValidateSnapshot(
  575. ColumnFamilyHandle* column_family, const Slice& key,
  576. SequenceNumber* tracked_at_seq) {
  577. assert(snapshot_);
  578. SequenceNumber snap_seq = snapshot_->GetSequenceNumber();
  579. if (*tracked_at_seq <= snap_seq) {
  580. // If the key has been previous validated (or locked) at a sequence number
  581. // earlier than the current snapshot's sequence number, we already know it
  582. // has not been modified aftter snap_seq either.
  583. return Status::OK();
  584. }
  585. // Otherwise we have either
  586. // 1: tracked_at_seq == kMaxSequenceNumber, i.e., first time tracking the key
  587. // 2: snap_seq < tracked_at_seq: last time we lock the key was via
  588. // do_validate=false which means we had skipped ValidateSnapshot. In both
  589. // cases we should do ValidateSnapshot now.
  590. *tracked_at_seq = snap_seq;
  591. ColumnFamilyHandle* cfh =
  592. column_family ? column_family : db_impl_->DefaultColumnFamily();
  593. return TransactionUtil::CheckKeyForConflicts(
  594. db_impl_, cfh, key.ToString(), snap_seq, false /* cache_only */);
  595. }
  596. bool PessimisticTransaction::TryStealingLocks() {
  597. assert(IsExpired());
  598. TransactionState expected = STARTED;
  599. return std::atomic_compare_exchange_strong(&txn_state_, &expected,
  600. LOCKS_STOLEN);
  601. }
  602. void PessimisticTransaction::UnlockGetForUpdate(
  603. ColumnFamilyHandle* column_family, const Slice& key) {
  604. txn_db_impl_->UnLock(this, GetColumnFamilyID(column_family), key.ToString());
  605. }
  606. Status PessimisticTransaction::SetName(const TransactionName& name) {
  607. Status s;
  608. if (txn_state_ == STARTED) {
  609. if (name_.length()) {
  610. s = Status::InvalidArgument("Transaction has already been named.");
  611. } else if (txn_db_impl_->GetTransactionByName(name) != nullptr) {
  612. s = Status::InvalidArgument("Transaction name must be unique.");
  613. } else if (name.length() < 1 || name.length() > 512) {
  614. s = Status::InvalidArgument(
  615. "Transaction name length must be between 1 and 512 chars.");
  616. } else {
  617. name_ = name;
  618. txn_db_impl_->RegisterTransaction(this);
  619. }
  620. } else {
  621. s = Status::InvalidArgument("Transaction is beyond state for naming.");
  622. }
  623. return s;
  624. }
  625. } // namespace ROCKSDB_NAMESPACE
  626. #endif // ROCKSDB_LITE