OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LockMgrImpl.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <atomic>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <type_traits>
24 
25 #include "Catalog/Catalog.h"
28 #include "Shared/types.h"
29 
30 namespace lockmgr {
31 
32 class TableSchemaLockMgr;
33 class TableDataLockMgr;
34 class InsertDataLockMgr;
35 
37 
39  public:
40  MutexTracker(std::unique_ptr<heavyai::DistributedSharedMutex> dmutex)
41  : ref_count_(0u), dmutex_(std::move(dmutex)) {}
42 
43  virtual void lock();
44  virtual bool try_lock();
45  virtual void unlock();
46 
47  virtual void lock_shared();
48  virtual bool try_lock_shared();
49  virtual void unlock_shared();
50 
51  virtual bool isAcquired() const { return ref_count_.load() > 0; }
52 
53  private:
54  std::atomic<size_t> ref_count_;
56  std::unique_ptr<heavyai::DistributedSharedMutex> dmutex_;
57 };
58 
61 
62 template <typename LOCK>
64  static_assert(std::is_same_v<LOCK, ReadLockBase> ||
65  std::is_same_v<LOCK, WriteLockBase>);
66 
67  public:
69 
71  : mutex_(other.mutex_), lock_(std::move(other.lock_)) {
72  other.mutex_ = nullptr;
73  }
74 
75  TrackedRefLock(const TrackedRefLock&) = delete;
76  TrackedRefLock& operator=(const TrackedRefLock&) = delete;
77 
78  private:
80  LOCK lock_;
81 
83  CHECK(m);
84  return m;
85  }
86 };
87 
90 
91 template <typename T>
93  public:
94  virtual T operator()() const = 0;
95 
97 };
98 
99 template <typename T, typename LOCK>
101  public:
102  T operator()() const final { return obj_; }
103 
104  protected:
105  LockContainerImpl(T obj, LOCK&& lock) : obj_(obj), lock_(std::move(lock)) {}
106 
108  LOCK lock_;
109 };
110 
111 namespace helpers {
113  const std::string& tableName);
114 
115 template <typename LOCK_TYPE, typename LOCK_MGR_TYPE>
116 LOCK_TYPE getLockForKeyImpl(const ChunkKey& chunk_key) {
117  auto& table_lock_mgr = LOCK_MGR_TYPE::instance();
118  return LOCK_TYPE(table_lock_mgr.getTableMutex(chunk_key));
119 }
120 
121 template <typename LOCK_TYPE, typename LOCK_MGR_TYPE>
123  const std::string& table_name) {
124  const auto chunk_key = chunk_key_for_table(cat, table_name);
125 
126  auto& table_lock_mgr = LOCK_MGR_TYPE::instance();
127  return LOCK_TYPE(table_lock_mgr.getTableMutex(chunk_key));
128 }
129 
130 } // namespace helpers
131 
132 template <class T>
133 class TableLockMgrImpl {
134  static_assert(std::is_same_v<T, TableSchemaLockMgr> ||
135  std::is_same_v<T, TableDataLockMgr> ||
136  std::is_same_v<T, InsertDataLockMgr>);
137 
138  public:
139  static T& instance();
140 
141  virtual ~TableLockMgrImpl() = default;
142 
143  virtual MutexTracker* getTableMutex(const ChunkKey& table_key);
144 
145  std::set<ChunkKey> getLockedTables() const;
146 
148  const std::string& table_name);
149 
150  static WriteLock getWriteLockForTable(const ChunkKey& table_key);
151 
153  const std::string& table_name);
154 
155  static ReadLock getReadLockForTable(const ChunkKey& table_key);
156 
157  protected:
159 
160  virtual std::unique_ptr<heavyai::DistributedSharedMutex> getClusterTableMutex(
161  const ChunkKey& table_key) const;
162 
163  mutable std::mutex map_mutex_;
164  std::map<ChunkKey, std::unique_ptr<MutexTracker>> table_mutex_map_;
165 
166  private:
168  const std::string& table_name);
169 
170  static void validateExistingTable(const Catalog_Namespace::Catalog& catalog,
171  const std::string& table_name);
172 
173  static int32_t validateAndGetExistingTableId(const Catalog_Namespace::Catalog& catalog,
174  const std::string& table_name);
175 };
176 
177 template <typename T>
178 std::ostream& operator<<(std::ostream& os, const TableLockMgrImpl<T>& lock_mgr) {
179  for (const auto& table_key : lock_mgr.getLockedTables()) {
180  for (const auto& k : table_key) {
181  os << k << " ";
182  }
183  os << "\n";
184  }
185  return os;
186 }
187 
188 } // namespace lockmgr
MutexTypeBase mutex_
Definition: LockMgrImpl.h:55
static MutexTracker * getMutexTracker(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:271
std::vector< int > ChunkKey
Definition: types.h:36
static int32_t validateAndGetExistingTableId(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:289
virtual std::unique_ptr< heavyai::DistributedSharedMutex > getClusterTableMutex(const ChunkKey &table_key) const
Definition: LockMgr.cpp:168
std::string cat(Ts &&...args)
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:143
virtual bool try_lock_shared()
Definition: LockMgr.cpp:78
virtual void lock()
Definition: LockMgr.cpp:38
heavyai::unique_lock< MutexTracker > WriteLockBase
Definition: LockMgrImpl.h:59
virtual bool try_lock()
Definition: LockMgr.cpp:47
std::set< ChunkKey > getLockedTables() const
Definition: LockMgr.cpp:124
LOCK_TYPE getLockForKeyImpl(const ChunkKey &chunk_key)
Definition: LockMgrImpl.h:116
T operator()() const final
Definition: LockMgrImpl.h:102
static void validateExistingTable(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:283
heavyai::shared_mutex MutexTypeBase
Definition: LockMgrImpl.h:36
std::shared_lock< T > shared_lock
This file contains the class specification and related data structures for Catalog.
TrackedRefLock< WriteLockBase > WriteLock
Definition: LockMgrImpl.h:88
TrackedRefLock< ReadLockBase > ReadLock
Definition: LockMgrImpl.h:89
virtual T operator()() const =0
std::map< ChunkKey, std::unique_ptr< MutexTracker > > table_mutex_map_
Definition: LockMgrImpl.h:164
heavyai::shared_lock< MutexTracker > ReadLockBase
Definition: LockMgrImpl.h:60
std::unique_lock< T > unique_lock
MutexTracker(std::unique_ptr< heavyai::DistributedSharedMutex > dmutex)
Definition: LockMgrImpl.h:40
ChunkKey chunk_key_for_table(const Catalog_Namespace::Catalog &cat, const std::string &tableName)
Definition: LockMgr.cpp:25
TrackedRefLock(MutexTracker *m)
Definition: LockMgrImpl.h:68
LOCK_TYPE getLockForTableImpl(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: LockMgrImpl.h:122
std::unique_ptr< heavyai::DistributedSharedMutex > dmutex_
Definition: LockMgrImpl.h:56
virtual void unlock()
Definition: LockMgr.cpp:60
virtual MutexTracker * getTableMutex(const ChunkKey &table_key)
Definition: LockMgr.cpp:107
static WriteLock getWriteLockForTable(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: LockMgr.cpp:137
T & instance()
Definition: LockMgr.cpp:101
virtual void unlock_shared()
Definition: LockMgr.cpp:91
virtual void lock_shared()
Definition: LockMgr.cpp:69
#define CHECK(condition)
Definition: Logger.h:291
MutexTracker * mutex_
Definition: LockMgrImpl.h:79
TrackedRefLock(TrackedRefLock &&other)
Definition: LockMgrImpl.h:70
std::atomic< size_t > ref_count_
Definition: LockMgrImpl.h:54
std::shared_timed_mutex shared_mutex
TrackedRefLock & operator=(const TrackedRefLock &)=delete
static MutexTracker * checkPointer(MutexTracker *m)
Definition: LockMgrImpl.h:82
static ReadLock getReadLockForTable(Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: LockMgr.cpp:152
virtual bool isAcquired() const
Definition: LockMgrImpl.h:51
LockContainerImpl(T obj, LOCK &&lock)
Definition: LockMgrImpl.h:105
virtual ~TableLockMgrImpl()=default