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).
11 #include <unordered_map>
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"
23 namespace ROCKSDB_NAMESPACE
{
25 class ColumnFamilyHandle
;
30 struct DeadlockInfoBuffer
{
32 std::vector
<DeadlockPath
> paths_buffer_
;
34 std::mutex paths_buffer_mutex_
;
35 std::vector
<DeadlockPath
> Normalize();
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();
45 struct TrackedTrxInfo
{
46 autovector
<TransactionID
> m_neighbors
;
49 std::string m_waiting_key
;
52 class PointLockManager
: public LockManager
{
54 PointLockManager(PessimisticTransactionDB
* db
,
55 const TransactionDBOptions
& opt
);
57 PointLockManager(const PointLockManager
&) = delete;
58 PointLockManager
& operator=(const PointLockManager
&) = delete;
60 ~PointLockManager() override
;
62 bool IsPointLockSupported() const override
{ return true; }
64 bool IsRangeLockSupported() const override
{ return false; }
66 const LockTrackerFactory
& GetLockTrackerFactory() const override
{
67 return PointLockTrackerFactory::Get();
70 void AddColumnFamily(const ColumnFamilyHandle
* cf
) override
;
71 void RemoveColumnFamily(const ColumnFamilyHandle
* cf
) override
;
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
;
79 void UnLock(PessimisticTransaction
* txn
, const LockTracker
& tracker
,
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
;
86 PointLockStatus
GetPointLockStatus() override
;
88 RangeLockStatus
GetRangeLockStatus() override
;
90 std::vector
<DeadlockPath
> GetDeadlockInfoBuffer() override
;
92 void Resize(uint32_t new_size
) override
;
95 PessimisticTransactionDB
* txn_db_impl_
;
97 // Default number of lock map stripes per column family
98 const size_t default_num_stripes_
;
100 // Limit on number of keys locked per column family
101 const int64_t max_num_locks_
;
103 // The following lock order must be satisfied in order to avoid deadlocking
106 // - stripe mutexes in ascending cf id, ascending stripe order
107 // - wait_txn_map_mutex_
109 // Must be held when accessing/modifying lock_maps_.
110 InstrumentedMutex lock_map_mutex_
;
112 // Map of ColumnFamilyId to locked key info
113 using LockMaps
= std::unordered_map
<uint32_t, std::shared_ptr
<LockMap
>>;
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_
;
120 // Must be held when modifying wait_txn_map_ and rev_wait_txn_map_.
121 std::mutex wait_txn_map_mutex_
;
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_
;
129 // Used to allocate mutexes/condvars to use when locking keys
130 std::shared_ptr
<TransactionDBMutexFactory
> mutex_factory_
;
132 bool IsLockExpired(TransactionID txn_id
, const LockInfo
& lock_info
, Env
* env
,
133 uint64_t* wait_time
);
135 std::shared_ptr
<LockMap
> GetLockMap(uint32_t column_family_id
);
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
);
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
);
147 void UnLockKey(PessimisticTransaction
* txn
, const std::string
& key
,
148 LockMapStripe
* stripe
, LockMap
* lock_map
, Env
* env
);
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
);
160 } // namespace ROCKSDB_NAMESPACE
161 #endif // ROCKSDB_LITE