ClickHouse/src/Common/GetPriorityForLoadBalancing.cpp

50 lines
1.5 KiB
C++
Raw Normal View History

2021-10-20 08:35:37 +00:00
#include <Common/GetPriorityForLoadBalancing.h>
namespace DB
{
2022-03-21 14:55:01 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
2021-10-22 12:23:25 +00:00
std::function<size_t(size_t index)> GetPriorityForLoadBalancing::getPriorityFunc(LoadBalancing load_balance, size_t offset, size_t pool_size) const
2021-10-20 08:35:37 +00:00
{
std::function<size_t(size_t index)> get_priority;
2021-10-22 12:23:25 +00:00
switch (load_balance)
2021-10-20 08:35:37 +00:00
{
case LoadBalancing::NEAREST_HOSTNAME:
2022-03-21 14:55:01 +00:00
if (hostname_differences.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR, "It's a bug: hostname_differences is not initialized");
2021-10-20 08:35:37 +00:00
get_priority = [&](size_t i) { return hostname_differences[i]; };
break;
case LoadBalancing::IN_ORDER:
get_priority = [](size_t i) { return i; };
break;
case LoadBalancing::RANDOM:
break;
case LoadBalancing::FIRST_OR_RANDOM:
2021-10-22 12:23:25 +00:00
get_priority = [offset](size_t i) -> size_t { return i != offset; };
2021-10-20 08:35:37 +00:00
break;
case LoadBalancing::ROUND_ROBIN:
if (last_used >= pool_size)
last_used = 0;
++last_used;
/* Consider pool_size equals to 5
* last_used = 1 -> get_priority: 0 1 2 3 4
* last_used = 2 -> get_priority: 4 0 1 2 3
* last_used = 3 -> get_priority: 4 3 0 1 2
* ...
* */
2021-10-21 07:57:21 +00:00
get_priority = [&](size_t i)
{
2021-10-20 08:35:37 +00:00
++i;
return i < last_used ? pool_size - i : i - last_used;
};
break;
}
return get_priority;
}
}