From bee8445485f1a865d40b321400c616c832e34c70 Mon Sep 17 00:00:00 2001 From: huxiguo Date: Wed, 29 Sep 2021 16:04:40 +0800 Subject: [PATCH 1/4] wr/wrr policy degradation --- src/brpc/load_balancer.cpp | 1 + src/brpc/load_balancer.h | 1 + .../weighted_randomized_load_balancer.cpp | 27 +++++++++++-------- .../weighted_round_robin_load_balancer.cpp | 25 ++++++++++------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/brpc/load_balancer.cpp b/src/brpc/load_balancer.cpp index 18f6a7a2f7..3a52589ad7 100644 --- a/src/brpc/load_balancer.cpp +++ b/src/brpc/load_balancer.cpp @@ -24,6 +24,7 @@ namespace brpc { DEFINE_bool(show_lb_in_vars, false, "Describe LoadBalancers in vars"); +DEFINE_bool(wlb_policy_degradation, false, "Weighted LoadBalancers policy degradation"); BRPC_VALIDATE_GFLAG(show_lb_in_vars, PassValidate); // For assigning unique names for lb. diff --git a/src/brpc/load_balancer.h b/src/brpc/load_balancer.h index 830c9efa7e..934715fec9 100644 --- a/src/brpc/load_balancer.h +++ b/src/brpc/load_balancer.h @@ -108,6 +108,7 @@ class LoadBalancer : public NonConstDescribable, public Destroyable { }; DECLARE_bool(show_lb_in_vars); +DECLARE_bool(wlb_policy_degradation); // A intrusively shareable load balancer created from name. class SharedLoadBalancer : public SharedObject, public NonConstDescribable { diff --git a/src/brpc/policy/weighted_randomized_load_balancer.cpp b/src/brpc/policy/weighted_randomized_load_balancer.cpp index 5ef4fc327a..b76d51afd9 100644 --- a/src/brpc/policy/weighted_randomized_load_balancer.cpp +++ b/src/brpc/policy/weighted_randomized_load_balancer.cpp @@ -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_wlb_policy_degradation) { + LOG(WARNING) << "Invalid weight is set: " << id.tag + << ". Now, 'weight' has been set to 1 by default."; + weight = 1; + } 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; } diff --git a/src/brpc/policy/weighted_round_robin_load_balancer.cpp b/src/brpc/policy/weighted_round_robin_load_balancer.cpp index 1e6add5d78..ff8a73ab74 100644 --- a/src/brpc/policy/weighted_round_robin_load_balancer.cpp +++ b/src/brpc/policy/weighted_round_robin_load_balancer.cpp @@ -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_wlb_policy_degradation) { + LOG(WARNING) << "Invalid weight is set: " << id.tag + << ". Now, 'weight' has been set to 1 by default."; + weight = 1; + } 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; } From 9a4066efa0c0c795fcfed237b0161d1cbb4b4ea9 Mon Sep 17 00:00:00 2001 From: HU Date: Wed, 15 Dec 2021 20:29:35 +0800 Subject: [PATCH 2/4] Update load_balancer.cpp --- src/brpc/load_balancer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/brpc/load_balancer.cpp b/src/brpc/load_balancer.cpp index e4463f8e8a..16e051d65e 100644 --- a/src/brpc/load_balancer.cpp +++ b/src/brpc/load_balancer.cpp @@ -24,7 +24,11 @@ namespace brpc { DEFINE_bool(show_lb_in_vars, false, "Describe LoadBalancers in vars"); -DEFINE_int32(default_weight_of_wlb, 0, "Default weight of Weighted LoadBalancer"); +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. From 75e66f12e601318ca23306e1c4107217aa76cb22 Mon Sep 17 00:00:00 2001 From: HU Date: Fri, 17 Jun 2022 14:17:26 +0800 Subject: [PATCH 3/4] Update weighted_randomized_load_balancer.cpp --- src/brpc/policy/weighted_randomized_load_balancer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/brpc/policy/weighted_randomized_load_balancer.cpp b/src/brpc/policy/weighted_randomized_load_balancer.cpp index 3690b4f04d..0e741ef8e9 100644 --- a/src/brpc/policy/weighted_randomized_load_balancer.cpp +++ b/src/brpc/policy/weighted_randomized_load_balancer.cpp @@ -38,7 +38,7 @@ bool WeightedRandomizedLoadBalancer::Add(Servers& bg, const ServerId& id) { 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."; + << ". Now, 'weight' has been set to 'FLAGS_default_weight_of_wlb' by default."; weight = FLAGS_default_weight_of_wlb; } else { LOG(ERROR) << "Invalid weight is set: " << id.tag; From 65921af55d377fd53835390b27a5e8e2228351fd Mon Sep 17 00:00:00 2001 From: HU Date: Fri, 17 Jun 2022 14:19:35 +0800 Subject: [PATCH 4/4] Update weighted_round_robin_load_balancer.cpp --- src/brpc/policy/weighted_round_robin_load_balancer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/brpc/policy/weighted_round_robin_load_balancer.cpp b/src/brpc/policy/weighted_round_robin_load_balancer.cpp index ada0ad4fbb..82c2008595 100644 --- a/src/brpc/policy/weighted_round_robin_load_balancer.cpp +++ b/src/brpc/policy/weighted_round_robin_load_balancer.cpp @@ -81,7 +81,7 @@ bool WeightedRoundRobinLoadBalancer::Add(Servers& bg, const ServerId& id) { 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."; + << ". Now, 'weight' has been set to 'FLAGS_default_weight_of_wlb' by default."; weight = FLAGS_default_weight_of_wlb; } else { LOG(ERROR) << "Invalid weight is set: " << id.tag;