OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lockmgr::TableLockMgrImpl< T > Class Template Reference

#include <Catalog.h>

+ Inheritance diagram for lockmgr::TableLockMgrImpl< T >:

Public Member Functions

virtual ~TableLockMgrImpl ()=default
 
virtual MutexTrackergetTableMutex (const ChunkKey table_key)
 
std::set< ChunkKeygetLockedTables () const
 

Static Public Member Functions

static T & instance ()
 
static WriteLock getWriteLockForTable (Catalog_Namespace::Catalog &cat, const std::string &table_name)
 
static WriteLock getWriteLockForTable (const ChunkKey table_key)
 
static ReadLock getReadLockForTable (Catalog_Namespace::Catalog &cat, const std::string &table_name)
 
static ReadLock getReadLockForTable (const ChunkKey table_key)
 

Protected Member Functions

 TableLockMgrImpl ()
 
virtual std::unique_ptr
< heavyai::DistributedSharedMutex
getClusterTableMutex (const ChunkKey table_key)
 

Protected Attributes

std::mutex map_mutex_
 
std::map< ChunkKey,
std::unique_ptr< MutexTracker > > 
table_mutex_map_
 

Detailed Description

template<class T>
class lockmgr::TableLockMgrImpl< T >

Definition at line 68 of file Catalog.h.

Constructor & Destructor Documentation

template<class T>
virtual lockmgr::TableLockMgrImpl< T >::~TableLockMgrImpl ( )
virtualdefault
template<class T>
lockmgr::TableLockMgrImpl< T >::TableLockMgrImpl ( )
inlineprotected

Definition at line 262 of file LockMgrImpl.h.

262 {}

Member Function Documentation

template<class T>
virtual std::unique_ptr<heavyai::DistributedSharedMutex> lockmgr::TableLockMgrImpl< T >::getClusterTableMutex ( const ChunkKey  table_key)
inlineprotectedvirtual

Definition at line 264 of file LockMgrImpl.h.

Referenced by lockmgr::TableLockMgrImpl< TableDataLockMgr >::getTableMutex().

