Miscellaneous [#CLICKHOUSE-2].

This commit is contained in:
Alexey Milovidov 2017-07-27 23:52:14 +03:00
parent 3355a1a7fe
commit 5bff1e0bfc
2 changed files with 19 additions and 31 deletions

View File

@ -66,18 +66,8 @@ namespace mysqlxx
{
private:
using PoolPtr = std::shared_ptr<Pool>;
using Replicas = std::vector<PoolPtr>;
struct Replica
{
PoolPtr pool;
int priority = 0;
Replica() {}
Replica(PoolPtr pool_, int priority_)
: pool(pool_), priority(priority_) {}
};
using Replicas = std::vector<Replica>;
/// [priority][index] -> replica.
using ReplicasByPriority = std::map<int, Replicas>;

View File

@ -22,16 +22,14 @@ PoolWithFailover::PoolWithFailover(const Poco::Util::AbstractConfiguration & cfg
int priority = cfg.getInt(replica_name + ".priority", 0);
replicas_by_priority[priority].emplace_back(
std::make_shared<Pool>(cfg, replica_name, default_connections, max_connections, config_name.c_str()),
priority);
std::make_shared<Pool>(cfg, replica_name, default_connections, max_connections, config_name.c_str()));
}
}
}
else
{
replicas_by_priority[0].emplace_back(
std::make_shared<Pool>(cfg, config_name, default_connections, max_connections),
0);
std::make_shared<Pool>(cfg, config_name, default_connections, max_connections));
}
}
@ -45,13 +43,13 @@ PoolWithFailover::PoolWithFailover(const std::string & config_name, const unsign
PoolWithFailover::PoolWithFailover(const PoolWithFailover & other)
: max_tries{other.max_tries}
{
for (const auto & replica_with_priority : other.replicas_by_priority)
for (const auto & priority_replicas : other.replicas_by_priority)
{
Replicas replicas;
replicas.reserve(replica_with_priority.second.size());
for (const auto & replica : replica_with_priority.second)
replicas.emplace_back(std::make_shared<Pool>(*replica.pool), replica.priority);
replicas_by_priority.emplace(replica_with_priority.first, std::move(replicas));
replicas.reserve(priority_replicas.second.size());
for (const auto & pool : priority_replicas.second)
replicas.emplace_back(std::make_shared<Pool>(*pool));
replicas_by_priority.emplace(priority_replicas.first, std::move(replicas));
}
}
@ -61,7 +59,7 @@ PoolWithFailover::Entry PoolWithFailover::Get()
Poco::Util::Application & app = Poco::Util::Application::instance();
/// If we cannot connect to some replica due to pool overflow, than we will wait and connect.
Replica * full_pool = nullptr;
PoolPtr * full_pool = nullptr;
for (size_t try_no = 0; try_no < max_tries; ++try_no)
{
@ -72,11 +70,11 @@ PoolWithFailover::Entry PoolWithFailover::Get()
Replicas & replicas = priority_replicas.second;
for (size_t i = 0, size = replicas.size(); i < size; ++i)
{
Replica & replica = replicas[i];
PoolPtr & pool = replicas[i];
try
{
Entry entry = replica.pool->tryGet();
Entry entry = pool->tryGet();
if (!entry.isNull())
{
@ -91,14 +89,14 @@ PoolWithFailover::Entry PoolWithFailover::Get()
{
if (e.displayText() == "mysqlxx::Pool is full") /// NOTE: String comparison is trashy code.
{
full_pool = &replica;
full_pool = &pool;
}
app.logger().warning("Connection to " + replica.pool->getDescription() + " failed: " + e.displayText());
app.logger().warning("Connection to " + pool->getDescription() + " failed: " + e.displayText());
continue;
}
app.logger().warning("Connection to " + replica.pool->getDescription() + " failed.");
app.logger().warning("Connection to " + pool->getDescription() + " failed.");
}
}
@ -107,15 +105,15 @@ PoolWithFailover::Entry PoolWithFailover::Get()
if (full_pool)
{
app.logger().error("All connections failed, trying to wait on a full pool " + full_pool->pool->getDescription());
return full_pool->pool->Get();
app.logger().error("All connections failed, trying to wait on a full pool " + (*full_pool)->getDescription());
return (*full_pool)->Get();
}
std::stringstream message;
message << "Connections to all replicas failed: ";
for (ReplicasByPriority::const_iterator it = replicas_by_priority.begin(); it != replicas_by_priority.end(); ++it)
for (Replicas::const_iterator jt = it->second.begin(); jt != it->second.end(); ++jt)
message << (it == replicas_by_priority.begin() && jt == it->second.begin() ? "" : ", ") << jt->pool->getDescription();
for (auto it = replicas_by_priority.begin(); it != replicas_by_priority.end(); ++it)
for (auto jt = it->second.begin(); jt != it->second.end(); ++jt)
message << (it == replicas_by_priority.begin() && jt == it->second.begin() ? "" : ", ") << (*jt)->getDescription();
throw Poco::Exception(message.str());
}