acl/lib_acl_cpp/include/acl_cpp/redis/redis_cluster.hpp

146 lines
4.5 KiB
C++
Raw Normal View History

#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include <vector>
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/connpool/connect_manager.hpp"
namespace acl
{
class redis_pool;
2015-03-29 19:27:41 +08:00
/**
* redis <EFBFBD>ͻ<EFBFBD><EFBFBD>˼<EFBFBD>Ⱥ<EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis <EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(redis_command)<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>еĿͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>֧<EFBFBD>ּ<EFBFBD>Ⱥ<EFBFBD><EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD>
* redis client cluster class. The class's object is set in the redis_command
* using redis_command::set_cluster(redis_cluster*), and all the redis client
* command will support the redis cluster mode.
*/
class ACL_CPP_API redis_cluster : public connect_manager
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* constructor
* @param conn_timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>);
* timeout in seconds for connecting the redis-server
* @param rw_timeout {int}<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IO <EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>);
* read/write timeout in seconds from/to the redis-server
* @param max_slot {int} <EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ; the max hash-slot value of keys
*/
redis_cluster(int conn_timeout, int rw_timeout, int max_slot = 16384);
virtual ~redis_cluster();
/**
* <EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD>ϣ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>;
* get one connection pool with the given slot
* @param slot {int} <EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD>ֵ;
* the hash-slot value of key
* @return {redis_pool*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>Ĺ<EFBFBD>ϣ<EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD> NULL;
* return the connection pool of the hash-slot, and return NULL
* when the slot not exists
*/
redis_pool* peek_slot(int slot);
/**
* <EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD>ϣ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* dynamicly set redis-server addr with one slot, which is protected
* by thread mutex internal, no one will be set if the slot were
* beyyond the max hash-slot
* @param slot {int} <EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD>ֵ;
* the hash-slot
* @param addr {const char*} redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ;
* one redis-server addr
*/
void set_slot(int slot, const char* addr);
2015-03-01 23:50:53 +08:00
/**
* <EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>۶<EFBFBD>Ӧ<EFBFBD><EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ã<EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* dynamicly remove one slot and redis-server addr mapping, which is
* protected by thread mutex
* @param slot {int} <EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD>ֵ;
* hash-slot value
2015-03-01 23:50:53 +08:00
*/
void clear_slot(int slot);
/**
* <EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ;
* get the max hash-slot
* @return {int}
*/
int get_max_slot() const
{
return max_slot_;
}
2015-03-01 23:50:53 +08:00
//////////////////////////////////////////////////////////////////////
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ֵ<EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>ֵΪ 15;
* set redirect limit for MOVE/ASK, default is 15
* @param max {int} <EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>ֻ<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>ֵ > 0 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч;
* the redirect times limit for MOVE/ASK commands
2015-03-01 23:50:53 +08:00
*/
void set_redirect_max(int max);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD>ֵ;
* get redirect limit of MOVE/ASK commands in one redis redirect process
2015-03-01 23:50:53 +08:00
* @return {int}
*/
int get_redirect_max() const
{
return redirect_max_;
}
/**
2015-03-08 21:34:00 +08:00
* <EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> >= 2 ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>ֵΪ 100 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-03-01 23:50:53 +08:00
* <EFBFBD>ô<EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ
* ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD> redis.conf <EFBFBD>е<EFBFBD> cluster-node-timeout <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ȴ<EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* if redirect happenning more than 2 in one redis command process,
* the process can sleep for a one avoiding redirect too fast, you
* can set the waiting time with microsecond here, default value is
* 100 microseconds; this only happends when redis-server died.
* @param n {int} ÿ<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>ֵΪ 100 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* microseonds to sleep when redirect times are more than 2,
* default is 100 ms
2015-03-01 23:50:53 +08:00
*/
void set_redirect_sleep(int n);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> set_redirect_sleep <EFBFBD><EFBFBD><EFBFBD>õĻ<EFBFBD>Ĭ<EFBFBD>ϵ<EFBFBD>ʱ<EFBFBD><EFBFBD>;
* get sleep time set by set_redirect_sleep function
* @return {int} <EFBFBD><EFBFBD>λΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* return sleep value in microsecond
2015-03-01 23:50:53 +08:00
*/
int get_redirect_sleep() const
{
return redirect_sleep_;
}
protected:
/**
* <EFBFBD><EFBFBD><EFBFBD>ി<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<EFBFBD><EFBFBD><EFBFBD>;
* virtual function of base class, which is used to create
* the connection pool
* @param addr {const char*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>ip:port;
* the server addr for the connection pool, such as ip:port
* @param count {int} <EFBFBD><EFBFBD><EFBFBD>ӳصĴ<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
* the max connections in one connection pool
* @param idx {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳض<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>±<EFBFBD>λ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD> 0 <EFBFBD><EFBFBD>ʼ);
* the index of the connection pool in pool array
*/
virtual connect_pool* create_pool(const char* addr,
int count, size_t idx);
private:
int conn_timeout_;
int rw_timeout_;
int max_slot_;
const char** slot_addrs_;
std::vector<char*> addrs_;
2015-03-01 23:50:53 +08:00
int redirect_max_;
int redirect_sleep_;
};
} // namespace acl