2021-11-16 08:38:49 +00:00
|
|
|
import pytest
|
2021-11-29 12:35:28 +00:00
|
|
|
|
|
|
|
import functools
|
2021-11-16 08:38:49 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
|
|
|
|
2021-11-29 12:35:28 +00:00
|
|
|
shard_configs = {
|
|
|
|
i: f'config/config_shard{i}.xml'
|
|
|
|
for i in [1, 3]
|
|
|
|
}
|
|
|
|
|
2021-11-16 08:38:49 +00:00
|
|
|
nodes = [
|
|
|
|
cluster.add_instance(
|
|
|
|
f'node{i}',
|
2021-11-29 12:35:28 +00:00
|
|
|
main_configs=[shard_configs.get(i, 'config/config.xml')],
|
2021-11-16 08:38:49 +00:00
|
|
|
stay_alive=True,
|
|
|
|
with_zookeeper=True
|
|
|
|
) for i in range(5)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def start_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
|
|
|
yield cluster
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
2021-11-29 12:35:28 +00:00
|
|
|
def check_on_cluster(nodes, expected, *, what, cluster_name='test_auto_cluster', msg=None, retries=5):
|
2021-11-16 08:38:49 +00:00
|
|
|
"""
|
2021-11-29 12:35:28 +00:00
|
|
|
Select data from `system.clusters` on specified nodes and check the result
|
2021-11-16 08:38:49 +00:00
|
|
|
"""
|
2021-11-18 09:45:57 +00:00
|
|
|
assert 1 <= retries <= 6
|
2021-11-16 09:03:15 +00:00
|
|
|
|
2021-11-16 08:38:49 +00:00
|
|
|
for retry in range(1, retries + 1):
|
2021-11-29 12:35:28 +00:00
|
|
|
nodes_res = {
|
|
|
|
node.name: int(node.query(f"SELECT {what} FROM system.clusters WHERE cluster = '{cluster_name}'"))
|
2021-11-16 08:38:49 +00:00
|
|
|
for node in nodes
|
2021-11-19 09:42:00 +00:00
|
|
|
}
|
2021-11-29 12:35:28 +00:00
|
|
|
if all(actual == expected for actual in nodes_res.values()):
|
2021-11-16 08:38:49 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if retry != retries:
|
|
|
|
time.sleep(2 ** retry)
|
|
|
|
else:
|
2021-11-29 12:35:28 +00:00
|
|
|
msg = msg or f"Wrong '{what}' result"
|
|
|
|
raise Exception(f'{msg}: {nodes_res}, expected: {expected} (after {retries} retries)')
|
2021-11-16 08:38:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cluster_discovery_startup_and_stop(start_cluster):
|
|
|
|
"""
|
|
|
|
Start cluster, check nodes count in system.clusters,
|
|
|
|
then stop/start some nodes and check that it (dis)appeared in cluster.
|
|
|
|
"""
|
|
|
|
|
2021-11-29 12:35:28 +00:00
|
|
|
check_nodes_count = functools.partial(check_on_cluster, what='count()', msg='Wrong nodes count in cluster')
|
|
|
|
check_shard_num = functools.partial(check_on_cluster, what='count(DISTINCT shard_num)', msg='Wrong shard_num count in cluster')
|
|
|
|
|
|
|
|
total_shards = len(shard_configs) + 1
|
|
|
|
check_nodes_count([nodes[0], nodes[2]], len(nodes))
|
|
|
|
check_shard_num([nodes[0], nodes[2]], total_shards)
|
2021-11-16 08:38:49 +00:00
|
|
|
|
|
|
|
nodes[1].stop_clickhouse(kill=True)
|
2021-11-29 12:35:28 +00:00
|
|
|
check_nodes_count([nodes[0], nodes[2]], len(nodes) - 1)
|
|
|
|
check_shard_num([nodes[0], nodes[2]], total_shards - 1)
|
2021-11-16 08:38:49 +00:00
|
|
|
|
|
|
|
nodes[3].stop_clickhouse()
|
2021-11-29 12:35:28 +00:00
|
|
|
check_nodes_count([nodes[0], nodes[2]], len(nodes) - 2)
|
2021-11-16 08:38:49 +00:00
|
|
|
|
|
|
|
nodes[1].start_clickhouse()
|
2021-11-29 12:35:28 +00:00
|
|
|
check_nodes_count([nodes[0], nodes[2]], len(nodes) - 1)
|
2021-11-16 08:38:49 +00:00
|
|
|
|
|
|
|
nodes[3].start_clickhouse()
|
2021-11-29 12:35:28 +00:00
|
|
|
check_nodes_count([nodes[0], nodes[2]], len(nodes))
|
2021-11-18 09:45:57 +00:00
|
|
|
|
2021-11-29 12:35:28 +00:00
|
|
|
check_nodes_count([nodes[1], nodes[2]], 2, cluster_name='two_shards', retries=1)
|