OmniSciDB  a5dc49c757
 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 (const 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) const
 

Protected Attributes

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

Static Private Member Functions

static MutexTrackergetMutexTracker (const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
 
static void validateExistingTable (const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
 
static int32_t validateAndGetExistingTableId (const 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 158 of file LockMgrImpl.h.

158 {}

Member Function Documentation

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

Definition at line 168 of file LockMgr.cpp.

References cat(), CHECK, CHECK_EQ, CHUNK_KEY_DB_IDX, CHUNK_KEY_TABLE_IDX, g_base_path, Catalog_Namespace::SysCatalog::getCatalog(), Catalog_Namespace::SysCatalog::instance(), shared::kCatalogDirectoryName, shared::kDataDirectoryName, shared::kLockfilesDirectoryName, anonymous_namespace{Utm.h}::n, to_string(), UNREACHABLE, setup::version, VLOG, and File_Namespace::write().

168  {
169  std::unique_ptr<heavyai::DistributedSharedMutex> table_mutex;
170 
171  std::string table_key_as_text;
172  for (auto n : table_key) {
173  table_key_as_text += (!table_key_as_text.empty() ? "_" : "") + std::to_string(n);
174  }
175 
176  // A callback used for syncing with most of the changed Catalog metadata, in-general,
177  // such as the list of tables that exist, dashboards, etc. This is accomplished by
178  // read locking, and immediately unlocking, dcatalogMutex_, so
179  // cat->reloadCatalogMetadataUnlocked() will be called.
180  auto cb_reload_catalog_metadata = [table_key](bool write) {
181  if constexpr (T::kind == "insert") {
182  CHECK(write); // The insert lock is for writing, never for reading.
183  }
184  auto cat =
186  CHECK(cat);
188  *cat->dcatalogMutex_);
189  };
190 
191  if constexpr (T::kind == "schema") {
192  // A callback used for reloading the Catalog schema for the one table being locked.
193  auto cb_reload_table_metadata = [table_key, table_key_as_text](size_t version) {
194  VLOG(2) << "reloading table metadata for: table_" << table_key_as_text;
195  CHECK_EQ(table_key.size(), 2U);
197  table_key[CHUNK_KEY_DB_IDX]);
198  CHECK(cat);
200  *cat->dcatalogMutex_);
201  cat->reloadTableMetadataUnlocked(table_key[CHUNK_KEY_TABLE_IDX]);
202  };
203 
204  // Create the table mutex.
206  cb_reload_catalog_metadata, // pre_lock_callback
207  cb_reload_table_metadata // reload_cache_callback
208  };
209  auto schema_lockfile{
210  std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
212  ("table_" + table_key_as_text + "." + T::kind.data() + ".lockfile")};
213  table_mutex =
214  std::make_unique<heavyai::DistributedSharedMutex>(schema_lockfile.string(), cbs);
215  } else if constexpr (T::kind == "data" || T::kind == "insert") {
216  // A callback used for reloading the DataMgr data for the one table being locked.
217  auto cb_reload_table_data = [table_key, table_key_as_text](size_t version) {
218  VLOG(2) << "invalidating table caches for new version " << version << " of: table_"
219  << table_key_as_text;
220  CHECK_EQ(table_key.size(), 2U);
222  table_key[CHUNK_KEY_DB_IDX]);
223  CHECK(cat);
224  cat->invalidateCachesForTable(table_key[CHUNK_KEY_TABLE_IDX]);
225  };
226 
227  // Create the rows mutex.
228  auto rows_lockfile{std::filesystem::path(g_base_path) /
230  ("table_" + table_key_as_text + ".rows.lockfile")};
231  std::shared_ptr<heavyai::DistributedSharedMutex> rows_mutex =
232  std::make_shared<heavyai::DistributedSharedMutex>(
233  rows_lockfile.string(),
234  cb_reload_table_data // reload_cache_callback
235  );
236 
237  // A callback used for syncing with outside changes to this table's row data.
238  auto cb_reload_row_data = [table_key, rows_mutex](bool write) {
239  heavyai::shared_lock<heavyai::DistributedSharedMutex> rows_read_lock(*rows_mutex);
240  };
241 
242  // A callback to notify other nodes about our changes to this table's row data.
243  auto cb_notify_about_row_data = [table_key, rows_mutex](bool write) {
244  if (write) {
246  *rows_mutex);
247  }
248  };
249 
250  // Create the table mutex.
252  cb_reload_catalog_metadata, // pre_lock_callback
253  {},
254  cb_reload_row_data, // post_lock_callback
255  cb_notify_about_row_data // pre_unlock_callback
256  };
257  auto table_lockfile{
258  std::filesystem::path(g_base_path) / shared::kLockfilesDirectoryName /
260  ("table_" + table_key_as_text + "." + T::kind.data() + ".lockfile")};
261  table_mutex =
262  std::make_unique<heavyai::DistributedSharedMutex>(table_lockfile.string(), cbs);
263  } else {
264  UNREACHABLE() << "unexpected lockmgr kind: " << T::kind;
265  }
266 
267  return table_mutex;
268 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
const std::string kDataDirectoryName
std::string cat(Ts &&...args)
#define CHUNK_KEY_DB_IDX
Definition: types.h:38
#define UNREACHABLE()
Definition: Logger.h:338
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:143
std::shared_lock< T > shared_lock
static SysCatalog & instance()
Definition: SysCatalog.h:343
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:291
const std::string kLockfilesDirectoryName
constexpr double n
Definition: Utm.h:38
#define VLOG(n)
Definition: Logger.h:388

