2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2021-01-13 19:03:07 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2018-01-23 22:56:46 +00:00
|
|
|
#include <DataTypes/DataTypeEnum.h>
|
2022-11-20 12:23:31 +00:00
|
|
|
#include <DataTypes/DataTypeUUID.h>
|
2019-10-10 16:30:30 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPart.h>
|
2017-06-25 00:01:10 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/PartLog.h>
|
2020-05-20 20:16:32 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-11-19 15:19:20 +00:00
|
|
|
#include <Interpreters/ProfileEventsExt.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <DataTypes/DataTypeMap.h>
|
2017-03-07 17:13:54 +00:00
|
|
|
|
2020-12-30 16:37:08 +00:00
|
|
|
#include <Common/CurrentThread.h>
|
2017-03-07 17:13:54 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2022-05-04 17:16:42 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2022-05-04 17:16:42 +00:00
|
|
|
PartLogElement::MergeReasonType PartLogElement::getMergeReasonType(MergeType merge_type)
|
|
|
|
{
|
2022-05-04 17:16:42 +00:00
|
|
|
switch (merge_type)
|
|
|
|
{
|
2022-07-13 14:03:39 +00:00
|
|
|
case MergeType::Regular:
|
|
|
|
return REGULAR_MERGE;
|
|
|
|
case MergeType::TTLDelete:
|
|
|
|
return TTL_DELETE_MERGE;
|
|
|
|
case MergeType::TTLRecompress:
|
|
|
|
return TTL_RECOMPRESS_MERGE;
|
2022-05-04 17:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unknown MergeType {}", static_cast<UInt64>(merge_type));
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:03:39 +00:00
|
|
|
PartLogElement::PartMergeAlgorithm PartLogElement::getMergeAlgorithm(MergeAlgorithm merge_algorithm_)
|
|
|
|
{
|
|
|
|
switch (merge_algorithm_)
|
|
|
|
{
|
|
|
|
case MergeAlgorithm::Undecided:
|
|
|
|
return UNDECIDED;
|
|
|
|
case MergeAlgorithm::Horizontal:
|
|
|
|
return HORIZONTAL;
|
|
|
|
case MergeAlgorithm::Vertical:
|
|
|
|
return VERTICAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Unknown MergeAlgorithm {}", static_cast<UInt64>(merge_algorithm_));
|
|
|
|
}
|
|
|
|
|
2021-06-28 11:42:21 +00:00
|
|
|
NamesAndTypesList PartLogElement::getNamesAndTypes()
|
2017-03-07 17:13:54 +00:00
|
|
|
{
|
2018-01-23 22:56:46 +00:00
|
|
|
auto event_type_datatype = std::make_shared<DataTypeEnum8>(
|
2018-09-09 02:23:24 +00:00
|
|
|
DataTypeEnum8::Values
|
|
|
|
{
|
2019-08-01 16:37:50 +00:00
|
|
|
{"NewPart", static_cast<Int8>(NEW_PART)},
|
|
|
|
{"MergeParts", static_cast<Int8>(MERGE_PARTS)},
|
|
|
|
{"DownloadPart", static_cast<Int8>(DOWNLOAD_PART)},
|
|
|
|
{"RemovePart", static_cast<Int8>(REMOVE_PART)},
|
|
|
|
{"MutatePart", static_cast<Int8>(MUTATE_PART)},
|
2019-09-03 11:32:25 +00:00
|
|
|
{"MovePart", static_cast<Int8>(MOVE_PART)},
|
|
|
|
}
|
|
|
|
);
|
2021-02-03 08:29:34 +00:00
|
|
|
|
2022-05-04 17:16:42 +00:00
|
|
|
auto merge_reason_datatype = std::make_shared<DataTypeEnum8>(
|
|
|
|
DataTypeEnum8::Values
|
|
|
|
{
|
|
|
|
{"NotAMerge", static_cast<Int8>(NOT_A_MERGE)},
|
|
|
|
{"RegularMerge", static_cast<Int8>(REGULAR_MERGE)},
|
|
|
|
{"TTLDeleteMerge", static_cast<Int8>(TTL_DELETE_MERGE)},
|
|
|
|
{"TTLRecompressMerge", static_cast<Int8>(TTL_RECOMPRESS_MERGE)},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-07-13 14:03:39 +00:00
|
|
|
auto merge_algorithm_datatype = std::make_shared<DataTypeEnum8>(
|
|
|
|
DataTypeEnum8::Values
|
|
|
|
{
|
|
|
|
{"Undecided", static_cast<Int8>(UNDECIDED)},
|
|
|
|
{"Horizontal", static_cast<Int8>(HORIZONTAL)},
|
|
|
|
{"Vertical", static_cast<Int8>(VERTICAL)},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-02-03 08:14:46 +00:00
|
|
|
ColumnsWithTypeAndName columns_with_type_and_name;
|
2018-01-23 22:56:46 +00:00
|
|
|
|
2021-02-03 08:29:34 +00:00
|
|
|
return {
|
2021-06-28 11:42:21 +00:00
|
|
|
{"query_id", std::make_shared<DataTypeString>()},
|
|
|
|
{"event_type", std::move(event_type_datatype)},
|
2022-05-04 17:16:42 +00:00
|
|
|
{"merge_reason", std::move(merge_reason_datatype)},
|
2022-07-13 14:03:39 +00:00
|
|
|
{"merge_algorithm", std::move(merge_algorithm_datatype)},
|
2021-06-28 11:42:21 +00:00
|
|
|
{"event_date", std::make_shared<DataTypeDate>()},
|
2021-02-03 08:14:46 +00:00
|
|
|
|
2021-06-28 11:42:21 +00:00
|
|
|
{"event_time", std::make_shared<DataTypeDateTime>()},
|
|
|
|
{"event_time_microseconds", std::make_shared<DataTypeDateTime64>(6)},
|
2021-02-03 08:14:46 +00:00
|
|
|
|
2021-06-28 11:42:21 +00:00
|
|
|
{"duration_ms", std::make_shared<DataTypeUInt64>()},
|
2021-02-03 08:14:46 +00:00
|
|
|
|
2021-06-28 11:42:21 +00:00
|
|
|
{"database", std::make_shared<DataTypeString>()},
|
|
|
|
{"table", std::make_shared<DataTypeString>()},
|
2022-11-20 12:23:31 +00:00
|
|
|
{"table_uuid", std::make_shared<DataTypeUUID>()},
|
2021-06-28 11:42:21 +00:00
|
|
|
{"part_name", std::make_shared<DataTypeString>()},
|
|
|
|
{"partition_id", std::make_shared<DataTypeString>()},
|
2022-05-25 14:54:49 +00:00
|
|
|
{"part_type", std::make_shared<DataTypeString>()},
|
2022-03-10 15:44:19 +00:00
|
|
|
{"disk_name", std::make_shared<DataTypeString>()},
|
2021-06-28 11:42:21 +00:00
|
|
|
{"path_on_disk", std::make_shared<DataTypeString>()},
|
2018-01-23 22:56:46 +00:00
|
|
|
|
2021-06-28 11:42:21 +00:00
|
|
|
{"rows", std::make_shared<DataTypeUInt64>()},
|
|
|
|
{"size_in_bytes", std::make_shared<DataTypeUInt64>()}, // On disk
|
2018-01-23 22:56:46 +00:00
|
|
|
|
|
|
|
/// Merge-specific info
|
2021-06-28 11:42:21 +00:00
|
|
|
{"merged_from", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())},
|
|
|
|
{"bytes_uncompressed", std::make_shared<DataTypeUInt64>()}, // Result bytes
|
|
|
|
{"read_rows", std::make_shared<DataTypeUInt64>()},
|
|
|
|
{"read_bytes", std::make_shared<DataTypeUInt64>()},
|
|
|
|
{"peak_memory_usage", std::make_shared<DataTypeUInt64>()},
|
2018-01-23 22:56:46 +00:00
|
|
|
|
|
|
|
/// Is there an error during the execution or commit
|
2021-06-28 11:42:21 +00:00
|
|
|
{"error", std::make_shared<DataTypeUInt16>()},
|
|
|
|
{"exception", std::make_shared<DataTypeString>()},
|
2021-11-19 15:19:20 +00:00
|
|
|
|
|
|
|
{"ProfileEvents", std::make_shared<DataTypeMap>(std::make_shared<DataTypeString>(), std::make_shared<DataTypeUInt64>())},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
NamesAndAliases PartLogElement::getNamesAndAliases()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
{"ProfileEvents.Names", {std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())}, "mapKeys(ProfileEvents)"},
|
|
|
|
{"ProfileEvents.Values", {std::make_shared<DataTypeArray>(std::make_shared<DataTypeUInt64>())}, "mapValues(ProfileEvents)"},
|
2023-04-17 12:45:04 +00:00
|
|
|
{"name", {std::make_shared<DataTypeString>()}, "part_name"},
|
2018-05-07 02:01:11 +00:00
|
|
|
};
|
2017-03-07 17:13:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 20:15:18 +00:00
|
|
|
void PartLogElement::appendToBlock(MutableColumns & columns) const
|
2017-03-07 17:13:54 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
2020-12-30 16:37:08 +00:00
|
|
|
columns[i++]->insert(query_id);
|
2018-10-22 08:54:54 +00:00
|
|
|
columns[i++]->insert(event_type);
|
2022-05-04 17:16:42 +00:00
|
|
|
columns[i++]->insert(merge_reason);
|
2022-07-13 14:03:39 +00:00
|
|
|
columns[i++]->insert(merge_algorithm);
|
2020-04-17 13:26:44 +00:00
|
|
|
columns[i++]->insert(DateLUT::instance().toDayNum(event_time).toUnderType());
|
2018-10-22 08:54:54 +00:00
|
|
|
columns[i++]->insert(event_time);
|
2021-02-03 08:14:46 +00:00
|
|
|
columns[i++]->insert(event_time_microseconds);
|
2018-10-22 08:54:54 +00:00
|
|
|
columns[i++]->insert(duration_ms);
|
2017-03-07 17:13:54 +00:00
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
columns[i++]->insert(database_name);
|
|
|
|
columns[i++]->insert(table_name);
|
2022-11-20 12:23:31 +00:00
|
|
|
columns[i++]->insert(table_uuid);
|
2017-12-15 18:23:05 +00:00
|
|
|
columns[i++]->insert(part_name);
|
2018-09-13 12:48:46 +00:00
|
|
|
columns[i++]->insert(partition_id);
|
2022-05-25 14:54:49 +00:00
|
|
|
columns[i++]->insert(part_type.toString());
|
2022-03-10 15:44:19 +00:00
|
|
|
columns[i++]->insert(disk_name);
|
2019-09-03 11:32:25 +00:00
|
|
|
columns[i++]->insert(path_on_disk);
|
2017-03-07 17:13:54 +00:00
|
|
|
|
2018-10-22 08:54:54 +00:00
|
|
|
columns[i++]->insert(rows);
|
|
|
|
columns[i++]->insert(bytes_compressed_on_disk);
|
2018-01-23 22:56:46 +00:00
|
|
|
|
|
|
|
Array source_part_names_array;
|
|
|
|
source_part_names_array.reserve(source_part_names.size());
|
|
|
|
for (const auto & name : source_part_names)
|
|
|
|
source_part_names_array.push_back(name);
|
|
|
|
|
|
|
|
columns[i++]->insert(source_part_names_array);
|
|
|
|
|
2018-10-22 08:54:54 +00:00
|
|
|
columns[i++]->insert(bytes_uncompressed);
|
|
|
|
columns[i++]->insert(rows_read);
|
|
|
|
columns[i++]->insert(bytes_read_uncompressed);
|
2020-03-19 11:31:21 +00:00
|
|
|
columns[i++]->insert(peak_memory_usage);
|
2017-12-15 18:23:05 +00:00
|
|
|
|
2018-10-22 08:54:54 +00:00
|
|
|
columns[i++]->insert(error);
|
2018-01-23 22:56:46 +00:00
|
|
|
columns[i++]->insert(exception);
|
2017-03-07 17:13:54 +00:00
|
|
|
|
2021-11-19 15:19:20 +00:00
|
|
|
if (profile_counters)
|
|
|
|
{
|
|
|
|
auto * column = columns[i++].get();
|
|
|
|
ProfileEvents::dumpToMapColumn(*profile_counters, column, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
columns[i++]->insertDefault();
|
|
|
|
}
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 19:26:06 +00:00
|
|
|
bool PartLog::addNewParts(
|
2023-01-23 12:45:28 +00:00
|
|
|
ContextPtr current_context, const PartLog::PartLogEntries & parts, const ExecutionStatus & execution_status)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
if (parts.empty())
|
|
|
|
return true;
|
|
|
|
|
2019-03-21 19:22:38 +00:00
|
|
|
std::shared_ptr<PartLog> part_log;
|
2018-01-23 22:56:46 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-01-23 12:45:28 +00:00
|
|
|
auto table_id = parts.front().part->storage.getStorageID();
|
2021-04-10 23:33:54 +00:00
|
|
|
part_log = current_context->getPartLog(table_id.database_name); // assume parts belong to the same table
|
2018-01-23 22:56:46 +00:00
|
|
|
if (!part_log)
|
|
|
|
return false;
|
|
|
|
|
2020-12-30 16:37:08 +00:00
|
|
|
auto query_id = CurrentThread::getQueryId();
|
|
|
|
|
2023-01-23 12:45:28 +00:00
|
|
|
for (const auto & part_log_entry : parts)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2023-01-23 12:45:28 +00:00
|
|
|
const auto & part = part_log_entry.part;
|
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
PartLogElement elem;
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2022-07-17 15:22:12 +00:00
|
|
|
if (!query_id.empty())
|
|
|
|
elem.query_id.insert(0, query_id.data(), query_id.size());
|
2020-12-30 16:37:08 +00:00
|
|
|
|
2023-02-19 22:15:09 +00:00
|
|
|
elem.event_type = PartLogElement::NEW_PART;
|
2021-01-13 19:26:06 +00:00
|
|
|
|
|
|
|
// construct event_time and event_time_microseconds using the same time point
|
|
|
|
// so that the two times will always be equal up to a precision of a second.
|
|
|
|
const auto time_now = std::chrono::system_clock::now();
|
2022-10-03 18:52:14 +00:00
|
|
|
elem.event_time = timeInSeconds(time_now);
|
|
|
|
elem.event_time_microseconds = timeInMicroseconds(time_now);
|
2023-01-23 12:45:28 +00:00
|
|
|
elem.duration_ms = part_log_entry.elapsed_ns / 1000000;
|
2017-06-25 00:01:10 +00:00
|
|
|
|
2019-12-10 20:47:05 +00:00
|
|
|
elem.database_name = table_id.database_name;
|
|
|
|
elem.table_name = table_id.table_name;
|
2022-11-20 12:23:31 +00:00
|
|
|
elem.table_uuid = table_id.uuid;
|
2018-09-13 12:48:46 +00:00
|
|
|
elem.partition_id = part->info.partition_id;
|
2019-01-31 17:30:56 +00:00
|
|
|
elem.part_name = part->name;
|
2022-10-23 03:29:26 +00:00
|
|
|
elem.disk_name = part->getDataPartStorage().getDiskName();
|
|
|
|
elem.path_on_disk = part->getDataPartStorage().getFullPath();
|
2022-05-25 14:54:49 +00:00
|
|
|
elem.part_type = part->getType();
|
2018-01-23 22:56:46 +00:00
|
|
|
|
2020-03-23 13:32:02 +00:00
|
|
|
elem.bytes_compressed_on_disk = part->getBytesOnDisk();
|
2018-05-21 13:49:54 +00:00
|
|
|
elem.rows = part->rows_count;
|
2018-01-23 22:56:46 +00:00
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
elem.error = static_cast<UInt16>(execution_status.code);
|
|
|
|
elem.exception = execution_status.message;
|
2018-01-23 22:56:46 +00:00
|
|
|
|
2023-01-23 12:45:28 +00:00
|
|
|
elem.profile_counters = part_log_entry.profile_counters;
|
2021-11-19 15:19:20 +00:00
|
|
|
|
2023-07-28 07:23:34 +00:00
|
|
|
part_log->add(std::move(elem));
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
2018-01-23 22:56:46 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-05-30 21:57:37 +00:00
|
|
|
tryLogCurrentException(part_log ? part_log->log : &Poco::Logger::get("PartLog"), __PRETTY_FUNCTION__);
|
2018-01-23 22:56:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-07 17:13:54 +00:00
|
|
|
|
2018-01-23 22:56:46 +00:00
|
|
|
return true;
|
2017-06-25 00:01:10 +00:00
|
|
|
}
|
2017-03-07 17:13:54 +00:00
|
|
|
|
2023-01-23 12:45:28 +00:00
|
|
|
bool PartLog::addNewPart(ContextPtr context, const PartLog::PartLogEntry & part, const ExecutionStatus & execution_status)
|
|
|
|
{
|
|
|
|
return addNewParts(context, {part}, execution_status);
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:01:12 +00:00
|
|
|
|
|
|
|
PartLog::PartLogEntries PartLog::createPartLogEntries(const MutableDataPartsVector & parts, UInt64 elapsed_ns, ProfileCountersSnapshotPtr profile_counters)
|
2023-01-23 12:45:28 +00:00
|
|
|
{
|
2023-01-31 14:01:12 +00:00
|
|
|
PartLogEntries part_log_entries;
|
2023-01-23 12:45:28 +00:00
|
|
|
part_log_entries.reserve(parts.size());
|
|
|
|
|
|
|
|
for (const auto & part : parts)
|
2023-01-31 14:01:12 +00:00
|
|
|
part_log_entries.emplace_back(part, elapsed_ns, profile_counters);
|
2023-01-23 12:45:28 +00:00
|
|
|
|
2023-01-31 14:01:12 +00:00
|
|
|
return part_log_entries;
|
2023-01-23 12:45:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:13:54 +00:00
|
|
|
}
|