ClickHouse/src/Storages/MergeTree/checkDataPart.cpp

209 lines
7.3 KiB
C++
Raw Normal View History

2019-03-20 16:18:13 +00:00
#include <algorithm>
#include <optional>
#include <Poco/File.h>
#include <Poco/DirectoryIterator.h>
#include <Storages/MergeTree/MergeTreeIndexGranularity.h>
#include <Storages/MergeTree/checkDataPart.h>
2020-01-13 14:53:32 +00:00
#include <Storages/MergeTree/MergeTreeDataPartCompact.h>
#include <Storages/MergeTree/MergeTreeDataPartInMemory.h>
2018-12-28 18:15:26 +00:00
#include <Compression/CompressedReadBuffer.h>
#include <IO/HashingReadBuffer.h>
#include <Common/CurrentMetrics.h>
namespace CurrentMetrics
{
extern const Metric ReplicatedChecks;
}
namespace DB
{
namespace ErrorCodes
{
extern const int CORRUPTED_DATA;
2020-01-17 12:24:27 +00:00
extern const int UNKNOWN_PART_TYPE;
extern const int MEMORY_LIMIT_EXCEEDED;
extern const int CANNOT_ALLOCATE_MEMORY;
extern const int CANNOT_MUNMAP;
extern const int CANNOT_MREMAP;
}
bool isNotEnoughMemoryErrorCode(int code)
{
/// Don't count the part as broken if there is not enough memory to load it.
/// In fact, there can be many similar situations.
/// But it is OK, because there is a safety guard against deleting too many parts.
return code == ErrorCodes::MEMORY_LIMIT_EXCEEDED
|| code == ErrorCodes::CANNOT_ALLOCATE_MEMORY
|| code == ErrorCodes::CANNOT_MUNMAP
|| code == ErrorCodes::CANNOT_MREMAP;
}
2020-01-13 14:53:32 +00:00
IMergeTreeDataPart::Checksums checkDataPart(
2020-02-28 17:14:55 +00:00
const DiskPtr & disk,
const String & full_relative_path,
2020-01-13 14:53:32 +00:00
const NamesAndTypesList & columns_list,
const MergeTreeDataPartType & part_type,
const NameSet & files_without_checksums,
bool require_checksums,
std::function<bool()> is_cancelled)
{
/** Responsibility:
* - read list of columns from columns.txt;
* - read checksums if exist;
2020-01-13 14:53:32 +00:00
* - validate list of columns and checksums
*/
CurrentMetrics::Increment metric_increment{CurrentMetrics::ReplicatedChecks};
2020-02-28 17:14:55 +00:00
String path = full_relative_path;
if (!path.empty() && path.back() != '/')
path += "/";
2020-01-13 14:53:32 +00:00
NamesAndTypesList columns_txt;
{
2020-02-28 17:14:55 +00:00
auto buf = disk->readFile(path + "columns.txt");
columns_txt.readText(*buf);
assertEOF(*buf);
}
2020-01-13 14:53:32 +00:00
if (columns_txt != columns_list)
throw Exception("Columns doesn't match in part " + path
+ ". Expected: " + columns_list.toString()
+ ". Found: " + columns_txt.toString(), ErrorCodes::CORRUPTED_DATA);
/// Real checksums based on contents of data. Must correspond to checksums.txt. If not - it means the data is broken.
2020-01-13 14:53:32 +00:00
IMergeTreeDataPart::Checksums checksums_data;
2020-04-07 17:08:46 +00:00
/// This function calculates checksum for both compressed and decompressed contents of compressed file.
2020-02-28 17:14:55 +00:00
auto checksum_compressed_file = [](const DiskPtr & disk_, const String & file_path)
{
2020-02-28 17:14:55 +00:00
auto file_buf = disk_->readFile(file_path);
HashingReadBuffer compressed_hashing_buf(*file_buf);
2020-01-13 14:53:32 +00:00
CompressedReadBuffer uncompressing_buf(compressed_hashing_buf);
HashingReadBuffer uncompressed_hashing_buf(uncompressing_buf);
2020-09-08 16:28:49 +00:00
uncompressed_hashing_buf.ignoreAll();
2020-01-13 14:53:32 +00:00
return IMergeTreeDataPart::Checksums::Checksum
{
2020-01-13 14:53:32 +00:00
compressed_hashing_buf.count(), compressed_hashing_buf.getHash(),
uncompressed_hashing_buf.count(), uncompressed_hashing_buf.getHash()
};
};
2019-12-02 23:50:53 +00:00
/// This function calculates only checksum of file content (compressed or uncompressed).
auto checksum_file = [](const DiskPtr & disk_, const String & file_path)
{
auto file_buf = disk_->readFile(file_path);
HashingReadBuffer hashing_buf(*file_buf);
2020-09-08 16:28:49 +00:00
hashing_buf.ignoreAll();
return IMergeTreeDataPart::Checksums::Checksum{hashing_buf.count(), hashing_buf.getHash()};
};
bool check_uncompressed = true;
2020-04-07 17:08:46 +00:00
/// First calculate checksums for columns data
2020-01-13 14:53:32 +00:00
if (part_type == MergeTreeDataPartType::COMPACT)
{
const auto & file_name = MergeTreeDataPartCompact::DATA_FILE_NAME_WITH_EXTENSION;
checksums_data.files[file_name] = checksum_file(disk, path + file_name);
/// Uncompressed checksums in compact parts are computed in a complex way.
/// We check only checksum of compressed file.
check_uncompressed = false;
2020-01-13 14:53:32 +00:00
}
else if (part_type == MergeTreeDataPartType::WIDE)
{
for (const auto & column : columns_list)
{
2021-03-09 14:46:52 +00:00
auto serialization = IDataType::getSerialization(column,
[&](const String & stream_name)
{
return disk->exists(stream_name + IMergeTreeDataPart::DATA_FILE_EXTENSION);
});
serialization->enumerateStreams([&](const ISerialization::SubstreamPath & substream_path)
2020-01-13 14:53:32 +00:00
{
2021-03-09 14:46:52 +00:00
String file_name = ISerialization::getFileNameForStream(column, substream_path) + ".bin";
2020-02-28 17:14:55 +00:00
checksums_data.files[file_name] = checksum_compressed_file(disk, path + file_name);
2020-01-13 14:53:32 +00:00
}, {});
}
2020-01-13 14:53:32 +00:00
}
else
{
throw Exception("Unknown type in part " + path, ErrorCodes::UNKNOWN_PART_TYPE);
}
2020-04-07 17:08:46 +00:00
/// Checksums from the rest files listed in checksums.txt. May be absent. If present, they are subsequently compared with the actual data checksums.
2020-01-13 14:53:32 +00:00
IMergeTreeDataPart::Checksums checksums_txt;
2020-02-28 17:14:55 +00:00
if (require_checksums || disk->exists(path + "checksums.txt"))
{
2020-02-28 17:14:55 +00:00
auto buf = disk->readFile(path + "checksums.txt");
checksums_txt.read(*buf);
assertEOF(*buf);
}
const auto & checksum_files_txt = checksums_txt.files;
for (auto it = disk->iterateDirectory(path); it->isValid(); it->next())
{
const String & file_name = it->name();
auto checksum_it = checksums_data.files.find(file_name);
2020-04-07 23:13:53 +00:00
2020-04-07 17:08:46 +00:00
/// Skip files that we already calculated. Also skip metadata files that are not checksummed.
if (checksum_it == checksums_data.files.end() && !files_without_checksums.count(file_name))
{
auto txt_checksum_it = checksum_files_txt.find(file_name);
if (txt_checksum_it == checksum_files_txt.end() || txt_checksum_it->second.uncompressed_size == 0)
{
2020-04-07 17:08:46 +00:00
/// The file is not compressed.
checksums_data.files[file_name] = checksum_file(disk, it->path());
}
else /// If we have both compressed and uncompressed in txt, than calculate them
{
checksums_data.files[file_name] = checksum_compressed_file(disk, it->path());
}
}
}
2020-01-13 14:53:32 +00:00
if (is_cancelled())
return {};
if (require_checksums || !checksums_txt.files.empty())
checksums_txt.checkEqual(checksums_data, check_uncompressed);
return checksums_data;
}
IMergeTreeDataPart::Checksums checkDataPartInMemory(const DataPartInMemoryPtr & data_part)
{
IMergeTreeDataPart::Checksums data_checksums;
data_checksums.files["data.bin"] = data_part->calculateBlockChecksum();
data_part->checksums.checkEqual(data_checksums, true);
return data_checksums;
}
2020-01-13 14:53:32 +00:00
IMergeTreeDataPart::Checksums checkDataPart(
2019-03-20 16:18:13 +00:00
MergeTreeData::DataPartPtr data_part,
bool require_checksums,
std::function<bool()> is_cancelled)
{
if (auto part_in_memory = asInMemoryPart(data_part))
return checkDataPartInMemory(part_in_memory);
2020-06-03 18:59:18 +00:00
2019-03-20 16:18:13 +00:00
return checkDataPart(
data_part->volume->getDisk(),
2020-02-28 17:14:55 +00:00
data_part->getFullRelativePath(),
2020-01-16 16:15:01 +00:00
data_part->getColumns(),
2020-01-13 14:53:32 +00:00
data_part->getType(),
data_part->getFileNamesWithoutChecksums(),
2019-03-20 16:18:13 +00:00
require_checksums,
is_cancelled);
}
}