+ Here is the call graph for this function:

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

Definition at line 124 of file LockMgr.cpp.

124  {
125  std::set<ChunkKey> ret;
126  std::lock_guard<std::mutex> access_map_lock(map_mutex_);
127  for (const auto& kv : table_mutex_map_) {
128  if (kv.second->isAcquired()) {
129  ret.insert(kv.first);
130  }
131  }
132 
133  return ret;
134 }
std::map< ChunkKey, std::unique_ptr< MutexTracker > > table_mutex_map_
Definition: LockMgrImpl.h:164
template<class T >
MutexTracker * lockmgr::TableLockMgrImpl< T >::getMutexTracker ( const Catalog_Namespace::Catalog catalog,
const std::string &  table_name 
)
staticprivate

Definition at line 271 of file LockMgr.cpp.

References CHECK, Catalog_Namespace::Catalog::getDatabaseId(), and lockmgr::instance().

273  {
274  ChunkKey chunk_key{catalog.getDatabaseId(),
275  validateAndGetExistingTableId(catalog, table_name)};
276  auto& table_lock_mgr = T::instance();
277  MutexTracker* tracker = table_lock_mgr.getTableMutex(chunk_key);
278  CHECK(tracker);
279  return tracker;
280 }
std::vector< int > ChunkKey
Definition: types.h:36
static int32_t validateAndGetExistingTableId(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:289
int getDatabaseId() const
Definition: Catalog.h:326
T & instance()
Definition: LockMgr.cpp:101
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the call graph for this function:

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

Definition at line 152 of file LockMgr.cpp.

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

153  {
154  auto lock = ReadLock(getMutexTracker(cat, table_name));
155  // Ensure table still exists after lock is acquired.
156  validateAndGetExistingTableId(cat, table_name);
157  return lock;
158 }
static MutexTracker * getMutexTracker(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:271
static int32_t validateAndGetExistingTableId(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:289
TrackedRefLock< ReadLockBase > ReadLock
Definition: LockMgrImpl.h:89

+ Here is the caller graph for this function:

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

Definition at line 161 of file LockMgr.cpp.

References lockmgr::instance().

161  {
162  auto& table_lock_mgr = T::instance();
163  return ReadLock(table_lock_mgr.getTableMutex(table_key));
164 }
TrackedRefLock< ReadLockBase > ReadLock
Definition: LockMgrImpl.h:89
T & instance()
Definition: LockMgr.cpp:101

+ Here is the call graph for this function:

template<class T >
MutexTracker * lockmgr::TableLockMgrImpl< T >::getTableMutex ( const ChunkKey table_key)
virtual

Definition at line 107 of file LockMgr.cpp.

107  {
108  std::lock_guard<std::mutex> access_map_lock(map_mutex_);
109  auto mutex_it = table_mutex_map_.find(table_key);
110  if (mutex_it != table_mutex_map_.end()) {
111  return mutex_it->second.get();
112  }
113 
114  // NOTE(sy): Only used by --multi-instance clusters.
115  std::unique_ptr<heavyai::DistributedSharedMutex> dmutex =
116  getClusterTableMutex(table_key);
117 
118  return table_mutex_map_
119  .emplace(table_key, std::make_unique<MutexTracker>(std::move(dmutex)))
120  .first->second.get();
121 }
virtual std::unique_ptr< heavyai::DistributedSharedMutex > getClusterTableMutex(const ChunkKey &table_key) const
Definition: LockMgr.cpp:168
std::map< ChunkKey, std::unique_ptr< MutexTracker > > table_mutex_map_
Definition: LockMgrImpl.h:164
template<class T >
WriteLock lockmgr::TableLockMgrImpl< T >::getWriteLockForTable ( const Catalog_Namespace::Catalog cat,
const std::string &  table_name 
)
static

Definition at line 137 of file LockMgr.cpp.

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::load_table_binary_columnar(), and TableArchiver::restoreTable().

138  {
139  auto lock = WriteLock(getMutexTracker(cat, table_name));
140  // Ensure table still exists after lock is acquired.
141  validateExistingTable(cat, table_name);
142  return lock;
143 }
static MutexTracker * getMutexTracker(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:271
static void validateExistingTable(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:283
TrackedRefLock< WriteLockBase > WriteLock
Definition: LockMgrImpl.h:88

+ Here is the caller graph for this function:

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

Definition at line 146 of file LockMgr.cpp.

References lockmgr::instance().

146  {
147  auto& table_lock_mgr = T::instance();
148  return WriteLock(table_lock_mgr.getTableMutex(table_key));
149 }
TrackedRefLock< WriteLockBase > WriteLock
Definition: LockMgrImpl.h:88
T & instance()
Definition: LockMgr.cpp:101

+ Here is the call graph for this function:

template<class T>
static T& lockmgr::TableLockMgrImpl< T >::instance ( )
static
template<class T >
int32_t lockmgr::TableLockMgrImpl< T >::validateAndGetExistingTableId ( const Catalog_Namespace::Catalog catalog,
const std::string &  table_name 
)
staticprivate

Definition at line 289 of file LockMgr.cpp.

References Catalog_Namespace::Catalog::getTableId(), and Catalog_Namespace::Catalog::name().

291  {
292  auto table_id = catalog.getTableId(table_name);
293  if (!table_id.has_value()) {
294  throw Catalog_Namespace::TableNotFoundException(table_name, catalog.name());
295  }
296  return table_id.value();
297 }
std::optional< int32_t > getTableId(const std::string &table_name) const
Definition: Catalog.cpp:1881
std::string name() const
Definition: Catalog.h:348

+ Here is the call graph for this function:

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

Definition at line 283 of file LockMgr.cpp.

284  {
285  validateAndGetExistingTableId(catalog, table_name);
286 }
static int32_t validateAndGetExistingTableId(const Catalog_Namespace::Catalog &catalog, const std::string &table_name)
Definition: LockMgr.cpp:289

Member Data Documentation

template<class T>
std::mutex lockmgr::TableLockMgrImpl< T >::map_mutex_
mutableprotected

Definition at line 163 of file LockMgrImpl.h.

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

Definition at line 164 of file LockMgrImpl.h.


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