2019-09-24 22:28:59 +00:00
|
|
|
#if __has_include(<mysql.h>)
|
|
|
|
#include <mysql.h>
|
|
|
|
#include <mysqld_error.h>
|
2018-07-25 11:29:26 +00:00
|
|
|
#else
|
2017-01-24 21:07:08 +00:00
|
|
|
#include <mysql/mysql.h>
|
|
|
|
#include <mysql/mysqld_error.h>
|
2018-07-25 11:29:26 +00:00
|
|
|
#endif
|
2017-04-19 00:25:57 +00:00
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
#include <mysqlxx/Pool.h>
|
|
|
|
|
2019-07-10 20:47:39 +00:00
|
|
|
#include <common/sleep.h>
|
2019-05-19 09:14:23 +00:00
|
|
|
|
2017-01-13 21:24:59 +00:00
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
|
|
|
|
namespace mysqlxx
|
|
|
|
{
|
|
|
|
|
|
|
|
void Pool::Entry::incrementRefCount()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!data)
|
|
|
|
return;
|
2020-10-08 17:45:18 +00:00
|
|
|
/// First reference, initialize thread
|
|
|
|
if (data->ref_count.fetch_add(1) == 0)
|
2020-02-27 09:34:06 +00:00
|
|
|
mysql_thread_init();
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 20:36:10 +00:00
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
void Pool::Entry::decrementRefCount()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!data)
|
|
|
|
return;
|
2020-10-08 17:45:18 +00:00
|
|
|
|
2020-10-08 17:46:59 +00:00
|
|
|
/// We were the last user of this thread, deinitialize it
|
2020-10-08 17:45:18 +00:00
|
|
|
if (data->ref_count.fetch_sub(1) == 1)
|
|
|
|
mysql_thread_end();
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-13 21:18:07 +00:00
|
|
|
Pool::Pool(const Poco::Util::AbstractConfiguration & cfg, const std::string & config_name,
|
2017-04-01 07:20:54 +00:00
|
|
|
unsigned default_connections_, unsigned max_connections_,
|
|
|
|
const char * parent_config_name_)
|
2021-02-27 08:18:28 +00:00
|
|
|
: logger(Poco::Logger::get("mysqlxx::Pool"))
|
|
|
|
, default_connections(default_connections_)
|
|
|
|
, max_connections(max_connections_)
|
2017-01-13 21:18:07 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
server = cfg.getString(config_name + ".host");
|
|
|
|
|
|
|
|
if (parent_config_name_)
|
|
|
|
{
|
|
|
|
const std::string parent_config_name(parent_config_name_);
|
|
|
|
db = cfg.getString(config_name + ".db", cfg.getString(parent_config_name + ".db", ""));
|
|
|
|
user = cfg.has(config_name + ".user")
|
|
|
|
? cfg.getString(config_name + ".user")
|
|
|
|
: cfg.getString(parent_config_name + ".user");
|
|
|
|
password = cfg.has(config_name + ".password")
|
|
|
|
? cfg.getString(config_name + ".password")
|
|
|
|
: cfg.getString(parent_config_name + ".password");
|
2017-07-14 12:45:32 +00:00
|
|
|
|
|
|
|
if (!cfg.has(config_name + ".port") && !cfg.has(config_name + ".socket")
|
|
|
|
&& !cfg.has(parent_config_name + ".port") && !cfg.has(parent_config_name + ".socket"))
|
|
|
|
throw Poco::Exception("mysqlxx::Pool configuration: expected port or socket");
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
port = cfg.has(config_name + ".port")
|
|
|
|
? cfg.getInt(config_name + ".port")
|
2017-07-14 12:45:32 +00:00
|
|
|
: cfg.getInt(parent_config_name + ".port", 0);
|
|
|
|
socket = cfg.has(config_name + ".socket")
|
|
|
|
? cfg.getString(config_name + ".socket")
|
|
|
|
: cfg.getString(parent_config_name + ".socket", "");
|
2017-09-09 21:26:02 +00:00
|
|
|
ssl_ca = cfg.has(config_name + ".ssl_ca")
|
|
|
|
? cfg.getString(config_name + ".ssl_ca")
|
|
|
|
: cfg.getString(parent_config_name + ".ssl_ca", "");
|
|
|
|
ssl_cert = cfg.has(config_name + ".ssl_cert")
|
|
|
|
? cfg.getString(config_name + ".ssl_cert")
|
|
|
|
: cfg.getString(parent_config_name + ".ssl_cert", "");
|
|
|
|
ssl_key = cfg.has(config_name + ".ssl_key")
|
|
|
|
? cfg.getString(config_name + ".ssl_key")
|
|
|
|
: cfg.getString(parent_config_name + ".ssl_key", "");
|
2018-10-01 15:55:21 +00:00
|
|
|
|
|
|
|
enable_local_infile = cfg.getBool(config_name + ".enable_local_infile",
|
|
|
|
cfg.getBool(parent_config_name + ".enable_local_infile", MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE));
|
2021-02-26 06:49:49 +00:00
|
|
|
|
|
|
|
opt_reconnect = cfg.getBool(config_name + ".opt_reconnect",
|
|
|
|
cfg.getBool(parent_config_name + ".opt_reconnect", MYSQLXX_DEFAULT_MYSQL_OPT_RECONNECT));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
db = cfg.getString(config_name + ".db", "");
|
|
|
|
user = cfg.getString(config_name + ".user");
|
|
|
|
password = cfg.getString(config_name + ".password");
|
2017-07-14 12:45:32 +00:00
|
|
|
|
|
|
|
if (!cfg.has(config_name + ".port") && !cfg.has(config_name + ".socket"))
|
|
|
|
throw Poco::Exception("mysqlxx::Pool configuration: expected port or socket");
|
|
|
|
|
|
|
|
port = cfg.getInt(config_name + ".port", 0);
|
|
|
|
socket = cfg.getString(config_name + ".socket", "");
|
2017-09-09 21:26:02 +00:00
|
|
|
ssl_ca = cfg.getString(config_name + ".ssl_ca", "");
|
|
|
|
ssl_cert = cfg.getString(config_name + ".ssl_cert", "");
|
|
|
|
ssl_key = cfg.getString(config_name + ".ssl_key", "");
|
2018-10-01 15:55:21 +00:00
|
|
|
|
|
|
|
enable_local_infile = cfg.getBool(
|
|
|
|
config_name + ".enable_local_infile", MYSQLXX_DEFAULT_ENABLE_LOCAL_INFILE);
|
2021-02-26 06:49:49 +00:00
|
|
|
|
|
|
|
opt_reconnect = cfg.getBool(config_name + ".opt_reconnect", MYSQLXX_DEFAULT_MYSQL_OPT_RECONNECT);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect_timeout = cfg.getInt(config_name + ".connect_timeout",
|
|
|
|
cfg.getInt("mysql_connect_timeout",
|
|
|
|
MYSQLXX_DEFAULT_TIMEOUT));
|
|
|
|
|
|
|
|
rw_timeout =
|
|
|
|
cfg.getInt(config_name + ".rw_timeout",
|
|
|
|
cfg.getInt("mysql_rw_timeout",
|
|
|
|
MYSQLXX_DEFAULT_RW_TIMEOUT));
|
2017-01-13 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
Pool::~Pool()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2017-01-13 20:37:37 +00:00
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
for (auto & connection : connections)
|
|
|
|
delete static_cast<Connection *>(connection);
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
Pool::Entry Pool::get()
|
2017-01-13 20:37:37 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
for (;;)
|
|
|
|
{
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.trace("(%s): Iterating through existing MySQL connections", getDescription());
|
|
|
|
|
2017-09-07 21:04:48 +00:00
|
|
|
for (auto & connection : connections)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-09-07 21:04:48 +00:00
|
|
|
if (connection->ref_count == 0)
|
|
|
|
return Entry(connection, this);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.trace("(%s): Trying to allocate a new connection.", getDescription());
|
2017-04-01 07:20:54 +00:00
|
|
|
if (connections.size() < static_cast<size_t>(max_connections))
|
|
|
|
{
|
|
|
|
Connection * conn = allocConnection();
|
|
|
|
if (conn)
|
|
|
|
return Entry(conn, this);
|
2021-02-27 08:18:28 +00:00
|
|
|
|
|
|
|
logger.trace("(%s): Unable to create a new connection: Allocation failed.", getDescription());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logger.trace("(%s): Unable to create a new connection: Max number of connections has been reached.", getDescription());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lock.unlock();
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.trace("(%s): Sleeping for %d seconds.", getDescription(), MYSQLXX_POOL_SLEEP_ON_CONNECT_FAIL);
|
2019-07-10 20:47:39 +00:00
|
|
|
sleepForSeconds(MYSQLXX_POOL_SLEEP_ON_CONNECT_FAIL);
|
2017-04-01 07:20:54 +00:00
|
|
|
lock.lock();
|
|
|
|
}
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Pool::Entry Pool::tryGet()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
|
2020-12-03 20:36:10 +00:00
|
|
|
/// Try to pick an idle connection from already allocated
|
|
|
|
for (auto connection_it = connections.cbegin(); connection_it != connections.cend();)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-12-03 20:36:10 +00:00
|
|
|
Connection * connection_ptr = *connection_it;
|
|
|
|
/// Fixme: There is a race condition here b/c we do not synchronize with Pool::Entry's copy-assignment operator
|
|
|
|
if (connection_ptr->ref_count == 0)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2021-03-04 14:34:05 +00:00
|
|
|
{
|
|
|
|
Entry res(connection_ptr, this);
|
|
|
|
if (res.tryForceConnected()) /// Tries to reestablish connection as well
|
|
|
|
return res;
|
|
|
|
}
|
2020-12-03 20:36:10 +00:00
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.debug("(%s): Idle connection to MySQL server cannot be recovered, dropping it.", getDescription());
|
2020-12-03 20:36:10 +00:00
|
|
|
|
|
|
|
/// This one is disconnected, cannot be reestablished and so needs to be disposed of.
|
|
|
|
connection_it = connections.erase(connection_it);
|
|
|
|
::delete connection_ptr; /// TODO: Manual memory management is awkward (matches allocConnection() method)
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2020-12-03 20:36:10 +00:00
|
|
|
else
|
|
|
|
++connection_it;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (connections.size() >= max_connections)
|
|
|
|
throw Poco::Exception("mysqlxx::Pool is full");
|
|
|
|
|
2020-12-03 20:36:10 +00:00
|
|
|
Connection * connection_ptr = allocConnection(true);
|
|
|
|
if (connection_ptr)
|
|
|
|
return {connection_ptr, this};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-12-03 20:36:10 +00:00
|
|
|
return {};
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 20:36:10 +00:00
|
|
|
|
2020-02-27 09:34:06 +00:00
|
|
|
void Pool::removeConnection(Connection* connection)
|
2019-05-24 14:44:32 +00:00
|
|
|
{
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.trace("(%s): Removing connection.", getDescription());
|
|
|
|
|
2020-02-27 09:34:06 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
if (connection)
|
2019-05-24 14:44:32 +00:00
|
|
|
{
|
2020-02-27 09:34:06 +00:00
|
|
|
if (connection->ref_count > 0)
|
|
|
|
{
|
|
|
|
connection->conn.disconnect();
|
|
|
|
connection->ref_count = 0;
|
|
|
|
}
|
|
|
|
connections.remove(connection);
|
2019-05-24 14:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-27 09:34:06 +00:00
|
|
|
void Pool::Entry::disconnect()
|
|
|
|
{
|
|
|
|
pool->removeConnection(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-13 21:24:59 +00:00
|
|
|
void Pool::Entry::forceConnected() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (data == nullptr)
|
|
|
|
throw Poco::RuntimeException("Tried to access NULL database connection.");
|
|
|
|
|
|
|
|
bool first = true;
|
2020-12-03 20:36:10 +00:00
|
|
|
while (!tryForceConnected())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
2019-07-10 20:47:39 +00:00
|
|
|
sleepForSeconds(MYSQLXX_POOL_SLEEP_ON_CONNECT_FAIL);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
pool->logger.debug("Entry: Reconnecting to MySQL server %s", pool->description);
|
2017-04-01 07:20:54 +00:00
|
|
|
data->conn.connect(
|
|
|
|
pool->db.c_str(),
|
2017-09-09 21:26:02 +00:00
|
|
|
pool->server.c_str(),
|
|
|
|
pool->user.c_str(),
|
|
|
|
pool->password.c_str(),
|
|
|
|
pool->port,
|
|
|
|
pool->socket.c_str(),
|
|
|
|
pool->ssl_ca.c_str(),
|
|
|
|
pool->ssl_cert.c_str(),
|
|
|
|
pool->ssl_key.c_str(),
|
|
|
|
pool->connect_timeout,
|
2018-10-01 15:55:21 +00:00
|
|
|
pool->rw_timeout,
|
2021-02-26 08:42:37 +00:00
|
|
|
pool->enable_local_infile,
|
|
|
|
pool->opt_reconnect);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2020-12-03 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Pool::Entry::tryForceConnected() const
|
|
|
|
{
|
|
|
|
auto * const mysql_driver = data->conn.getDriver();
|
|
|
|
const auto prev_connection_id = mysql_thread_id(mysql_driver);
|
2021-02-27 08:18:28 +00:00
|
|
|
|
|
|
|
pool->logger.trace("Entry(connection %lu): sending PING to check if it is alive.", prev_connection_id);
|
2020-12-03 20:36:10 +00:00
|
|
|
if (data->conn.ping()) /// Attempts to reestablish lost connection
|
|
|
|
{
|
|
|
|
const auto current_connection_id = mysql_thread_id(mysql_driver);
|
|
|
|
if (prev_connection_id != current_connection_id)
|
|
|
|
{
|
2021-02-27 08:18:28 +00:00
|
|
|
pool->logger.debug("Entry(connection %lu): Reconnected to MySQL server. Connection id changed: %lu -> %lu",
|
|
|
|
current_connection_id, prev_connection_id, current_connection_id);
|
2020-12-03 20:36:10 +00:00
|
|
|
}
|
2021-02-27 08:18:28 +00:00
|
|
|
|
|
|
|
pool->logger.trace("Entry(connection %lu): PING ok.", current_connection_id);
|
2020-12-03 20:36:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
pool->logger.trace("Entry(connection %lu): PING failed.", prev_connection_id);
|
2020-12-03 20:36:10 +00:00
|
|
|
return false;
|
2017-01-13 21:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
void Pool::initialize()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!initialized)
|
|
|
|
{
|
2018-08-20 19:35:04 +00:00
|
|
|
description = db + "@" + server + ":" + std::to_string(port) + " as user " + user;
|
2017-01-13 20:37:37 +00:00
|
|
|
|
2017-12-27 21:45:05 +00:00
|
|
|
for (unsigned i = 0; i < default_connections; ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
allocConnection();
|
2017-01-13 20:37:37 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-13 21:18:07 +00:00
|
|
|
Pool::Connection * Pool::allocConnection(bool dont_throw_if_failed_first_time)
|
2017-01-13 20:37:37 +00:00
|
|
|
{
|
2021-02-27 08:18:28 +00:00
|
|
|
std::unique_ptr<Connection> conn_ptr{new Connection};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.debug("Connecting to %s", description);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
conn_ptr->conn.connect(
|
2017-04-01 07:20:54 +00:00
|
|
|
db.c_str(),
|
2017-09-09 21:26:02 +00:00
|
|
|
server.c_str(),
|
|
|
|
user.c_str(),
|
|
|
|
password.c_str(),
|
|
|
|
port,
|
|
|
|
socket.c_str(),
|
|
|
|
ssl_ca.c_str(),
|
|
|
|
ssl_cert.c_str(),
|
|
|
|
ssl_key.c_str(),
|
|
|
|
connect_timeout,
|
2018-10-01 15:55:21 +00:00
|
|
|
rw_timeout,
|
2021-02-26 08:42:37 +00:00
|
|
|
enable_local_infile,
|
|
|
|
opt_reconnect);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (mysqlxx::ConnectionFailed & e)
|
|
|
|
{
|
2021-02-27 08:18:28 +00:00
|
|
|
logger.error(e.what());
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if ((!was_successful && !dont_throw_if_failed_first_time)
|
|
|
|
|| e.errnum() == ER_ACCESS_DENIED_ERROR
|
|
|
|
|| e.errnum() == ER_DBACCESS_DENIED_ERROR
|
|
|
|
|| e.errnum() == ER_BAD_DB_ERROR)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 08:18:28 +00:00
|
|
|
connections.push_back(conn_ptr.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
was_successful = true;
|
2021-02-27 08:18:28 +00:00
|
|
|
return conn_ptr.release();
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|