ClickHouse/libs/libmysqlxx/include/mysqlxx/PoolWithFailover.h

109 lines
3.5 KiB
C++
Raw Normal View History

#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
namespace mysqlxx
{
2017-07-27 20:22:53 +00:00
/** MySQL connection pool with support of failover.
* 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:
*
* <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:
*
* <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:
*
* <mysql_metrica>
2017-07-27 20:22:53 +00:00
* <port>3306</port>
* <user>metrica</user>
* <password></password>
* <db>Metrica</db>
2017-07-27 20:22:53 +00:00
* <replica>
* <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-07-27 20:22:53 +00:00
/// [priority][index] -> replica.
using ReplicasByPriority = std::map<int, Replicas>;
ReplicasByPriority replicas_by_priority;
2017-07-27 20:22:53 +00:00
/// Number of connection tries.
size_t max_tries;
2017-07-27 20:22:53 +00:00
/// Mutex for set of replicas.
std::mutex mutex;
public:
using Entry = Pool::Entry;
/**
2017-07-27 20:22:53 +00:00
* config_name Name of parameter in configuration file.
* 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.
*/
PoolWithFailover(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);
PoolWithFailover(const Poco::Util::AbstractConfiguration & config,
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);
PoolWithFailover(const PoolWithFailover & other);
PoolWithFailover & operator=(const PoolWithFailover &) = delete;
2017-07-27 20:22:53 +00:00
/** Allocates a connection to use. */
Entry Get();
};
}