2018-07-11 12:43:55 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeMutationEntry.h>
|
|
|
|
#include <IO/Operators.h>
|
2021-11-19 16:51:03 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2018-07-11 12:43:55 +00:00
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
2021-12-14 20:06:34 +00:00
|
|
|
#include <Interpreters/TransactionLog.h>
|
2022-07-05 07:39:52 +00:00
|
|
|
#include <Backups/BackupEntryFromMemory.h>
|
2018-07-11 12:43:55 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2018-07-11 12:43:55 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-11-19 16:51:03 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
String MergeTreeMutationEntry::versionToFileName(UInt64 block_number_)
|
|
|
|
{
|
|
|
|
assert(block_number_);
|
|
|
|
return fmt::format("mutation_{}.txt", block_number_);
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt64 MergeTreeMutationEntry::tryParseFileName(const String & file_name_)
|
|
|
|
{
|
|
|
|
UInt64 maybe_block_number = 0;
|
|
|
|
ReadBufferFromString file_name_buf(file_name_);
|
|
|
|
if (!checkString("mutation_", file_name_buf))
|
|
|
|
return 0;
|
|
|
|
if (!tryReadIntText(maybe_block_number, file_name_buf))
|
|
|
|
return 0;
|
|
|
|
if (!checkString(".txt", file_name_buf))
|
|
|
|
return 0;
|
|
|
|
assert(maybe_block_number);
|
|
|
|
return maybe_block_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt64 MergeTreeMutationEntry::parseFileName(const String & file_name_)
|
|
|
|
{
|
|
|
|
if (UInt64 maybe_block_number = tryParseFileName(file_name_))
|
|
|
|
return maybe_block_number;
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Cannot parse mutation version from file name, expected 'mutation_<UInt64>.txt', got '{}'", file_name_);
|
|
|
|
}
|
|
|
|
|
2021-12-14 20:06:34 +00:00
|
|
|
MergeTreeMutationEntry::MergeTreeMutationEntry(MutationCommands commands_, DiskPtr disk_, const String & path_prefix_, UInt64 tmp_number,
|
2022-04-08 11:34:40 +00:00
|
|
|
const TransactionID & tid_, const WriteSettings & settings)
|
2018-07-11 12:43:55 +00:00
|
|
|
: create_time(time(nullptr))
|
|
|
|
, commands(std::move(commands_))
|
2020-02-27 16:47:40 +00:00
|
|
|
, disk(std::move(disk_))
|
2018-07-11 12:43:55 +00:00
|
|
|
, path_prefix(path_prefix_)
|
|
|
|
, file_name("tmp_mutation_" + toString(tmp_number) + ".txt")
|
|
|
|
, is_temp(true)
|
2021-12-14 20:06:34 +00:00
|
|
|
, tid(tid_)
|
2018-07-11 12:43:55 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-03-23 18:00:42 +00:00
|
|
|
auto out = disk->writeFile(std::filesystem::path(path_prefix) / file_name, DBMS_DEFAULT_BUFFER_SIZE, WriteMode::Rewrite, settings);
|
2020-02-27 16:47:40 +00:00
|
|
|
*out << "format version: 1\n"
|
2018-07-11 12:43:55 +00:00
|
|
|
<< "create time: " << LocalDateTime(create_time) << "\n";
|
2022-03-16 19:16:26 +00:00
|
|
|
*out << "commands: ";
|
2022-12-22 13:31:42 +00:00
|
|
|
commands.writeText(*out, /* with_pure_metadata_commands = */ false);
|
2022-03-16 19:16:26 +00:00
|
|
|
*out << "\n";
|
2022-03-17 17:26:18 +00:00
|
|
|
if (tid.isPrehistoric())
|
|
|
|
{
|
|
|
|
csn = Tx::PrehistoricCSN;
|
|
|
|
}
|
|
|
|
else
|
2022-01-28 17:47:37 +00:00
|
|
|
{
|
|
|
|
*out << "tid: ";
|
|
|
|
TransactionID::write(tid, *out);
|
|
|
|
*out << "\n";
|
|
|
|
}
|
2020-02-27 16:47:40 +00:00
|
|
|
out->sync();
|
2018-07-11 12:43:55 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
removeFile();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 16:51:03 +00:00
|
|
|
void MergeTreeMutationEntry::commit(UInt64 block_number_)
|
2018-07-11 12:43:55 +00:00
|
|
|
{
|
2021-11-19 16:51:03 +00:00
|
|
|
assert(block_number_);
|
2018-07-11 12:43:55 +00:00
|
|
|
block_number = block_number_;
|
2021-11-19 16:51:03 +00:00
|
|
|
String new_file_name = versionToFileName(block_number);
|
2020-02-27 16:47:40 +00:00
|
|
|
disk->moveFile(path_prefix + file_name, path_prefix + new_file_name);
|
2018-07-11 12:43:55 +00:00
|
|
|
is_temp = false;
|
|
|
|
file_name = new_file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergeTreeMutationEntry::removeFile()
|
|
|
|
{
|
|
|
|
if (!file_name.empty())
|
|
|
|
{
|
2020-02-27 16:47:40 +00:00
|
|
|
if (!disk->exists(path_prefix + file_name))
|
2018-07-11 12:43:55 +00:00
|
|
|
return;
|
|
|
|
|
2021-01-14 16:24:13 +00:00
|
|
|
disk->removeFile(path_prefix + file_name);
|
2018-07-11 12:43:55 +00:00
|
|
|
file_name.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:26:18 +00:00
|
|
|
void MergeTreeMutationEntry::writeCSN(CSN csn_)
|
|
|
|
{
|
|
|
|
csn = csn_;
|
|
|
|
auto out = disk->writeFile(path_prefix + file_name, 256, WriteMode::Append);
|
|
|
|
*out << "csn: " << csn << "\n";
|
|
|
|
out->finalize();
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
MergeTreeMutationEntry::MergeTreeMutationEntry(DiskPtr disk_, const String & path_prefix_, const String & file_name_)
|
|
|
|
: disk(std::move(disk_))
|
|
|
|
, path_prefix(path_prefix_)
|
2018-07-11 12:43:55 +00:00
|
|
|
, file_name(file_name_)
|
|
|
|
, is_temp(false)
|
|
|
|
{
|
2021-11-19 16:51:03 +00:00
|
|
|
block_number = parseFileName(file_name);
|
2020-02-27 16:47:40 +00:00
|
|
|
auto buf = disk->readFile(path_prefix + file_name);
|
2018-07-11 12:43:55 +00:00
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
*buf >> "format version: 1\n";
|
2018-07-11 12:43:55 +00:00
|
|
|
|
|
|
|
LocalDateTime create_time_dt;
|
2020-02-27 16:47:40 +00:00
|
|
|
*buf >> "create time: " >> create_time_dt >> "\n";
|
2021-03-15 19:23:27 +00:00
|
|
|
create_time = DateLUT::instance().makeDateTime(
|
|
|
|
create_time_dt.year(), create_time_dt.month(), create_time_dt.day(),
|
|
|
|
create_time_dt.hour(), create_time_dt.minute(), create_time_dt.second());
|
2018-07-11 12:43:55 +00:00
|
|
|
|
2022-03-16 19:16:26 +00:00
|
|
|
*buf >> "commands: ";
|
|
|
|
commands.readText(*buf);
|
|
|
|
*buf >> "\n";
|
|
|
|
|
2022-03-17 17:26:18 +00:00
|
|
|
if (buf->eof())
|
|
|
|
{
|
|
|
|
tid = Tx::PrehistoricTID;
|
|
|
|
csn = Tx::PrehistoricCSN;
|
|
|
|
}
|
|
|
|
else
|
2022-01-28 17:47:37 +00:00
|
|
|
{
|
|
|
|
*buf >> "tid: ";
|
|
|
|
tid = TransactionID::read(*buf);
|
|
|
|
*buf >> "\n";
|
2022-03-17 17:26:18 +00:00
|
|
|
|
|
|
|
if (!buf->eof())
|
|
|
|
{
|
|
|
|
*buf >> "csn: " >> csn >> "\n";
|
|
|
|
}
|
2022-01-28 17:47:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:47:40 +00:00
|
|
|
assertEOF(*buf);
|
2018-07-11 12:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MergeTreeMutationEntry::~MergeTreeMutationEntry()
|
|
|
|
{
|
|
|
|
if (is_temp && startsWith(file_name, "tmp_"))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
removeFile();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 07:39:52 +00:00
|
|
|
std::shared_ptr<const IBackupEntry> MergeTreeMutationEntry::backup() const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
out << "block number: " << block_number << "\n";
|
2022-07-06 10:03:10 +00:00
|
|
|
|
2022-07-05 07:39:52 +00:00
|
|
|
out << "commands: ";
|
2022-12-22 13:31:42 +00:00
|
|
|
commands.writeText(out, /* with_pure_metadata_commands = */ false);
|
2022-07-05 07:39:52 +00:00
|
|
|
out << "\n";
|
|
|
|
|
|
|
|
return std::make_shared<BackupEntryFromMemory>(out.str());
|
|
|
|
}
|
|
|
|
|
2018-07-11 12:43:55 +00:00
|
|
|
}
|