Commit 3d7b5476 authored by cdjgit's avatar cdjgit

fix for ci comments

parent dcc7e003
......@@ -108,9 +108,9 @@ const char* const DUMMY_SERVER_PORT_FILE = "dummy_server.port";
struct GlobalExtensions {
GlobalExtensions()
: ch_mh_lb("murmurhash3")
, ch_md5_lb("md5")
, ch_ketama_lb("ketama")
: ch_mh_lb(CONS_HASH_LB_MURMUR3)
, ch_md5_lb(CONS_HASH_LB_MD5)
, ch_ketama_lb(CONS_HASH_LB_KETAMA)
, constant_cl(0) {
}
......
......@@ -112,27 +112,29 @@ bool KetamaReplicaPolicy::Build(ServerId server,
namespace {
const std::map<std::string, const ReplicaPolicy*> g_replica_policy_map = {
{"murmurhash3", new DefaultReplicaPolicy(MurmurHash32)},
{"md5", new DefaultReplicaPolicy(MD5Hash32)},
{"ketama", new KetamaReplicaPolicy}
const std::array<std::pair<const ReplicaPolicy*, std::string>, CONS_HASH_LB_LAST>
g_replica_policy = {
std::make_pair(new DefaultReplicaPolicy(MurmurHash32), "murmurhash3"),
std::make_pair(new DefaultReplicaPolicy(MD5Hash32), "md5"),
std::make_pair(new KetamaReplicaPolicy, "ketama")
};
const ReplicaPolicy* GetReplicaPolicy(const std::string& name) {
auto iter = g_replica_policy_map.find(name);
if (iter != g_replica_policy_map.end()) {
return iter->second;
}
return nullptr;
inline const ReplicaPolicy* GetReplicaPolicy(ConsistentHashingLoadBalancerType type) {
return g_replica_policy.at(type).first;
}
inline const std::string& GetLbName(ConsistentHashingLoadBalancerType type) {
return g_replica_policy.at(type).second;
}
} // namespace
ConsistentHashingLoadBalancer::ConsistentHashingLoadBalancer(const char* name)
: _num_replicas(FLAGS_chash_num_replicas), _name(name) {
_replicas_policy = GetReplicaPolicy(name);
ConsistentHashingLoadBalancer::ConsistentHashingLoadBalancer(
ConsistentHashingLoadBalancerType type)
: _num_replicas(FLAGS_chash_num_replicas), _type(type) {
_replicas_policy = GetReplicaPolicy(_type);
CHECK(_replicas_policy)
<< "Fail to find replica policy for consistency lb: '" << name << '\'';
<< "Fail to find replica policy for consistency lb type: '" << type << '\'';
}
size_t ConsistentHashingLoadBalancer::AddBatch(
......@@ -260,7 +262,7 @@ size_t ConsistentHashingLoadBalancer::RemoveServersInBatch(
}
LoadBalancer *ConsistentHashingLoadBalancer::New() const {
return new (std::nothrow) ConsistentHashingLoadBalancer(_name.c_str());
return new (std::nothrow) ConsistentHashingLoadBalancer(_type);
}
void ConsistentHashingLoadBalancer::Destroy() {
......@@ -311,7 +313,7 @@ void ConsistentHashingLoadBalancer::Describe(
return;
}
os << "ConsistentHashingLoadBalancer {\n"
<< " hash function: " << _name << '\n'
<< " hash function: " << GetLbName(_type) << '\n'
<< " replica per host: " << _num_replicas << '\n';
std::map<butil::EndPoint, double> load_map;
GetLoads(&load_map);
......
......@@ -30,6 +30,15 @@ namespace policy {
class ReplicaPolicy;
enum ConsistentHashingLoadBalancerType {
CONS_HASH_LB_MURMUR3 = 0,
CONS_HASH_LB_MD5 = 1,
CONS_HASH_LB_KETAMA = 2,
// Identify the last one.
CONS_HASH_LB_LAST = 3
};
class ConsistentHashingLoadBalancer : public LoadBalancer {
public:
struct Node {
......@@ -45,7 +54,7 @@ public:
return hash < code;
}
};
explicit ConsistentHashingLoadBalancer(const char* name);
explicit ConsistentHashingLoadBalancer(ConsistentHashingLoadBalancerType type);
bool AddServer(const ServerId& server);
bool RemoveServer(const ServerId& server);
size_t AddServersInBatch(const std::vector<ServerId> &servers);
......@@ -66,7 +75,7 @@ private:
const ServerId& server, bool *executed);
const ReplicaPolicy* _replicas_policy;
size_t _num_replicas;
std::string _name;
ConsistentHashingLoadBalancerType _type;
butil::DoublyBufferedData<std::vector<Node> > _db_hash_ring;
};
......
......@@ -251,7 +251,7 @@ TEST_F(LoadBalancerTest, update_while_selection) {
} else if (round == 3) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else {
lb = new brpc::policy::ConsistentHashingLoadBalancer("murmurhash3");
lb = new brpc::policy::ConsistentHashingLoadBalancer(brpc::policy::CONS_HASH_LB_MURMUR3);
sa.hash = ::brpc::policy::MurmurHash32;
}
sa.lb = lb;
......@@ -364,7 +364,7 @@ TEST_F(LoadBalancerTest, fairness) {
} else if (3 == round || 4 == round) {
lb = new brpc::policy::WeightedRoundRobinLoadBalancer;
} else {
lb = new brpc::policy::ConsistentHashingLoadBalancer("murmurhash3");
lb = new brpc::policy::ConsistentHashingLoadBalancer(brpc::policy::CONS_HASH_LB_MURMUR3);
sa.hash = brpc::policy::MurmurHash32;
}
sa.lb = lb;
......@@ -485,12 +485,19 @@ TEST_F(LoadBalancerTest, fairness) {
}
TEST_F(LoadBalancerTest, consistent_hashing) {
::brpc::policy::HashFunc hashs[] = {
::brpc::policy::HashFunc hashs[CONS_HASH_LB_LAST] = {
::brpc::policy::MurmurHash32,
::brpc::policy::MD5Hash32,
::brpc::policy::KetamaHash
// ::brpc::policy::CRCHash32 crc is a bad hash function in test
};
::brpc::policy::ConsistentHashingLoadBalancerType hash_type[CONS_HASH_LB_LAST] = {
::brpc::policy::CONS_HASH_LB_MURMUR3,
::brpc::policy::CONS_HASH_LB_MD5,
::brpc::policy::CONS_HASH_LB_KETAMA
};
const char* servers[] = {
"10.92.115.19:8833",
"10.42.108.25:8833",
......@@ -499,7 +506,7 @@ TEST_F(LoadBalancerTest, consistent_hashing) {
"10.42.122.201:8833",
};
for (size_t round = 0; round < ARRAY_SIZE(hashs); ++round) {
brpc::policy::ConsistentHashingLoadBalancer chlb(brpc::policy::GetHashName(hashs[round]));
brpc::policy::ConsistentHashingLoadBalancer chlb(hash_type[round]);
std::vector<brpc::ServerId> ids;
std::vector<butil::EndPoint> addrs;
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