Commit 3690d3ba authored by root's avatar root

little change for algorithm: each tls has the same stride, but a different…

little change for algorithm: each tls has the same stride, but a different random beginning position of server list.
parent fdc19fe2
...@@ -128,8 +128,10 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut* ...@@ -128,8 +128,10 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut*
} }
TLS& tls = s.tls(); TLS& tls = s.tls();
if (tls.IsNeededCaculateNewStride(s->weight_sum, s->server_list.size())) { if (tls.IsNeededCaculateNewStride(s->weight_sum, s->server_list.size())) {
if (tls.stride == 0) {
tls.position = butil::fast_rand_less_than(s->server_list.size());
}
tls.stride = GetStride(s->weight_sum, s->server_list.size()); tls.stride = GetStride(s->weight_sum, s->server_list.size());
tls.offset = butil::fast_rand_less_than(tls.stride);
} }
// If server list changed, the position may be out of range. // If server list changed, the position may be out of range.
tls.position %= s->server_list.size(); tls.position %= s->server_list.size();
...@@ -150,9 +152,7 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut* ...@@ -150,9 +152,7 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut*
} }
int64_t WeightedRoundRobinLoadBalancer::GetBestServer( int64_t WeightedRoundRobinLoadBalancer::GetBestServer(
const std::vector<std::pair<SocketId, int>>& server_list, const std::vector<std::pair<SocketId, int>>& server_list, TLS& tls) {
TLS& tls) {
uint32_t comp_weight = 0;
int64_t final_server = -1; int64_t final_server = -1;
int stride = tls.stride; int stride = tls.stride;
int weight = 0; int weight = 0;
...@@ -160,25 +160,13 @@ int64_t WeightedRoundRobinLoadBalancer::GetBestServer( ...@@ -160,25 +160,13 @@ int64_t WeightedRoundRobinLoadBalancer::GetBestServer(
if (tls.HasRemainServer()) { if (tls.HasRemainServer()) {
weight = tls.remain_server.second; weight = tls.remain_server.second;
if (weight <= stride) { if (weight <= stride) {
TryToGetFinalServer(tls, tls.remain_server,
comp_weight, &final_server);
tls.ResetRemainServer(); tls.ResetRemainServer();
} else { } else {
TryToGetFinalServer(tls,
std::pair<SocketId, int>(tls.remain_server.first, stride),
comp_weight, &final_server);
tls.remain_server.second -= stride; tls.remain_server.second -= stride;
} }
} else { } else {
weight = server_list[tls.position].second; weight = server_list[tls.position].second;
if (weight <= stride) { if (weight > stride) {
TryToGetFinalServer(tls, server_list[tls.position],
comp_weight, &final_server);
} else {
TryToGetFinalServer(tls,
std::pair<SocketId, int>(
server_list[tls.position].first, stride),
comp_weight, &final_server);
tls.SetRemainServer(server_list[tls.position].first, tls.SetRemainServer(server_list[tls.position].first,
weight - stride); weight - stride);
} }
...@@ -186,6 +174,12 @@ int64_t WeightedRoundRobinLoadBalancer::GetBestServer( ...@@ -186,6 +174,12 @@ int64_t WeightedRoundRobinLoadBalancer::GetBestServer(
} }
stride -= weight; stride -= weight;
} }
if (tls.HasRemainServer()) {
final_server = tls.remain_server.first;
} else {
final_server = tls.position == 0 ? server_list.size() -1
: tls.position -1;
}
return final_server; return final_server;
} }
...@@ -200,17 +194,6 @@ uint32_t WeightedRoundRobinLoadBalancer::GetStride( ...@@ -200,17 +194,6 @@ uint32_t WeightedRoundRobinLoadBalancer::GetStride(
return average_weight; return average_weight;
} }
void WeightedRoundRobinLoadBalancer::TryToGetFinalServer(
const TLS& tls, const std::pair<SocketId, int> server,
uint32_t& comp_weight, int64_t* final_server) {
if (*final_server == -1) {
comp_weight += server.second;
if (comp_weight >= tls.offset) {
*final_server = server.first;
}
}
}
LoadBalancer* WeightedRoundRobinLoadBalancer::New() const { LoadBalancer* WeightedRoundRobinLoadBalancer::New() const {
return new (std::nothrow) WeightedRoundRobinLoadBalancer; return new (std::nothrow) WeightedRoundRobinLoadBalancer;
} }
......
...@@ -50,7 +50,6 @@ private: ...@@ -50,7 +50,6 @@ private:
TLS(): remain_server(0, 0) { } TLS(): remain_server(0, 0) { }
uint32_t position = 0; uint32_t position = 0;
uint32_t stride = 0; uint32_t stride = 0;
uint32_t offset = 0;
std::pair<SocketId, int> remain_server; std::pair<SocketId, int> remain_server;
bool HasRemainServer() const { bool HasRemainServer() const {
return remain_server.second != 0; return remain_server.second != 0;
......
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