2012-05-21 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-08-30 19:27:15 +00:00
|
|
|
|
#include <ext/shared_ptr_helper.hpp>
|
2016-08-26 21:25:05 +00:00
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
#include <DB/Storages/IStorage.h>
|
2012-10-22 19:55:19 +00:00
|
|
|
|
#include <DB/Client/ConnectionPool.h>
|
2012-11-06 17:04:38 +00:00
|
|
|
|
#include <DB/Client/ConnectionPoolWithFailover.h>
|
2012-07-26 20:16:57 +00:00
|
|
|
|
#include <DB/Interpreters/Settings.h>
|
2013-09-23 12:01:19 +00:00
|
|
|
|
#include <DB/Interpreters/Context.h>
|
2016-10-10 08:44:52 +00:00
|
|
|
|
#include <DB/Interpreters/Cluster.h>
|
2015-04-16 06:12:35 +00:00
|
|
|
|
#include <DB/Interpreters/ExpressionActions.h>
|
2016-03-25 11:48:45 +00:00
|
|
|
|
#include <common/logger_useful.h>
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Распределённая таблица, находящаяся на нескольких серверах.
|
|
|
|
|
* Использует данные заданной БД и таблицы на каждом сервере.
|
2012-05-30 04:45:49 +00:00
|
|
|
|
*
|
|
|
|
|
* Можно передать один адрес, а не несколько.
|
|
|
|
|
* В этом случае, таблицу можно считать удалённой, а не распределённой.
|
2012-05-21 20:38:34 +00:00
|
|
|
|
*/
|
2016-08-30 19:27:15 +00:00
|
|
|
|
class StorageDistributed : private ext::shared_ptr_helper<StorageDistributed>, public IStorage
|
2012-05-21 20:38:34 +00:00
|
|
|
|
{
|
2016-08-30 19:27:15 +00:00
|
|
|
|
friend class ext::shared_ptr_helper<StorageDistributed>;
|
2014-08-15 09:50:05 +00:00
|
|
|
|
friend class DistributedBlockOutputStream;
|
|
|
|
|
friend class DirectoryMonitor;
|
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
public:
|
2013-02-06 11:26:35 +00:00
|
|
|
|
static StoragePtr create(
|
2012-05-21 20:38:34 +00:00
|
|
|
|
const std::string & name_, /// Имя таблицы.
|
|
|
|
|
NamesAndTypesListPtr columns_, /// Список столбцов.
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
2012-05-21 20:38:34 +00:00
|
|
|
|
const String & remote_database_, /// БД на удалённых серверах.
|
|
|
|
|
const String & remote_table_, /// Имя таблицы на удалённых серверах.
|
2013-12-10 17:06:57 +00:00
|
|
|
|
const String & cluster_name,
|
2014-08-12 13:46:46 +00:00
|
|
|
|
Context & context_,
|
2014-08-13 09:20:15 +00:00
|
|
|
|
const ASTPtr & sharding_key_,
|
|
|
|
|
const String & data_path_);
|
2012-11-06 17:04:38 +00:00
|
|
|
|
|
2014-02-07 15:11:57 +00:00
|
|
|
|
static StoragePtr create(
|
|
|
|
|
const std::string & name_, /// Имя таблицы.
|
|
|
|
|
NamesAndTypesListPtr columns_, /// Список столбцов.
|
|
|
|
|
const String & remote_database_, /// БД на удалённых серверах.
|
|
|
|
|
const String & remote_table_, /// Имя таблицы на удалённых серверах.
|
2016-10-10 08:44:52 +00:00
|
|
|
|
ClusterPtr & owned_cluster_,
|
2014-08-13 09:20:15 +00:00
|
|
|
|
Context & context_);
|
2014-02-07 15:11:57 +00:00
|
|
|
|
|
2014-10-03 17:55:36 +00:00
|
|
|
|
std::string getName() const override { return "Distributed"; }
|
|
|
|
|
std::string getTableName() const override { return name; }
|
|
|
|
|
bool supportsSampling() const override { return true; }
|
|
|
|
|
bool supportsFinal() const override { return true; }
|
|
|
|
|
bool supportsPrewhere() const override { return true; }
|
2015-01-16 15:22:12 +00:00
|
|
|
|
bool supportsParallelReplicas() const override { return true; }
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2014-10-10 15:45:43 +00:00
|
|
|
|
const NamesAndTypesList & getColumnsListImpl() const override { return *columns; }
|
2014-10-03 17:55:36 +00:00
|
|
|
|
NameAndTypePair getColumn(const String & column_name) const override;
|
|
|
|
|
bool hasColumn(const String & column_name) const override;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2014-10-03 17:55:36 +00:00
|
|
|
|
bool isRemote() const override { return true; }
|
2012-10-30 20:15:07 +00:00
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
BlockInputStreams read(
|
|
|
|
|
const Names & column_names,
|
|
|
|
|
ASTPtr query,
|
2014-12-17 11:53:17 +00:00
|
|
|
|
const Context & context,
|
2013-02-01 19:02:04 +00:00
|
|
|
|
const Settings & settings,
|
2012-05-22 18:32:45 +00:00
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2012-05-21 20:38:34 +00:00
|
|
|
|
size_t max_block_size = DEFAULT_BLOCK_SIZE,
|
2014-10-03 17:55:36 +00:00
|
|
|
|
unsigned threads = 1) override;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2015-09-10 20:43:42 +00:00
|
|
|
|
BlockOutputStreamPtr write(ASTPtr query, const Settings & settings) override;
|
2014-08-12 13:46:46 +00:00
|
|
|
|
|
2014-03-20 13:28:49 +00:00
|
|
|
|
void drop() override {}
|
2014-10-03 17:55:36 +00:00
|
|
|
|
void rename(const String & new_path_to_db, const String & new_database_name, const String & new_table_name) override { name = new_table_name; }
|
2013-09-23 12:01:19 +00:00
|
|
|
|
/// в подтаблицах добавлять и удалять столбы нужно вручную
|
|
|
|
|
/// структура подтаблиц не проверяется
|
2016-01-28 01:00:27 +00:00
|
|
|
|
void alter(const AlterCommands & params, const String & database_name, const String & table_name, const Context & context) override;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
2014-08-21 12:07:29 +00:00
|
|
|
|
void shutdown() override;
|
2014-08-13 11:26:13 +00:00
|
|
|
|
|
2016-03-01 17:47:53 +00:00
|
|
|
|
void reshardPartitions(ASTPtr query, const String & database_name,
|
|
|
|
|
const Field & first_partition, const Field & last_partition,
|
|
|
|
|
const WeightedZooKeeperPaths & weighted_zookeeper_paths,
|
2016-03-25 11:48:45 +00:00
|
|
|
|
const ASTPtr & sharding_key_expr, bool do_copy, const Field & coordinator,
|
2016-01-28 01:00:27 +00:00
|
|
|
|
const Settings & settings) override;
|
|
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
|
/// От каждой реплики получить описание соответствующей локальной таблицы.
|
|
|
|
|
BlockInputStreams describe(const Context & context, const Settings & settings);
|
|
|
|
|
|
2014-08-13 09:20:15 +00:00
|
|
|
|
const ExpressionActionsPtr & getShardingKeyExpr() const { return sharding_key_expr; }
|
|
|
|
|
const String & getShardingKeyColumnName() const { return sharding_key_column_name; }
|
2015-09-18 13:36:10 +00:00
|
|
|
|
size_t getShardCount() const;
|
2014-08-13 09:20:15 +00:00
|
|
|
|
const String & getPath() const { return path; }
|
2015-09-18 13:36:10 +00:00
|
|
|
|
std::string getRemoteDatabaseName() const { return remote_database; }
|
|
|
|
|
std::string getRemoteTableName() const { return remote_table; }
|
2016-10-10 08:44:52 +00:00
|
|
|
|
std::string getClusterName() const { return cluster_name; } /// Returns empty string if tables is used by TableFunctionRemote
|
2014-08-13 11:26:13 +00:00
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
private:
|
2013-02-06 11:26:35 +00:00
|
|
|
|
StorageDistributed(
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
|
const String & remote_database_,
|
|
|
|
|
const String & remote_table_,
|
2016-10-10 08:44:52 +00:00
|
|
|
|
const String & cluster_name_,
|
2014-08-15 09:50:05 +00:00
|
|
|
|
Context & context_,
|
2014-08-13 09:20:15 +00:00
|
|
|
|
const ASTPtr & sharding_key_ = nullptr,
|
|
|
|
|
const String & data_path_ = String{});
|
2014-06-12 19:23:06 +00:00
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
StorageDistributed(
|
|
|
|
|
const std::string & name_,
|
|
|
|
|
NamesAndTypesListPtr columns_,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns_,
|
|
|
|
|
const ColumnDefaults & column_defaults_,
|
|
|
|
|
const String & remote_database_,
|
|
|
|
|
const String & remote_table_,
|
2016-10-10 08:44:52 +00:00
|
|
|
|
const String & cluster_name_,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
Context & context_,
|
|
|
|
|
const ASTPtr & sharding_key_ = nullptr,
|
|
|
|
|
const String & data_path_ = String{});
|
|
|
|
|
|
2014-08-13 11:26:13 +00:00
|
|
|
|
|
2014-08-15 09:50:05 +00:00
|
|
|
|
/// create directory monitor thread by subdirectory name
|
|
|
|
|
void createDirectoryMonitor(const std::string & name);
|
|
|
|
|
/// create directory monitors for each existing subdirectory
|
|
|
|
|
void createDirectoryMonitors();
|
2014-08-21 12:07:29 +00:00
|
|
|
|
/// ensure directory monitor creation
|
2014-08-19 08:04:13 +00:00
|
|
|
|
void requireDirectoryMonitor(const std::string & name);
|
2014-08-13 11:26:13 +00:00
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
|
ClusterPtr getCluster() const;
|
|
|
|
|
|
|
|
|
|
private:
|
2012-06-18 06:19:13 +00:00
|
|
|
|
String name;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
NamesAndTypesListPtr columns;
|
|
|
|
|
String remote_database;
|
|
|
|
|
String remote_table;
|
|
|
|
|
|
2014-08-15 09:50:05 +00:00
|
|
|
|
Context & context;
|
2016-03-25 11:48:45 +00:00
|
|
|
|
Logger * log = &Logger::get("StorageDistributed");
|
2014-02-23 02:27:09 +00:00
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
|
/// для реализации TableFunctionRemote.
|
2016-05-28 14:14:18 +00:00
|
|
|
|
std::shared_ptr<Cluster> owned_cluster;
|
2014-02-23 02:27:09 +00:00
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
|
/// Пусто если используется для реализации TableFunctionRemote.
|
|
|
|
|
const String cluster_name;
|
2014-08-12 13:46:46 +00:00
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
|
bool has_sharding_key;
|
2014-08-13 09:20:15 +00:00
|
|
|
|
ExpressionActionsPtr sharding_key_expr;
|
|
|
|
|
String sharding_key_column_name;
|
2014-12-30 03:53:41 +00:00
|
|
|
|
String path; /// Может быть пустым, если data_path_ пустой. В этом случае, директория для данных для отправки не создаётся.
|
2014-08-13 11:26:13 +00:00
|
|
|
|
|
2014-08-19 08:04:13 +00:00
|
|
|
|
class DirectoryMonitor;
|
|
|
|
|
std::unordered_map<std::string, std::unique_ptr<DirectoryMonitor>> directory_monitors;
|
2012-05-21 20:38:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|