Revert "Revert "Merge pull request #45493 from azat/fix-detach""

This reverts commit 9dc4f02d15.
This commit is contained in:
Alexander Tokmakov 2023-01-24 14:15:51 +01:00
parent f818e3070d
commit 42a976fe3d
7 changed files with 19 additions and 10 deletions

View File

@ -379,14 +379,14 @@ void DatabaseOnDisk::renameTable(
if (dictionary && table && !table->isDictionary()) if (dictionary && table && !table->isDictionary())
throw Exception(ErrorCodes::INCORRECT_QUERY, "Use RENAME/EXCHANGE TABLE (instead of RENAME/EXCHANGE DICTIONARY) for tables"); throw Exception(ErrorCodes::INCORRECT_QUERY, "Use RENAME/EXCHANGE TABLE (instead of RENAME/EXCHANGE DICTIONARY) for tables");
table_lock = table->lockExclusively(
local_context->getCurrentQueryId(), local_context->getSettingsRef().lock_acquire_timeout);
detachTable(local_context, table_name); detachTable(local_context, table_name);
UUID prev_uuid = UUIDHelpers::Nil; UUID prev_uuid = UUIDHelpers::Nil;
try try
{ {
table_lock = table->lockExclusively(
local_context->getCurrentQueryId(), local_context->getSettingsRef().lock_acquire_timeout);
table_metadata_path = getObjectMetadataPath(table_name); table_metadata_path = getObjectMetadataPath(table_name);
attach_query = parseQueryFromMetadata(log, local_context, table_metadata_path); attach_query = parseQueryFromMetadata(log, local_context, table_metadata_path);
auto & create = attach_query->as<ASTCreateQuery &>(); auto & create = attach_query->as<ASTCreateQuery &>();

View File

@ -236,6 +236,7 @@ StoragePtr DatabaseWithOwnTablesBase::detachTableUnlocked(const String & table_n
backQuote(database_name), backQuote(table_name)); backQuote(database_name), backQuote(table_name));
res = it->second; res = it->second;
tables.erase(it); tables.erase(it);
res->is_detached = true;
auto table_id = res->getStorageID(); auto table_id = res->getStorageID();
if (table_id.hasUUID()) if (table_id.hasUUID())
@ -272,6 +273,10 @@ void DatabaseWithOwnTablesBase::attachTableUnlocked(const String & table_name, c
DatabaseCatalog::instance().removeUUIDMapping(table_id.uuid); DatabaseCatalog::instance().removeUUIDMapping(table_id.uuid);
throw Exception(ErrorCodes::TABLE_ALREADY_EXISTS, "Table {} already exists.", table_id.getFullTableName()); throw Exception(ErrorCodes::TABLE_ALREADY_EXISTS, "Table {} already exists.", table_id.getFullTableName());
} }
/// It is important to reset is_detached here since in case of RENAME in
/// non-Atomic database the is_detached is set to true before RENAME.
table->is_detached = false;
} }
void DatabaseWithOwnTablesBase::shutdown() void DatabaseWithOwnTablesBase::shutdown()

View File

@ -448,7 +448,7 @@ void DatabaseMySQL::detachTablePermanently(ContextPtr, const String & table_name
remove_or_detach_tables.erase(table_name); remove_or_detach_tables.erase(table_name);
throw; throw;
} }
table_iter->second.second->is_dropped = true; table_iter->second.second->is_detached = true;
} }
void DatabaseMySQL::dropTable(ContextPtr local_context, const String & table_name, bool /*sync*/) void DatabaseMySQL::dropTable(ContextPtr local_context, const String & table_name, bool /*sync*/)

View File

@ -287,6 +287,10 @@ BlockIO InterpreterDropQuery::executeToTemporaryTable(const String & table_name,
table->drop(); table->drop();
table->is_dropped = true; table->is_dropped = true;
} }
else if (kind == ASTDropQuery::Kind::Detach)
{
table->is_detached = true;
}
} }
} }

View File

@ -50,10 +50,10 @@ TableLockHolder IStorage::lockForShare(const String & query_id, const std::chron
{ {
TableLockHolder result = tryLockTimed(drop_lock, RWLockImpl::Read, query_id, acquire_timeout); TableLockHolder result = tryLockTimed(drop_lock, RWLockImpl::Read, query_id, acquire_timeout);
if (is_dropped) if (is_dropped || is_detached)
{ {
auto table_id = getStorageID(); auto table_id = getStorageID();
throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {}.{} is dropped", table_id.database_name, table_id.table_name); throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {}.{} is dropped or detached", table_id.database_name, table_id.table_name);
} }
return result; return result;
} }
@ -62,7 +62,7 @@ TableLockHolder IStorage::tryLockForShare(const String & query_id, const std::ch
{ {
TableLockHolder result = tryLockTimed(drop_lock, RWLockImpl::Read, query_id, acquire_timeout); TableLockHolder result = tryLockTimed(drop_lock, RWLockImpl::Read, query_id, acquire_timeout);
if (is_dropped) if (is_dropped || is_detached)
{ {
// Table was dropped while acquiring the lock // Table was dropped while acquiring the lock
result = nullptr; result = nullptr;
@ -81,7 +81,7 @@ IStorage::AlterLockHolder IStorage::lockForAlter(const std::chrono::milliseconds
"Possible deadlock avoided. Client should retry.", "Possible deadlock avoided. Client should retry.",
getStorageID().getFullTableName(), acquire_timeout.count()); getStorageID().getFullTableName(), acquire_timeout.count());
if (is_dropped) if (is_dropped || is_detached)
throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {} is dropped or detached", getStorageID()); throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {} is dropped or detached", getStorageID());
return lock; return lock;
@ -93,7 +93,7 @@ TableExclusiveLockHolder IStorage::lockExclusively(const String & query_id, cons
TableExclusiveLockHolder result; TableExclusiveLockHolder result;
result.drop_lock = tryLockTimed(drop_lock, RWLockImpl::Write, query_id, acquire_timeout); result.drop_lock = tryLockTimed(drop_lock, RWLockImpl::Write, query_id, acquire_timeout);
if (is_dropped) if (is_dropped || is_detached)
throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {} is dropped or detached", getStorageID()); throw Exception(ErrorCodes::TABLE_IS_DROPPED, "Table {} is dropped or detached", getStorageID());
return result; return result;

View File

@ -562,6 +562,7 @@ public:
virtual void onActionLockRemove(StorageActionBlockType /* action_type */) {} virtual void onActionLockRemove(StorageActionBlockType /* action_type */) {}
std::atomic<bool> is_dropped{false}; std::atomic<bool> is_dropped{false};
std::atomic<bool> is_detached{false};
/// Does table support index for IN sections /// Does table support index for IN sections
virtual bool supportsIndexForIn() const { return false; } virtual bool supportsIndexForIn() const { return false; }

View File

@ -470,7 +470,6 @@ void StorageLiveView::drop()
DatabaseCatalog::instance().removeViewDependency(select_table_id, table_id); DatabaseCatalog::instance().removeViewDependency(select_table_id, table_id);
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
is_dropped = true;
condition.notify_all(); condition.notify_all();
} }