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*
}
TLS& tls = s.tls();
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.offset = butil::fast_rand_less_than(tls.stride);
}
// If server list changed, the position may be out of range.
tls.position %= s->server_list.size();
......@@ -150,9 +152,7 @@ int WeightedRoundRobinLoadBalancer::SelectServer(const SelectIn& in, SelectOut*
}
int64_t WeightedRoundRobinLoadBalancer::GetBestServer(
const std::vector<std::pair<SocketId, int>>& server_list,
TLS& tls) {
uint32_t comp_weight = 0;
const std::vector<std::pair<SocketId, int>>& server_list, TLS& tls) {
int64_t final_server = -1;
int stride = tls.stride;
int weight = 0;
......@@ -160,25 +160,13 @@ int64_t WeightedRoundRobinLoadBalancer::GetBestServer(
if (tls.HasRemainServer()) {
weight = tls.remain_server.second;
if (weight <= stride) {
TryToGetFinalServer(tls, tls.remain_server,
comp_weight, &final_server);
tls.ResetRemainServer();
} else {
TryToGetFinalServer(tls,
std::pair<SocketId, int>(tls.remain_server.first, stride),
comp_weight, &final_server);
tls.remain_server.second -= stride;
}
} else {
weight = server_list[tls.position].second;
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);
if (weight > stride) {
tls.SetRemainServer(server_list[tls.position].first,
weight - stride);
}
......@@ -186,6 +174,12 @@ int64_t WeightedRoundRobinLoadBalancer::GetBestServer(
}
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;
}
......@@ -200,17 +194,6 @@ uint32_t WeightedRoundRobinLoadBalancer::GetStride(
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 {
return new (std::nothrow) WeightedRoundRobinLoadBalancer;
}
......
......@@ -50,7 +50,6 @@ private:
TLS(): remain_server(0, 0) { }
uint32_t position = 0;
uint32_t stride = 0;
uint32_t offset = 0;
std::pair<SocketId, int> remain_server;
bool HasRemainServer() const {
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