2019-04-15 09:30:45 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-15 09:55:57 +00:00
|
|
|
#include <common/types.h>
|
2019-04-15 09:30:45 +00:00
|
|
|
#include <Storages/MergeTree/MergeSelector.h>
|
2020-09-02 12:16:12 +00:00
|
|
|
#include <Storages/TTLDescription.h>
|
2019-04-15 09:30:45 +00:00
|
|
|
|
2020-07-28 14:38:34 +00:00
|
|
|
#include <map>
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Merge selector, which is used to remove values with expired ttl.
|
2020-09-02 10:30:04 +00:00
|
|
|
* It selects parts to merge by greedy algorithm:
|
2019-04-15 09:30:45 +00:00
|
|
|
* 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.
|
2020-07-28 14:38:34 +00:00
|
|
|
* Finally, merge selector updates TTL merge timer for the selected partition
|
2019-04-15 09:30:45 +00:00
|
|
|
*/
|
2020-09-02 10:30:04 +00:00
|
|
|
class ITTLMergeSelector : public IMergeSelector
|
2019-04-15 09:30:45 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-07-28 14:38:34 +00:00
|
|
|
using PartitionIdToTTLs = std::map<String, time_t>;
|
|
|
|
|
2020-09-02 10:30:04 +00:00
|
|
|
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-02 10:30:04 +00:00
|
|
|
{
|
|
|
|
}
|
2019-04-15 09:30:45 +00:00
|
|
|
|
2020-09-03 13:29:18 +00:00
|
|
|
PartsRange select(
|
|
|
|
const PartsRanges & parts_ranges,
|
2019-04-15 09:30:45 +00:00
|
|
|
const size_t max_total_size_to_merge) override;
|
2020-07-28 14:38:34 +00:00
|
|
|
|
2020-09-07 07:59:14 +00:00
|
|
|
/// Get TTL value for part, may depend on child type and some settings in
|
|
|
|
/// constructor.
|
2020-09-02 10:30:04 +00:00
|
|
|
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;
|
2020-09-02 10:30:04 +00:00
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
private:
|
2020-07-28 14:38:34 +00:00
|
|
|
PartitionIdToTTLs & merge_due_times;
|
|
|
|
Int64 merge_cooldown_time;
|
2020-09-02 10:30:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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.
|
2020-09-02 10:30:04 +00:00
|
|
|
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.
|
2020-10-20 15:10:24 +00:00
|
|
|
bool isTTLAlreadySatisfied(const IMergeSelector::Part &) const override;
|
2020-09-02 12:16:12 +00:00
|
|
|
|
2020-09-02 10:30:04 +00:00
|
|
|
private:
|
2019-07-28 10:30:46 +00:00
|
|
|
bool only_drop_parts;
|
2019-04-15 09:30:45 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 07:59:14 +00:00
|
|
|
/// Select parts to merge using information about recompression TTL and
|
|
|
|
/// compression codec of existing parts.
|
2020-09-02 10:30:04 +00:00
|
|
|
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-02 10:30:04 +00:00
|
|
|
|
2020-09-07 07:59:14 +00:00
|
|
|
/// Return part min recompression TTL.
|
2020-09-02 10:30:04 +00:00
|
|
|
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;
|
2020-09-02 10:30:04 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
}
|