forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockMgr.h
272 lines (236 loc) · 9.88 KB
/
LockMgr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright 2020 OmniSci, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "LockMgr/LegacyLockMgr.h"
#include "LockMgr/LockMgrImpl.h"
#include <map>
#include <memory>
#include <string>
#include "Catalog/Catalog.h"
#include "Shared/mapd_shared_mutex.h"
#include "Shared/types.h"
namespace lockmgr {
/**
* @brief Locks protecting a physical table object returned from the catalog.
* Table Metadata Locks prevent incompatible concurrent operations on table objects.
* For example, before dropping or altering a table, a metadata write lock must be
* acquired. This prevents concurrent read + drop, concurrent drops, etc.
*/
class TableSchemaLockMgr : public TableLockMgrImpl<TableSchemaLockMgr> {
public:
static TableSchemaLockMgr& instance() {
static TableSchemaLockMgr table_lock_mgr;
return table_lock_mgr;
}
private:
TableSchemaLockMgr() {}
};
/**
* @brief Prevents simultaneous inserts into the same table.
* To allow concurrent Insert/Select queries, Insert queries only obtain a write lock on
* table data when checkpointing (flushing chunks to disk). Inserts/Data load will take an
* exclusive (write) lock to ensure only one insert proceeds on each table at a time.
*/
class InsertDataLockMgr : public TableLockMgrImpl<InsertDataLockMgr> {
public:
static InsertDataLockMgr& instance() {
static InsertDataLockMgr insert_data_lock_mgr;
return insert_data_lock_mgr;
}
protected:
InsertDataLockMgr() {}
};
/**
* @brief 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.
*/
class TableDataLockMgr : public TableLockMgrImpl<TableDataLockMgr> {
public:
static TableDataLockMgr& instance() {
static TableDataLockMgr data_lock_mgr;
return data_lock_mgr;
}
protected:
TableDataLockMgr() {}
};
class TableLockContainerImpl {
std::string getTableName() const { return table_name_; }
protected:
TableLockContainerImpl(const std::string& table_name) : table_name_(table_name) {}
std::string table_name_;
};
template <typename LOCK_TYPE>
class TableSchemaLockContainer
: public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
public TableLockContainerImpl {
static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
std::is_same<LOCK_TYPE, WriteLock>::value);
public:
TableSchemaLockContainer(const TableSchemaLockContainer&) = delete; // non-copyable
};
template <>
class TableSchemaLockContainer<ReadLock>
: public LockContainerImpl<const TableDescriptor*, ReadLock>,
public TableLockContainerImpl {
public:
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog& cat,
const std::string& table_name,
const bool populate_fragmenter = true) {
VLOG(1) << "Acquiring Table Schema Read Lock for table: " << table_name;
return TableSchemaLockContainer<ReadLock>(
cat.getMetadataForTable(table_name, populate_fragmenter),
TableSchemaLockMgr::getReadLockForTable(cat, table_name));
}
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog& cat,
const int table_id) {
const auto td = cat.getMetadataForTable(table_id);
if (!td) {
throw std::runtime_error("Table/View ID " + std::to_string(table_id) +
" for catalog " + cat.getCurrentDB().dbName +
" does not exist. Cannot aquire read lock");
}
VLOG(1) << "Acquiring Table Schema Read Lock for table: " << td->tableName;
return TableSchemaLockContainer<ReadLock>(
td, TableSchemaLockMgr::getReadLockForTable(cat, td->tableName));
}
private:
TableSchemaLockContainer<ReadLock>(const TableDescriptor* obj, ReadLock&& lock)
: LockContainerImpl<const TableDescriptor*, ReadLock>(obj, std::move(lock))
, TableLockContainerImpl(obj ? obj->tableName : "") {}
};
template <>
class TableSchemaLockContainer<WriteLock>
: public LockContainerImpl<const TableDescriptor*, WriteLock>,
public TableLockContainerImpl {
public:
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog& cat,
const std::string& table_name,
const bool populate_fragmenter = true) {
VLOG(1) << "Acquiring Table Schema Write Lock for table: " << table_name;
return TableSchemaLockContainer<WriteLock>(
cat.getMetadataForTable(table_name, populate_fragmenter),
TableSchemaLockMgr::getWriteLockForTable(cat, table_name));
}
static auto acquireTableDescriptor(const Catalog_Namespace::Catalog& cat,
const int table_id) {
const auto td = cat.getMetadataForTable(table_id);
if (!td) {
throw std::runtime_error("Table/View ID " + std::to_string(table_id) +
" for catalog " + cat.getCurrentDB().dbName +
" does not exist. Cannot aquire write lock");
}
VLOG(1) << "Acquiring Table Schema Write Lock for table: " << td->tableName;
return TableSchemaLockContainer<WriteLock>(
td, TableSchemaLockMgr::getWriteLockForTable(cat, td->tableName));
}
private:
TableSchemaLockContainer<WriteLock>(const TableDescriptor* obj, WriteLock&& lock)
: LockContainerImpl<const TableDescriptor*, WriteLock>(obj, std::move(lock))
, TableLockContainerImpl(obj ? obj->tableName : "") {}
};
template <typename LOCK_TYPE>
class TableDataLockContainer
: public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
public TableLockContainerImpl {
static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
std::is_same<LOCK_TYPE, WriteLock>::value);
public:
TableDataLockContainer(const TableDataLockContainer&) = delete; // non-copyable
};
template <>
class TableDataLockContainer<WriteLock>
: public LockContainerImpl<const TableDescriptor*, WriteLock>,
public TableLockContainerImpl {
public:
static auto acquire(const int db_id, const TableDescriptor* td) {
CHECK(td);
ChunkKey chunk_key{db_id, td->tableId};
VLOG(1) << "Acquiring Table Data Write Lock for table: " << td->tableName;
return TableDataLockContainer<WriteLock>(
td, TableDataLockMgr::getWriteLockForTable(chunk_key));
}
private:
TableDataLockContainer<WriteLock>(const TableDescriptor* obj, WriteLock&& lock)
: LockContainerImpl<const TableDescriptor*, WriteLock>(obj, std::move(lock))
, TableLockContainerImpl(obj->tableName) {}
};
template <>
class TableDataLockContainer<ReadLock>
: public LockContainerImpl<const TableDescriptor*, ReadLock>,
public TableLockContainerImpl {
public:
static auto acquire(const int db_id, const TableDescriptor* td) {
CHECK(td);
ChunkKey chunk_key{db_id, td->tableId};
VLOG(1) << "Acquiring Table Data Read Lock for table: " << td->tableName;
return TableDataLockContainer<ReadLock>(
td, TableDataLockMgr::getReadLockForTable(chunk_key));
}
private:
TableDataLockContainer<ReadLock>(const TableDescriptor* obj, ReadLock&& lock)
: LockContainerImpl<const TableDescriptor*, ReadLock>(obj, std::move(lock))
, TableLockContainerImpl(obj->tableName) {}
};
template <typename LOCK_TYPE>
class TableInsertLockContainer
: public LockContainerImpl<const TableDescriptor*, LOCK_TYPE>,
public TableLockContainerImpl {
static_assert(std::is_same<LOCK_TYPE, ReadLock>::value ||
std::is_same<LOCK_TYPE, WriteLock>::value);
public:
TableInsertLockContainer(const TableInsertLockContainer&) = delete; // non-copyable
};
template <>
class TableInsertLockContainer<WriteLock>
: public LockContainerImpl<const TableDescriptor*, WriteLock>,
public TableLockContainerImpl {
public:
static auto acquire(const int db_id, const TableDescriptor* td) {
CHECK(td);
ChunkKey chunk_key{db_id, td->tableId};
VLOG(1) << "Acquiring Table Insert Write Lock for table: " << td->tableName;
return TableInsertLockContainer<WriteLock>(
td, InsertDataLockMgr::getWriteLockForTable(chunk_key));
}
private:
TableInsertLockContainer<WriteLock>(const TableDescriptor* obj, WriteLock&& lock)
: LockContainerImpl<const TableDescriptor*, WriteLock>(obj, std::move(lock))
, TableLockContainerImpl(obj->tableName) {}
};
template <>
class TableInsertLockContainer<ReadLock>
: public LockContainerImpl<const TableDescriptor*, ReadLock>,
public TableLockContainerImpl {
public:
static auto acquire(const int db_id, const TableDescriptor* td) {
CHECK(td);
ChunkKey chunk_key{db_id, td->tableId};
VLOG(1) << "Acquiring Table Insert Read Lock for table: " << td->tableName;
return TableInsertLockContainer<ReadLock>(
td, InsertDataLockMgr::getReadLockForTable(chunk_key));
}
private:
TableInsertLockContainer<ReadLock>(const TableDescriptor* obj, ReadLock&& lock)
: LockContainerImpl<const TableDescriptor*, ReadLock>(obj, std::move(lock))
, TableLockContainerImpl(obj->tableName) {}
};
using LockedTableDescriptors =
std::vector<std::unique_ptr<lockmgr::AbstractLockContainer<const TableDescriptor*>>>;
} // namespace lockmgr