Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wr/wrr policy degradation #1571

Merged
merged 7 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/brpc/load_balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
namespace brpc {

DEFINE_bool(show_lb_in_vars, false, "Describe LoadBalancers in vars");
DEFINE_int32(default_weight_of_wlb, 0, "Default weight value of Weighted LoadBalancer(wlb). "
"wlb policy degradation is enabled when default_weight_of_wlb > 0 to avoid some "
"problems when user is using wlb but forgot to set the weights of some of their "
"downstream instances. Then these instances will be set default_weight_of_wlb as "
"their weights. wlb policy degradation is not enabled by default.");
BRPC_VALIDATE_GFLAG(show_lb_in_vars, PassValidate);

// For assigning unique names for lb.
Expand Down
1 change: 1 addition & 0 deletions src/brpc/load_balancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class LoadBalancer : public NonConstDescribable, public Destroyable {
};

DECLARE_bool(show_lb_in_vars);
DECLARE_int32(default_weight_of_wlb);

// A intrusively shareable load balancer created from name.
class SharedLoadBalancer : public SharedObject, public NonConstDescribable {
Expand Down
27 changes: 16 additions & 11 deletions src/brpc/policy/weighted_randomized_load_balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@ bool WeightedRandomizedLoadBalancer::Add(Servers& bg, const ServerId& id) {
bg.server_list.reserve(128);
}
uint32_t weight = 0;
if (butil::StringToUint(id.tag, &weight) &&
weight > 0) {
bool insert_server =
bg.server_map.emplace(id.id, bg.server_list.size()).second;
if (insert_server) {
uint64_t current_weight_sum = bg.weight_sum + weight;
bg.server_list.emplace_back(id.id, weight, current_weight_sum);
bg.weight_sum = current_weight_sum;
return true;
if (!butil::StringToUint(id.tag, &weight) || weight <= 0) {
if (FLAGS_default_weight_of_wlb > 0) {
LOG(WARNING) << "Invalid weight is set: " << id.tag
<< ". Now, 'weight' has been set to 1 by default.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里输出的 1 应该改成 FLAGS_default_weight_of_wlb 吧

weight = FLAGS_default_weight_of_wlb;
} else {
LOG(ERROR) << "Invalid weight is set: " << id.tag;
return false;
}
} else {
LOG(ERROR) << "Invalid weight is set: " << id.tag;
}
bool insert_server =
bg.server_map.emplace(id.id, bg.server_list.size()).second;
if (insert_server) {
uint64_t current_weight_sum = bg.weight_sum + weight;
bg.server_list.emplace_back(id.id, weight, current_weight_sum);
bg.weight_sum = current_weight_sum;
return true;
}
return false;
}
Expand Down
25 changes: 15 additions & 10 deletions src/brpc/policy/weighted_round_robin_load_balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,22 @@ bool WeightedRoundRobinLoadBalancer::Add(Servers& bg, const ServerId& id) {
bg.server_list.reserve(128);
}
uint32_t weight = 0;
if (butil::StringToUint(id.tag, &weight) &&
weight > 0) {
bool insert_server =
bg.server_map.emplace(id.id, bg.server_list.size()).second;
if (insert_server) {
bg.server_list.emplace_back(id.id, weight);
bg.weight_sum += weight;
return true;
if (!butil::StringToUint(id.tag, &weight) || weight <= 0) {
if (FLAGS_default_weight_of_wlb > 0) {
LOG(WARNING) << "Invalid weight is set: " << id.tag
<< ". Now, 'weight' has been set to 1 by default.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

weight = FLAGS_default_weight_of_wlb;
} else {
LOG(ERROR) << "Invalid weight is set: " << id.tag;
return false;
}
} else {
LOG(ERROR) << "Invalid weight is set: " << id.tag;
}
bool insert_server =
bg.server_map.emplace(id.id, bg.server_list.size()).second;
if (insert_server) {
bg.server_list.emplace_back(id.id, weight);
bg.weight_sum += weight;
return true;
}
return false;
}
Expand Down