ClickHouse/src/Storages/StorageExternalDistributed.cpp

280 lines
9.6 KiB
C++
Raw Normal View History

#include "StorageExternalDistributed.h"
2021-03-27 14:35:44 +00:00
#include <Storages/StorageFactory.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Core/Settings.h>
#include <Interpreters/Context.h>
#include <DataTypes/DataTypeString.h>
#include <Formats/FormatFactory.h>
#include <Parsers/ASTLiteral.h>
#include <Common/parseAddress.h>
#include <Processors/Pipe.h>
#include <Common/parseRemoteDescription.h>
#include <Storages/StorageMySQL.h>
#include <Storages/MySQL/MySQLSettings.h>
2021-03-27 19:18:05 +00:00
#include <Storages/StoragePostgreSQL.h>
2021-04-21 12:32:57 +00:00
#include <Storages/StorageURL.h>
#include <common/logger_useful.h>
2021-03-27 14:35:44 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int BAD_ARGUMENTS;
}
StorageExternalDistributed::StorageExternalDistributed(
2021-03-27 14:35:44 +00:00
const StorageID & table_id_,
2021-03-28 10:27:37 +00:00
ExternalStorageEngine table_engine,
2021-03-27 14:35:44 +00:00
const String & cluster_description,
const String & remote_database,
const String & remote_table,
2021-03-27 14:35:44 +00:00
const String & username,
const String & password,
const ColumnsDescription & columns_,
const ConstraintsDescription & constraints_,
2021-04-23 12:18:23 +00:00
const String & comment,
ContextPtr context)
2021-03-27 14:35:44 +00:00
: IStorage(table_id_)
{
StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(columns_);
storage_metadata.setConstraints(constraints_);
2021-04-23 12:18:23 +00:00
storage_metadata.setComment(comment);
2021-03-27 14:35:44 +00:00
setInMemoryMetadata(storage_metadata);
size_t max_addresses = context->getSettingsRef().glob_expansion_max_elements;
2021-03-27 14:35:44 +00:00
std::vector<String> shards_descriptions = parseRemoteDescription(cluster_description, 0, cluster_description.size(), ',', max_addresses);
2021-04-01 12:09:44 +00:00
std::vector<std::pair<std::string, UInt16>> addresses;
2021-03-27 14:35:44 +00:00
2021-04-21 16:17:45 +00:00
#if USE_MYSQL || USE_LIBPQXX
/// For each shard pass replicas description into storage, replicas are managed by storage's PoolWithFailover.
2021-03-27 14:35:44 +00:00
for (const auto & shard_description : shards_descriptions)
{
StoragePtr shard;
2021-03-28 10:27:37 +00:00
switch (table_engine)
{
2021-03-28 10:27:37 +00:00
#if USE_MYSQL
case ExternalStorageEngine::MySQL:
{
2021-04-01 12:09:44 +00:00
addresses = parseRemoteDescriptionForExternalDatabase(shard_description, max_addresses, 3306);
2021-04-01 11:30:20 +00:00
2021-03-28 10:27:37 +00:00
mysqlxx::PoolWithFailover pool(
remote_database,
2021-04-01 10:27:24 +00:00
addresses,
username, password);
2021-03-28 10:27:37 +00:00
shard = StorageMySQL::create(
table_id_,
std::move(pool),
remote_database,
remote_table,
/* replace_query = */ false,
/* on_duplicate_clause = */ "",
2021-04-23 12:18:23 +00:00
columns_,
constraints_,
String{},
context,
MySQLSettings{});
2021-03-28 10:27:37 +00:00
break;
}
#endif
#if USE_LIBPQXX
case ExternalStorageEngine::PostgreSQL:
{
2021-04-01 12:09:44 +00:00
addresses = parseRemoteDescriptionForExternalDatabase(shard_description, max_addresses, 5432);
2021-04-01 11:30:20 +00:00
2021-05-07 11:18:49 +00:00
auto pool = std::make_shared<postgres::PoolWithFailover>(
2021-03-28 10:27:37 +00:00
remote_database,
2021-04-01 10:27:24 +00:00
addresses,
2021-03-28 10:27:37 +00:00
username, password,
context->getSettingsRef().postgresql_connection_pool_size,
context->getSettingsRef().postgresql_connection_pool_wait_timeout);
2021-03-28 10:27:37 +00:00
2021-04-23 12:18:23 +00:00
shard = StoragePostgreSQL::create(table_id_, std::move(pool), remote_table, columns_, constraints_, String{}, context);
2021-03-28 10:27:37 +00:00
break;
}
#endif
default:
2021-04-21 16:17:45 +00:00
{
2021-03-28 10:27:37 +00:00
throw Exception(ErrorCodes::BAD_ARGUMENTS,
2021-04-21 12:32:57 +00:00
"Unsupported table engine. Supported engines are: MySQL, PostgreSQL, URL");
2021-04-21 16:17:45 +00:00
}
}
shards.emplace(std::move(shard));
2021-03-27 14:35:44 +00:00
}
2021-04-21 16:17:45 +00:00
#else
(void)table_engine;
(void)remote_database;
(void)remote_table;
(void)username;
(void)password;
(void)shards_descriptions;
(void)addresses;
#endif
2021-03-27 14:35:44 +00:00
}
2021-04-21 12:32:57 +00:00
StorageExternalDistributed::StorageExternalDistributed(
const String & addresses_description,
const StorageID & table_id,
const String & format_name,
const std::optional<FormatSettings> & format_settings,
const String & compression_method,
const ColumnsDescription & columns,
const ConstraintsDescription & constraints,
ContextPtr context)
: IStorage(table_id)
{
StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(columns);
storage_metadata.setConstraints(constraints);
setInMemoryMetadata(storage_metadata);
size_t max_addresses = context->getSettingsRef().glob_expansion_max_elements;
2021-04-21 14:36:04 +00:00
/// Generate addresses without splitting for failover options
std::vector<String> url_descriptions = parseRemoteDescription(addresses_description, 0, addresses_description.size(), ',', max_addresses);
std::vector<String> uri_options;
2021-04-21 12:32:57 +00:00
2021-04-21 14:36:04 +00:00
for (const auto & url_description : url_descriptions)
2021-04-21 12:32:57 +00:00
{
2021-04-21 14:36:04 +00:00
/// For each uri (which acts like shard) check if it has failover options
uri_options = parseRemoteDescription(url_description, 0, url_description.size(), '|', max_addresses);
StoragePtr shard;
if (uri_options.size() > 1)
{
shard = std::make_shared<StorageURLWithFailover>(
uri_options,
table_id,
format_name,
format_settings,
columns, constraints, context,
compression_method);
}
else
{
Poco::URI uri(url_description);
shard = std::make_shared<StorageURL>(
2021-05-02 16:33:45 +00:00
uri, table_id, format_name, format_settings, columns, constraints, String{}, context, compression_method);
2021-04-21 14:36:04 +00:00
LOG_DEBUG(&Poco::Logger::get("StorageURLDistributed"), "Adding URL: {}", url_description);
}
shards.emplace(std::move(shard));
2021-03-27 14:35:44 +00:00
}
}
Pipe StorageExternalDistributed::read(
const Names & column_names,
2021-03-27 14:35:44 +00:00
const StorageMetadataPtr & metadata_snapshot,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
unsigned num_streams)
2021-03-27 14:35:44 +00:00
{
Pipes pipes;
for (const auto & shard : shards)
{
pipes.emplace_back(shard->read(
column_names,
metadata_snapshot,
query_info,
context,
processed_stage,
max_block_size,
num_streams
));
2021-03-27 14:35:44 +00:00
}
return Pipe::unitePipes(std::move(pipes));
}
void registerStorageExternalDistributed(StorageFactory & factory)
2021-03-27 14:35:44 +00:00
{
factory.registerStorage("ExternalDistributed", [](const StorageFactory::Arguments & args)
2021-03-27 14:35:44 +00:00
{
ASTs & engine_args = args.engine_args;
if (engine_args.size() != 6)
2021-03-27 14:35:44 +00:00
throw Exception(
"Storage MySQLiDistributed requires 5 parameters: ExternalDistributed('engine_name', 'cluster_description', database, table, 'user', 'password').",
2021-03-27 14:35:44 +00:00
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
for (auto & engine_arg : engine_args)
engine_arg = evaluateConstantExpressionOrIdentifierAsLiteral(engine_arg, args.getLocalContext());
2021-03-27 14:35:44 +00:00
const String & engine_name = engine_args[0]->as<ASTLiteral &>().value.safeGet<String>();
2021-04-21 12:32:57 +00:00
const String & addresses_description = engine_args[1]->as<ASTLiteral &>().value.safeGet<String>();
2021-03-27 14:35:44 +00:00
2021-03-28 10:27:37 +00:00
StorageExternalDistributed::ExternalStorageEngine table_engine;
2021-04-21 12:32:57 +00:00
if (engine_name == "URL")
{
table_engine = StorageExternalDistributed::ExternalStorageEngine::URL;
const String & format_name = engine_args[2]->as<ASTLiteral &>().value.safeGet<String>();
String compression_method = "auto";
if (engine_args.size() == 4)
compression_method = engine_args[3]->as<ASTLiteral &>().value.safeGet<String>();
auto format_settings = StorageURL::getFormatSettingsFromArgs(args);
return StorageExternalDistributed::create(
addresses_description,
args.table_id,
format_name,
format_settings,
compression_method,
args.columns,
args.constraints,
args.getContext());
}
2021-03-28 10:27:37 +00:00
else
2021-04-21 12:32:57 +00:00
{
if (engine_name == "MySQL")
table_engine = StorageExternalDistributed::ExternalStorageEngine::MySQL;
else if (engine_name == "PostgreSQL")
table_engine = StorageExternalDistributed::ExternalStorageEngine::PostgreSQL;
else
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"External storage engine {} is not supported for StorageExternalDistributed. Supported engines are: MySQL, PostgreSQL, URL",
engine_name);
const String & remote_database = engine_args[2]->as<ASTLiteral &>().value.safeGet<String>();
const String & remote_table = engine_args[3]->as<ASTLiteral &>().value.safeGet<String>();
const String & username = engine_args[4]->as<ASTLiteral &>().value.safeGet<String>();
const String & password = engine_args[5]->as<ASTLiteral &>().value.safeGet<String>();
return StorageExternalDistributed::create(
args.table_id,
table_engine,
addresses_description,
remote_database,
remote_table,
username,
password,
args.columns,
args.constraints,
2021-05-02 16:33:45 +00:00
args.comment,
2021-04-21 12:32:57 +00:00
args.getContext());
}
2021-03-27 14:35:44 +00:00
},
{
2021-04-21 12:32:57 +00:00
.source_access_type = AccessType::SOURCES,
2021-03-27 14:35:44 +00:00
});
}
}