MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / DDLGuard

Method DDLGuard

src/Interpreters/DatabaseCatalog.cpp:2208–2245  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2206
2207
2208DDLGuard::DDLGuard(Map & map_, SharedMutex & db_mutex_, std::unique_lock<std::mutex> guards_lock_, const String & elem, const String & database_name)
2209 : map(map_), db_mutex(db_mutex_), guards_lock(std::move(guards_lock_))
2210{
2211 it = map.emplace(elem, Entry{std::make_unique<std::mutex>(), 0}).first;
2212 ++it->second.counter;
2213 guards_lock.unlock();
2214 table_lock = std::unique_lock(*it->second.mutex);
2215 is_database_guard = elem.empty();
2216 if (!is_database_guard)
2217 {
2218 static constexpr int MAX_TRY = 10;
2219 static constexpr UInt64 INTERVAL_MS = 100;
2220 bool acquired_db_mutex_lock = false;
2221 for (int i = 0; i < MAX_TRY; ++i)
2222 {
2223 acquired_db_mutex_lock = db_mutex.try_lock_shared();
2224 if (acquired_db_mutex_lock)
2225 break;
2226
2227 if (!DatabaseCatalog::instance().isDatabaseExist(database_name))
2228 {
2229 releaseTableLock();
2230 throw Exception(ErrorCodes::UNKNOWN_DATABASE, "Database {} is currently dropped or renamed", database_name);
2231 }
2232
2233 sleepForMilliseconds(INTERVAL_MS);
2234 }
2235 if (!acquired_db_mutex_lock)
2236 {
2237 releaseTableLock();
2238 throw Exception(
2239 ErrorCodes::TIMEOUT_EXCEEDED,
2240 "Unable to acquire the database lock of {} after {} ms",
2241 database_name,
2242 MAX_TRY * INTERVAL_MS);
2243 }
2244 }
2245}
2246
2247void DDLGuard::releaseTableLock() noexcept
2248{

Callers

nothing calls this directly

Calls 7

sleepForMillisecondsFunction · 0.85
isDatabaseExistMethod · 0.80
ExceptionClass · 0.70
emplaceMethod · 0.45
unlockMethod · 0.45
emptyMethod · 0.45
try_lock_sharedMethod · 0.45

Tested by

no test coverage detected