ClickHouse/src/Storages/MergeTree/TTLMergeSelector.h

95 lines
3.2 KiB
C++
Raw Normal View History

#pragma once
2021-10-02 07:13:14 +00:00
#include <base/types.h>
#include <Storages/MergeTree/MergeSelector.h>
2020-09-02 12:16:12 +00:00
#include <Storages/TTLDescription.h>
#include <map>
namespace DB
{
/** Merge selector, which is used to remove values with expired ttl.
* It selects parts to merge by greedy algorithm:
* 1. Finds part with the most earliest expired ttl and includes it to result.
* 2. Tries to find the longest range of parts with expired ttl, that includes part from step 1.
* Finally, merge selector updates TTL merge timer for the selected partition
*/
class ITTLMergeSelector : public IMergeSelector
{
public:
using PartitionIdToTTLs = std::map<String, time_t>;
ITTLMergeSelector(PartitionIdToTTLs & merge_due_times_, time_t current_time_, Int64 merge_cooldown_time_)
2020-09-02 12:16:12 +00:00
: current_time(current_time_)
, merge_due_times(merge_due_times_)
, merge_cooldown_time(merge_cooldown_time_)
{
}
2020-09-03 13:29:18 +00:00
PartsRange select(
const PartsRanges & parts_ranges,
const size_t max_total_size_to_merge) override;
2020-09-07 07:59:14 +00:00
/// Get TTL value for part, may depend on child type and some settings in
/// constructor.
virtual time_t getTTLForPart(const IMergeSelector::Part & part) const = 0;
2020-09-07 07:59:14 +00:00
/// Sometimes we can check that TTL already satisfied using information
/// stored in part and don't assign merge for such part.
2020-09-02 12:16:12 +00:00
virtual bool isTTLAlreadySatisfied(const IMergeSelector::Part & part) const = 0;
protected:
time_t current_time;
private:
PartitionIdToTTLs & merge_due_times;
Int64 merge_cooldown_time;
};
2020-09-07 07:59:14 +00:00
/// Select parts to merge using information about delete TTL. Depending on flag
/// only_drop_parts can use max or min TTL value.
class TTLDeleteMergeSelector : public ITTLMergeSelector
{
public:
using PartitionIdToTTLs = std::map<String, time_t>;
TTLDeleteMergeSelector(PartitionIdToTTLs & merge_due_times_, time_t current_time_, Int64 merge_cooldown_time_, bool only_drop_parts_)
: ITTLMergeSelector(merge_due_times_, current_time_, merge_cooldown_time_)
, only_drop_parts(only_drop_parts_) {}
time_t getTTLForPart(const IMergeSelector::Part & part) const override;
2020-09-07 07:59:14 +00:00
/// Delete TTL should be checked only by TTL time, there are no other ways
/// to satisfy it.
bool isTTLAlreadySatisfied(const IMergeSelector::Part &) const override;
2020-09-02 12:16:12 +00:00
private:
bool only_drop_parts;
};
2020-09-07 07:59:14 +00:00
/// Select parts to merge using information about recompression TTL and
/// compression codec of existing parts.
class TTLRecompressMergeSelector : public ITTLMergeSelector
{
public:
2020-09-02 12:16:12 +00:00
TTLRecompressMergeSelector(PartitionIdToTTLs & merge_due_times_, time_t current_time_, Int64 merge_cooldown_time_, const TTLDescriptions & recompression_ttls_)
: ITTLMergeSelector(merge_due_times_, current_time_, merge_cooldown_time_)
, recompression_ttls(recompression_ttls_)
{}
2020-09-07 07:59:14 +00:00
/// Return part min recompression TTL.
time_t getTTLForPart(const IMergeSelector::Part & part) const override;
2020-09-02 12:16:12 +00:00
2020-09-07 07:59:14 +00:00
/// Checks that part's codec is not already equal to required codec
2020-10-27 11:04:03 +00:00
/// according to recompression TTL. It doesn't make sense to assign such
2020-09-07 07:59:14 +00:00
/// merge.
2020-09-02 12:16:12 +00:00
bool isTTLAlreadySatisfied(const IMergeSelector::Part & part) const override;
private:
TTLDescriptions recompression_ttls;
};
}