2019-04-15 09:30:45 +00:00
|
|
|
#include <Storages/MergeTree/TTLMergeSelector.h>
|
2020-07-28 14:38:34 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2020-07-28 14:38:34 +00:00
|
|
|
#include <cmath>
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-07-28 14:38:34 +00:00
|
|
|
const String & getPartitionIdForPart(const TTLMergeSelector::Part & part_info)
|
|
|
|
{
|
|
|
|
const MergeTreeData::DataPartPtr & part = *static_cast<const MergeTreeData::DataPartPtr *>(part_info.data);
|
|
|
|
return part->info.partition_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
IMergeSelector::PartsInPartition TTLMergeSelector::select(
|
|
|
|
const Partitions & partitions,
|
|
|
|
const size_t max_total_size_to_merge)
|
|
|
|
{
|
|
|
|
using Iterator = IMergeSelector::PartsInPartition::const_iterator;
|
|
|
|
Iterator best_begin;
|
|
|
|
ssize_t partition_to_merge_index = -1;
|
|
|
|
time_t partition_to_merge_min_ttl = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < partitions.size(); ++i)
|
|
|
|
{
|
2020-07-28 14:38:34 +00:00
|
|
|
const auto & mergeable_parts_in_partition = partitions[i];
|
|
|
|
if (mergeable_parts_in_partition.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto & partition_id = getPartitionIdForPart(mergeable_parts_in_partition.front());
|
|
|
|
const auto & next_merge_time_for_partition = merge_due_times[partition_id];
|
|
|
|
if (next_merge_time_for_partition > current_time)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (Iterator part_it = mergeable_parts_in_partition.cbegin(); part_it != mergeable_parts_in_partition.cend(); ++part_it)
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
2020-07-28 14:38:34 +00:00
|
|
|
time_t ttl = only_drop_parts ? part_it->max_ttl : part_it->min_ttl;
|
2019-07-28 10:30:46 +00:00
|
|
|
|
|
|
|
if (ttl && (partition_to_merge_index == -1 || ttl < partition_to_merge_min_ttl))
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
2019-07-28 10:30:46 +00:00
|
|
|
partition_to_merge_min_ttl = ttl;
|
2019-04-15 09:30:45 +00:00
|
|
|
partition_to_merge_index = i;
|
2020-07-28 14:38:34 +00:00
|
|
|
best_begin = part_it;
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition_to_merge_index == -1 || partition_to_merge_min_ttl > current_time)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto & best_partition = partitions[partition_to_merge_index];
|
|
|
|
Iterator best_end = best_begin + 1;
|
|
|
|
size_t total_size = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2019-07-28 10:30:46 +00:00
|
|
|
time_t ttl = only_drop_parts ? best_begin->max_ttl : best_begin->min_ttl;
|
|
|
|
|
|
|
|
if (!ttl || ttl > current_time
|
2019-04-15 09:30:45 +00:00
|
|
|
|| (max_total_size_to_merge && total_size > max_total_size_to_merge))
|
|
|
|
{
|
|
|
|
++best_begin;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_size += best_begin->size;
|
|
|
|
if (best_begin == best_partition.begin())
|
|
|
|
break;
|
|
|
|
|
|
|
|
--best_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (best_end != best_partition.end())
|
|
|
|
{
|
2019-07-28 10:30:46 +00:00
|
|
|
time_t ttl = only_drop_parts ? best_end->max_ttl : best_end->min_ttl;
|
|
|
|
|
|
|
|
if (!ttl || ttl > current_time
|
2019-04-15 09:30:45 +00:00
|
|
|
|| (max_total_size_to_merge && total_size > max_total_size_to_merge))
|
|
|
|
break;
|
|
|
|
|
|
|
|
total_size += best_end->size;
|
|
|
|
++best_end;
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:38:34 +00:00
|
|
|
const auto & best_partition_id = getPartitionIdForPart(best_partition.front());
|
|
|
|
merge_due_times[best_partition_id] = current_time + merge_cooldown_time;
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
return PartsInPartition(best_begin, best_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|