2013-04-04 14:39:59 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "Pool.h"
|
|
|
|
|
|
|
|
|
|
|
2013-04-18 10:23:40 +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-04 14:39:59 +00:00
|
|
|
|
namespace mysqlxx
|
|
|
|
|
{
|
|
|
|
|
/** Пул соединений с MySQL.
|
|
|
|
|
* Знает о наборе реплик с приоритетами.
|
|
|
|
|
* Пробует соединяться с репликами в порядке приоритета. При равном приоритете предпочитается реплика, к которой дольше всего не было попытки подключения.
|
2013-09-04 15:29:02 +00:00
|
|
|
|
*
|
2013-04-04 14:39:59 +00:00
|
|
|
|
* Использование аналогично mysqlxx::Pool. В конфиге задание сервера может выглядеть так же, как для Pool:
|
|
|
|
|
* <mysql_metrica>
|
|
|
|
|
* <host>mtstat01c*</host>
|
|
|
|
|
* <port>3306</port>
|
|
|
|
|
* <user>metrica</user>
|
|
|
|
|
* <password></password>
|
|
|
|
|
* <db>Metrica</db>
|
|
|
|
|
* </mysql_metrica>
|
2013-09-04 15:29:02 +00:00
|
|
|
|
*
|
2013-04-04 14:39:59 +00:00
|
|
|
|
* или так:
|
2013-09-04 15:29:02 +00:00
|
|
|
|
*
|
2013-04-04 14:39:59 +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>
|
2013-09-04 15:29:02 +00:00
|
|
|
|
*
|
|
|
|
|
* или так:
|
|
|
|
|
*
|
|
|
|
|
* <mysql_metrica>
|
|
|
|
|
* <port>3306</port>
|
|
|
|
|
* <user>metrica</user>
|
|
|
|
|
* <password></password>
|
|
|
|
|
* <db>Metrica</db>
|
2013-12-06 17:29:01 +00:00
|
|
|
|
* <replica>
|
2013-09-04 15:29:02 +00:00
|
|
|
|
* <host>mtstat01c</host>
|
|
|
|
|
* <priority>0</priority>
|
2013-12-06 17:29:01 +00:00
|
|
|
|
* </replica>
|
|
|
|
|
* <replica>
|
2013-09-04 15:29:02 +00:00
|
|
|
|
* <host>mtstat01d</host>
|
|
|
|
|
* <priority>1</priority>
|
|
|
|
|
* </replica>
|
|
|
|
|
* </mysql_metrica>
|
2013-04-04 14:39:59 +00:00
|
|
|
|
*/
|
|
|
|
|
class PoolWithFailover
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
typedef Poco::SharedPtr<Pool> PoolPtr;
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
struct Replica
|
|
|
|
|
{
|
|
|
|
|
PoolPtr pool;
|
|
|
|
|
int priority;
|
|
|
|
|
int error_count;
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
Replica() : priority(0), error_count(0) {}
|
|
|
|
|
Replica(PoolPtr pool_, int priority_)
|
|
|
|
|
: pool(pool_), priority(priority_), error_count(0) {}
|
|
|
|
|
};
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
typedef std::vector<Replica> Replicas;
|
|
|
|
|
/// [приоритет][номер] -> реплика.
|
|
|
|
|
typedef std::map<int, Replicas> ReplicasByPriority;
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
ReplicasByPriority replicas_by_priority;
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-18 10:23:40 +00:00
|
|
|
|
/// Количество попыток подключения.
|
|
|
|
|
size_t max_tries;
|
2013-04-04 14:39:59 +00:00
|
|
|
|
/// Mutex для доступа к списку реплик.
|
|
|
|
|
Poco::FastMutex mutex;
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
public:
|
|
|
|
|
typedef Pool::Entry Entry;
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* @param config_name Имя параметра в конфигурационном файле.
|
2013-04-18 10:23:40 +00:00
|
|
|
|
* @param default_connections Количество подключений по умолчанию к какждой реплике.
|
|
|
|
|
* @param max_connections Максимальное количество подключений к какждой реплике.
|
|
|
|
|
* @param max_tries_ Количество попыток подключения.
|
2013-04-04 14:39:59 +00:00
|
|
|
|
*/
|
|
|
|
|
PoolWithFailover(const std::string & config_name,
|
2013-04-18 10:23:40 +00:00
|
|
|
|
unsigned default_connections = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_START_CONNECTIONS,
|
|
|
|
|
unsigned max_connections = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_CONNECTIONS,
|
2013-09-05 11:44:10 +00:00
|
|
|
|
size_t max_tries_ = MYSQLXX_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES);
|
2013-09-04 15:29:02 +00:00
|
|
|
|
|
2013-04-04 14:39:59 +00:00
|
|
|
|
/** Выделяет соединение для работы. */
|
2013-09-05 11:44:10 +00:00
|
|
|
|
Entry Get();
|
2013-04-04 14:39:59 +00:00
|
|
|
|
};
|
|
|
|
|
}
|