minor fixes

This commit is contained in:
Alexander Tokmakov 2022-03-21 15:55:01 +01:00
parent d0217a0025
commit 3cca5fb181
6 changed files with 27 additions and 16 deletions

View File

@ -3,12 +3,19 @@
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
std::function<size_t(size_t index)> GetPriorityForLoadBalancing::getPriorityFunc(LoadBalancing load_balance, size_t offset, size_t pool_size) const
{
std::function<size_t(size_t index)> get_priority;
switch (load_balance)
{
case LoadBalancing::NEAREST_HOSTNAME:
if (hostname_differences.empty())
throw Exception(ErrorCodes::LOGICAL_ERROR, "It's a bug: hostname_differences is not initialized");
get_priority = [&](size_t i) { return hostname_differences[i]; };
break;
case LoadBalancing::IN_ORDER:

View File

@ -11,9 +11,14 @@ public:
GetPriorityForLoadBalancing(LoadBalancing load_balancing_) : load_balancing(load_balancing_) {}
GetPriorityForLoadBalancing(){}
bool operator!=(const GetPriorityForLoadBalancing & other)
bool operator == (const GetPriorityForLoadBalancing & other) const
{
return load_balancing != other.load_balancing || hostname_differences != other.hostname_differences;
return load_balancing == other.load_balancing && hostname_differences == other.hostname_differences;
}
bool operator != (const GetPriorityForLoadBalancing & other) const
{
return !(*this == other);
}
std::function<size_t(size_t index)> getPriorityFunc(LoadBalancing load_balance, size_t offset, size_t pool_size) const;

View File

@ -290,8 +290,8 @@ bool ZooKeeper::configChanged(const Poco::Util::AbstractConfiguration & config,
if (args.get_priority_load_balancing != get_priority_load_balancing)
return true;
return std::tie(args.implementation, args.hosts, args.identity, args.session_timeout_ms, args.operation_timeout_ms, args.chroot)
!= std::tie(implementation, hosts, identity, session_timeout_ms, operation_timeout_ms, chroot);
return std::tie(args.implementation, args.hosts, args.identity, args.session_timeout_ms, args.operation_timeout_ms, args.chroot, args.get_priority_load_balancing)
!= std::tie(implementation, hosts, identity, session_timeout_ms, operation_timeout_ms, chroot, args.get_priority_load_balancing);
}

View File

@ -13,8 +13,8 @@
#include <Common/Stopwatch.h>
#include <Common/ZooKeeper/IKeeper.h>
#include <Common/ZooKeeper/ZooKeeperConstants.h>
#include <Common/randomSeed.h>
#include <Common/GetPriorityForLoadBalancing.h>
#include <Common/thread_local_rng.h>
#include <unistd.h>
#include <random>
@ -48,7 +48,7 @@ struct ShuffleHost
void randomize()
{
random = rng();
random = thread_local_rng();
}
static bool compare(const ShuffleHost & lhs, const ShuffleHost & rhs)
@ -56,9 +56,6 @@ struct ShuffleHost
return std::forward_as_tuple(lhs.priority, lhs.random)
< std::forward_as_tuple(rhs.priority, rhs.random);
}
private:
std::minstd_rand rng = std::minstd_rand(randomSeed());
};
using GetPriorityForLoadBalancing = DB::GetPriorityForLoadBalancing;

View File

@ -124,6 +124,7 @@ bool isLocalAddress(const Poco::Net::SocketAddress & address, UInt16 clickhouse_
size_t getHostNameDifference(const std::string & local_hostname, const std::string & host)
{
/// FIXME should we replace it with Levenstein distance? (we already have it in NamePrompter)
size_t hostname_difference = 0;
for (size_t i = 0; i < std::min(local_hostname.length(), host.length()); ++i)
if (local_hostname[i] != host[i])

View File

@ -3,11 +3,12 @@ from helpers.cluster import ClickHouseCluster
cluster = ClickHouseCluster(__file__, zookeeper_config_path='configs/zookeeper_load_balancing.xml')
node1 = cluster.add_instance('node1', with_zookeeper=True,
# use 3-letter hostnames, so getHostNameDifference("nod1", "zoo1") will work as expected
node1 = cluster.add_instance('nod1', with_zookeeper=True,
main_configs=["configs/zookeeper_load_balancing.xml"])
node2 = cluster.add_instance('node2', with_zookeeper=True,
node2 = cluster.add_instance('nod2', with_zookeeper=True,
main_configs=["configs/zookeeper_load_balancing.xml"])
node3 = cluster.add_instance('node3', with_zookeeper=True,
node3 = cluster.add_instance('nod3', with_zookeeper=True,
main_configs=["configs/zookeeper_load_balancing.xml"])
def change_balancing(old, new, reload=True):
@ -56,7 +57,7 @@ def test_in_order(started_cluster):
print(str(node3.exec_in_container(['bash', '-c', "lsof -a -i4 -i6 -itcp -w | grep ':2181' | grep ESTABLISHED"], privileged=True, user='root')))
assert '1' == str(node3.exec_in_container(['bash', '-c', "lsof -a -i4 -i6 -itcp -w | grep 'testzookeeperconfigloadbalancing_zoo1_1.*testzookeeperconfigloadbalancing_default:2181' | grep ESTABLISHED | wc -l"], privileged=True, user='root')).strip()
finally:
change_balancing('first_or_random', 'random', reload=False)
change_balancing('in_order', 'random', reload=False)
def test_nearest_hostname(started_cluster):
@ -71,7 +72,7 @@ def test_nearest_hostname(started_cluster):
print(str(node3.exec_in_container(['bash', '-c', "lsof -a -i4 -i6 -itcp -w | grep ':2181' | grep ESTABLISHED"], privileged=True, user='root')))
assert '1' == str(node3.exec_in_container(['bash', '-c', "lsof -a -i4 -i6 -itcp -w | grep 'testzookeeperconfigloadbalancing_zoo3_1.*testzookeeperconfigloadbalancing_default:2181' | grep ESTABLISHED | wc -l"], privileged=True, user='root')).strip()
finally:
change_balancing('first_or_random', 'random', reload=False)
change_balancing('nearest_hostname', 'random', reload=False)
def test_round_robin(started_cluster):
@ -88,6 +89,6 @@ def test_round_robin(started_cluster):
print(str(node3.exec_in_container(['bash', '-c', "lsof -a -i4 -i6 -itcp -w | grep ':2181' | grep ESTABLISHED"], privileged=True, user='root')))
assert '1' == str(node3.exec_in_container(['bash', '-c', "lsof -a -i4 -i6 -itcp -w | grep 'testzookeeperconfigloadbalancing_zoo2_1.*testzookeeperconfigloadbalancing_default:2181' | grep ESTABLISHED | wc -l"], privileged=True, user='root')).strip()
started_cluster.start_zookeeper_nodes(["zoo1"])
finally:
change_balancing('first_or_random', 'random', reload=False)
started_cluster.start_zookeeper_nodes(["zoo1"])
change_balancing('round_robin', 'random', reload=False)