2022-10-23 03:29:26 +00:00
|
|
|
#include "Storages/MergeTree/IDataPartStorage.h"
|
2019-03-20 16:18:13 +00:00
|
|
|
#include <algorithm>
|
2017-12-03 00:46:34 +00:00
|
|
|
#include <optional>
|
|
|
|
|
2017-12-03 03:43:48 +00:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2017-12-03 00:46:34 +00:00
|
|
|
|
2019-03-30 13:44:23 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeIndexGranularity.h>
|
2017-12-03 00:48:19 +00:00
|
|
|
#include <Storages/MergeTree/checkDataPart.h>
|
2020-01-13 14:53:32 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartCompact.h>
|
2020-06-22 18:56:53 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataPartInMemory.h>
|
2018-12-28 18:15:26 +00:00
|
|
|
#include <Compression/CompressedReadBuffer.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/HashingReadBuffer.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2014-07-22 08:21:16 +00:00
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric ReplicatedChecks;
|
|
|
|
}
|
|
|
|
|
2014-07-22 08:21:16 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CORRUPTED_DATA;
|
2020-01-17 12:24:27 +00:00
|
|
|
extern const int UNKNOWN_PART_TYPE;
|
2020-04-20 01:44:24 +00:00
|
|
|
extern const int MEMORY_LIMIT_EXCEEDED;
|
|
|
|
extern const int CANNOT_ALLOCATE_MEMORY;
|
|
|
|
extern const int CANNOT_MUNMAP;
|
|
|
|
extern const int CANNOT_MREMAP;
|
2021-02-10 14:12:49 +00:00
|
|
|
extern const int UNEXPECTED_FILE_IN_DATA_PART;
|
2020-04-20 01:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
IMergeTreeDataPart::Checksums checkDataPart(
|
2021-02-10 14:12:49 +00:00
|
|
|
MergeTreeData::DataPartPtr data_part,
|
2022-10-23 03:29:26 +00:00
|
|
|
const IDataPartStorage & data_part_storage,
|
2020-01-13 14:53:32 +00:00
|
|
|
const NamesAndTypesList & columns_list,
|
|
|
|
const MergeTreeDataPartType & part_type,
|
2020-08-26 15:29:46 +00:00
|
|
|
const NameSet & files_without_checksums,
|
2017-12-03 00:46:34 +00:00
|
|
|
bool require_checksums,
|
|
|
|
std::function<bool()> is_cancelled)
|
2014-07-22 08:21:16 +00:00
|
|
|
{
|
2017-12-03 00:46:34 +00:00
|
|
|
/** 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
|
2017-12-03 00:46:34 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-21 01:47:28 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::ReplicatedChecks};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
NamesAndTypesList columns_txt;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2014-07-22 08:21:16 +00:00
|
|
|
{
|
2022-10-23 03:29:26 +00:00
|
|
|
auto buf = data_part_storage.readFile("columns.txt", {}, std::nullopt, std::nullopt);
|
2020-02-28 17:14:55 +00:00
|
|
|
columns_txt.readText(*buf);
|
|
|
|
assertEOF(*buf);
|
2014-07-22 08:21:16 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
if (columns_txt != columns_list)
|
2022-10-23 03:29:26 +00:00
|
|
|
throw Exception("Columns doesn't match in part " + data_part_storage.getFullPath()
|
2020-01-13 14:53:32 +00:00
|
|
|
+ ". Expected: " + columns_list.toString()
|
|
|
|
+ ". Found: " + columns_txt.toString(), ErrorCodes::CORRUPTED_DATA);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-03 00:46:34 +00:00
|
|
|
/// 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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-07 17:08:46 +00:00
|
|
|
/// This function calculates checksum for both compressed and decompressed contents of compressed file.
|
2022-10-23 03:29:26 +00:00
|
|
|
auto checksum_compressed_file = [](const IDataPartStorage & data_part_storage_, const String & file_path)
|
2014-07-22 10:35:24 +00:00
|
|
|
{
|
2022-10-23 03:29:26 +00:00
|
|
|
auto file_buf = data_part_storage_.readFile(file_path, {}, std::nullopt, std::nullopt);
|
2020-02-28 17:14:55 +00:00
|
|
|
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);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-09-08 16:28:49 +00:00
|
|
|
uncompressed_hashing_buf.ignoreAll();
|
2020-01-13 14:53:32 +00:00
|
|
|
return IMergeTreeDataPart::Checksums::Checksum
|
2015-05-28 04:32:38 +00:00
|
|
|
{
|
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
|
|
|
|
2022-03-14 15:17:43 +00:00
|
|
|
auto ratio_of_defaults = data_part->storage.getSettings()->ratio_of_defaults_for_sparse_serialization;
|
|
|
|
SerializationInfoByName serialization_infos(columns_txt, SerializationInfo::Settings{ratio_of_defaults, false});
|
2021-05-19 01:48:46 +00:00
|
|
|
|
2022-10-23 03:29:26 +00:00
|
|
|
if (data_part_storage.exists(IMergeTreeDataPart::SERIALIZATION_FILE_NAME))
|
2021-05-19 01:48:46 +00:00
|
|
|
{
|
2022-10-23 03:29:26 +00:00
|
|
|
auto serialization_file = data_part_storage.readFile(IMergeTreeDataPart::SERIALIZATION_FILE_NAME, {}, std::nullopt, std::nullopt);
|
2021-12-08 15:29:00 +00:00
|
|
|
serialization_infos.readJSON(*serialization_file);
|
2021-05-19 01:48:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 03:03:52 +00:00
|
|
|
auto get_serialization = [&serialization_infos](const auto & column)
|
|
|
|
{
|
|
|
|
auto it = serialization_infos.find(column.name);
|
|
|
|
return it == serialization_infos.end()
|
|
|
|
? column.type->getDefaultSerialization()
|
|
|
|
: column.type->getSerialization(*it->second);
|
|
|
|
};
|
|
|
|
|
2020-07-10 23:33:36 +00:00
|
|
|
/// This function calculates only checksum of file content (compressed or uncompressed).
|
2022-04-22 16:58:09 +00:00
|
|
|
auto checksum_file = [&](const String & file_name)
|
2020-07-10 23:33:36 +00:00
|
|
|
{
|
2022-10-29 14:26:34 +00:00
|
|
|
auto file_buf = data_part_storage.readFile(file_name, {}, std::nullopt, std::nullopt);
|
|
|
|
HashingReadBuffer hashing_buf(*file_buf);
|
|
|
|
hashing_buf.ignoreAll();
|
|
|
|
checksums_data.files[file_name] = IMergeTreeDataPart::Checksums::Checksum(hashing_buf.count(), hashing_buf.getHash());
|
2020-07-10 23:33:36 +00:00
|
|
|
};
|
|
|
|
|
2022-10-29 14:26:34 +00:00
|
|
|
/// Do not check uncompressed for projections. But why?
|
|
|
|
bool check_uncompressed = !data_part->isProjectionPart();
|
|
|
|
|
2020-04-07 17:08:46 +00:00
|
|
|
/// First calculate checksums for columns data
|
2022-03-14 14:42:09 +00:00
|
|
|
if (part_type == MergeTreeDataPartType::Compact)
|
2020-01-13 14:53:32 +00:00
|
|
|
{
|
2022-04-22 16:58:09 +00:00
|
|
|
checksum_file(MergeTreeDataPartCompact::DATA_FILE_NAME_WITH_EXTENSION);
|
2020-07-10 23:33:36 +00:00
|
|
|
/// 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
|
|
|
}
|
2022-03-14 14:42:09 +00:00
|
|
|
else if (part_type == MergeTreeDataPartType::Wide)
|
2020-01-13 14:53:32 +00:00
|
|
|
{
|
|
|
|
for (const auto & column : columns_list)
|
|
|
|
{
|
2021-11-02 03:03:52 +00:00
|
|
|
get_serialization(column)->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";
|
2022-04-22 16:58:09 +00:00
|
|
|
checksums_data.files[file_name] = checksum_compressed_file(data_part_storage, file_name);
|
2021-10-11 22:01:00 +00:00
|
|
|
});
|
2015-05-28 04:32:38 +00:00
|
|
|
}
|
2020-01-13 14:53:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-23 03:29:26 +00:00
|
|
|
throw Exception("Unknown type in part " + data_part_storage.getFullPath(), ErrorCodes::UNKNOWN_PART_TYPE);
|
2014-07-22 10:35:24 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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;
|
2017-12-03 03:43:48 +00:00
|
|
|
|
2022-10-23 03:29:26 +00:00
|
|
|
if (require_checksums || data_part_storage.exists("checksums.txt"))
|
2017-12-03 03:43:48 +00:00
|
|
|
{
|
2022-10-23 03:29:26 +00:00
|
|
|
auto buf = data_part_storage.readFile("checksums.txt", {}, std::nullopt, std::nullopt);
|
2020-02-28 17:14:55 +00:00
|
|
|
checksums_txt.read(*buf);
|
|
|
|
assertEOF(*buf);
|
2014-07-22 08:21:16 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-10-29 14:26:34 +00:00
|
|
|
NameSet projections_on_disk;
|
2020-04-06 17:00:29 +00:00
|
|
|
const auto & checksum_files_txt = checksums_txt.files;
|
2022-10-23 03:29:26 +00:00
|
|
|
for (auto it = data_part_storage.iterate(); it->isValid(); it->next())
|
2020-04-06 17:00:29 +00:00
|
|
|
{
|
2022-10-29 14:26:34 +00:00
|
|
|
auto file_name = it->name();
|
|
|
|
|
|
|
|
/// We will check projections later.
|
|
|
|
if (data_part_storage.isDirectory(file_name) && endsWith(file_name, ".proj"))
|
|
|
|
{
|
|
|
|
projections_on_disk.insert(file_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:00:29 +00:00
|
|
|
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.
|
2022-04-18 10:18:43 +00:00
|
|
|
if (checksum_it == checksums_data.files.end() && !files_without_checksums.contains(file_name))
|
2020-04-06 17:00:29 +00:00
|
|
|
{
|
|
|
|
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.
|
2022-04-22 16:58:09 +00:00
|
|
|
checksum_file(file_name);
|
2020-04-06 17:00:29 +00:00
|
|
|
}
|
2021-02-10 14:12:49 +00:00
|
|
|
else /// If we have both compressed and uncompressed in txt, then calculate them
|
2020-04-06 17:00:29 +00:00
|
|
|
{
|
2022-04-22 16:58:09 +00:00
|
|
|
checksums_data.files[file_name] = checksum_compressed_file(data_part_storage, file_name);
|
2020-04-06 17:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 14:26:34 +00:00
|
|
|
for (const auto & [name, projection] : data_part->getProjectionParts())
|
|
|
|
{
|
|
|
|
if (is_cancelled())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto projection_file = name + ".proj";
|
|
|
|
auto projection_checksums = checkDataPart(
|
|
|
|
projection, *data_part_storage.getProjection(projection_file),
|
|
|
|
projection->getColumns(), projection->getType(),
|
|
|
|
projection->getFileNamesWithoutChecksums(),
|
|
|
|
require_checksums, is_cancelled);
|
|
|
|
|
|
|
|
checksums_data.files[projection_file] = IMergeTreeDataPart::Checksums::Checksum(
|
|
|
|
projection_checksums.getTotalSizeOnDisk(),
|
|
|
|
projection_checksums.getTotalChecksumUInt128());
|
|
|
|
|
|
|
|
projections_on_disk.erase(projection_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (require_checksums && !projections_on_disk.empty())
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_FILE_IN_DATA_PART,
|
|
|
|
"Found unexpected projection directories: {}",
|
|
|
|
fmt::join(projections_on_disk, ","));
|
|
|
|
}
|
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
if (is_cancelled())
|
|
|
|
return {};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-03 00:46:34 +00:00
|
|
|
if (require_checksums || !checksums_txt.files.empty())
|
2020-07-10 23:33:36 +00:00
|
|
|
checksums_txt.checkEqual(checksums_data, check_uncompressed);
|
2022-10-29 14:26:34 +00:00
|
|
|
|
2017-12-03 00:46:34 +00:00
|
|
|
return checksums_data;
|
2014-07-22 08:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:56:53 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-06-22 18:56:53 +00:00
|
|
|
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(
|
2021-02-10 14:12:49 +00:00
|
|
|
data_part,
|
2022-10-23 03:29:26 +00:00
|
|
|
data_part->getDataPartStorage(),
|
2020-01-16 16:15:01 +00:00
|
|
|
data_part->getColumns(),
|
2020-01-13 14:53:32 +00:00
|
|
|
data_part->getType(),
|
2020-08-26 15:29:46 +00:00
|
|
|
data_part->getFileNamesWithoutChecksums(),
|
2019-03-20 16:18:13 +00:00
|
|
|
require_checksums,
|
|
|
|
is_cancelled);
|
|
|
|
}
|
|
|
|
|
2014-07-22 08:21:16 +00:00
|
|
|
}
|