2021-02-17 17:58:04 +00:00
|
|
|
#include <algorithm>
|
2021-02-24 11:55:16 +00:00
|
|
|
#include <ctime>
|
2021-02-17 17:58:04 +00:00
|
|
|
#include <random>
|
2021-02-24 11:55:16 +00:00
|
|
|
#include <thread>
|
2013-09-05 11:44:10 +00:00
|
|
|
#include <mysqlxx/PoolWithFailover.h>
|
2017-07-27 21:07:41 +00:00
|
|
|
|
|
|
|
|
2021-03-27 20:49:26 +00:00
|
|
|
/// Duplicate of code from StringUtils.h. Copied here for less dependencies.
|
|
|
|
static bool startsWith(const std::string & s, const char * prefix)
|
|
|
|
{
|
|
|
|
return s.size() >= strlen(prefix) && 0 == memcmp(s.data(), prefix, strlen(prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-05 11:44:10 +00:00
|
|
|
using namespace mysqlxx;
|
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
PoolWithFailover::PoolWithFailover(
|
|
|
|
const Poco::Util::AbstractConfiguration & config_,
|
|
|
|
const std::string & config_name_,
|
|
|
|
const unsigned default_connections_,
|
|
|
|
const unsigned max_connections_,
|
|
|
|
const size_t max_tries_)
|
2020-05-09 22:59:34 +00:00
|
|
|
: max_tries(max_tries_)
|
2021-09-01 21:52:38 +00:00
|
|
|
, shareable(config_.getBool(config_name_ + ".share_connection", false))
|
|
|
|
, wait_timeout(UINT64_MAX)
|
2013-09-05 11:44:10 +00:00
|
|
|
{
|
2020-05-09 22:59:34 +00:00
|
|
|
if (config_.has(config_name_ + ".replica"))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys replica_keys;
|
2020-05-09 22:59:34 +00:00
|
|
|
config_.keys(config_name_, replica_keys);
|
2017-07-27 20:22:53 +00:00
|
|
|
for (const auto & replica_config_key : replica_keys)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-27 21:07:41 +00:00
|
|
|
/// There could be another elements in the same level in configuration file, like "password", "port"...
|
|
|
|
if (startsWith(replica_config_key, "replica"))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-05-09 22:59:34 +00:00
|
|
|
std::string replica_name = config_name_ + "." + replica_config_key;
|
2017-07-27 20:40:32 +00:00
|
|
|
|
2020-05-09 22:59:34 +00:00
|
|
|
int priority = config_.getInt(replica_name + ".priority", 0);
|
2017-07-27 20:40:32 +00:00
|
|
|
|
|
|
|
replicas_by_priority[priority].emplace_back(
|
2020-05-09 22:59:34 +00:00
|
|
|
std::make_shared<Pool>(config_, replica_name, default_connections_, max_connections_, config_name_.c_str()));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 17:58:04 +00:00
|
|
|
|
2021-02-17 18:51:05 +00:00
|
|
|
/// PoolWithFailover objects are stored in a cache inside PoolFactory.
|
|
|
|
/// This cache is reset by ExternalDictionariesLoader after every SYSTEM RELOAD DICTIONAR{Y|IES}
|
|
|
|
/// which triggers massive re-constructing of connection pools.
|
2021-02-24 08:48:04 +00:00
|
|
|
/// The state of PRNGs like std::mt19937 is considered to be quite heavy
|
2021-02-17 18:51:05 +00:00
|
|
|
/// thus here we attempt to optimize its construction.
|
2021-02-24 11:55:16 +00:00
|
|
|
static thread_local std::mt19937 rnd_generator(
|
|
|
|
std::hash<std::thread::id>{}(std::this_thread::get_id()) + std::clock());
|
2021-02-17 17:58:04 +00:00
|
|
|
for (auto & [_, replicas] : replicas_by_priority)
|
|
|
|
{
|
|
|
|
if (replicas.size() > 1)
|
|
|
|
std::shuffle(replicas.begin(), replicas.end(), rnd_generator);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-27 20:40:32 +00:00
|
|
|
replicas_by_priority[0].emplace_back(
|
2020-05-09 22:59:34 +00:00
|
|
|
std::make_shared<Pool>(config_, config_name_, default_connections_, max_connections_));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-02-05 18:47:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
|
|
|
|
PoolWithFailover::PoolWithFailover(
|
|
|
|
const std::string & config_name_,
|
|
|
|
const unsigned default_connections_,
|
|
|
|
const unsigned max_connections_,
|
|
|
|
const size_t max_tries_)
|
|
|
|
: PoolWithFailover{Poco::Util::Application::instance().config(),
|
|
|
|
config_name_, default_connections_, max_connections_, max_tries_}
|
2020-05-09 22:59:34 +00:00
|
|
|
{
|
|
|
|
}
|
2015-02-05 18:47:45 +00:00
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
|
|
|
|
PoolWithFailover::PoolWithFailover(
|
|
|
|
const std::string & database,
|
2021-04-01 10:27:24 +00:00
|
|
|
const RemoteDescription & addresses,
|
2021-03-24 18:15:31 +00:00
|
|
|
const std::string & user,
|
|
|
|
const std::string & password,
|
2021-05-15 04:40:43 +00:00
|
|
|
unsigned default_connections_,
|
|
|
|
unsigned max_connections_,
|
2021-09-01 21:52:38 +00:00
|
|
|
size_t max_tries_,
|
2021-12-13 22:06:46 +00:00
|
|
|
uint64_t wait_timeout_,
|
|
|
|
size_t connect_timeout_,
|
|
|
|
size_t rw_timeout_)
|
2021-03-24 18:15:31 +00:00
|
|
|
: max_tries(max_tries_)
|
2021-03-28 12:45:49 +00:00
|
|
|
, shareable(false)
|
2021-09-01 21:52:38 +00:00
|
|
|
, wait_timeout(wait_timeout_)
|
2021-03-24 18:15:31 +00:00
|
|
|
{
|
2021-04-01 10:27:24 +00:00
|
|
|
/// Replicas have the same priority, but traversed replicas are moved to the end of the queue.
|
|
|
|
for (const auto & [host, port] : addresses)
|
2021-03-24 18:15:31 +00:00
|
|
|
{
|
2021-05-15 04:40:43 +00:00
|
|
|
replicas_by_priority[0].emplace_back(std::make_shared<Pool>(database,
|
|
|
|
host, user, password, port,
|
|
|
|
/* socket_ = */ "",
|
2021-12-13 22:06:46 +00:00
|
|
|
connect_timeout_,
|
|
|
|
rw_timeout_,
|
2021-05-15 04:40:43 +00:00
|
|
|
default_connections_,
|
|
|
|
max_connections_));
|
2021-03-24 18:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-05 18:47:45 +00:00
|
|
|
PoolWithFailover::PoolWithFailover(const PoolWithFailover & other)
|
2021-03-24 18:15:31 +00:00
|
|
|
: max_tries{other.max_tries}
|
|
|
|
, shareable{other.shareable}
|
2021-09-01 21:52:38 +00:00
|
|
|
, wait_timeout(other.wait_timeout)
|
2015-02-05 18:47:45 +00:00
|
|
|
{
|
2019-10-08 17:27:00 +00:00
|
|
|
if (shareable)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-10-08 17:27:00 +00:00
|
|
|
replicas_by_priority = other.replicas_by_priority;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto & priority_replicas : other.replicas_by_priority)
|
|
|
|
{
|
|
|
|
Replicas 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));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2013-09-05 11:44:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
PoolWithFailover::Entry PoolWithFailover::get()
|
2013-09-05 11:44:10 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Util::Application & app = Poco::Util::Application::instance();
|
2017-07-27 21:07:41 +00:00
|
|
|
std::lock_guard<std::mutex> locker(mutex);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-27 20:22:53 +00:00
|
|
|
/// If we cannot connect to some replica due to pool overflow, than we will wait and connect.
|
2017-07-27 20:52:14 +00:00
|
|
|
PoolPtr * full_pool = nullptr;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t try_no = 0; try_no < max_tries; ++try_no)
|
|
|
|
{
|
|
|
|
full_pool = nullptr;
|
2021-08-27 18:07:10 +00:00
|
|
|
|
2017-07-27 20:40:32 +00:00
|
|
|
for (auto & priority_replicas : replicas_by_priority)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-27 20:40:32 +00:00
|
|
|
Replicas & replicas = priority_replicas.second;
|
|
|
|
for (size_t i = 0, size = replicas.size(); i < size; ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-27 20:52:14 +00:00
|
|
|
PoolPtr & pool = replicas[i];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-09-01 21:52:38 +00:00
|
|
|
Entry entry = shareable ? pool->get(wait_timeout) : pool->tryGet();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!entry.isNull())
|
|
|
|
{
|
2017-07-27 20:22:53 +00:00
|
|
|
/// Move all traversed replicas to the end of queue.
|
|
|
|
/// (No need to move replicas with another priority)
|
2017-04-01 07:20:54 +00:00
|
|
|
std::rotate(replicas.begin(), replicas.begin() + i + 1, replicas.end());
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
2019-02-23 15:10:14 +00:00
|
|
|
if (e.displayText().find("mysqlxx::Pool is full") != std::string::npos) /// NOTE: String comparison is trashy code.
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-27 20:52:14 +00:00
|
|
|
full_pool = &pool;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 20:52:14 +00:00
|
|
|
app.logger().warning("Connection to " + pool->getDescription() + " failed: " + e.displayText());
|
2017-04-01 07:20:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-27 20:52:14 +00:00
|
|
|
app.logger().warning("Connection to " + pool->getDescription() + " failed.");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 19:35:04 +00:00
|
|
|
app.logger().error("Connection to all replicas failed " + std::to_string(try_no + 1) + " times");
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (full_pool)
|
|
|
|
{
|
2017-07-27 20:52:14 +00:00
|
|
|
app.logger().error("All connections failed, trying to wait on a full pool " + (*full_pool)->getDescription());
|
2021-09-01 21:52:38 +00:00
|
|
|
return (*full_pool)->get(wait_timeout);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream message;
|
|
|
|
message << "Connections to all replicas failed: ";
|
2017-07-27 20:52:14 +00:00
|
|
|
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();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
throw Poco::Exception(message.str());
|
2013-09-05 11:44:10 +00:00
|
|
|
}
|