pessimistic_transaction.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #pragma once
  6. #ifndef ROCKSDB_LITE
  7. #include <algorithm>
  8. #include <atomic>
  9. #include <mutex>
  10. #include <stack>
  11. #include <string>
  12. #include <unordered_map>
  13. #include <vector>
  14. #include "db/write_callback.h"
  15. #include "rocksdb/db.h"
  16. #include "rocksdb/slice.h"
  17. #include "rocksdb/snapshot.h"
  18. #include "rocksdb/status.h"
  19. #include "rocksdb/types.h"
  20. #include "rocksdb/utilities/transaction.h"
  21. #include "rocksdb/utilities/transaction_db.h"
  22. #include "rocksdb/utilities/write_batch_with_index.h"
  23. #include "util/autovector.h"
  24. #include "utilities/transactions/transaction_base.h"
  25. #include "utilities/transactions/transaction_util.h"
  26. namespace ROCKSDB_NAMESPACE {
  27. class PessimisticTransactionDB;
  28. // A transaction under pessimistic concurrency control. This class implements
  29. // the locking API and interfaces with the lock manager as well as the
  30. // pessimistic transactional db.
  31. class PessimisticTransaction : public TransactionBaseImpl {
  32. public:
  33. PessimisticTransaction(TransactionDB* db, const WriteOptions& write_options,
  34. const TransactionOptions& txn_options,
  35. const bool init = true);
  36. // No copying allowed
  37. PessimisticTransaction(const PessimisticTransaction&) = delete;
  38. void operator=(const PessimisticTransaction&) = delete;
  39. virtual ~PessimisticTransaction();
  40. void Reinitialize(TransactionDB* txn_db, const WriteOptions& write_options,
  41. const TransactionOptions& txn_options);
  42. Status Prepare() override;
  43. Status Commit() override;
  44. // It is basically Commit without going through Prepare phase. The write batch
  45. // is also directly provided instead of expecting txn to gradually batch the
  46. // transactions writes to an internal write batch.
  47. Status CommitBatch(WriteBatch* batch);
  48. Status Rollback() override;
  49. Status RollbackToSavePoint() override;
  50. Status SetName(const TransactionName& name) override;
  51. // Generate a new unique transaction identifier
  52. static TransactionID GenTxnID();
  53. TransactionID GetID() const override { return txn_id_; }
  54. std::vector<TransactionID> GetWaitingTxns(uint32_t* column_family_id,
  55. std::string* key) const override {
  56. std::lock_guard<std::mutex> lock(wait_mutex_);
  57. std::vector<TransactionID> ids(waiting_txn_ids_.size());
  58. if (key) *key = waiting_key_ ? *waiting_key_ : "";
  59. if (column_family_id) *column_family_id = waiting_cf_id_;
  60. std::copy(waiting_txn_ids_.begin(), waiting_txn_ids_.end(), ids.begin());
  61. return ids;
  62. }
  63. void SetWaitingTxn(autovector<TransactionID> ids, uint32_t column_family_id,
  64. const std::string* key) {
  65. std::lock_guard<std::mutex> lock(wait_mutex_);
  66. waiting_txn_ids_ = ids;
  67. waiting_cf_id_ = column_family_id;
  68. waiting_key_ = key;
  69. }
  70. void ClearWaitingTxn() {
  71. std::lock_guard<std::mutex> lock(wait_mutex_);
  72. waiting_txn_ids_.clear();
  73. waiting_cf_id_ = 0;
  74. waiting_key_ = nullptr;
  75. }
  76. // Returns the time (in microseconds according to Env->GetMicros())
  77. // that this transaction will be expired. Returns 0 if this transaction does
  78. // not expire.
  79. uint64_t GetExpirationTime() const { return expiration_time_; }
  80. // returns true if this transaction has an expiration_time and has expired.
  81. bool IsExpired() const;
  82. // Returns the number of microseconds a transaction can wait on acquiring a
  83. // lock or -1 if there is no timeout.
  84. int64_t GetLockTimeout() const { return lock_timeout_; }
  85. void SetLockTimeout(int64_t timeout) override {
  86. lock_timeout_ = timeout * 1000;
  87. }
  88. // Returns true if locks were stolen successfully, false otherwise.
  89. bool TryStealingLocks();
  90. bool IsDeadlockDetect() const override { return deadlock_detect_; }
  91. int64_t GetDeadlockDetectDepth() const { return deadlock_detect_depth_; }
  92. protected:
  93. // Refer to
  94. // TransactionOptions::use_only_the_last_commit_time_batch_for_recovery
  95. bool use_only_the_last_commit_time_batch_for_recovery_ = false;
  96. virtual Status PrepareInternal() = 0;
  97. virtual Status CommitWithoutPrepareInternal() = 0;
  98. // batch_cnt if non-zero is the number of sub-batches. A sub-batch is a batch
  99. // with no duplicate keys. If zero, then the number of sub-batches is unknown.
  100. virtual Status CommitBatchInternal(WriteBatch* batch,
  101. size_t batch_cnt = 0) = 0;
  102. virtual Status CommitInternal() = 0;
  103. virtual Status RollbackInternal() = 0;
  104. virtual void Initialize(const TransactionOptions& txn_options);
  105. Status LockBatch(WriteBatch* batch, TransactionKeyMap* keys_to_unlock);
  106. Status TryLock(ColumnFamilyHandle* column_family, const Slice& key,
  107. bool read_only, bool exclusive, const bool do_validate = true,
  108. const bool assume_tracked = false) override;
  109. void Clear() override;
  110. PessimisticTransactionDB* txn_db_impl_;
  111. DBImpl* db_impl_;
  112. // If non-zero, this transaction should not be committed after this time (in
  113. // microseconds according to Env->NowMicros())
  114. uint64_t expiration_time_;
  115. private:
  116. friend class TransactionTest_ValidateSnapshotTest_Test;
  117. // Used to create unique ids for transactions.
  118. static std::atomic<TransactionID> txn_id_counter_;
  119. // Unique ID for this transaction
  120. TransactionID txn_id_;
  121. // IDs for the transactions that are blocking the current transaction.
  122. //
  123. // empty if current transaction is not waiting.
  124. autovector<TransactionID> waiting_txn_ids_;
  125. // The following two represents the (cf, key) that a transaction is waiting
  126. // on.
  127. //
  128. // If waiting_key_ is not null, then the pointer should always point to
  129. // a valid string object. The reason is that it is only non-null when the
  130. // transaction is blocked in the TransactionLockMgr::AcquireWithTimeout
  131. // function. At that point, the key string object is one of the function
  132. // parameters.
  133. uint32_t waiting_cf_id_;
  134. const std::string* waiting_key_;
  135. // Mutex protecting waiting_txn_ids_, waiting_cf_id_ and waiting_key_.
  136. mutable std::mutex wait_mutex_;
  137. // Timeout in microseconds when locking a key or -1 if there is no timeout.
  138. int64_t lock_timeout_;
  139. // Whether to perform deadlock detection or not.
  140. bool deadlock_detect_;
  141. // Whether to perform deadlock detection or not.
  142. int64_t deadlock_detect_depth_;
  143. // Refer to TransactionOptions::skip_concurrency_control
  144. bool skip_concurrency_control_;
  145. virtual Status ValidateSnapshot(ColumnFamilyHandle* column_family,
  146. const Slice& key,
  147. SequenceNumber* tracked_at_seq);
  148. void UnlockGetForUpdate(ColumnFamilyHandle* column_family,
  149. const Slice& key) override;
  150. };
  151. class WriteCommittedTxn : public PessimisticTransaction {
  152. public:
  153. WriteCommittedTxn(TransactionDB* db, const WriteOptions& write_options,
  154. const TransactionOptions& txn_options);
  155. // No copying allowed
  156. WriteCommittedTxn(const WriteCommittedTxn&) = delete;
  157. void operator=(const WriteCommittedTxn&) = delete;
  158. virtual ~WriteCommittedTxn() {}
  159. private:
  160. Status PrepareInternal() override;
  161. Status CommitWithoutPrepareInternal() override;
  162. Status CommitBatchInternal(WriteBatch* batch, size_t batch_cnt) override;
  163. Status CommitInternal() override;
  164. Status RollbackInternal() override;
  165. };
  166. } // namespace ROCKSDB_NAMESPACE
  167. #endif // ROCKSDB_LITE