2022-06-08 12:09:59 +00:00
|
|
|
#include <Storages/Freeze.h>
|
|
|
|
|
2022-06-13 14:46:30 +00:00
|
|
|
#include <Disks/ObjectStorages/IMetadataStorage.h>
|
2022-06-08 12:09:59 +00:00
|
|
|
#include <Storages/PartitionCommands.h>
|
|
|
|
#include <Common/escapeForFileName.h>
|
|
|
|
#include <Common/logger_useful.h>
|
|
|
|
|
2022-08-17 08:04:46 +00:00
|
|
|
/**
|
2022-08-18 08:43:50 +00:00
|
|
|
* When ClickHouse has frozen data on remote storage it required 'smart' data removing during UNFREEZE.
|
2022-08-17 08:04:46 +00:00
|
|
|
* For remote storage actually frozen not remote data but local metadata with referrers on remote data.
|
|
|
|
* So remote data can be referred from working and frozen data sets (or two frozen) at same time.
|
|
|
|
* In this case during UNFREEZE ClickHouse should remove only local metadata and keep remote data.
|
|
|
|
* But when data was already removed from working data set ClickHouse should remove remote data too.
|
|
|
|
* To detect is current data used or not in some other place ClickHouse uses
|
|
|
|
* - ref_count from metadata to check if data used in some other metadata on the same replica;
|
|
|
|
* - Keeper record to check if data used on other replica.
|
|
|
|
* StorageReplicatedMergeTree::removeSharedDetachedPart makes required checks, so here this method
|
|
|
|
* called for each frozen part.
|
|
|
|
*/
|
|
|
|
|
2022-06-08 12:09:59 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2022-07-21 13:57:28 +00:00
|
|
|
|
2022-08-18 09:44:57 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2022-08-18 10:03:45 +00:00
|
|
|
extern const int SUPPORT_IS_DISABLED;
|
2022-08-18 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 12:09:59 +00:00
|
|
|
void FreezeMetaData::fill(const StorageReplicatedMergeTree & storage)
|
|
|
|
{
|
|
|
|
replica_name = storage.getReplicaName();
|
|
|
|
zookeeper_name = storage.getZooKeeperName();
|
|
|
|
table_shared_id = storage.getTableSharedID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreezeMetaData::save(DiskPtr data_disk, const String & path) const
|
|
|
|
{
|
2022-06-13 14:46:30 +00:00
|
|
|
auto metadata_storage = data_disk->getMetadataStorage();
|
2022-06-08 12:09:59 +00:00
|
|
|
|
|
|
|
auto file_path = getFileName(path);
|
2022-06-13 14:46:30 +00:00
|
|
|
auto tx = metadata_storage->createTransaction();
|
|
|
|
WriteBufferFromOwnString buffer;
|
|
|
|
|
|
|
|
writeIntText(version, buffer);
|
|
|
|
buffer.write("\n", 1);
|
2022-08-18 10:03:45 +00:00
|
|
|
if (version == 1)
|
2022-08-18 09:44:57 +00:00
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
/// is_replicated and is_remote are not used
|
|
|
|
bool is_replicated = true;
|
|
|
|
writeBoolText(is_replicated, buffer);
|
|
|
|
buffer.write("\n", 1);
|
|
|
|
bool is_remote = true;
|
|
|
|
writeBoolText(is_remote, buffer);
|
|
|
|
buffer.write("\n", 1);
|
|
|
|
}
|
|
|
|
writeString(escapeForFileName(replica_name), buffer);
|
2022-06-13 14:46:30 +00:00
|
|
|
buffer.write("\n", 1);
|
|
|
|
writeString(zookeeper_name, buffer);
|
|
|
|
buffer.write("\n", 1);
|
|
|
|
writeString(table_shared_id, buffer);
|
|
|
|
buffer.write("\n", 1);
|
|
|
|
|
|
|
|
tx->writeStringToFile(file_path, buffer.str());
|
|
|
|
tx->commit();
|
2022-06-08 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FreezeMetaData::load(DiskPtr data_disk, const String & path)
|
|
|
|
{
|
2022-06-13 14:46:30 +00:00
|
|
|
auto metadata_storage = data_disk->getMetadataStorage();
|
2022-06-08 12:09:59 +00:00
|
|
|
auto file_path = getFileName(path);
|
|
|
|
|
2022-06-13 14:46:30 +00:00
|
|
|
if (!metadata_storage->exists(file_path))
|
2022-06-08 12:09:59 +00:00
|
|
|
return false;
|
2022-06-13 14:46:30 +00:00
|
|
|
auto metadata_str = metadata_storage->readFileToString(file_path);
|
|
|
|
ReadBufferFromString buffer(metadata_str);
|
|
|
|
readIntText(version, buffer);
|
2022-06-21 09:17:52 +00:00
|
|
|
if (version < 1 or version > 2)
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
2022-08-17 08:04:46 +00:00
|
|
|
LOG_ERROR(&Poco::Logger::get("FreezeMetaData"), "Unknown frozen metadata version: {}", version);
|
2022-06-08 12:09:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-13 14:46:30 +00:00
|
|
|
DB::assertChar('\n', buffer);
|
2022-08-18 10:03:45 +00:00
|
|
|
if (version == 1)
|
2022-08-18 09:44:57 +00:00
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
/// is_replicated and is_remote are not used
|
|
|
|
bool is_replicated;
|
|
|
|
readBoolText(is_replicated, buffer);
|
|
|
|
DB::assertChar('\n', buffer);
|
|
|
|
bool is_remote;
|
|
|
|
readBoolText(is_remote, buffer);
|
|
|
|
DB::assertChar('\n', buffer);
|
|
|
|
}
|
|
|
|
auto unescaped_replica_name = unescapeForFileName(replica_name);
|
|
|
|
readString(unescaped_replica_name, buffer);
|
2022-06-13 14:46:30 +00:00
|
|
|
DB::assertChar('\n', buffer);
|
|
|
|
readString(zookeeper_name, buffer);
|
|
|
|
DB::assertChar('\n', buffer);
|
|
|
|
readString(table_shared_id, buffer);
|
|
|
|
DB::assertChar('\n', buffer);
|
2022-06-08 12:09:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreezeMetaData::clean(DiskPtr data_disk, const String & path)
|
|
|
|
{
|
2022-06-13 14:46:30 +00:00
|
|
|
auto metadata_storage = data_disk->getMetadataStorage();
|
|
|
|
auto fname = getFileName(path);
|
|
|
|
if (metadata_storage->exists(fname))
|
|
|
|
{
|
|
|
|
auto tx = metadata_storage->createTransaction();
|
|
|
|
tx->unlinkFile(fname);
|
|
|
|
tx->commit();
|
|
|
|
}
|
2022-06-08 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String FreezeMetaData::getFileName(const String & path)
|
|
|
|
{
|
|
|
|
return fs::path(path) / "frozen_metadata.txt";
|
|
|
|
}
|
|
|
|
|
2022-08-18 08:43:50 +00:00
|
|
|
Unfreezer::Unfreezer(ContextPtr context) : local_context(context), zookeeper()
|
|
|
|
{
|
|
|
|
const auto & config = local_context->getConfigRef();
|
|
|
|
static constexpr auto config_key = "enable_system_unfreeze";
|
2022-08-18 10:03:45 +00:00
|
|
|
if (!config.getBool(config_key, false))
|
2022-08-18 09:44:57 +00:00
|
|
|
{
|
2022-08-18 08:43:50 +00:00
|
|
|
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED, "Support for SYSTEM UNFREEZE query is disabled. You can enable it via '{}' server setting", config_key);
|
|
|
|
}
|
|
|
|
if (local_context->hasZooKeeper())
|
|
|
|
zookeeper = local_context->getZooKeeper();
|
|
|
|
}
|
|
|
|
|
2022-07-21 13:57:28 +00:00
|
|
|
BlockIO Unfreezer::unfreeze(const String & backup_name)
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
LOG_DEBUG(log, "Unfreezing backup {}", escapeForFileName(backup_name));
|
2022-07-22 10:10:08 +00:00
|
|
|
auto disks_map = local_context->getDisksMap();
|
2022-06-08 12:09:59 +00:00
|
|
|
Disks disks;
|
|
|
|
for (auto & [name, disk]: disks_map)
|
|
|
|
{
|
|
|
|
disks.push_back(disk);
|
|
|
|
}
|
|
|
|
auto backup_path = fs::path(backup_directory_prefix) / escapeForFileName(backup_name);
|
2022-06-21 09:17:52 +00:00
|
|
|
auto store_paths = {backup_path / "store", backup_path / "data"};
|
2022-06-08 12:09:59 +00:00
|
|
|
|
|
|
|
PartitionCommandsResultInfo result_info;
|
|
|
|
|
|
|
|
for (const auto & disk: disks)
|
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
for (auto store_path: store_paths)
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
if (!disk->exists(store_path))
|
|
|
|
continue;
|
|
|
|
for (auto prefix_it = disk->iterateDirectory(store_path); prefix_it->isValid(); prefix_it->next())
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
auto prefix_directory = store_path / prefix_it->name();
|
|
|
|
for (auto table_it = disk->iterateDirectory(prefix_directory); table_it->isValid(); table_it->next())
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
auto table_directory = prefix_directory / table_it->name();
|
|
|
|
auto current_result_info = unfreezePartitionsFromTableDirectory(
|
2022-07-22 10:10:08 +00:00
|
|
|
[](const String &) { return true; }, backup_name, {disk}, table_directory);
|
2022-06-21 09:17:52 +00:00
|
|
|
for (auto & command_result : current_result_info)
|
|
|
|
{
|
|
|
|
command_result.command_type = "SYSTEM UNFREEZE";
|
|
|
|
}
|
|
|
|
result_info.insert(
|
|
|
|
result_info.end(),
|
|
|
|
std::make_move_iterator(current_result_info.begin()),
|
|
|
|
std::make_move_iterator(current_result_info.end()));
|
2022-06-08 12:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (disk->exists(backup_path))
|
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
/// After unfreezing we need to clear revision.txt file and empty directories
|
2022-06-08 12:09:59 +00:00
|
|
|
disk->removeRecursive(backup_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockIO result;
|
|
|
|
if (!result_info.empty())
|
|
|
|
{
|
|
|
|
result.pipeline = QueryPipeline(convertCommandsResultToSource(result_info));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-21 13:57:28 +00:00
|
|
|
bool Unfreezer::removeFreezedPart(DiskPtr disk, const String & path, const String & part_name, ContextPtr local_context, zkutil::ZooKeeperPtr zookeeper)
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
|
|
|
if (disk->supportZeroCopyReplication())
|
|
|
|
{
|
|
|
|
FreezeMetaData meta;
|
|
|
|
if (meta.load(disk, path))
|
|
|
|
{
|
2022-06-21 09:17:52 +00:00
|
|
|
FreezeMetaData::clean(disk, path);
|
2022-07-21 13:57:28 +00:00
|
|
|
return StorageReplicatedMergeTree::removeSharedDetachedPart(disk, path, part_name, meta.table_shared_id, meta.zookeeper_name, meta.replica_name, "", local_context, zookeeper);
|
2022-06-08 12:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disk->removeRecursive(path);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-22 10:10:08 +00:00
|
|
|
PartitionCommandsResultInfo Unfreezer::unfreezePartitionsFromTableDirectory(MergeTreeData::MatcherFn matcher, const String & backup_name, const Disks & disks, const fs::path & table_directory)
|
2022-06-08 12:09:59 +00:00
|
|
|
{
|
|
|
|
PartitionCommandsResultInfo result;
|
|
|
|
|
|
|
|
for (const auto & disk : disks)
|
|
|
|
{
|
|
|
|
if (!disk->exists(table_directory))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto it = disk->iterateDirectory(table_directory); it->isValid(); it->next())
|
|
|
|
{
|
|
|
|
const auto & partition_directory = it->name();
|
|
|
|
|
2022-07-22 10:10:08 +00:00
|
|
|
/// Partition ID is prefix of part directory name: <partition id>_<rest of part directory name>
|
2022-06-08 12:09:59 +00:00
|
|
|
auto found = partition_directory.find('_');
|
|
|
|
if (found == std::string::npos)
|
|
|
|
continue;
|
|
|
|
auto partition_id = partition_directory.substr(0, found);
|
|
|
|
|
|
|
|
if (!matcher(partition_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto & path = it->path();
|
|
|
|
|
2022-07-22 10:10:08 +00:00
|
|
|
bool keep_shared = removeFreezedPart(disk, path, partition_directory, local_context, zookeeper);
|
2022-06-08 12:09:59 +00:00
|
|
|
|
|
|
|
result.push_back(PartitionCommandResultInfo{
|
|
|
|
.partition_id = partition_id,
|
|
|
|
.part_name = partition_directory,
|
|
|
|
.backup_path = disk->getPath() + table_directory.generic_string(),
|
|
|
|
.part_backup_path = disk->getPath() + path,
|
|
|
|
.backup_name = backup_name,
|
|
|
|
});
|
|
|
|
|
|
|
|
LOG_DEBUG(log, "Unfreezed part by path {}, keep shared data: {}", disk->getPath() + path, keep_shared);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_DEBUG(log, "Unfreezed {} parts", result.size());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|