2019-03-20 16:18:13 +00:00
|
|
|
#include <algorithm>
|
2017-12-03 00:46:34 +00:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <Poco/File.h>
|
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>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/MarkInCompressedFile.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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric ReplicatedChecks;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 08:21:16 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CORRUPTED_DATA;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
IMergeTreeDataPart::Checksums checkDataPart(
|
2019-03-20 16:18:13 +00:00
|
|
|
const String & full_path,
|
2020-01-13 14:53:32 +00:00
|
|
|
const NamesAndTypesList & columns_list,
|
|
|
|
const MergeTreeDataPartType & part_type,
|
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
|
|
|
*/
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::ReplicatedChecks};
|
|
|
|
|
2019-03-20 16:18:13 +00:00
|
|
|
String path = full_path;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!path.empty() && path.back() != '/')
|
|
|
|
path += "/";
|
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
NamesAndTypesList columns_txt;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
ReadBufferFromFile buf(path + "columns.txt");
|
2020-01-13 14:53:32 +00:00
|
|
|
columns_txt.readText(buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
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-01-13 14:53:32 +00:00
|
|
|
auto checksum_compressed_file = [](const String & file_path)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-01-13 14:53:32 +00:00
|
|
|
ReadBufferFromFile file_buf(file_path);
|
|
|
|
HashingReadBuffer compressed_hashing_buf(file_buf);
|
|
|
|
CompressedReadBuffer uncompressing_buf(compressed_hashing_buf);
|
|
|
|
HashingReadBuffer uncompressed_hashing_buf(uncompressing_buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
uncompressed_hashing_buf.tryIgnore(std::numeric_limits<size_t>::max());
|
|
|
|
return IMergeTreeDataPart::Checksums::Checksum
|
2017-04-01 07:20:54 +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
|
|
|
|
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_compressed_file(path + file_name);
|
|
|
|
}
|
|
|
|
else if (part_type == MergeTreeDataPartType::WIDE)
|
|
|
|
{
|
|
|
|
for (const auto & column : columns_list)
|
|
|
|
{
|
|
|
|
column.type->enumerateStreams([&](const IDataType::SubstreamPath & substream_path)
|
|
|
|
{
|
|
|
|
String file_name = IDataType::getFileNameForStream(column.name, substream_path) + ".bin";
|
|
|
|
checksums_data.files[file_name] = checksum_compressed_file(path + file_name);
|
|
|
|
}, {});
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2020-01-13 14:53:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception("Unknown type in part " + path, ErrorCodes::UNKNOWN_PART_TYPE);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 03:43:48 +00:00
|
|
|
Poco::DirectoryIterator dir_end;
|
|
|
|
for (Poco::DirectoryIterator dir_it(path); dir_it != dir_end; ++dir_it)
|
|
|
|
{
|
|
|
|
const String & file_name = dir_it.name();
|
2020-01-13 14:53:32 +00:00
|
|
|
auto checksum_it = checksums_data.files.find(file_name);
|
|
|
|
if (checksum_it == checksums_data.files.end() && file_name != "checksums.txt" && file_name != "columns.txt")
|
2017-12-03 03:43:48 +00:00
|
|
|
{
|
|
|
|
ReadBufferFromFile file_buf(dir_it->path());
|
|
|
|
HashingReadBuffer hashing_buf(file_buf);
|
|
|
|
hashing_buf.tryIgnore(std::numeric_limits<size_t>::max());
|
2020-01-13 14:53:32 +00:00
|
|
|
checksums_data.files[file_name] = IMergeTreeDataPart::Checksums::Checksum(hashing_buf.count(), hashing_buf.getHash());
|
2017-12-03 03:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
/// Checksums from file checksums.txt. May be absent. If present, they are subsequently compared with the actual data checksums.
|
|
|
|
IMergeTreeDataPart::Checksums checksums_txt;
|
2017-12-03 03:43:48 +00:00
|
|
|
|
2020-01-13 14:53:32 +00:00
|
|
|
if (require_checksums || Poco::File(path + "checksums.txt").exists())
|
2017-12-03 03:43:48 +00:00
|
|
|
{
|
2020-01-13 14:53:32 +00:00
|
|
|
ReadBufferFromFile buf(path + "checksums.txt");
|
|
|
|
checksums_txt.read(buf);
|
2017-12-03 03:43:48 +00:00
|
|
|
assertEOF(buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
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())
|
2017-04-01 07:20:54 +00:00
|
|
|
checksums_txt.checkEqual(checksums_data, true);
|
|
|
|
|
2017-12-03 00:46:34 +00:00
|
|
|
return checksums_data;
|
2014-07-22 08:21:16 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return checkDataPart(
|
|
|
|
data_part->getFullPath(),
|
2020-01-16 16:15:01 +00:00
|
|
|
data_part->getColumns(),
|
2020-01-13 14:53:32 +00:00
|
|
|
data_part->getType(),
|
2019-03-20 16:18:13 +00:00
|
|
|
require_checksums,
|
|
|
|
is_cancelled);
|
|
|
|
}
|
|
|
|
|
2014-07-22 08:21:16 +00:00
|
|
|
}
|