2012-05-21 20:38:34 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#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>
|
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
|
|
|
|
*/
|
|
|
|
|
class StorageDistributed : public IStorage
|
|
|
|
|
{
|
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_, /// Имя таблицы на удалённых серверах.
|
2014-02-23 02:27:09 +00:00
|
|
|
|
SharedPtr<Cluster> & 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
|
|
|
|
|
2014-08-21 12:07:29 +00:00
|
|
|
|
BlockOutputStreamPtr write(ASTPtr query) 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
|
|
|
|
/// в подтаблицах добавлять и удалять столбы нужно вручную
|
|
|
|
|
/// структура подтаблиц не проверяется
|
2014-10-03 17:55:36 +00:00
|
|
|
|
void alter(const AlterCommands & params, const String & database_name, const String & table_name, 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
|
|
|
|
|
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; }
|
|
|
|
|
const String & getPath() const { return path; }
|
2014-08-12 13:46:46 +00:00
|
|
|
|
|
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_,
|
2013-12-07 16:51:29 +00:00
|
|
|
|
Cluster & cluster_,
|
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_,
|
|
|
|
|
Cluster & cluster_,
|
|
|
|
|
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
|
|
|
|
|
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;
|
2014-02-23 02:27:09 +00:00
|
|
|
|
|
|
|
|
|
/// Используется только, если таблица должна владеть объектом Cluster, которым больше никто не владеет - для реализации TableFunctionRemote.
|
|
|
|
|
SharedPtr<Cluster> owned_cluster;
|
|
|
|
|
|
|
|
|
|
/// Соединения с удалёнными серверами.
|
2013-12-07 16:51:29 +00:00
|
|
|
|
Cluster & cluster;
|
2014-08-12 13:46:46 +00:00
|
|
|
|
|
2014-08-13 09:20:15 +00:00
|
|
|
|
ExpressionActionsPtr sharding_key_expr;
|
|
|
|
|
String sharding_key_column_name;
|
2014-08-12 13:46:46 +00:00
|
|
|
|
bool write_enabled;
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|