2019-11-20 13:33:41 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeMarksLoader.h>
|
2020-02-03 12:46:25 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <Poco/File.h>
|
2019-11-20 13:33:41 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2019-11-20 13:33:41 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-02-03 12:46:25 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int CANNOT_READ_ALL_DATA;
|
2020-02-03 12:46:25 +00:00
|
|
|
extern const int CORRUPTED_DATA;
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:33:41 +00:00
|
|
|
MergeTreeMarksLoader::MergeTreeMarksLoader(
|
2020-02-27 16:47:40 +00:00
|
|
|
DiskPtr disk_,
|
2019-11-20 13:33:41 +00:00
|
|
|
MarkCache * mark_cache_,
|
|
|
|
const String & mrk_path_,
|
2020-02-03 12:46:25 +00:00
|
|
|
size_t marks_count_,
|
|
|
|
const MergeTreeIndexGranularityInfo & index_granularity_info_,
|
2019-11-20 13:33:41 +00:00
|
|
|
bool save_marks_in_cache_,
|
2020-02-03 12:46:25 +00:00
|
|
|
size_t columns_in_mark_)
|
2020-02-27 16:47:40 +00:00
|
|
|
: disk(std::move(disk_))
|
|
|
|
, mark_cache(mark_cache_)
|
2019-11-20 13:33:41 +00:00
|
|
|
, mrk_path(mrk_path_)
|
2020-02-03 12:46:25 +00:00
|
|
|
, marks_count(marks_count_)
|
|
|
|
, index_granularity_info(index_granularity_info_)
|
2019-11-20 13:33:41 +00:00
|
|
|
, save_marks_in_cache(save_marks_in_cache_)
|
2020-02-03 12:46:25 +00:00
|
|
|
, columns_in_mark(columns_in_mark_) {}
|
2019-11-20 13:33:41 +00:00
|
|
|
|
|
|
|
const MarkInCompressedFile & MergeTreeMarksLoader::getMark(size_t row_index, size_t column_index)
|
|
|
|
{
|
|
|
|
if (!marks)
|
|
|
|
loadMarks();
|
2020-02-03 12:46:25 +00:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (column_index >= columns_in_mark)
|
2019-12-18 16:41:11 +00:00
|
|
|
throw Exception("Column index: " + toString(column_index)
|
2020-02-03 12:46:25 +00:00
|
|
|
+ " is out of range [0, " + toString(columns_in_mark) + ")", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (*marks)[row_index * columns_in_mark + column_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkCache::MappedPtr MergeTreeMarksLoader::loadMarksImpl()
|
|
|
|
{
|
|
|
|
/// Memory for marks must not be accounted as memory usage for query, because they are stored in shared cache.
|
2020-10-21 00:31:12 +00:00
|
|
|
MemoryTracker::BlockerInThread temporarily_disable_memory_tracker;
|
2020-02-03 12:46:25 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
size_t file_size = disk->getFileSize(mrk_path);
|
2020-02-03 12:46:25 +00:00
|
|
|
size_t mark_size = index_granularity_info.getMarkSizeInBytes(columns_in_mark);
|
|
|
|
size_t expected_file_size = mark_size * marks_count;
|
|
|
|
|
|
|
|
if (expected_file_size != file_size)
|
|
|
|
throw Exception(
|
2020-02-27 16:47:40 +00:00
|
|
|
"Bad size of marks file '" + fullPath(disk, mrk_path) + "': " + std::to_string(file_size) + ", must be: " + std::to_string(expected_file_size),
|
2020-02-03 12:46:25 +00:00
|
|
|
ErrorCodes::CORRUPTED_DATA);
|
2019-12-18 16:41:11 +00:00
|
|
|
|
2020-02-03 12:46:25 +00:00
|
|
|
auto res = std::make_shared<MarksInCompressedFile>(marks_count * columns_in_mark);
|
|
|
|
|
|
|
|
if (!index_granularity_info.is_adaptive)
|
|
|
|
{
|
|
|
|
/// Read directly to marks.
|
2020-02-27 16:47:40 +00:00
|
|
|
auto buffer = disk->readFile(mrk_path, file_size);
|
2020-02-28 11:54:18 +00:00
|
|
|
buffer->readStrict(reinterpret_cast<char *>(res->data()), file_size);
|
2020-02-03 12:46:25 +00:00
|
|
|
|
2020-02-28 11:54:18 +00:00
|
|
|
if (!buffer->eof())
|
|
|
|
throw Exception("Cannot read all marks from file " + mrk_path + ", eof: " + std::to_string(buffer->eof())
|
|
|
|
+ ", buffer size: " + std::to_string(buffer->buffer().size()) + ", file size: " + std::to_string(file_size), ErrorCodes::CANNOT_READ_ALL_DATA);
|
2020-02-03 12:46:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-27 16:47:40 +00:00
|
|
|
auto buffer = disk->readFile(mrk_path, file_size);
|
2020-02-03 12:46:25 +00:00
|
|
|
size_t i = 0;
|
2020-02-27 16:47:40 +00:00
|
|
|
while (!buffer->eof())
|
2020-02-03 12:46:25 +00:00
|
|
|
{
|
2020-02-27 16:47:40 +00:00
|
|
|
res->read(*buffer, i * columns_in_mark, columns_in_mark);
|
|
|
|
buffer->seek(sizeof(size_t), SEEK_CUR);
|
2020-02-03 12:46:25 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i * mark_size != file_size)
|
|
|
|
throw Exception("Cannot read all marks from file " + mrk_path, ErrorCodes::CANNOT_READ_ALL_DATA);
|
|
|
|
}
|
|
|
|
res->protect();
|
|
|
|
return res;
|
2019-11-20 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MergeTreeMarksLoader::loadMarks()
|
|
|
|
{
|
|
|
|
if (mark_cache)
|
|
|
|
{
|
|
|
|
auto key = mark_cache->hash(mrk_path);
|
|
|
|
if (save_marks_in_cache)
|
|
|
|
{
|
2020-04-22 06:22:14 +00:00
|
|
|
auto callback = [this]{ return loadMarksImpl(); };
|
2020-02-03 12:46:25 +00:00
|
|
|
marks = mark_cache->getOrSet(key, callback);
|
2019-11-20 13:33:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
marks = mark_cache->get(key);
|
|
|
|
if (!marks)
|
2020-02-03 12:46:25 +00:00
|
|
|
marks = loadMarksImpl();
|
2019-11-20 13:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-02-03 12:46:25 +00:00
|
|
|
marks = loadMarksImpl();
|
2019-11-20 13:33:41 +00:00
|
|
|
|
|
|
|
if (!marks)
|
2019-12-18 16:41:11 +00:00
|
|
|
throw Exception("Failed to load marks: " + mrk_path, ErrorCodes::LOGICAL_ERROR);
|
2019-11-20 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|