]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/transactions/lock/point/point_lock_manager.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / utilities / transactions / lock / point / point_lock_manager.h
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 #pragma once
7 #ifndef ROCKSDB_LITE
8
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <utility>
13 #include <vector>
14
15 #include "monitoring/instrumented_mutex.h"
16 #include "rocksdb/utilities/transaction.h"
17 #include "util/autovector.h"
18 #include "util/hash_map.h"
19 #include "util/thread_local.h"
20 #include "utilities/transactions/lock/lock_manager.h"
21 #include "utilities/transactions/lock/point/point_lock_tracker.h"
22
23 namespace ROCKSDB_NAMESPACE {
24
25 class ColumnFamilyHandle;
26 struct LockInfo;
27 struct LockMap;
28 struct LockMapStripe;
29
30 struct DeadlockInfoBuffer {
31 private:
32 std::vector<DeadlockPath> paths_buffer_;
33 uint32_t buffer_idx_;
34 std::mutex paths_buffer_mutex_;
35 std::vector<DeadlockPath> Normalize();
36
37 public:
38 explicit DeadlockInfoBuffer(uint32_t n_latest_dlocks)
39 : paths_buffer_(n_latest_dlocks), buffer_idx_(0) {}
40 void AddNewPath(DeadlockPath path);
41 void Resize(uint32_t target_size);
42 std::vector<DeadlockPath> PrepareBuffer();
43 };
44
45 struct TrackedTrxInfo {
46 autovector<TransactionID> m_neighbors;
47 uint32_t m_cf_id;
48 bool m_exclusive;
49 std::string m_waiting_key;
50 };
51
52 class PointLockManager : public LockManager {
53 public:
54 PointLockManager(PessimisticTransactionDB* db,
55 const TransactionDBOptions& opt);
56 // No copying allowed
57 PointLockManager(const PointLockManager&) = delete;
58 PointLockManager& operator=(const PointLockManager&) = delete;
59
60 ~PointLockManager() override;
61
62 bool IsPointLockSupported() const override { return true; }
63
64 bool IsRangeLockSupported() const override { return false; }
65
66 const LockTrackerFactory& GetLockTrackerFactory() const override {
67 return PointLockTrackerFactory::Get();
68 }
69
70 void AddColumnFamily(const ColumnFamilyHandle* cf) override;
71 void RemoveColumnFamily(const ColumnFamilyHandle* cf) override;
72
73 Status TryLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
74 const std::string& key, Env* env, bool exclusive) override;
75 Status TryLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
76 const Endpoint& start, const Endpoint& end, Env* env,
77 bool exclusive) override;
78
79 void UnLock(PessimisticTransaction* txn, const LockTracker& tracker,
80 Env* env) override;
81 void UnLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
82 const std::string& key, Env* env) override;
83 void UnLock(PessimisticTransaction* txn, ColumnFamilyId column_family_id,
84 const Endpoint& start, const Endpoint& end, Env* env) override;
85
86 PointLockStatus GetPointLockStatus() override;
87
88 RangeLockStatus GetRangeLockStatus() override;
89
90 std::vector<DeadlockPath> GetDeadlockInfoBuffer() override;
91
92 void Resize(uint32_t new_size) override;
93
94 private:
95 PessimisticTransactionDB* txn_db_impl_;
96
97 // Default number of lock map stripes per column family
98 const size_t default_num_stripes_;
99
100 // Limit on number of keys locked per column family
101 const int64_t max_num_locks_;
102
103 // The following lock order must be satisfied in order to avoid deadlocking
104 // ourselves.
105 // - lock_map_mutex_
106 // - stripe mutexes in ascending cf id, ascending stripe order
107 // - wait_txn_map_mutex_
108 //
109 // Must be held when accessing/modifying lock_maps_.
110 InstrumentedMutex lock_map_mutex_;
111
112 // Map of ColumnFamilyId to locked key info
113 using LockMaps = std::unordered_map<uint32_t, std::shared_ptr<LockMap>>;
114 LockMaps lock_maps_;
115
116 // Thread-local cache of entries in lock_maps_. This is an optimization
117 // to avoid acquiring a mutex in order to look up a LockMap
118 std::unique_ptr<ThreadLocalPtr> lock_maps_cache_;
119
120 // Must be held when modifying wait_txn_map_ and rev_wait_txn_map_.
121 std::mutex wait_txn_map_mutex_;
122
123 // Maps from waitee -> number of waiters.
124 HashMap<TransactionID, int> rev_wait_txn_map_;
125 // Maps from waiter -> waitee.
126 HashMap<TransactionID, TrackedTrxInfo> wait_txn_map_;
127 DeadlockInfoBuffer dlock_buffer_;
128
129 // Used to allocate mutexes/condvars to use when locking keys
130 std::shared_ptr<TransactionDBMutexFactory> mutex_factory_;
131
132 bool IsLockExpired(TransactionID txn_id, const LockInfo& lock_info, Env* env,
133 uint64_t* wait_time);
134
135 std::shared_ptr<LockMap> GetLockMap(uint32_t column_family_id);
136
137 Status AcquireWithTimeout(PessimisticTransaction* txn, LockMap* lock_map,
138 LockMapStripe* stripe, uint32_t column_family_id,
139 const std::string& key, Env* env, int64_t timeout,
140 LockInfo&& lock_info);
141
142 Status AcquireLocked(LockMap* lock_map, LockMapStripe* stripe,
143 const std::string& key, Env* env,
144 LockInfo&& lock_info, uint64_t* wait_time,
145 autovector<TransactionID>* txn_ids);
146
147 void UnLockKey(PessimisticTransaction* txn, const std::string& key,
148 LockMapStripe* stripe, LockMap* lock_map, Env* env);
149
150 bool IncrementWaiters(const PessimisticTransaction* txn,
151 const autovector<TransactionID>& wait_ids,
152 const std::string& key, const uint32_t& cf_id,
153 const bool& exclusive, Env* const env);
154 void DecrementWaiters(const PessimisticTransaction* txn,
155 const autovector<TransactionID>& wait_ids);
156 void DecrementWaitersImpl(const PessimisticTransaction* txn,
157 const autovector<TransactionID>& wait_ids);
158 };
159
160 } // namespace ROCKSDB_NAMESPACE
161 #endif // ROCKSDB_LITE