2013-04-04 14:39:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Pool.h"
|
|
|
|
|
|
|
|
|
2017-07-27 20:22:53 +00:00
|
|
|
#define MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_START_CONNECTIONS 1
|
|
|
|
#define MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_CONNECTIONS 16
|
|
|
|
#define MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES 3
|
2013-04-18 10:23:40 +00:00
|
|
|
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
namespace mysqlxx
|
|
|
|
{
|
2017-07-27 20:22:53 +00:00
|
|
|
/** MySQL connection pool with support of failover.
|
2021-03-24 18:15:31 +00:00
|
|
|
*
|
|
|
|
* For dictionary source:
|
2017-07-27 20:22:53 +00:00
|
|
|
* Have information about replicas and their priorities.
|
|
|
|
* Tries to connect to replica in an order of priority. When equal priority, choose replica with maximum time without connections.
|
|
|
|
*
|
|
|
|
* It could be configured without replicas, exactly as ordinary Pool:
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
|
|
|
* <mysql_metrica>
|
|
|
|
* <host>mtstat01c*</host>
|
|
|
|
* <port>3306</port>
|
|
|
|
* <user>metrica</user>
|
|
|
|
* <password></password>
|
|
|
|
* <db>Metrica</db>
|
|
|
|
* </mysql_metrica>
|
|
|
|
*
|
2017-07-27 20:22:53 +00:00
|
|
|
* Or like this:
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
|
|
|
* <mysql_metrica>
|
|
|
|
* <replica>
|
|
|
|
* <host>mtstat01c</host>
|
|
|
|
* <port>3306</port>
|
|
|
|
* <user>metrica</user>
|
|
|
|
* <password></password>
|
|
|
|
* <db>Metrica</db>
|
|
|
|
* <priority>0</priority>
|
|
|
|
* </replica>
|
|
|
|
* <replica>
|
|
|
|
* <host>mtstat01d</host>
|
|
|
|
* <port>3306</port>
|
|
|
|
* <user>metrica</user>
|
|
|
|
* <password></password>
|
|
|
|
* <db>Metrica</db>
|
|
|
|
* <priority>1</priority>
|
|
|
|
* </replica>
|
|
|
|
* </mysql_metrica>
|
|
|
|
*
|
2017-07-27 20:22:53 +00:00
|
|
|
* Or like this:
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
|
|
|
* <mysql_metrica>
|
2017-07-27 20:22:53 +00:00
|
|
|
* <port>3306</port>
|
2017-04-01 07:20:54 +00:00
|
|
|
* <user>metrica</user>
|
|
|
|
* <password></password>
|
|
|
|
* <db>Metrica</db>
|
2017-07-27 20:22:53 +00:00
|
|
|
* <replica>
|
2017-04-01 07:20:54 +00:00
|
|
|
* <host>mtstat01c</host>
|
|
|
|
* <priority>0</priority>
|
|
|
|
* </replica>
|
|
|
|
* <replica>
|
|
|
|
* <host>mtstat01d</host>
|
|
|
|
* <priority>1</priority>
|
|
|
|
* </replica>
|
|
|
|
* </mysql_metrica>
|
|
|
|
*/
|
|
|
|
class PoolWithFailover final
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
using PoolPtr = std::shared_ptr<Pool>;
|
2017-07-27 20:52:14 +00:00
|
|
|
using Replicas = std::vector<PoolPtr>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-03-28 10:27:37 +00:00
|
|
|
/// [priority][index] -> replica. Highest priority is 0.
|
2017-04-01 07:20:54 +00:00
|
|
|
using ReplicasByPriority = std::map<int, Replicas>;
|
|
|
|
ReplicasByPriority replicas_by_priority;
|
|
|
|
|
2017-07-27 20:22:53 +00:00
|
|
|
/// Number of connection tries.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t max_tries;
|
2017-07-27 20:22:53 +00:00
|
|
|
/// Mutex for set of replicas.
|
2017-04-01 07:20:54 +00:00
|
|
|
std::mutex mutex;
|
2019-10-08 17:27:00 +00:00
|
|
|
/// Can the Pool be shared
|
|
|
|
bool shareable;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
using Entry = Pool::Entry;
|
2021-04-01 10:27:24 +00:00
|
|
|
using RemoteDescription = std::vector<std::pair<std::string, uint16_t>>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-24 18:15:31 +00:00
|
|
|
* * Mysql dictionary sourse related params:
|
|
|
|
* config_name Name of parameter in configuration file for dictionary source.
|
|
|
|
*
|
|
|
|
* * Mysql storage related parameters:
|
|
|
|
* replicas_description
|
|
|
|
*
|
|
|
|
* * Mutual parameters:
|
2017-07-27 20:22:53 +00:00
|
|
|
* default_connections Number of connection in pool to each replica at start.
|
|
|
|
* max_connections Maximum number of connections in pool to each replica.
|
|
|
|
* max_tries_ Max number of connection tries.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2021-03-24 18:15:31 +00:00
|
|
|
PoolWithFailover(
|
|
|
|
const std::string & config_name_,
|
2020-05-09 22:59:34 +00:00
|
|
|
unsigned default_connections_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_START_CONNECTIONS,
|
|
|
|
unsigned max_connections_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_CONNECTIONS,
|
|
|
|
size_t max_tries_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
PoolWithFailover(
|
|
|
|
const Poco::Util::AbstractConfiguration & config_,
|
2020-05-09 22:59:34 +00:00
|
|
|
const std::string & config_name_,
|
|
|
|
unsigned default_connections_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_START_CONNECTIONS,
|
|
|
|
unsigned max_connections_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_CONNECTIONS,
|
|
|
|
size_t max_tries_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-03-24 18:15:31 +00:00
|
|
|
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_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_START_CONNECTIONS,
|
|
|
|
unsigned max_connections_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_CONNECTIONS,
|
2021-03-28 10:27:37 +00:00
|
|
|
size_t max_tries_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES);
|
2021-03-24 18:15:31 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
PoolWithFailover(const PoolWithFailover & other);
|
|
|
|
|
2017-07-27 20:22:53 +00:00
|
|
|
/** Allocates a connection to use. */
|
2020-03-23 02:12:31 +00:00
|
|
|
Entry get();
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2021-03-27 14:35:44 +00:00
|
|
|
|
|
|
|
using PoolWithFailoverPtr = std::shared_ptr<PoolWithFailover>;
|
2013-04-04 14:39:59 +00:00
|
|
|
}
|