lock_tracker.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include <memory>
  7. #include "rocksdb/rocksdb_namespace.h"
  8. #include "rocksdb/status.h"
  9. #include "rocksdb/types.h"
  10. #include "rocksdb/utilities/transaction_db.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. // Request for locking a single key.
  13. struct PointLockRequest {
  14. // The id of the key's column family.
  15. ColumnFamilyId column_family_id = 0;
  16. // The key to lock.
  17. std::string key;
  18. // The sequence number from which there is no concurrent update to key.
  19. SequenceNumber seq = 0;
  20. // Whether the lock is acquired only for read.
  21. bool read_only = false;
  22. // Whether the lock is in exclusive mode.
  23. bool exclusive = true;
  24. };
  25. // Request for locking a range of keys.
  26. struct RangeLockRequest {
  27. // The id of the key's column family.
  28. ColumnFamilyId column_family_id;
  29. // The range to be locked
  30. Endpoint start_endp;
  31. Endpoint end_endp;
  32. };
  33. struct PointLockStatus {
  34. // Whether the key is locked.
  35. bool locked = false;
  36. // Whether the key is locked in exclusive mode.
  37. bool exclusive = true;
  38. // The sequence number in the tracked PointLockRequest.
  39. SequenceNumber seq = 0;
  40. };
  41. // Return status when calling LockTracker::Untrack.
  42. enum class UntrackStatus {
  43. // The lock is not tracked at all, so no lock to untrack.
  44. NOT_TRACKED,
  45. // The lock is untracked but not removed from the tracker.
  46. UNTRACKED,
  47. // The lock is removed from the tracker.
  48. REMOVED,
  49. };
  50. // Tracks the lock requests.
  51. // In PessimisticTransaction, it tracks the locks acquired through LockMgr;
  52. // In OptimisticTransaction, since there is no LockMgr, it tracks the lock
  53. // intention. Not thread-safe.
  54. class LockTracker {
  55. public:
  56. virtual ~LockTracker() {}
  57. // Whether supports locking a specific key.
  58. virtual bool IsPointLockSupported() const = 0;
  59. // Whether supports locking a range of keys.
  60. virtual bool IsRangeLockSupported() const = 0;
  61. // Tracks the acquirement of a lock on key.
  62. //
  63. // If this method is not supported, leave it as a no-op.
  64. virtual void Track(const PointLockRequest& /*lock_request*/) = 0;
  65. // Untracks the lock on a key.
  66. // seq and exclusive in lock_request are not used.
  67. //
  68. // If this method is not supported, leave it as a no-op and
  69. // returns NOT_TRACKED.
  70. virtual UntrackStatus Untrack(const PointLockRequest& /*lock_request*/) = 0;
  71. // Counterpart of Track(const PointLockRequest&) for RangeLockRequest.
  72. virtual void Track(const RangeLockRequest& /*lock_request*/) = 0;
  73. // Counterpart of Untrack(const PointLockRequest&) for RangeLockRequest.
  74. virtual UntrackStatus Untrack(const RangeLockRequest& /*lock_request*/) = 0;
  75. // Merges lock requests tracked in the specified tracker into the current
  76. // tracker.
  77. //
  78. // E.g. for point lock, if a key in tracker is not yet tracked,
  79. // track this new key; otherwise, merge the tracked information of the key
  80. // such as lock's exclusiveness, read/write statistics.
  81. //
  82. // If this method is not supported, leave it as a no-op.
  83. //
  84. // REQUIRED: the specified tracker must be of the same concrete class type as
  85. // the current tracker.
  86. virtual void Merge(const LockTracker& /*tracker*/) = 0;
  87. // This is a reverse operation of Merge.
  88. //
  89. // E.g. for point lock, if a key exists in both current and the sepcified
  90. // tracker, then subtract the information (such as read/write statistics) of
  91. // the key in the specified tracker from the current tracker.
  92. //
  93. // If this method is not supported, leave it as a no-op.
  94. //
  95. // REQUIRED:
  96. // The specified tracker must be of the same concrete class type as
  97. // the current tracker.
  98. // The tracked locks in the specified tracker must be a subset of those
  99. // tracked by the current tracker.
  100. virtual void Subtract(const LockTracker& /*tracker*/) = 0;
  101. // Clears all tracked locks.
  102. virtual void Clear() = 0;
  103. // Gets the new locks (excluding the locks that have been tracked before the
  104. // save point) tracked since the specified save point, the result is stored
  105. // in an internally constructed LockTracker and returned.
  106. //
  107. // save_point_tracker is the tracker used by a SavePoint to track locks
  108. // tracked after creating the SavePoint.
  109. //
  110. // The implementation should document whether point lock, or range lock, or
  111. // both are considered in this method.
  112. // If this method is not supported, returns nullptr.
  113. //
  114. // REQUIRED:
  115. // The save_point_tracker must be of the same concrete class type as the
  116. // current tracker.
  117. // The tracked locks in the specified tracker must be a subset of those
  118. // tracked by the current tracker.
  119. virtual LockTracker* GetTrackedLocksSinceSavePoint(
  120. const LockTracker& /*save_point_tracker*/) const = 0;
  121. // Gets lock related information of the key.
  122. //
  123. // If point lock is not supported, always returns LockStatus with
  124. // locked=false.
  125. virtual PointLockStatus GetPointLockStatus(
  126. ColumnFamilyId /*column_family_id*/,
  127. const std::string& /*key*/) const = 0;
  128. // Gets number of tracked point locks.
  129. //
  130. // If point lock is not supported, always returns 0.
  131. virtual uint64_t GetNumPointLocks() const = 0;
  132. class ColumnFamilyIterator {
  133. public:
  134. virtual ~ColumnFamilyIterator() {}
  135. // Whether there are remaining column families.
  136. virtual bool HasNext() const = 0;
  137. // Gets next column family id.
  138. //
  139. // If HasNext is false, calling this method has undefined behavior.
  140. virtual ColumnFamilyId Next() = 0;
  141. };
  142. // Gets an iterator for column families.
  143. //
  144. // Returned iterator must not be nullptr.
  145. // If there is no column family to iterate,
  146. // returns an empty non-null iterator.
  147. // Caller owns the returned pointer.
  148. virtual ColumnFamilyIterator* GetColumnFamilyIterator() const = 0;
  149. class KeyIterator {
  150. public:
  151. virtual ~KeyIterator() {}
  152. // Whether there are remaining keys.
  153. virtual bool HasNext() const = 0;
  154. // Gets the next key.
  155. //
  156. // If HasNext is false, calling this method has undefined behavior.
  157. virtual const std::string& Next() = 0;
  158. };
  159. // Gets an iterator for keys with tracked point locks in the column family.
  160. //
  161. // The column family must exist.
  162. // Returned iterator must not be nullptr.
  163. // Caller owns the returned pointer.
  164. virtual KeyIterator* GetKeyIterator(
  165. ColumnFamilyId /*column_family_id*/) const = 0;
  166. };
  167. // LockTracker should always be constructed through this factory.
  168. // Each LockManager owns a LockTrackerFactory.
  169. class LockTrackerFactory {
  170. public:
  171. // Caller owns the returned pointer.
  172. virtual LockTracker* Create() const = 0;
  173. virtual ~LockTrackerFactory() {}
  174. };
  175. } // namespace ROCKSDB_NAMESPACE