2014-09-10 11:34:26 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/MemoryTracker.h>
|
|
|
|
#include <Storages/MergeTree/MergeTreeData.h>
|
2015-02-10 21:10:58 +00:00
|
|
|
#include <memory>
|
2014-09-10 11:34:26 +00:00
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
|
|
|
#include <atomic>
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2016-10-27 22:50:02 +00:00
|
|
|
/** Maintains a list of currently running merges.
|
|
|
|
* For implementation of system.merges table.
|
|
|
|
*/
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric Merge;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 11:34:26 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
struct MergeInfo
|
2016-12-23 20:23:46 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string database;
|
|
|
|
std::string table;
|
|
|
|
std::string result_part_name;
|
2019-11-20 08:51:52 +00:00
|
|
|
std::string result_part_path;
|
2017-04-01 07:20:54 +00:00
|
|
|
Array source_part_names;
|
2019-11-20 08:51:52 +00:00
|
|
|
Array source_part_paths;
|
2018-09-11 11:16:40 +00:00
|
|
|
std::string partition_id;
|
2019-01-13 22:02:33 +00:00
|
|
|
bool is_mutation;
|
2017-04-01 07:20:54 +00:00
|
|
|
Float64 elapsed;
|
|
|
|
Float64 progress;
|
|
|
|
UInt64 num_parts;
|
|
|
|
UInt64 total_size_bytes_compressed;
|
|
|
|
UInt64 total_size_marks;
|
2019-03-26 12:37:42 +00:00
|
|
|
UInt64 total_rows_count;
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 bytes_read_uncompressed;
|
|
|
|
UInt64 bytes_written_uncompressed;
|
|
|
|
UInt64 rows_read;
|
|
|
|
UInt64 rows_written;
|
|
|
|
UInt64 columns_written;
|
|
|
|
UInt64 memory_usage;
|
2020-02-02 20:01:13 +00:00
|
|
|
UInt64 thread_id;
|
2016-12-23 20:23:46 +00:00
|
|
|
};
|
|
|
|
|
2019-01-13 22:02:33 +00:00
|
|
|
struct FutureMergedMutatedPart;
|
2016-12-23 20:23:46 +00:00
|
|
|
|
|
|
|
struct MergeListElement : boost::noncopyable
|
2014-09-10 11:34:26 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const std::string database;
|
|
|
|
const std::string table;
|
2018-09-11 11:16:40 +00:00
|
|
|
std::string partition_id;
|
2019-01-14 12:25:25 +00:00
|
|
|
|
|
|
|
const std::string result_part_name;
|
2019-11-20 08:51:52 +00:00
|
|
|
const std::string result_part_path;
|
2019-01-14 12:25:25 +00:00
|
|
|
Int64 result_data_version{};
|
|
|
|
bool is_mutation{};
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 num_parts{};
|
|
|
|
Names source_part_names;
|
2019-11-20 08:51:52 +00:00
|
|
|
Names source_part_paths;
|
2019-01-14 12:25:25 +00:00
|
|
|
Int64 source_data_version{};
|
|
|
|
|
|
|
|
Stopwatch watch;
|
|
|
|
std::atomic<Float64> progress{};
|
|
|
|
std::atomic<bool> is_cancelled{};
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 total_size_bytes_compressed{};
|
|
|
|
UInt64 total_size_marks{};
|
2019-03-26 12:37:42 +00:00
|
|
|
UInt64 total_rows_count{};
|
2017-04-01 07:20:54 +00:00
|
|
|
std::atomic<UInt64> bytes_read_uncompressed{};
|
|
|
|
std::atomic<UInt64> bytes_written_uncompressed{};
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// In case of Vertical algorithm they are actual only for primary key columns
|
|
|
|
std::atomic<UInt64> rows_read{};
|
|
|
|
std::atomic<UInt64> rows_written{};
|
2016-11-22 19:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Updated only for Vertical algorithm
|
|
|
|
std::atomic<UInt64> columns_written{};
|
2016-11-03 12:00:44 +00:00
|
|
|
|
2018-06-09 15:29:08 +00:00
|
|
|
MemoryTracker memory_tracker{VariableContext::Process};
|
2018-02-01 17:55:08 +00:00
|
|
|
MemoryTracker * background_thread_memory_tracker;
|
|
|
|
MemoryTracker * background_thread_memory_tracker_prev_parent = nullptr;
|
2016-07-31 03:53:16 +00:00
|
|
|
|
2020-02-02 20:01:13 +00:00
|
|
|
UInt64 thread_id;
|
2016-12-23 20:23:46 +00:00
|
|
|
|
|
|
|
|
2019-01-13 22:02:33 +00:00
|
|
|
MergeListElement(const std::string & database, const std::string & table, const FutureMergedMutatedPart & future_part);
|
2016-12-23 20:23:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
MergeInfo getInfo() const;
|
2016-12-23 20:23:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~MergeListElement();
|
2015-04-16 06:12:35 +00:00
|
|
|
};
|
2014-09-10 11:34:26 +00:00
|
|
|
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
class MergeList;
|
|
|
|
|
|
|
|
class MergeListEntry
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
MergeList & list;
|
2015-04-16 06:12:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using container_t = std::list<MergeListElement>;
|
|
|
|
container_t::iterator it;
|
2014-09-10 11:34:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CurrentMetrics::Increment num_merges {CurrentMetrics::Merge};
|
2016-01-21 01:47:28 +00:00
|
|
|
|
2014-09-10 11:34:26 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
MergeListEntry(const MergeListEntry &) = delete;
|
|
|
|
MergeListEntry & operator=(const MergeListEntry &) = delete;
|
2015-04-16 06:12:35 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
MergeListEntry(MergeList & list_, const container_t::iterator it_) : list(list_), it{it_} {}
|
2017-04-01 07:20:54 +00:00
|
|
|
~MergeListEntry();
|
2014-09-10 11:34:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
MergeListElement * operator->() { return &*it; }
|
2018-05-25 19:44:14 +00:00
|
|
|
const MergeListElement * operator->() const { return &*it; }
|
2015-04-16 06:12:35 +00:00
|
|
|
};
|
2014-09-10 11:34:26 +00:00
|
|
|
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
class MergeList
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class MergeListEntry;
|
2014-09-10 11:34:26 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using container_t = std::list<MergeListElement>;
|
|
|
|
using info_container_t = std::list<MergeInfo>;
|
2015-04-16 06:12:35 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
mutable std::mutex mutex;
|
|
|
|
container_t merges;
|
2015-04-16 06:12:35 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Entry = MergeListEntry;
|
|
|
|
using EntryPtr = std::unique_ptr<Entry>;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
EntryPtr insert(Args &&... args)
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock{mutex};
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<Entry>(*this, merges.emplace(merges.end(), std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
info_container_t get() const
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock{mutex};
|
2017-04-01 07:20:54 +00:00
|
|
|
info_container_t res;
|
|
|
|
for (const auto & merge_element : merges)
|
|
|
|
res.emplace_back(merge_element.getInfo());
|
|
|
|
return res;
|
|
|
|
}
|
2019-01-14 12:25:25 +00:00
|
|
|
|
2019-02-04 12:53:25 +00:00
|
|
|
void cancelPartMutations(const String & partition_id, Int64 mutation_version)
|
2019-01-14 12:25:25 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
for (auto & merge_element : merges)
|
|
|
|
{
|
2019-02-04 12:53:25 +00:00
|
|
|
if ((partition_id.empty() || merge_element.partition_id == partition_id)
|
|
|
|
&& merge_element.source_data_version < mutation_version
|
2019-01-14 12:25:25 +00:00
|
|
|
&& merge_element.result_data_version >= mutation_version)
|
|
|
|
merge_element.is_cancelled = true;
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 11:34:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-16 06:12:35 +00:00
|
|
|
inline MergeListEntry::~MergeListEntry()
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock{list.mutex};
|
2017-04-01 07:20:54 +00:00
|
|
|
list.merges.erase(it);
|
2015-04-16 06:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-10 11:34:26 +00:00
|
|
|
}
|