2017-06-19 20:06:35 +00:00
|
|
|
#include <Common/ZooKeeper/Types.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/MergeTree/ReplicatedMergeTreeLogEntry.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
2017-07-31 21:39:24 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2016-06-08 13:08:20 +00:00
|
|
|
void ReplicatedMergeTreeLogEntryData::writeText(WriteBuffer & out) const
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2017-04-14 13:28:18 +00:00
|
|
|
out << "format version: 4\n"
|
2017-04-01 07:20:54 +00:00
|
|
|
<< "create_time: " << LocalDateTime(create_time ? create_time : time(0)) << "\n"
|
|
|
|
<< "source replica: " << source_replica << '\n'
|
|
|
|
<< "block_id: " << escape << block_id << '\n';
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GET_PART:
|
|
|
|
out << "get\n" << new_part_name;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MERGE_PARTS:
|
|
|
|
out << "merge\n";
|
|
|
|
for (const String & s : parts_to_merge)
|
|
|
|
out << s << '\n';
|
|
|
|
out << "into\n" << new_part_name;
|
2017-04-17 15:14:56 +00:00
|
|
|
out << "\ndeduplicate: " << deduplicate;
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DROP_RANGE:
|
|
|
|
if (detach)
|
|
|
|
out << "detach\n";
|
|
|
|
else
|
|
|
|
out << "drop\n";
|
|
|
|
out << new_part_name;
|
|
|
|
break;
|
|
|
|
|
2017-06-22 11:01:30 +00:00
|
|
|
case CLEAR_COLUMN:
|
|
|
|
out << "clear_column\n"
|
2017-06-16 16:47:09 +00:00
|
|
|
<< escape << column_name
|
|
|
|
<< "\nfrom\n"
|
|
|
|
<< new_part_name;
|
|
|
|
break;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
default:
|
|
|
|
throw Exception("Unknown log entry type: " + DB::toString<int>(type), ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
out << '\n';
|
|
|
|
|
|
|
|
if (quorum)
|
|
|
|
out << "quorum: " << quorum << '\n';
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 13:08:20 +00:00
|
|
|
void ReplicatedMergeTreeLogEntryData::readText(ReadBuffer & in)
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt8 format_version = 0;
|
|
|
|
String type_str;
|
|
|
|
|
|
|
|
in >> "format version: " >> format_version >> "\n";
|
|
|
|
|
2017-04-14 13:28:18 +00:00
|
|
|
if (format_version < 1 || format_version > 4)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Unknown ReplicatedMergeTreeLogEntry format version: " + DB::toString(format_version), ErrorCodes::UNKNOWN_FORMAT_VERSION);
|
|
|
|
|
|
|
|
if (format_version >= 2)
|
|
|
|
{
|
|
|
|
LocalDateTime create_time_dt;
|
|
|
|
in >> "create_time: " >> create_time_dt >> "\n";
|
|
|
|
create_time = create_time_dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
in >> "source replica: " >> source_replica >> "\n";
|
|
|
|
|
|
|
|
if (format_version >= 3)
|
|
|
|
{
|
|
|
|
in >> "block_id: " >> escape >> block_id >> "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
in >> type_str >> "\n";
|
|
|
|
|
|
|
|
if (type_str == "get")
|
|
|
|
{
|
|
|
|
type = GET_PART;
|
|
|
|
in >> new_part_name;
|
|
|
|
}
|
|
|
|
else if (type_str == "merge")
|
|
|
|
{
|
|
|
|
type = MERGE_PARTS;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
String s;
|
|
|
|
in >> s >> "\n";
|
|
|
|
if (s == "into")
|
|
|
|
break;
|
|
|
|
parts_to_merge.push_back(s);
|
|
|
|
}
|
|
|
|
in >> new_part_name;
|
2017-04-17 15:14:56 +00:00
|
|
|
if (format_version >= 4)
|
|
|
|
in >> "\ndeduplicate: " >> deduplicate;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (type_str == "drop" || type_str == "detach")
|
|
|
|
{
|
|
|
|
type = DROP_RANGE;
|
|
|
|
detach = type_str == "detach";
|
|
|
|
in >> new_part_name;
|
|
|
|
}
|
2017-06-22 11:01:30 +00:00
|
|
|
else if (type_str == "clear_column")
|
2017-06-16 16:47:09 +00:00
|
|
|
{
|
2017-06-22 11:01:30 +00:00
|
|
|
type = CLEAR_COLUMN;
|
2017-06-16 16:47:09 +00:00
|
|
|
in >> escape >> column_name >> "\nfrom\n" >> new_part_name;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
else if (type_str == "attach")
|
|
|
|
{
|
2017-06-25 00:51:51 +00:00
|
|
|
/// Obsolete. TODO: Remove after half year.
|
2017-04-01 07:20:54 +00:00
|
|
|
type = ATTACH_PART;
|
|
|
|
String source_type;
|
|
|
|
in >> source_type;
|
2017-05-24 21:38:56 +00:00
|
|
|
if (source_type != "detached")
|
|
|
|
throw Exception("Bad format: expected 'detached', found '" + source_type + "'", ErrorCodes::CANNOT_PARSE_TEXT);
|
2017-06-25 00:51:51 +00:00
|
|
|
String source_part_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
in >> "\n" >> source_part_name >> "\ninto\n" >> new_part_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
in >> "\n";
|
|
|
|
|
|
|
|
/// Optional field.
|
|
|
|
if (!in.eof())
|
|
|
|
in >> "quorum: " >> quorum >> "\n";
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 13:08:20 +00:00
|
|
|
String ReplicatedMergeTreeLogEntryData::toString() const
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2017-07-31 21:39:24 +00:00
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
writeText(out);
|
|
|
|
return out.str();
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReplicatedMergeTreeLogEntry::Ptr ReplicatedMergeTreeLogEntry::parse(const String & s, const zkutil::Stat & stat)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ReadBufferFromString in(s);
|
|
|
|
Ptr res = std::make_shared<ReplicatedMergeTreeLogEntry>();
|
|
|
|
res->readText(in);
|
|
|
|
assertEOF(in);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res->create_time)
|
|
|
|
res->create_time = stat.ctime / 1000;
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|