Commit cc795ae5 authored by caidaojin's avatar caidaojin

bug fix && unitest fix

parent 50eed9b0
...@@ -108,7 +108,7 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port"; ...@@ -108,7 +108,7 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port";
struct GlobalExtensions { struct GlobalExtensions {
GlobalExtensions() GlobalExtensions()
: ch_mh_lb("murmurhash3") : ch_mh_lb("murmurhash32")
, ch_md5_lb("md5") , ch_md5_lb("md5")
, ch_ketama_lb("ketama") , ch_ketama_lb("ketama")
, constant_cl(0) { , constant_cl(0) {
......
...@@ -33,11 +33,9 @@ DEFINE_int32(chash_num_replicas, 100, ...@@ -33,11 +33,9 @@ DEFINE_int32(chash_num_replicas, 100,
namespace { namespace {
using HashFun = uint32_t(*)(const void*, size_t);
bool BuildReplicasDefault(const ServerId server, bool BuildReplicasDefault(const ServerId server,
const size_t num_replicas, const size_t num_replicas,
HashFun hash, ConsistentHashingLoadBalancer::HashFunc hash,
std::vector<ConsistentHashingLoadBalancer::Node>* replicas) { std::vector<ConsistentHashingLoadBalancer::Node>* replicas) {
SocketUniquePtr ptr; SocketUniquePtr ptr;
if (Socket::AddressFailedAsWell(server.id, &ptr) == -1) { if (Socket::AddressFailedAsWell(server.id, &ptr) == -1) {
......
...@@ -43,6 +43,7 @@ public: ...@@ -43,6 +43,7 @@ public:
return hash < code; return hash < code;
} }
}; };
using HashFun = uint32_t(*)(const void*, size_t);
using BuildReplicasFunc = using BuildReplicasFunc =
std::function<bool (const ServerId server, const size_t num_replicas, std::vector<Node>* replicas)>; std::function<bool (const ServerId server, const size_t num_replicas, std::vector<Node>* replicas)>;
explicit ConsistentHashingLoadBalancer(const char* name); explicit ConsistentHashingLoadBalancer(const char* name);
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
namespace brpc { namespace brpc {
namespace policy { namespace policy {
uint32_t MD5HashSignature(const void* key, size_t len, unsigned char* results) { void MD5HashSignature(const void* key, size_t len, unsigned char* results) {
MD5_CTX my_md5; MD5_CTX my_md5;
MD5_Init(&my_md5); MD5_Init(&my_md5);
MD5_Update(&my_md5, (const unsigned char *)key, len); MD5_Update(&my_md5, (const unsigned char *)key, len);
...@@ -39,6 +39,10 @@ uint32_t MD5Hash32(const void* key, size_t len) { ...@@ -39,6 +39,10 @@ uint32_t MD5Hash32(const void* key, size_t len) {
| (results[0] & 0xFF); | (results[0] & 0xFF);
} }
uint32_t KetamaHash(const void* key, size_t len) {
return MD5Hash32(key, len);
}
uint32_t MD5Hash32V(const butil::StringPiece* keys, size_t num_keys) { uint32_t MD5Hash32V(const butil::StringPiece* keys, size_t num_keys) {
MD5_CTX ctx; MD5_CTX ctx;
MD5_Init(&ctx); MD5_Init(&ctx);
...@@ -153,7 +157,7 @@ uint32_t CRCHash32(const void* key, size_t len) { ...@@ -153,7 +157,7 @@ uint32_t CRCHash32(const void* key, size_t len) {
const char *GetHashName(uint32_t (*hasher)(const void* key, size_t len)) { const char *GetHashName(uint32_t (*hasher)(const void* key, size_t len)) {
if (hasher == MurmurHash32) { if (hasher == MurmurHash32) {
return "murmurhash3"; return "murmurhash32";
} }
if (hasher == MD5Hash32) { if (hasher == MD5Hash32) {
return "md5"; return "md5";
...@@ -161,6 +165,10 @@ const char *GetHashName(uint32_t (*hasher)(const void* key, size_t len)) { ...@@ -161,6 +165,10 @@ const char *GetHashName(uint32_t (*hasher)(const void* key, size_t len)) {
if (hasher == CRCHash32) { if (hasher == CRCHash32) {
return "crc32"; return "crc32";
} }
if (hasher == KetamaHash) {
return "ketama";
}
return "user_defined"; return "user_defined";
} }
......
...@@ -25,13 +25,15 @@ ...@@ -25,13 +25,15 @@
namespace brpc { namespace brpc {
namespace policy { namespace policy {
uint32_t MD5HashSignature(const void* key, size_t len, unsigned char* results); void MD5HashSignature(const void* key, size_t len, unsigned char* results);
uint32_t MD5Hash32(const void* key, size_t len); uint32_t MD5Hash32(const void* key, size_t len);
uint32_t MD5Hash32V(const butil::StringPiece* keys, size_t num_keys); uint32_t MD5Hash32V(const butil::StringPiece* keys, size_t num_keys);
uint32_t MurmurHash32(const void* key, size_t len); uint32_t MurmurHash32(const void* key, size_t len);
uint32_t MurmurHash32V(const butil::StringPiece* keys, size_t num_keys); uint32_t MurmurHash32V(const butil::StringPiece* keys, size_t num_keys);
uint32_t KetamaHash(const void* key, size_t len);
} // namespace policy } // namespace policy
} // namespace brpc } // namespace brpc
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
namespace brpc { namespace brpc {
namespace policy { namespace policy {
extern uint32_t CRCHash32(const char *key, size_t len); extern uint32_t CRCHash32(const char *key, size_t len);
extern const char* GetHashName(uint32_t (*hasher)(const void* key, size_t len));
}} }}
namespace { namespace {
...@@ -250,8 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) { ...@@ -250,8 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
} else if (round == 3) { } else if (round == 3) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer; lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else { } else {
lb = new brpc::policy::ConsistentHashingLoadBalancer( lb = new brpc::policy::ConsistentHashingLoadBalancer("murmurhash32");
::brpc::policy::MurmurHash32);
sa.hash = ::brpc::policy::MurmurHash32; sa.hash = ::brpc::policy::MurmurHash32;
} }
sa.lb = lb; sa.lb = lb;
...@@ -364,8 +364,7 @@ TEST_F(LoadBalancerTest, fairness) { ...@@ -364,8 +364,7 @@ TEST_F(LoadBalancerTest, fairness) {
} else if (3 == round || 4 == round) { } else if (3 == round || 4 == round) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer; lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else { } else {
lb = new brpc::policy::ConsistentHashingLoadBalancer( lb = new brpc::policy::ConsistentHashingLoadBalancer("murmurhash32");
brpc::policy::MurmurHash32);
sa.hash = brpc::policy::MurmurHash32; sa.hash = brpc::policy::MurmurHash32;
} }
sa.lb = lb; sa.lb = lb;
...@@ -488,7 +487,8 @@ TEST_F(LoadBalancerTest, fairness) { ...@@ -488,7 +487,8 @@ TEST_F(LoadBalancerTest, fairness) {
TEST_F(LoadBalancerTest, consistent_hashing) { TEST_F(LoadBalancerTest, consistent_hashing) {
::brpc::policy::ConsistentHashingLoadBalancer::HashFunc hashs[] = { ::brpc::policy::ConsistentHashingLoadBalancer::HashFunc hashs[] = {
::brpc::policy::MurmurHash32, ::brpc::policy::MurmurHash32,
::brpc::policy::MD5Hash32 ::brpc::policy::MD5Hash32,
::brpc::policy::KetamaHash
// ::brpc::policy::CRCHash32 crc is a bad hash function in test // ::brpc::policy::CRCHash32 crc is a bad hash function in test
}; };
const char* servers[] = { const char* servers[] = {
...@@ -499,7 +499,7 @@ TEST_F(LoadBalancerTest, consistent_hashing) { ...@@ -499,7 +499,7 @@ TEST_F(LoadBalancerTest, consistent_hashing) {
"10.42.122.201:8833", "10.42.122.201:8833",
}; };
for (size_t round = 0; round < ARRAY_SIZE(hashs); ++round) { for (size_t round = 0; round < ARRAY_SIZE(hashs); ++round) {
brpc::policy::ConsistentHashingLoadBalancer chlb(hashs[round]); brpc::policy::ConsistentHashingLoadBalancer chlb(brpc::policy::GetHashName(hashs[round]));
std::vector<brpc::ServerId> ids; std::vector<brpc::ServerId> ids;
std::vector<butil::EndPoint> addrs; std::vector<butil::EndPoint> addrs;
for (int j = 0;j < 5; ++j) for (int j = 0;j < 5; ++j)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment