2012-05-21 20:38:34 +00:00
|
|
|
#include <DB/Parsers/formatAST.h>
|
|
|
|
|
|
|
|
#include <DB/DataStreams/RemoteBlockInputStream.h>
|
|
|
|
|
|
|
|
#include <DB/Storages/StorageDistributed.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
StorageDistributed::StorageDistributed(
|
|
|
|
const std::string & name_,
|
|
|
|
NamesAndTypesListPtr columns_,
|
2012-11-06 17:04:38 +00:00
|
|
|
const StorageDistributed::Addresses & addresses,
|
2012-05-21 20:38:34 +00:00
|
|
|
const String & remote_database_,
|
|
|
|
const String & remote_table_,
|
2012-08-02 17:33:31 +00:00
|
|
|
const DataTypeFactory & data_type_factory_,
|
|
|
|
const Settings & settings)
|
2012-11-06 17:04:38 +00:00
|
|
|
: name(name_), columns(columns_),
|
2012-05-21 20:38:34 +00:00
|
|
|
remote_database(remote_database_), remote_table(remote_table_),
|
|
|
|
data_type_factory(data_type_factory_)
|
|
|
|
{
|
|
|
|
for (Addresses::const_iterator it = addresses.begin(); it != addresses.end(); ++it)
|
2012-10-22 19:55:19 +00:00
|
|
|
pools.push_back(new ConnectionPool(
|
|
|
|
settings.distributed_connections_pool_size,
|
2012-07-26 19:42:20 +00:00
|
|
|
it->host().toString(), it->port(), "", data_type_factory, "server", Protocol::Compression::Enable,
|
2012-07-26 20:16:57 +00:00
|
|
|
settings.connect_timeout, settings.receive_timeout, settings.send_timeout));
|
2012-05-21 20:38:34 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 17:04:38 +00:00
|
|
|
StorageDistributed::StorageDistributed(
|
|
|
|
const std::string & name_,
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
const StorageDistributed::AddressesWithFailover & addresses,
|
|
|
|
const String & remote_database_,
|
|
|
|
const String & remote_table_,
|
|
|
|
const DataTypeFactory & data_type_factory_,
|
|
|
|
const Settings & settings)
|
|
|
|
: name(name_), columns(columns_),
|
|
|
|
remote_database(remote_database_), remote_table(remote_table_),
|
|
|
|
data_type_factory(data_type_factory_)
|
|
|
|
{
|
|
|
|
for (AddressesWithFailover::const_iterator it = addresses.begin(); it != addresses.end(); ++it)
|
|
|
|
{
|
|
|
|
ConnectionPools replicas;
|
|
|
|
replicas.reserve(it->size());
|
|
|
|
|
|
|
|
for (Addresses::const_iterator jt = it->begin(); jt != it->end(); ++jt)
|
|
|
|
replicas.push_back(new ConnectionPool(
|
|
|
|
settings.distributed_connections_pool_size,
|
|
|
|
jt->host().toString(), jt->port(), "", data_type_factory, "server", Protocol::Compression::Enable,
|
2012-11-06 18:20:00 +00:00
|
|
|
settings.connect_timeout_with_failover_ms, settings.receive_timeout, settings.send_timeout));
|
2012-11-06 17:04:38 +00:00
|
|
|
|
|
|
|
pools.push_back(new ConnectionPoolWithFailover(replicas, settings.connections_with_failover_max_tries));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-06 11:26:35 +00:00
|
|
|
StoragePtr StorageDistributed::create(
|
|
|
|
const std::string & name_,
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
const StorageDistributed::Addresses & addresses,
|
|
|
|
const String & remote_database_,
|
|
|
|
const String & remote_table_,
|
|
|
|
const DataTypeFactory & data_type_factory_,
|
|
|
|
const Settings & settings)
|
|
|
|
{
|
|
|
|
return (new StorageDistributed(name_, columns_, addresses, remote_database_, remote_table_, data_type_factory_, settings))->thisPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr StorageDistributed::create(
|
|
|
|
const std::string & name_,
|
|
|
|
NamesAndTypesListPtr columns_,
|
|
|
|
const StorageDistributed::AddressesWithFailover & addresses,
|
|
|
|
const String & remote_database_,
|
|
|
|
const String & remote_table_,
|
|
|
|
const DataTypeFactory & data_type_factory_,
|
|
|
|
const Settings & settings)
|
|
|
|
{
|
|
|
|
return (new StorageDistributed(name_, columns_, addresses, remote_database_, remote_table_, data_type_factory_, settings))->thisPtr();
|
|
|
|
}
|
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
|
|
BlockInputStreams StorageDistributed::read(
|
|
|
|
const Names & column_names,
|
|
|
|
ASTPtr query,
|
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,
|
2012-05-30 04:45:49 +00:00
|
|
|
unsigned threads)
|
2012-05-21 20:38:34 +00:00
|
|
|
{
|
2012-10-22 19:55:19 +00:00
|
|
|
processed_stage = pools.size() == 1
|
2012-05-22 20:05:43 +00:00
|
|
|
? QueryProcessingStage::Complete
|
|
|
|
: QueryProcessingStage::WithMergeableState;
|
2012-05-22 18:32:45 +00:00
|
|
|
|
2012-05-21 20:38:34 +00:00
|
|
|
/// Заменим в запросе имена БД и таблицы.
|
|
|
|
ASTPtr modified_query_ast = query->clone();
|
|
|
|
ASTSelectQuery & select = dynamic_cast<ASTSelectQuery &>(*modified_query_ast);
|
|
|
|
select.database = new ASTIdentifier(StringRange(), remote_database, ASTIdentifier::Database);
|
|
|
|
select.table = new ASTIdentifier(StringRange(), remote_table, ASTIdentifier::Table);
|
|
|
|
|
|
|
|
std::stringstream s;
|
2012-05-30 01:38:02 +00:00
|
|
|
formatAST(select, s, 0, false, true);
|
2012-05-21 20:38:34 +00:00
|
|
|
String modified_query = s.str();
|
|
|
|
|
|
|
|
BlockInputStreams res;
|
|
|
|
|
2012-10-22 19:55:19 +00:00
|
|
|
for (ConnectionPools::iterator it = pools.begin(); it != pools.end(); ++it)
|
2013-02-01 19:02:04 +00:00
|
|
|
res.push_back(new RemoteBlockInputStream((*it)->get(), modified_query, &settings, processed_stage));
|
2012-05-21 20:38:34 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|