Commit c9a9ffa3 authored by caidaojin's avatar caidaojin

move SetParameters function to New()

parent f97f4b89
......@@ -74,16 +74,11 @@ int SharedLoadBalancer::Init(const char* lb_protocol) {
LOG(FATAL) << "Fail to find LoadBalancer by `" << lb_name << "'";
return -1;
}
LoadBalancer* lb_copy = lb->New();
if (lb_copy == NULL) {
LoadBalancer* _lb = lb->New(lb_params);
if (_lb == NULL) {
LOG(FATAL) << "Fail to new LoadBalancer";
return -1;
}
_lb = lb_copy;
if (!_lb->SetParameters(lb_params)) {
LOG(FATAL) << "Fail to set parameters of lb `" << lb_protocol << "'";
return -1;
}
if (FLAGS_show_lb_in_vars && !_exposed) {
ExposeLB();
}
......
......@@ -102,11 +102,7 @@ public:
// Create/destroy an instance.
// Caller is responsible for Destroy() the instance after usage.
virtual LoadBalancer* New() const = 0;
// Config user passed parameters to lb after construction which
// make lb function more flexible.
virtual bool SetParameters(const butil::StringPiece& params) { return true; }
virtual LoadBalancer* New(const butil::StringPiece& params) const = 0;
protected:
virtual ~LoadBalancer() { }
......
......@@ -31,6 +31,9 @@ namespace policy {
DEFINE_int32(chash_num_replicas, 100,
"default number of replicas per server in chash");
// Defined in hasher.cpp.
const char* GetHashName(HashFunc hasher);
class ReplicaPolicy {
public:
virtual ~ReplicaPolicy() = default;
......@@ -38,6 +41,7 @@ public:
virtual bool Build(ServerId server,
size_t num_replicas,
std::vector<ConsistentHashingLoadBalancer::Node>* replicas) const = 0;
virtual const char* name() const = 0;
};
class DefaultReplicaPolicy : public ReplicaPolicy {
......@@ -47,6 +51,9 @@ public:
virtual bool Build(ServerId server,
size_t num_replicas,
std::vector<ConsistentHashingLoadBalancer::Node>* replicas) const;
virtual const char* name() const { return GetHashName(_hash_func); }
private:
HashFunc _hash_func;
};
......@@ -77,6 +84,8 @@ public:
virtual bool Build(ServerId server,
size_t num_replicas,
std::vector<ConsistentHashingLoadBalancer::Node>* replicas) const;
virtual const char* name() const { return "ketama"; }
};
bool KetamaReplicaPolicy::Build(ServerId server,
......@@ -112,19 +121,14 @@ bool KetamaReplicaPolicy::Build(ServerId server,
namespace {
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 std::array<const ReplicaPolicy*, CONS_HASH_LB_LAST> g_replica_policy = {
new DefaultReplicaPolicy(MurmurHash32),
new DefaultReplicaPolicy(MD5Hash32),
new KetamaReplicaPolicy
};
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;
return g_replica_policy.at(type);
}
} // namespace
......@@ -261,8 +265,14 @@ size_t ConsistentHashingLoadBalancer::RemoveServersInBatch(
return n;
}
LoadBalancer *ConsistentHashingLoadBalancer::New() const {
return new (std::nothrow) ConsistentHashingLoadBalancer(_type);
LoadBalancer *ConsistentHashingLoadBalancer::New(const butil::StringPiece& params) const {
ConsistentHashingLoadBalancer* lb =
new (std::nothrow) ConsistentHashingLoadBalancer(_type);
if (lb != nullptr && !lb->SetParameters(params)) {
delete lb;
lb = nullptr;
}
return lb;
}
void ConsistentHashingLoadBalancer::Destroy() {
......@@ -313,7 +323,7 @@ void ConsistentHashingLoadBalancer::Describe(
return;
}
os << "ConsistentHashingLoadBalancer {\n"
<< " hash function: " << GetLbName(_type) << '\n'
<< " hash function: " << _replicas_policy->name() << '\n'
<< " replica per host: " << _num_replicas << '\n';
std::map<butil::EndPoint, double> load_map;
GetLoads(&load_map);
......
......@@ -59,11 +59,11 @@ public:
bool RemoveServer(const ServerId& server);
size_t AddServersInBatch(const std::vector<ServerId> &servers);
size_t RemoveServersInBatch(const std::vector<ServerId> &servers);
LoadBalancer *New() const;
LoadBalancer *New(const butil::StringPiece& params) const;
void Destroy();
int SelectServer(const SelectIn &in, SelectOut *out);
void Describe(std::ostream &os, const DescribeOptions& options);
virtual bool SetParameters(const butil::StringPiece& params);
bool SetParameters(const butil::StringPiece& params);
private:
void GetLoads(std::map<butil::EndPoint, double> *load_map);
......
......@@ -159,7 +159,7 @@ int DynPartLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
return EHOSTDOWN;
}
DynPartLoadBalancer* DynPartLoadBalancer::New() const {
DynPartLoadBalancer* DynPartLoadBalancer::New(const butil::StringPiece&) const {
return new (std::nothrow) DynPartLoadBalancer;
}
......
......@@ -36,7 +36,7 @@ public:
size_t AddServersInBatch(const std::vector<ServerId>& servers);
size_t RemoveServersInBatch(const std::vector<ServerId>& servers);
int SelectServer(const SelectIn& in, SelectOut* out);
DynPartLoadBalancer* New() const;
DynPartLoadBalancer* New(const butil::StringPiece&) const;
void Destroy();
void Describe(std::ostream&, const DescribeOptions& options);
......
......@@ -460,7 +460,8 @@ int64_t LocalityAwareLoadBalancer::Weight::Update(
return ResetWeight(index, end_time_us);
}
LocalityAwareLoadBalancer* LocalityAwareLoadBalancer::New() const {
LocalityAwareLoadBalancer* LocalityAwareLoadBalancer::New(
const butil::StringPiece&) const {
return new (std::nothrow) LocalityAwareLoadBalancer;
}
......
......@@ -44,7 +44,7 @@ public:
bool RemoveServer(const ServerId& id);
size_t AddServersInBatch(const std::vector<ServerId>& servers);
size_t RemoveServersInBatch(const std::vector<ServerId>& servers);
LocalityAwareLoadBalancer* New() const;
LocalityAwareLoadBalancer* New(const butil::StringPiece&) const;
void Destroy();
int SelectServer(const SelectIn& in, SelectOut* out);
void Feedback(const CallInfo& info);
......
......@@ -134,7 +134,8 @@ int RandomizedLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
return EHOSTDOWN;
}
RandomizedLoadBalancer* RandomizedLoadBalancer::New() const {
RandomizedLoadBalancer* RandomizedLoadBalancer::New(
const butil::StringPiece&) const {
return new (std::nothrow) RandomizedLoadBalancer;
}
......
......@@ -36,7 +36,7 @@ public:
size_t AddServersInBatch(const std::vector<ServerId>& servers);
size_t RemoveServersInBatch(const std::vector<ServerId>& servers);
int SelectServer(const SelectIn& in, SelectOut* out);
RandomizedLoadBalancer* New() const;
RandomizedLoadBalancer* New(const butil::StringPiece&) const;
void Destroy();
void Describe(std::ostream& os, const DescribeOptions&);
......
......@@ -131,7 +131,8 @@ int RoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut* out) {
return EHOSTDOWN;
}
RoundRobinLoadBalancer* RoundRobinLoadBalancer::New() const {
RoundRobinLoadBalancer* RoundRobinLoadBalancer::New(
const butil::StringPiece&) const {
return new (std::nothrow) RoundRobinLoadBalancer;
}
......
......@@ -35,7 +35,7 @@ public:
size_t AddServersInBatch(const std::vector<ServerId>& servers);
size_t RemoveServersInBatch(const std::vector<ServerId>& servers);
int SelectServer(const SelectIn& in, SelectOut* out);
RoundRobinLoadBalancer* New() const;
RoundRobinLoadBalancer* New(const butil::StringPiece&) const;
void Destroy();
void Describe(std::ostream&, const DescribeOptions& options);
......
......@@ -213,7 +213,8 @@ SocketId WeightedRoundRobinLoadBalancer::GetServerInNextStride(
return final_server;
}
LoadBalancer* WeightedRoundRobinLoadBalancer::New() const {
LoadBalancer* WeightedRoundRobinLoadBalancer::New(
const butil::StringPiece&) const {
return new (std::nothrow) WeightedRoundRobinLoadBalancer;
}
......
......@@ -34,7 +34,7 @@ public:
size_t AddServersInBatch(const std::vector<ServerId>& servers);
size_t RemoveServersInBatch(const std::vector<ServerId>& servers);
int SelectServer(const SelectIn& in, SelectOut* out);
LoadBalancer* New() const;
LoadBalancer* New(const butil::StringPiece&) const;
void Destroy();
void Describe(std::ostream&, const DescribeOptions& options);
......
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