265  {
266  std::unique_ptr<heavyai::DistributedSharedMutex> table_mutex;
267 
268  std::string table_key_as_text;
269  for (auto n : table_key) {
270  table_key_as_text += (!table_key_as_text.empty() ? "_" : "") + std::to_string(n);
271  }
272 
273  // A callback used for syncing with most of the changed Catalog metadata, in-general,
274  // such as the list of tables that exist, dashboards, etc. This is accomplished by
275  // read locking, and immediately unlocking, dcatalogMutex_, so
276  // cat->reloadCatalogMetadataUnlocked() will be called.
277  auto cb_reload_catalog_metadata = [table_key](bool write) {
278  if constexpr (T::kind == "insert") {
279  CHECK(write); // The insert lock is for writing, never for reading.
280  }
282  table_key[CHUNK_KEY_DB_IDX]);
283  CHECK(cat);
285  *cat->dcatalogMutex_);
286  };
287 
288  if constexpr (T::kind == "schema") {
289  // A callback used for reloading the Catalog schema for the one table being locked.
290  auto cb_reload_table_metadata = [table_key, table_key_as_text](size_t version) {
291  VLOG(2) << "reloading table metadata for: table_" << table_key_as_text;
292  CHECK_EQ(table_key.size(), 2U);
294  table_key[CHUNK_KEY_DB_IDX]);
295  CHECK(cat);
296  std::unique_lock<heavyai::DistributedSharedMutex> dwrite_lock(
297  *cat->dcatalogMutex_);
298  cat->reloadTableMetadataUnlocked(table_key[CHUNK_KEY_TABLE_IDX]);
299  };
300 
301  // Create the table mutex.
303  /*pre_lock_callback=*/cb_reload_catalog_metadata,
304  /*reload_cache_callback=*/cb_reload_table_metadata};
305  auto schema_lockfile{
306  std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
308  ("table_" + table_key_as_text + "." + T::kind.data() + ".lockfile")};
309  table_mutex = std::make_unique<heavyai::DistributedSharedMutex>(
310  schema_lockfile.string(), cbs);
311  } else if constexpr (T::kind == "data" || T::kind == "insert") {
312  // A callback used for reloading the DataMgr data for the one table being locked.
313  auto cb_reload_table_data = [table_key, table_key_as_text](size_t version) {
314  VLOG(2) << "invalidating table caches for new version " << version
315  << " of: table_" << table_key_as_text;
316  CHECK_EQ(table_key.size(), 2U);
318  table_key[CHUNK_KEY_DB_IDX]);
319  CHECK(cat);
320  cat->invalidateCachesForTable(table_key[CHUNK_KEY_TABLE_IDX]);
321  };
322 
323  // Create the rows mutex.
324  auto rows_lockfile{std::filesystem::path(g_base_path) /
326  ("table_" + table_key_as_text + ".rows.lockfile")};
327  std::shared_ptr<heavyai::DistributedSharedMutex> rows_mutex =
328  std::make_shared<heavyai::DistributedSharedMutex>(
329  rows_lockfile.string(),
330  /*reload_cache_callback=*/cb_reload_table_data);
331 
332  // A callback used for syncing with outside changes to this table's row data.
333  auto cb_reload_row_data = [table_key, rows_mutex](bool /*write*/) {
334  std::shared_lock rows_read_lock(*rows_mutex);
335  };
336 
337  // A callback to notify other nodes about our changes to this table's row data.
338  auto cb_notify_about_row_data = [table_key, rows_mutex](bool write) {
339  if (write) {
340  std::unique_lock rows_write_lock(*rows_mutex);
341  }
342  };
343 
344  // Create the table mutex.
346  /*pre_lock_callback=*/cb_reload_catalog_metadata,
347  {},
348  /*post_lock_callback=*/cb_reload_row_data,
349  /*pre_unlock_callback=*/cb_notify_about_row_data};
350  auto table_lockfile{
351  std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
353  ("table_" + table_key_as_text + "." + T::kind.data() + ".lockfile")};
354  table_mutex =
355  std::make_unique<heavyai::DistributedSharedMutex>(table_lockfile.string(), cbs);
356  } else {
357  UNREACHABLE() << "unexpected lockmgr kind: " << T::kind;
358  }
359 
360  return table_mutex;
361  }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
const std::string kDataDirectoryName
std::string cat(Ts &&...args)
#define CHUNK_KEY_DB_IDX
Definition: types.h:38
#define UNREACHABLE()
Definition: Logger.h:266
std::string to_string(char const *&&v)
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:150
std::shared_lock< T > shared_lock
static SysCatalog & instance()
Definition: SysCatalog.h:337
std::string g_base_path
Definition: SysCatalog.cpp:62
string version
Definition: setup.in.py:73
std::unique_lock< T > unique_lock
#define CHUNK_KEY_TABLE_IDX
Definition: types.h:39
std::shared_ptr< Catalog > getCatalog(const std::string &dbName)
const std::string kCatalogDirectoryName
#define CHECK(condition)
Definition: Logger.h:222
const std::string kLockfilesDirectoryName
constexpr double n
Definition: Utm.h:38
#define VLOG(n)
Definition: Logger.h:316

+ Here is the caller graph for this function:

template<class T>
std::set<ChunkKey> lockmgr::TableLockMgrImpl< T >::getLockedTables ( ) const
inline

Definition at line 213 of file LockMgrImpl.h.

213  {
214  std::set<ChunkKey> ret;
215  std::lock_guard<std::mutex> access_map_lock(map_mutex_);
216  for (const auto& kv : table_mutex_map_) {
217  if (kv.second->isAcquired()) {
218  ret.insert(kv.first);
219  }
220  }
221 
222  return ret;
223  }
std::map< ChunkKey, std::unique_ptr< MutexTracker > > table_mutex_map_
Definition: LockMgrImpl.h:364
template<class T>
static ReadLock lockmgr::TableLockMgrImpl< T >::getReadLockForTable ( Catalog_Namespace::Catalog cat,
const std::string &  table_name 
)
inlinestatic

Definition at line 243 of file LockMgrImpl.h.

Referenced by lockmgr::TableInsertLockContainer< ReadLock >::acquire().

244  {
245  if (const auto tdp = cat.getMetadataForTable(table_name, false)) {
246  ChunkKey chunk_key{cat.getCurrentDB().dbId, tdp->tableId};
247  auto& table_lock_mgr = T::instance();
248  MutexTracker* tracker = table_lock_mgr.getTableMutex(chunk_key);
249  CHECK(tracker);
250  return ReadLock(tracker);
251  } else {
252  throw std::runtime_error("Table/View " + table_name + " for catalog " +
253  cat.getCurrentDB().dbName + " does not exist");
254  }
255  }
std::vector< int > ChunkKey
Definition: types.h:36
TrackedRefLock< ReadLockBase > ReadLock
Definition: LockMgrImpl.h:141
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:242
#define CHECK(condition)
Definition: Logger.h:222
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.

