2012-05-21 20:38:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/shared_ptr_helper.h>
|
2016-08-26 21:25:05 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2017-05-10 06:49:19 +00:00
|
|
|
#include <Common/SimpleIncrement.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Client/ConnectionPool.h>
|
|
|
|
#include <Client/ConnectionPoolWithFailover.h>
|
2019-03-22 12:08:30 +00:00
|
|
|
#include <Core/Settings.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <Interpreters/ExpressionActions.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>
|
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
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2017-06-06 18:36:13 +00:00
|
|
|
class StorageDistributed : public ext::shared_ptr_helper<StorageDistributed>, public IStorage
|
2012-05-21 20:38:34 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
friend class DistributedBlockOutputStream;
|
|
|
|
friend class StorageDistributedDirectoryMonitor;
|
2014-08-15 09:50:05 +00:00
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
public:
|
2017-06-06 18:48:38 +00:00
|
|
|
~StorageDistributed() override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-06 18:48:38 +00:00
|
|
|
static StoragePtr createWithOwnCluster(
|
2018-03-06 20:18:34 +00:00
|
|
|
const std::string & table_name_,
|
2018-03-12 13:47:01 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2018-07-24 13:10:34 +00:00
|
|
|
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
|
|
|
|
2018-07-24 13:10:34 +00:00
|
|
|
static StoragePtr createWithOwnCluster(
|
|
|
|
const std::string & table_name_,
|
|
|
|
const ColumnsDescription & columns_,
|
2018-07-25 12:31:47 +00:00
|
|
|
ASTPtr & remote_table_function_ptr_, /// Table function ptr.
|
2017-04-01 07:20:54 +00:00
|
|
|
ClusterPtr & owned_cluster_,
|
2017-05-23 18:37:14 +00:00
|
|
|
const Context & context_);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
std::string getName() const override { return "Distributed"; }
|
2018-03-06 20:18:34 +00:00
|
|
|
std::string getTableName() const override { return table_name; }
|
2017-04-01 07:20:54 +00:00
|
|
|
bool supportsSampling() const override { return true; }
|
|
|
|
bool supportsFinal() const override { return true; }
|
|
|
|
bool supportsPrewhere() const override { return true; }
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NameAndTypePair getColumn(const String & column_name) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
bool hasColumn(const String & column_name) const override;
|
|
|
|
|
|
|
|
bool isRemote() const override { return true; }
|
|
|
|
|
2018-04-19 14:47:09 +00:00
|
|
|
QueryProcessingStage::Enum getQueryProcessingStage(const Context & context) const override;
|
2018-06-29 11:42:44 +00:00
|
|
|
QueryProcessingStage::Enum getQueryProcessingStage(const Context & context, const ClusterPtr & cluster) const;
|
2018-04-19 14:47:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockInputStreams read(
|
|
|
|
const Names & column_names,
|
2017-07-15 03:48:36 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Context & context,
|
2018-04-19 14:47:09 +00:00
|
|
|
QueryProcessingStage::Enum processed_stage,
|
2019-02-18 23:38:44 +00:00
|
|
|
size_t max_block_size,
|
2017-06-02 15:54:39 +00:00
|
|
|
unsigned num_streams) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-27 18:26:24 +00:00
|
|
|
BlockOutputStreamPtr write(const ASTPtr & query, const Context & context) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
void drop() override {}
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2018-06-09 15:48:22 +00:00
|
|
|
/// Removes temporary data in local filesystem.
|
2018-12-28 13:39:44 +00:00
|
|
|
void truncate(const ASTPtr &, const Context &) override;
|
2018-04-21 00:35:20 +00:00
|
|
|
|
2018-03-06 20:18:34 +00:00
|
|
|
void rename(const String & /*new_path_to_db*/, const String & /*new_database_name*/, const String & new_table_name) override { table_name = new_table_name; }
|
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-03-05 10:12:20 +00:00
|
|
|
void alter(
|
|
|
|
const AlterCommands & params, const String & database_name, const String & table_name,
|
2019-03-07 20:52:25 +00:00
|
|
|
const Context & context, TableStructureWriteLockHolder & table_lock_holder) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-06 17:06:14 +00:00
|
|
|
void startup() override;
|
2017-04-01 07:20:54 +00:00
|
|
|
void shutdown() override;
|
|
|
|
|
2018-02-21 19:26:59 +00:00
|
|
|
String getDataPath() const override { return path; }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ExpressionActionsPtr & getShardingKeyExpr() const { return sharding_key_expr; }
|
|
|
|
const String & getShardingKeyColumnName() const { return sharding_key_column_name; }
|
|
|
|
size_t getShardCount() const;
|
|
|
|
const String & getPath() const { return path; }
|
|
|
|
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
|
2014-08-13 11:26:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// create directory monitors for each existing subdirectory
|
|
|
|
void createDirectoryMonitors();
|
2017-08-10 04:37:35 +00:00
|
|
|
/// ensure directory monitor thread creation by subdirectory name
|
2017-07-27 15:24:39 +00:00
|
|
|
void requireDirectoryMonitor(const std::string & name);
|
|
|
|
/// ensure connection pool creation and return it
|
|
|
|
ConnectionPoolPtr requireConnectionPool(const std::string & name);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ClusterPtr getCluster() const;
|
|
|
|
|
|
|
|
|
2018-03-06 20:18:34 +00:00
|
|
|
String table_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
String remote_database;
|
|
|
|
String remote_table;
|
2018-07-24 13:10:34 +00:00
|
|
|
ASTPtr remote_table_function_ptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
Context global_context;
|
2017-04-01 07:20:54 +00:00
|
|
|
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;
|
2017-04-16 15:00:33 +00:00
|
|
|
String path; /// Can be empty if data_path_ is empty. In this case, a directory for the data to be sent is not created.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-27 15:24:39 +00:00
|
|
|
struct ClusterNodeData
|
|
|
|
{
|
|
|
|
std::unique_ptr<StorageDistributedDirectoryMonitor> directory_monitor;
|
|
|
|
ConnectionPoolPtr conneciton_pool;
|
|
|
|
|
|
|
|
/// Creates connection_pool if not exists.
|
|
|
|
void requireConnectionPool(const std::string & name, const StorageDistributed & storage);
|
|
|
|
/// Creates directory_monitor if not exists.
|
|
|
|
void requireDirectoryMonitor(const std::string & name, StorageDistributed & storage);
|
2018-04-21 00:35:20 +00:00
|
|
|
|
|
|
|
void shutdownAndDropAllData();
|
2017-07-27 15:24:39 +00:00
|
|
|
};
|
|
|
|
std::unordered_map<std::string, ClusterNodeData> cluster_nodes_data;
|
2018-02-07 13:02:47 +00:00
|
|
|
std::mutex cluster_nodes_mutex;
|
2017-05-10 06:39:37 +00:00
|
|
|
|
|
|
|
/// Used for global monotonic ordering of files to send.
|
|
|
|
SimpleIncrement file_names_increment;
|
2017-11-03 21:50:22 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
StorageDistributed(
|
2018-03-16 02:08:31 +00:00
|
|
|
const String & database_name,
|
|
|
|
const String & table_name_,
|
2018-03-06 20:18:34 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2017-11-03 21:50:22 +00:00
|
|
|
const String & remote_database_,
|
|
|
|
const String & remote_table_,
|
|
|
|
const String & cluster_name_,
|
|
|
|
const Context & context_,
|
2017-12-30 03:49:02 +00:00
|
|
|
const ASTPtr & sharding_key_,
|
2018-03-16 02:08:31 +00:00
|
|
|
const String & data_path_,
|
|
|
|
bool attach);
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2018-07-24 13:10:34 +00:00
|
|
|
StorageDistributed(
|
|
|
|
const String & database_name,
|
|
|
|
const String & table_name_,
|
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
ASTPtr remote_table_function_ptr_,
|
|
|
|
const String & cluster_name_,
|
|
|
|
const Context & context_,
|
|
|
|
const ASTPtr & sharding_key_,
|
|
|
|
const String & data_path_,
|
|
|
|
bool attach);
|
2018-12-05 15:48:06 +00:00
|
|
|
|
|
|
|
ClusterPtr skipUnusedShards(ClusterPtr cluster, const SelectQueryInfo & query_info);
|
2012-05-21 20:38:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|