ClickHouse/src/Storages/MergeTree/ReplicatedMergeTreeSink.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.6 KiB
C++
Raw Normal View History

2014-04-02 07:59:43 +00:00
#pragma once
2021-07-23 19:33:59 +00:00
#include <Processors/Sinks/SinkToStorage.h>
#include <Storages/MergeTree/MergeTreeData.h>
2021-10-02 07:13:14 +00:00
#include <base/types.h>
2014-04-02 07:59:43 +00:00
namespace Poco { class Logger; }
namespace zkutil
{
class ZooKeeper;
using ZooKeeperPtr = std::shared_ptr<ZooKeeper>;
}
2014-04-02 07:59:43 +00:00
namespace DB
{
2016-01-17 05:22:22 +00:00
class StorageReplicatedMergeTree;
struct StorageSnapshot;
using StorageSnapshotPtr = std::shared_ptr<StorageSnapshot>;
2021-07-23 19:33:59 +00:00
class ReplicatedMergeTreeSink : public SinkToStorage
2014-04-02 07:59:43 +00:00
{
public:
2021-07-23 19:33:59 +00:00
ReplicatedMergeTreeSink(
StorageReplicatedMergeTree & storage_,
const StorageMetadataPtr & metadata_snapshot_,
size_t quorum_,
size_t quorum_timeout_ms_,
size_t max_parts_per_block_,
2020-09-30 23:16:27 +00:00
bool quorum_parallel_,
2020-11-13 07:54:05 +00:00
bool deduplicate_,
bool majority_quorum_,
ContextPtr context_,
// special flag to determine the ALTER TABLE ATTACH PART without the query context,
// needed to set the special LogEntryType::ATTACH_PART
bool is_attach_ = false);
~ReplicatedMergeTreeSink() override;
2021-07-26 14:47:29 +00:00
void onStart() override;
2021-07-23 19:33:59 +00:00
void consume(Chunk chunk) override;
void onFinish() override;
2021-07-23 19:33:59 +00:00
String getName() const override { return "ReplicatedMergeTreeSink"; }
2014-04-02 07:59:43 +00:00
/// For ATTACHing existing data on filesystem.
void writeExistingPart(MergeTreeData::MutableDataPartPtr & part);
/// For proper deduplication in MaterializedViews
bool lastBlockIsDuplicate() const override
{
/// If MV is responsible for deduplication, block is not considered duplicating.
if (context->getSettingsRef().deduplicate_blocks_in_dependent_materialized_views)
return false;
return last_block_is_duplicate;
}
2014-04-02 07:59:43 +00:00
private:
struct QuorumInfo
{
String status_path;
String is_active_node_value;
int is_active_node_version = -1;
int host_node_version = -1;
};
QuorumInfo quorum_info;
/// Checks active replicas.
/// Returns total number of replicas.
size_t checkQuorumPrecondition(zkutil::ZooKeeperPtr & zookeeper);
/// Rename temporary part and commit to ZooKeeper.
2022-06-23 16:21:46 +00:00
void commitPart(
zkutil::ZooKeeperPtr & zookeeper,
MergeTreeData::MutableDataPartPtr & part,
const String & block_id,
DataPartStorageBuilderPtr part_builder,
size_t replicas_num);
/// Wait for quorum to be satisfied on path (quorum_path) form part (part_name)
/// Also checks that replica still alive.
void waitForQuorum(
zkutil::ZooKeeperPtr & zookeeper, const std::string & part_name,
const std::string & quorum_path, const std::string & is_active_node_value, size_t replicas_num) const;
2014-04-02 07:59:43 +00:00
StorageReplicatedMergeTree & storage;
StorageMetadataPtr metadata_snapshot;
/// Empty means use majority quorum.
std::optional<size_t> required_quorum_size;
size_t getQuorumSize(size_t replicas_num) const;
bool isQuorumEnabled() const;
String quorumLogMessage(size_t replicas_num) const; /// Used in logs for debug purposes
size_t quorum_timeout_ms;
size_t max_parts_per_block;
bool is_attach = false;
2020-09-30 23:16:27 +00:00
bool quorum_parallel = false;
const bool deduplicate = true;
bool last_block_is_duplicate = false;
using Logger = Poco::Logger;
2020-05-30 21:57:37 +00:00
Poco::Logger * log;
2020-11-13 07:54:05 +00:00
ContextPtr context;
StorageSnapshotPtr storage_snapshot;
UInt64 chunk_dedup_seqnum = 0; /// input chunk ordinal number in case of dedup token
/// We can delay processing for previous chunk and start writing a new one.
struct DelayedChunk;
std::unique_ptr<DelayedChunk> delayed_chunk;
void finishDelayedChunk(zkutil::ZooKeeperPtr & zookeeper);
2014-04-02 07:59:43 +00:00
};
}