OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LockMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 "LockMgr/LegacyLockMgr.h"
20 #include "LockMgr/LockMgrImpl.h"
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 #include "Catalog/Catalog.h"
28 #include "Shared/types.h"
29 
30 namespace lockmgr {
31 
38 class TableSchemaLockMgr : public TableLockMgrImpl<TableSchemaLockMgr> {
39  public:
41  static TableSchemaLockMgr table_lock_mgr;
42  return table_lock_mgr;
43  }
44 
45  private:
47 };
48 
55 class InsertDataLockMgr : public TableLockMgrImpl<InsertDataLockMgr> {
56  public:
58  static InsertDataLockMgr insert_data_lock_mgr;
59  return insert_data_lock_mgr;
60  }
61 
62  protected:
64 };
65 
74 class TableDataLockMgr : public TableLockMgrImpl<TableDataLockMgr> {
75  public:
77  static TableDataLockMgr data_lock_mgr;
78  return data_lock_mgr;
79  }
80 
81  protected:
83 };
84 
86  std::string getTableName() const { return table_name_; }
87 
88  protected:
89  TableLockContainerImpl(const std::string& table_name) : table_name_(table_name) {}
90 
91  std::string table_name_;
92 };
93 
94 template <typename LOCK_TYPE>
96  : public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
97  public TableLockContainerImpl {
98  static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
99  std::is_same<LOCK_TYPE, WriteLock>::value);
100 
101  public:
102  TableSchemaLockContainer(const TableSchemaLockContainer&) = delete; // non-copyable
103 };
104 
105 template <>
107  : public LockContainerImpl<const TableDescriptor*, ReadLock>,
108  public TableLockContainerImpl {
109  public:
111  const std::string& table_name,
112  const bool populate_fragmenter = true) {
113  VLOG(1) << "Acquiring Table Schema Read Lock for table: " << table_name;
115  cat.getMetadataForTable(table_name, populate_fragmenter),
116  TableSchemaLockMgr::getReadLockForTable(cat, table_name));
117  }
118 
120  const int table_id) {
121  const auto td = cat.getMetadataForTable(table_id);
122  if (!td) {
123  throw std::runtime_error("Table/View ID " + std::to_string(table_id) +
124  " for catalog " + cat.getCurrentDB().dbName +
125  " does not exist. Cannot aquire read lock");
126  }
127  VLOG(1) << "Acquiring Table Schema Read Lock for table: " << td->tableName;
129  td, TableSchemaLockMgr::getReadLockForTable(cat, td->tableName));
130  }
131 
132  private:
135  , TableLockContainerImpl(obj ? obj->tableName : "") {}
136 };
137 
138 template <>
140  : public LockContainerImpl<const TableDescriptor*, WriteLock>,
141  public TableLockContainerImpl {
142  public:
144  const std::string& table_name,
145  const bool populate_fragmenter = true) {
146  VLOG(1) << "Acquiring Table Schema Write Lock for table: " << table_name;
148  cat.getMetadataForTable(table_name, populate_fragmenter),
150  }
151 
153  const int table_id) {
154  const auto td = cat.getMetadataForTable(table_id);
155  if (!td) {
156  throw std::runtime_error("Table/View ID " + std::to_string(table_id) +
157  " for catalog " + cat.getCurrentDB().dbName +
158  " does not exist. Cannot aquire write lock");
159  }
160  VLOG(1) << "Acquiring Table Schema Write Lock for table: " << td->tableName;
162  td, TableSchemaLockMgr::getWriteLockForTable(cat, td->tableName));
163  }
164 
165  private:
168  , TableLockContainerImpl(obj ? obj->tableName : "") {}
169 };
170 
171 template <typename LOCK_TYPE>
173  : public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
174  public TableLockContainerImpl {
175  static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
176  std::is_same<LOCK_TYPE, WriteLock>::value);
177 
178  public:
179  TableDataLockContainer(const TableDataLockContainer&) = delete; // non-copyable
180 };
181 
182 template <>
184  : public LockContainerImpl<const TableDescriptor*, WriteLock>,
185  public TableLockContainerImpl {
186  public:
187  static auto acquire(const int db_id, const TableDescriptor* td) {
188  CHECK(td);
189  ChunkKey chunk_key{db_id, td->tableId};
190  VLOG(1) << "Acquiring Table Data Write Lock for table: " << td->tableName;
193  }
194 
195  private:
198  , TableLockContainerImpl(obj->tableName) {}
199 };
200 
201 template <>
203  : public LockContainerImpl<const TableDescriptor*, ReadLock>,
204  public TableLockContainerImpl {
205  public:
206  static auto acquire(const int db_id, const TableDescriptor* td) {
207  CHECK(td);
208  ChunkKey chunk_key{db_id, td->tableId};
209  VLOG(1) << "Acquiring Table Data Read Lock for table: " << td->tableName;
212  }
213 
214  private:
217  , TableLockContainerImpl(obj->tableName) {}
218 };
219 
220 template <typename LOCK_TYPE>
222  : public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
223  public TableLockContainerImpl {
224  static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
225  std::is_same<LOCK_TYPE, WriteLock>::value);
226 
227  public:
228  TableInsertLockContainer(const TableInsertLockContainer&) = delete; // non-copyable
229 };
230 
231 template <>
233  : public LockContainerImpl<const TableDescriptor*, WriteLock>,
234  public TableLockContainerImpl {
235  public:
236  static auto acquire(const int db_id, const TableDescriptor* td) {
237  CHECK(td);
238  ChunkKey chunk_key{db_id, td->tableId};
239  VLOG(1) << "Acquiring Table Insert Write Lock for table: " << td->tableName;
242  }
243 
244  private:
247  , TableLockContainerImpl(obj->tableName) {}
248 };
249 
250 template <>
252  : public LockContainerImpl<const TableDescriptor*, ReadLock>,
253  public TableLockContainerImpl {
254  public:
255  static auto acquire(const int db_id, const TableDescriptor* td) {
256  CHECK(td);
257  ChunkKey chunk_key{db_id, td->tableId};
258  VLOG(1) << "Acquiring Table Insert Read Lock for table: " << td->tableName;
261  }
262 
263  private:
266  , TableLockContainerImpl(obj->tableName) {}
267 };
268 
270  std::vector<std::unique_ptr<lockmgr::AbstractLockContainer<const TableDescriptor*>>>;
271 
272 } // namespace lockmgr
std::vector< int > ChunkKey
Definition: types.h:37
std::vector< std::unique_ptr< lockmgr::AbstractLockContainer< const TableDescriptor * >>> LockedTableDescriptors
Definition: LockMgr.h:270
static ReadLock getReadLockForTable(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: LockMgrImpl.h:164
Locks protecting a physical table object returned from the catalog. Table Metadata Locks prevent inco...
Definition: LockMgr.h:38
static TableSchemaLockMgr & instance()
Definition: LockMgr.h:40
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:111
Locks protecting table data. Read queries take a read lock, while write queries (update, delete) obtain a write lock. Note that insert queries do not currently take a write lock (to allow concurrent inserts). Instead, insert queries obtain a write lock on the table metadata to allow existing read queries to finish (and block new ones) before flushing the inserted data to disk.
Definition: LockMgr.h:74
TableInsertLockContainer(const TableInsertLockContainer &)=delete
std::string tableName
static WriteLock getWriteLockForTable(const Catalog_Namespace::Catalog &cat, const std::string &table_name)
Definition: LockMgrImpl.h:155
std::string getTableName() const
Definition: LockMgr.h:86
static auto acquire(const int db_id, const TableDescriptor *td)
Definition: LockMgr.h:187
TableLockContainerImpl(const std::string &table_name)
Definition: LockMgr.h:89
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog &cat, const int table_id)
Definition: LockMgr.h:152
static TableDataLockMgr & instance()
Definition: LockMgr.h:76
std::string to_string(char const *&&v)
static auto acquire(const int db_id, const TableDescriptor *td)
Definition: LockMgr.h:206
This file contains the class specification and related data structures for Catalog.
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog &cat, const int table_id)
Definition: LockMgr.h:119
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog &cat, const std::string &table_name, const bool populate_fragmenter=true)
Definition: LockMgr.h:143
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:221
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog &cat, const std::string &table_name, const bool populate_fragmenter=true)
Definition: LockMgr.h:110
TableDataLockContainer(const TableDataLockContainer &)=delete
#define CHECK(condition)
Definition: Logger.h:209
TableSchemaLockContainer(const TableSchemaLockContainer &)=delete
static auto acquire(const int db_id, const TableDescriptor *td)
Definition: LockMgr.h:236
static InsertDataLockMgr & instance()
Definition: LockMgr.h:57
static auto acquire(const int db_id, const TableDescriptor *td)
Definition: LockMgr.h:255
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
Prevents simultaneous inserts into the same table. To allow concurrent Insert/Select queries...
Definition: LockMgr.h:55
#define VLOG(n)
Definition: Logger.h:303