+ Here is the caller graph for this function:

template<class T>
static ReadLock lockmgr::TableLockMgrImpl< T >::getReadLockForTable ( const ChunkKey  table_key)
inlinestatic

Definition at line 256 of file LockMgrImpl.h.

256  {
257  auto& table_lock_mgr = T::instance();
258  return ReadLock(table_lock_mgr.getTableMutex(table_key));
259  }
TrackedRefLock< ReadLockBase > ReadLock
Definition: LockMgrImpl.h:141
template<class T>
virtual MutexTracker* lockmgr::TableLockMgrImpl< T >::getTableMutex ( const ChunkKey  table_key)
inlinevirtual

Definition at line 197 of file LockMgrImpl.h.

197  {
198  std::lock_guard<std::mutex> access_map_lock(map_mutex_);
199  auto mutex_it = table_mutex_map_.find(table_key);
200  if (mutex_it != table_mutex_map_.end()) {
201  return mutex_it->second.get();
202  }
203 
204  // NOTE(sy): Only used by --multi-instance clusters.
205  std::unique_ptr<heavyai::DistributedSharedMutex> dmutex =
206  getClusterTableMutex(table_key);
207 
208  return table_mutex_map_
209  .emplace(table_key, std::make_unique<MutexTracker>(std::move(dmutex)))
210  .first->second.get();
211  }
virtual std::unique_ptr< heavyai::DistributedSharedMutex > getClusterTableMutex(const ChunkKey table_key)
Definition: LockMgrImpl.h:264
std::map< ChunkKey, std::unique_ptr< MutexTracker > > table_mutex_map_
Definition: LockMgrImpl.h:364
template<class T>
static WriteLock lockmgr::TableLockMgrImpl< T >::getWriteLockForTable ( Catalog_Namespace::Catalog cat,
const std::string &  table_name 
)
inlinestatic

Definition at line 225 of file LockMgrImpl.h.

Referenced by lockmgr::TableInsertLockContainer< WriteLock >::acquire(), Parser::CopyTableStmt::execute(), Parser::InsertValuesStmt::execute(), DBHandler::import_table(), DBHandler::importGeoTableSingle(), DBHandler::insert_chunks(), DBHandler::insert_data(), DBHandler::load_table(), DBHandler::load_table_binary(), DBHandler::load_table_binary_arrow(), DBHandler::loadTableBinaryColumnarInternal(), and TableArchiver::restoreTable().

226  {
227  if (const auto tdp = cat.getMetadataForTable(table_name, false)) {
228  ChunkKey chunk_key{cat.getCurrentDB().dbId, tdp->tableId};
229  auto& table_lock_mgr = T::instance();
230  MutexTracker* tracker = table_lock_mgr.getTableMutex(chunk_key);
231  CHECK(tracker);
232  return WriteLock(tracker);
233  } else {
234  throw std::runtime_error("Table/View " + table_name + " for catalog " +
235  cat.getCurrentDB().dbName + " does not exist");
236  }
237  }
std::vector< int > ChunkKey
Definition: types.h:36
TrackedRefLock< WriteLockBase > WriteLock
Definition: LockMgrImpl.h:140
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:242
#define CHECK(condition)
Definition: Logger.h:222
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.

+ Here is the caller graph for this function:

template<class T>
static WriteLock lockmgr::TableLockMgrImpl< T >::getWriteLockForTable ( const ChunkKey  table_key)
inlinestatic

Definition at line 238 of file LockMgrImpl.h.

238  {
239  auto& table_lock_mgr = T::instance();
240  return WriteLock(table_lock_mgr.getTableMutex(table_key));
241  }
TrackedRefLock< WriteLockBase > WriteLock
Definition: LockMgrImpl.h:140
template<class T>
static T& lockmgr::TableLockMgrImpl< T >::instance ( )
inlinestatic

Definition at line 191 of file LockMgrImpl.h.

191  {
192  static T mgr;
193  return mgr;
194  }

Member Data Documentation

template<class T>
std::mutex lockmgr::TableLockMgrImpl< T >::map_mutex_
mutableprotected
template<class T>
std::map<ChunkKey, std::unique_ptr<MutexTracker> > lockmgr::TableLockMgrImpl< T >::table_mutex_map_
protected

The documentation for this class was generated from the following files: