OmniSciDB  ca0c39ec8f
 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_
 

Static Private Member Functions

static MutexTrackergetMutexTracker (Catalog_Namespace::Catalog &catalog, const std::string &table_name)
 
static void validateExistingTable (Catalog_Namespace::Catalog &catalog, const std::string &table_name)
 
static int32_t validateAndGetExistingTableId (Catalog_Namespace::Catalog &catalog, const std::string &table_name)
 

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 252 of file LockMgrImpl.h.

252 {}

Member Function Documentation

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

Definition at line 254 of file LockMgrImpl.h.

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

255  {
256  std::unique_ptr<heavyai::DistributedSharedMutex> table_mutex;
257 
258  std::string table_key_as_text;
259  for (auto n : table_key) {
260  table_key_as_text += (!table_key_as_text.empty() ? "_" : "") + std::to_string(n);
261  }
262 
263  // A callback used for syncing with most of the changed Catalog metadata, in-general,
264  // such as the list of tables that exist, dashboards, etc. This is accomplished by
265  // read locking, and immediately unlocking, dcatalogMutex_, so
266  // cat->reloadCatalogMetadataUnlocked() will be called.
267  auto cb_reload_catalog_metadata = [table_key](bool write) {
268  if constexpr (T::kind == "insert") {
269  CHECK(write); // The insert lock is for writing, never for reading.
270  }
272  table_key[CHUNK_KEY_DB_IDX]);
273  CHECK(cat);
275  *cat->dcatalogMutex_);
276  };
277 
278  if constexpr (T::kind == "schema") {
279  // A callback used for reloading the Catalog schema for the one table being locked.
280  auto cb_reload_table_metadata = [table_key, table_key_as_text](size_t version) {
281  VLOG(2) << "reloading table metadata for: table_" << table_key_as_text;
282  CHECK_EQ(table_key.size(), 2U);
284  table_key[CHUNK_KEY_DB_IDX]);
285  CHECK(cat);
287  *cat->dcatalogMutex_);
288  cat->reloadTableMetadataUnlocked(table_key[CHUNK_KEY_TABLE_IDX]);
289  };
290 
291  // Create the table mutex.
293  /*pre_lock_callback=*/cb_reload_catalog_metadata,
294  /*reload_cache_callback=*/cb_reload_table_metadata};
295  auto schema_lockfile{
296  std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
298  ("table_" + table_key_as_text + "." + T::kind.data() + ".lockfile")};
299  table_mutex = std::make_unique<heavyai::DistributedSharedMutex>(
300  schema_lockfile.string(), cbs);
301  } else if constexpr (T::kind == "data" || T::kind == "insert") {
302  // A callback used for reloading the DataMgr data for the one table being locked.
303  auto cb_reload_table_data = [table_key, table_key_as_text](size_t version) {
304  VLOG(2) << "invalidating table caches for new version " << version
305  << " of: table_" << table_key_as_text;
306  CHECK_EQ(table_key.size(), 2U);
308  table_key[CHUNK_KEY_DB_IDX]);
309  CHECK(cat);
310  cat->invalidateCachesForTable(table_key[CHUNK_KEY_TABLE_IDX]);
311  };
312 
313  // Create the rows mutex.
314  auto rows_lockfile{std::filesystem::path(g_base_path) /
316  ("table_" + table_key_as_text + ".rows.lockfile")};
317  std::shared_ptr<heavyai::DistributedSharedMutex> rows_mutex =
318  std::make_shared<heavyai::DistributedSharedMutex>(
319  rows_lockfile.string(),
320  /*reload_cache_callback=*/cb_reload_table_data);
321 
322  // A callback used for syncing with outside changes to this table's row data.
323  auto cb_reload_row_data = [table_key, rows_mutex](bool /*write*/) {
324  heavyai::shared_lock<heavyai::DistributedSharedMutex> rows_read_lock(*rows_mutex);
325  };
326 
327  // A callback to notify other nodes about our changes to this table's row data.
328  auto cb_notify_about_row_data = [table_key, rows_mutex](bool write) {
329  if (write) {
331  *rows_mutex);
332  }
333  };
334 
335  // Create the table mutex.
337  /*pre_lock_callback=*/cb_reload_catalog_metadata,
338  {},
339  /*post_lock_callback=*/cb_reload_row_data,
340  /*pre_unlock_callback=*/cb_notify_about_row_data};
341  auto table_lockfile{
342  std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
344  ("table_" + table_key_as_text + "." + T::kind.data() + ".lockfile")};
345  table_mutex =
346  std::make_unique<heavyai::DistributedSharedMutex>(table_lockfile.string(), cbs);
347  } else {
348  UNREACHABLE() << "unexpected lockmgr kind: " << T::kind;
349  }
350 
351  return table_mutex;
352  }
#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:341
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:355
template<class T>
static MutexTracker* lockmgr::TableLockMgrImpl< T >::getMutexTracker ( Catalog_Namespace::Catalog catalog,
const std::string &  table_name 
)
inlinestaticprivate

Definition at line 358 of file LockMgrImpl.h.

Referenced by lockmgr::TableLockMgrImpl< TableDataLockMgr >::getReadLockForTable(), and lockmgr::TableLockMgrImpl< TableDataLockMgr >::getWriteLockForTable().

359  {
360  ChunkKey chunk_key{catalog.getDatabaseId(),
361  validateAndGetExistingTableId(catalog, table_name)};
362  auto& table_lock_mgr = T::instance();
363  MutexTracker* tracker = table_lock_mgr.getTableMutex(chunk_key);
364  CHECK(tracker);
365  return tracker;
366  }
std::vector< int > ChunkKey
Definition: types.h:36
int getDatabaseId() const
Definition: Catalog.h:298
static int32_t validateAndGetExistingTableId(Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgrImpl.h:373
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the caller graph for this function:

template<class T>
static ReadLock lockmgr::TableLockMgrImpl< T >::getReadLockForTable ( Catalog_Namespace::Catalog cat,
const std::string &  table_name 
)
inlinestatic

Definition at line 238 of file LockMgrImpl.h.

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

239  {
240  auto lock = ReadLock(getMutexTracker(cat, table_name));
241  // Ensure table still exists after lock is acquired.
242  validateAndGetExistingTableId(cat, table_name);
243  return lock;
244  }
TrackedRefLock< ReadLockBase > ReadLock
Definition: LockMgrImpl.h:141
static MutexTracker * getMutexTracker(Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgrImpl.h:358
static int32_t validateAndGetExistingTableId(Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgrImpl.h:373

+ 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 246 of file LockMgrImpl.h.

246  {
247  auto& table_lock_mgr = T::instance();
248  return ReadLock(table_lock_mgr.getTableMutex(table_key));
249  }
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:254
std::map< ChunkKey, std::unique_ptr< MutexTracker > > table_mutex_map_
Definition: LockMgrImpl.h:355
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  auto lock = WriteLock(getMutexTracker(cat, table_name));
228  // Ensure table still exists after lock is acquired.
229  validateExistingTable(cat, table_name);
230  return lock;
231  }
static void validateExistingTable(Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgrImpl.h:368
TrackedRefLock< WriteLockBase > WriteLock
Definition: LockMgrImpl.h:140
static MutexTracker * getMutexTracker(Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgrImpl.h:358

+ 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 233 of file LockMgrImpl.h.

233  {
234  auto& table_lock_mgr = T::instance();
235  return WriteLock(table_lock_mgr.getTableMutex(table_key));
236  }
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  }
template<class T>
static int32_t lockmgr::TableLockMgrImpl< T >::validateAndGetExistingTableId ( Catalog_Namespace::Catalog catalog,
const std::string &  table_name 
)
inlinestaticprivate

Definition at line 373 of file LockMgrImpl.h.

Referenced by lockmgr::TableLockMgrImpl< TableDataLockMgr >::getMutexTracker(), lockmgr::TableLockMgrImpl< TableDataLockMgr >::getReadLockForTable(), and lockmgr::TableLockMgrImpl< TableDataLockMgr >::validateExistingTable().

374  {
375  auto table_id = catalog.getTableId(table_name);
376  if (!table_id.has_value()) {
377  throw std::runtime_error("Table/View " + table_name + " for catalog " +
378  catalog.name() + " does not exist");
379  }
380  return table_id.value();
381  }
std::optional< int32_t > getTableId(const std::string &table_name) const
Definition: Catalog.cpp:1934
std::string name() const
Definition: Catalog.h:315

+ Here is the caller graph for this function:

template<class T>
static void lockmgr::TableLockMgrImpl< T >::validateExistingTable ( Catalog_Namespace::Catalog catalog,
const std::string &  table_name 
)
inlinestaticprivate

Definition at line 368 of file LockMgrImpl.h.

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

369  {
370  validateAndGetExistingTableId(catalog, table_name);
371  }
static int32_t validateAndGetExistingTableId(Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgrImpl.h:373

+ Here is the caller graph for this function:

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: