StorageDistributed: development [#METR-9244]

This commit is contained in:
Pavel Kartavyy 2013-11-28 10:31:17 +00:00
parent 831d6cfb4a
commit de71157b10
2 changed files with 78 additions and 12 deletions

View File

@ -106,6 +106,8 @@ private:
const Context & context_, const Context & context_,
const String & sign_column_name_ = ""); const String & sign_column_name_ = "");
bool checkLocalReplics(const Address & address);
String name; String name;
NamesAndTypesListPtr columns; NamesAndTypesListPtr columns;
String remote_database; String remote_database;
@ -116,6 +118,10 @@ private:
const Context & context; const Context & context;
/// Соединения с удалёнными серверами. /// Соединения с удалёнными серверами.
ConnectionPools pools; ConnectionPools pools;
/// количество реплик clickhouse сервера, расположенных локально
/// к локальным репликам обращаемся напрямую
size_t local_replics_num;
}; };
} }

View File

@ -4,6 +4,10 @@
#include <DB/Storages/StorageDistributed.h> #include <DB/Storages/StorageDistributed.h>
#include <Poco/Net/NetworkInterface.h>
#include <DB/Interpreters/InterpreterSelectQuery.h>
#include <boost/bind.hpp>
namespace DB namespace DB
{ {
@ -22,13 +26,23 @@ StorageDistributed::StorageDistributed(
remote_database(remote_database_), remote_table(remote_table_), remote_database(remote_database_), remote_table(remote_table_),
data_type_factory(data_type_factory_), data_type_factory(data_type_factory_),
sign_column_name(sign_column_name_), sign_column_name(sign_column_name_),
context(context_) context(context_),
local_replics_num(0)
{ {
for (Addresses::const_iterator it = addresses.begin(); it != addresses.end(); ++it) for (Addresses::const_iterator it = addresses.begin(); it != addresses.end(); ++it)
pools.push_back(new ConnectionPool( {
settings.distributed_connections_pool_size, if (checkLocalReplics(*it))
it->host_port.host().toString(), it->host_port.port(), "", it->user, it->password, data_type_factory, "server", Protocol::Compression::Enable, {
settings.connect_timeout, settings.receive_timeout, settings.send_timeout)); ++local_replics_num;
}
else
{
pools.push_back(new ConnectionPool(
settings.distributed_connections_pool_size,
it->host_port.host().toString(), it->host_port.port(), "", it->user, it->password, data_type_factory, "server", Protocol::Compression::Enable,
settings.connect_timeout, settings.receive_timeout, settings.send_timeout));
}
}
} }
StorageDistributed::StorageDistributed( StorageDistributed::StorageDistributed(
@ -45,7 +59,8 @@ StorageDistributed::StorageDistributed(
remote_database(remote_database_), remote_table(remote_table_), remote_database(remote_database_), remote_table(remote_table_),
data_type_factory(data_type_factory_), data_type_factory(data_type_factory_),
sign_column_name(sign_column_name_), sign_column_name(sign_column_name_),
context(context_) context(context_),
local_replics_num(0)
{ {
for (AddressesWithFailover::const_iterator it = addresses.begin(); it != addresses.end(); ++it) for (AddressesWithFailover::const_iterator it = addresses.begin(); it != addresses.end(); ++it)
{ {
@ -53,15 +68,48 @@ StorageDistributed::StorageDistributed(
replicas.reserve(it->size()); replicas.reserve(it->size());
for (Addresses::const_iterator jt = it->begin(); jt != it->end(); ++jt) for (Addresses::const_iterator jt = it->begin(); jt != it->end(); ++jt)
replicas.push_back(new ConnectionPool( {
settings.distributed_connections_pool_size, if (checkLocalReplics(*jt))
jt->host_port.host().toString(), jt->host_port.port(), "", jt->user, jt->password, data_type_factory, "server", Protocol::Compression::Enable, {
settings.connect_timeout_with_failover_ms, settings.receive_timeout, settings.send_timeout)); ++local_replics_num;
}
else
{
replicas.push_back(new ConnectionPool(
settings.distributed_connections_pool_size,
jt->host_port.host().toString(), jt->host_port.port(), "", jt->user, jt->password, data_type_factory, "server", Protocol::Compression::Enable,
settings.connect_timeout_with_failover_ms, settings.receive_timeout, settings.send_timeout));
}
}
pools.push_back(new ConnectionPoolWithFailover(replicas, settings.connections_with_failover_max_tries)); pools.push_back(new ConnectionPoolWithFailover(replicas, settings.connections_with_failover_max_tries));
} }
} }
static bool interfaceEqual(const Poco::Net::NetworkInterface & interface, Poco::Net::IPAddress & address)
{
return interface.address() == address;
}
bool StorageDistributed::checkLocalReplics(const Address & address)
{
/// Если среди реплик существует такая, что:
/// - её порт совпадает с портом, который слушает сервер;
/// - её хост резолвится в набор адресов, один из которых совпадает с одним из адресов сетевых интерфейсов сервера
/// то нужно всегда ходить на этот шард без межпроцессного взаимодействия
UInt16 clickhouse_port = Poco::Util::Application::instance().config().getInt("tcp_port", 0);
Poco::Net::NetworkInterface::NetworkInterfaceList interfaces = Poco::Net::NetworkInterface::list();
if (clickhouse_port == address.host_port.port() &&
interfaces.end() != std::find_if(interfaces.begin(), interfaces.end(),
boost::bind(interfaceEqual, _1, address.host_port.host())))
{
LOG_INFO(&Poco::Util::Application::instance().logger(), "Replica with address " << address.host_port.toString() << " will be processed as local.");
return true;
}
return false;
}
StoragePtr StorageDistributed::create( StoragePtr StorageDistributed::create(
const std::string & name_, const std::string & name_,
NamesAndTypesListPtr columns_, NamesAndTypesListPtr columns_,
@ -122,6 +170,18 @@ BlockInputStreams StorageDistributed::read(
for (ConnectionPools::iterator it = pools.begin(); it != pools.end(); ++it) for (ConnectionPools::iterator it = pools.begin(); it != pools.end(); ++it)
res.push_back(new RemoteBlockInputStream((*it)->get(), modified_query, &new_settings, processed_stage)); res.push_back(new RemoteBlockInputStream((*it)->get(), modified_query, &new_settings, processed_stage));
/// добавляем запросы к локальному clickhouse
{
DB::Context new_context = context;
new_context.setSettings(new_settings);
for(size_t i = 0; i < local_replics_num; ++i)
{
InterpreterSelectQuery interpreter(modified_query_ast, new_context, processed_stage);
res.push_back(interpreter.execute());
}
}
return res; return res;
} }