OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  " does not exist.");
125  }
126  VLOG(1) << "Acquiring Table Schema Read Lock for table: " << td->tableName;
128  td, TableSchemaLockMgr::getReadLockForTable(cat, td->tableName));
129  }
130 
131  private:
134  , TableLockContainerImpl(obj ? obj->tableName : "") {}
135 };
136 
137 template <>
139  : public LockContainerImpl<const TableDescriptor*, WriteLock>,
140  public TableLockContainerImpl {
141  public:
143  const std::string& table_name,
144  const bool populate_fragmenter = true) {
145  VLOG(1) << "Acquiring Table Schema Write Lock for table: " << table_name;
147  cat.getMetadataForTable(table_name, populate_fragmenter),
149  }
150 
152  const int table_id) {
153  const auto td = cat.getMetadataForTable(table_id);
154  if (!td) {
155  throw std::runtime_error("Table/View ID " + std::to_string(table_id) +
156  " does not exist.");
157  }
158  VLOG(1) << "Acquiring Table Schema Write Lock for table: " << td->tableName;
160  td, TableSchemaLockMgr::getWriteLockForTable(cat, td->tableName));
161  }
162 
163  private:
166  , TableLockContainerImpl(obj ? obj->tableName : "") {}
167 };
168 
169 template <typename LOCK_TYPE>
171  : public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
172  public TableLockContainerImpl {
173  static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
174  std::is_same<LOCK_TYPE, WriteLock>::value);
175 
176  public:
177  TableDataLockContainer(const TableDataLockContainer&) = delete; // non-copyable
178 };
179 
180 template <>
182  : public LockContainerImpl<const TableDescriptor*, WriteLock>,
183  public TableLockContainerImpl {
184  public:
185  static auto acquire(const int db_id, const TableDescriptor* td) {
186  CHECK(td);
187  ChunkKey chunk_key{db_id, td->tableId};
188  VLOG(1) << "Acquiring Table Data Write Lock for table: " << td->tableName;
191  }
192 
193  private:
196  , TableLockContainerImpl(obj->tableName) {}
197 };
198 
199 template <>
201  : public LockContainerImpl<const TableDescriptor*, ReadLock>,
202  public TableLockContainerImpl {
203  public:
204  static auto acquire(const int db_id, const TableDescriptor* td) {
205  CHECK(td);
206  ChunkKey chunk_key{db_id, td->tableId};
207  VLOG(1) << "Acquiring Table Data Read Lock for table: " << td->tableName;
210  }
211 
212  private:
215  , TableLockContainerImpl(obj->tableName) {}
216 };
217 
218 template <typename LOCK_TYPE>
220  : public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
221  public TableLockContainerImpl {
222  static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
223  std::is_same<LOCK_TYPE, WriteLock>::value);
224 
225  public:
226  TableInsertLockContainer(const TableInsertLockContainer&) = delete; // non-copyable
227 };
228 
229 template <>
231  : public LockContainerImpl<const TableDescriptor*, WriteLock>,
232  public TableLockContainerImpl {
233  public:
234  static auto acquire(const int db_id, const TableDescriptor* td) {
235  CHECK(td);
236  ChunkKey chunk_key{db_id, td->tableId};
237  VLOG(1) << "Acquiring Table Insert Write Lock for table: " << td->tableName;
240  }
241 
242  private:
245  , TableLockContainerImpl(obj->tableName) {}
246 };
247 
248 template <>
250  : public LockContainerImpl<const TableDescriptor*, ReadLock>,
251  public TableLockContainerImpl {
252  public:
253  static auto acquire(const int db_id, const TableDescriptor* td) {
254  CHECK(td);
255  ChunkKey chunk_key{db_id, td->tableId};
256  VLOG(1) << "Acquiring Table Insert Read Lock for table: " << td->tableName;
259  }
260 
261  private:
264  , TableLockContainerImpl(obj->tableName) {}
265 };
266 
268  std::vector<std::unique_ptr<lockmgr::AbstractLockContainer<const TableDescriptor*>>>;
269 
270 } // namespace lockmgr
std::vector< int > ChunkKey
Definition: types.h:37
std::vector< std::unique_ptr< lockmgr::AbstractLockContainer< const TableDescriptor * >>> LockedTableDescriptors
Definition: LockMgr.h:268
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:97
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:185
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:151
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:204
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:142
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:197
TableSchemaLockContainer(const TableSchemaLockContainer &)=delete
static auto acquire(const int db_id, const TableDescriptor *td)
Definition: LockMgr.h:234
static InsertDataLockMgr & instance()
Definition: LockMgr.h:57
static auto acquire(const int db_id, const TableDescriptor *td)
Definition: LockMgr.h:253
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
specifies the content in-memory of a row in the table metadata table
Prevents simultaneous inserts into the same table. To allow concurrent Insert/Select queries...
Definition: LockMgr.h:55
#define VLOG(n)
Definition: Logger.h:291