lock_manager.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "rocksdb/types.h"
  7. #include "rocksdb/utilities/transaction.h"
  8. #include "rocksdb/utilities/transaction_db.h"
  9. #include "utilities/transactions/lock/lock_tracker.h"
  10. #include "utilities/transactions/pessimistic_transaction.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class PessimisticTransactionDB;
  13. class LockManager {
  14. public:
  15. virtual ~LockManager() {}
  16. // Whether supports locking a specific key.
  17. virtual bool IsPointLockSupported() const = 0;
  18. // Whether supports locking a range of keys.
  19. virtual bool IsRangeLockSupported() const = 0;
  20. // Locks acquired through this LockManager should be tracked by
  21. // the LockTrackers created through the returned factory.
  22. virtual const LockTrackerFactory& GetLockTrackerFactory() const = 0;
  23. // Enable locking for the specified column family.
  24. // Caller should guarantee that this column family is not already enabled.
  25. virtual void AddColumnFamily(const ColumnFamilyHandle* cf) = 0;
  26. // Disable locking for the specified column family.
  27. // Caller should guarantee that this column family is no longer used.
  28. virtual void RemoveColumnFamily(const ColumnFamilyHandle* cf) = 0;
  29. // Attempt to lock a key or a key range. If OK status is returned, the caller
  30. // is responsible for calling UnLock() on this key.
  31. virtual Status TryLock(PessimisticTransaction* txn,
  32. ColumnFamilyId column_family_id,
  33. const std::string& key, Env* env, bool exclusive) = 0;
  34. // The range [start, end] are inclusive at both sides.
  35. virtual Status TryLock(PessimisticTransaction* txn,
  36. ColumnFamilyId column_family_id, const Endpoint& start,
  37. const Endpoint& end, Env* env, bool exclusive) = 0;
  38. // Unlock a key or a range locked by TryLock(). txn must be the same
  39. // Transaction that locked this key.
  40. virtual void UnLock(PessimisticTransaction* txn, const LockTracker& tracker,
  41. Env* env) = 0;
  42. virtual void UnLock(PessimisticTransaction* txn,
  43. ColumnFamilyId column_family_id, const std::string& key,
  44. Env* env) = 0;
  45. virtual void UnLock(PessimisticTransaction* txn,
  46. ColumnFamilyId column_family_id, const Endpoint& start,
  47. const Endpoint& end, Env* env) = 0;
  48. using PointLockStatus = std::unordered_multimap<ColumnFamilyId, KeyLockInfo>;
  49. virtual PointLockStatus GetPointLockStatus() = 0;
  50. using RangeLockStatus =
  51. std::unordered_multimap<ColumnFamilyId, RangeLockInfo>;
  52. virtual RangeLockStatus GetRangeLockStatus() = 0;
  53. virtual std::vector<DeadlockPath> GetDeadlockInfoBuffer() = 0;
  54. virtual void Resize(uint32_t new_size) = 0;
  55. };
  56. // LockManager should always be constructed through this factory method,
  57. // instead of constructing through concrete implementations' constructor.
  58. // Caller owns the returned pointer.
  59. std::shared_ptr<LockManager> NewLockManager(PessimisticTransactionDB* db,
  60. const TransactionDBOptions& opt);
  61. } // namespace ROCKSDB_NAMESPACE