ClickHouse/src/Storages/StorageDistributed.h

195 lines
6.9 KiB
C++
Raw Normal View History

2012-05-21 20:38:34 +00:00
#pragma once
2017-06-06 17:18:32 +00:00
#include <ext/shared_ptr_helper.h>
#include <Storages/IStorage.h>
#include <Common/SimpleIncrement.h>
#include <Client/ConnectionPool.h>
#include <Client/ConnectionPoolWithFailover.h>
#include <Core/Settings.h>
#include <Interpreters/Cluster.h>
2018-07-25 12:31:47 +00:00
#include <Parsers/ASTFunction.h>
2016-03-25 11:48:45 +00:00
#include <common/logger_useful.h>
#include <Common/ActionBlocker.h>
2012-05-21 20:38:34 +00:00
namespace DB
{
2016-12-08 02:49:04 +00:00
class Context;
2016-12-12 03:33:34 +00:00
class StorageDistributedDirectoryMonitor;
2016-12-08 02:49:04 +00:00
class Volume;
using VolumePtr = std::shared_ptr<Volume>;
class ExpressionActions;
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
2016-12-08 02:49:04 +00:00
2017-04-16 15:00:33 +00:00
/** A distributed table that resides on multiple servers.
* Uses data from the specified database and tables on each server.
2012-05-30 04:45:49 +00:00
*
2017-04-16 15:00:33 +00:00
* You can pass one address, not several.
* In this case, the table can be considered remote, rather than distributed.
2012-05-21 20:38:34 +00:00
*/
class StorageDistributed final : public ext::shared_ptr_helper<StorageDistributed>, public IStorage
2012-05-21 20:38:34 +00:00
{
2019-08-26 19:07:29 +00:00
friend struct ext::shared_ptr_helper<StorageDistributed>;
friend class DistributedBlockOutputStream;
friend class StorageDistributedDirectoryMonitor;
2012-05-21 20:38:34 +00:00
public:
~StorageDistributed() override;
static StoragePtr createWithOwnCluster(
2019-12-04 16:06:55 +00:00
const StorageID & table_id_,
const ColumnsDescription & columns_,
const String & remote_database_, /// database on remote servers.
const String & remote_table_, /// The name of the table on the remote servers.
ClusterPtr owned_cluster_,
const Context & context_);
2018-07-27 21:33:30 +00:00
static StoragePtr createWithOwnCluster(
2019-12-04 16:06:55 +00:00
const StorageID & table_id_,
const ColumnsDescription & columns_,
2018-07-25 12:31:47 +00:00
ASTPtr & remote_table_function_ptr_, /// Table function ptr.
ClusterPtr & owned_cluster_,
const Context & context_);
std::string getName() const override { return "Distributed"; }
bool supportsSampling() const override { return true; }
bool supportsFinal() const override { return true; }
bool supportsPrewhere() const override { return true; }
StoragePolicyPtr getStoragePolicy() const override;
NameAndTypePair getColumn(const String & column_name) const override;
bool hasColumn(const String & column_name) const override;
bool isRemote() const override { return true; }
QueryProcessingStage::Enum getQueryProcessingStage(const Context &, QueryProcessingStage::Enum /*to_stage*/, const ASTPtr &) const override;
Pipes read(
const Names & column_names,
const SelectQueryInfo & query_info,
const Context & context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
2017-06-02 15:54:39 +00:00
unsigned num_streams) override;
BlockOutputStreamPtr write(const ASTPtr & query, const Context & context) override;
2019-08-27 20:43:08 +00:00
void drop(TableStructureWriteLockHolder &) override {}
2018-04-21 00:35:20 +00:00
2018-06-09 15:48:22 +00:00
/// Removes temporary data in local filesystem.
2019-08-27 20:43:08 +00:00
void truncate(const ASTPtr &, const Context &, TableStructureWriteLockHolder &) override;
void rename(const String & new_path_to_table_data, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &) override;
void renameOnDisk(const String & new_path_to_table_data);
2018-04-21 00:35:20 +00:00
2019-12-26 18:17:05 +00:00
void checkAlterIsPossible(const AlterCommands & commands, const Settings & /* settings */) override;
2017-04-16 15:00:33 +00:00
/// in the sub-tables, you need to manually add and delete columns
/// the structure of the sub-table is not checked
2019-12-26 18:17:05 +00:00
void alter(const AlterCommands & params, const Context & context, TableStructureWriteLockHolder & table_lock_holder) override;
void startup() override;
void shutdown() override;
Strings getDataPaths() const override;
const ExpressionActionsPtr & getShardingKeyExpr() const { return sharding_key_expr; }
const String & getShardingKeyColumnName() const { return sharding_key_column_name; }
size_t getShardCount() const;
std::pair<const std::string &, const std::string &> getPath();
std::string getRemoteDatabaseName() const { return remote_database; }
std::string getRemoteTableName() const { return remote_table; }
std::string getClusterName() const { return cluster_name; } /// Returns empty string if tables is used by TableFunctionRemote
/// create directory monitors for each existing subdirectory
void createDirectoryMonitors(const std::string & disk);
/// ensure directory monitor thread and connectoin pool creation by disk and subdirectory name
void requireDirectoryMonitor(const std::string & disk, const std::string & name);
void flushClusterNodesAllData();
ClusterPtr getCluster() const;
/// Apply the following settings:
/// - optimize_skip_unused_shards
/// - force_optimize_skip_unused_shards
ClusterPtr getOptimizedCluster(const Context &, const ASTPtr & query_ptr) const;
ClusterPtr skipUnusedShards(ClusterPtr cluster, const ASTPtr & query_ptr, const Context & context) const;
ActionLock getActionLock(StorageActionBlockType type) override;
String remote_database;
String remote_table;
ASTPtr remote_table_function_ptr;
Context global_context;
Logger * log = &Logger::get("StorageDistributed");
/// Used to implement TableFunctionRemote.
std::shared_ptr<Cluster> owned_cluster;
/// Is empty if this storage implements TableFunctionRemote.
const String cluster_name;
bool has_sharding_key;
ExpressionActionsPtr sharding_key_expr;
String sharding_key_column_name;
/// Used for global monotonic ordering of files to send.
SimpleIncrement file_names_increment;
ActionBlocker monitors_blocker;
protected:
StorageDistributed(
2019-12-04 16:06:55 +00:00
const StorageID & id_,
const ColumnsDescription & columns_,
2019-08-24 21:20:20 +00:00
const ConstraintsDescription & constraints_,
const String & remote_database_,
const String & remote_table_,
const String & cluster_name_,
const Context & context_,
const ASTPtr & sharding_key_,
const String & storage_policy_,
2019-10-25 19:07:47 +00:00
const String & relative_data_path_,
2019-08-03 11:02:40 +00:00
bool attach_);
2018-07-27 21:33:30 +00:00
StorageDistributed(
2019-12-04 16:06:55 +00:00
const StorageID & id_,
const ColumnsDescription & columns_,
2019-08-24 21:20:20 +00:00
const ConstraintsDescription & constraints_,
ASTPtr remote_table_function_ptr_,
const String & cluster_name_,
const Context & context_,
const ASTPtr & sharding_key_,
const String & storage_policy_,
2019-10-25 19:07:47 +00:00
const String & relative_data_path_,
bool attach);
2018-12-05 15:48:06 +00:00
void createStorage();
String storage_policy;
String relative_data_path;
/// Can be empty if relative_data_path is empty. In this case, a directory for the data to be sent is not created.
VolumePtr volume;
struct ClusterNodeData
{
std::unique_ptr<StorageDistributedDirectoryMonitor> directory_monitor;
ConnectionPoolPtr conneciton_pool;
void flushAllData();
void shutdownAndDropAllData();
};
std::unordered_map<std::string, ClusterNodeData> cluster_nodes_data;
std::mutex cluster_nodes_mutex;
2012-05-21 20:38:34 +00:00
};
}