ClickHouse/tests/integration/test_cluster_discovery/test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
3.4 KiB
Python
Raw Normal View History

import functools
2024-09-27 10:19:39 +00:00
import pytest
2021-11-16 08:38:49 +00:00
from helpers.cluster import ClickHouseCluster
2024-09-27 10:19:39 +00:00
from .common import check_on_cluster
2021-11-16 08:38:49 +00:00
cluster = ClickHouseCluster(__file__)
shard_configs = {
2022-08-12 11:05:13 +00:00
"node0": "config/config.xml",
"node1": "config/config_shard1.xml",
"node2": "config/config.xml",
"node3": "config/config_shard3.xml",
"node4": "config/config.xml",
2022-08-11 15:42:32 +00:00
"node_observer": "config/config_observer.xml",
}
nodes = {
2022-08-11 15:42:32 +00:00
node_name: cluster.add_instance(
node_name,
main_configs=[shard_config, "config/config_discovery_path.xml"],
2021-11-16 08:38:49 +00:00
stay_alive=True,
with_zookeeper=True,
)
2022-08-11 15:42:32 +00:00
for node_name, shard_config in shard_configs.items()
}
2021-11-16 08:38:49 +00:00
@pytest.fixture(scope="module")
def start_cluster():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
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.
"""
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",
)
2022-08-11 15:42:32 +00:00
# `- 1` because one node is an observer
total_shards = len(set(shard_configs.values())) - 1
total_nodes = len(nodes) - 1
2021-11-16 08:38:49 +00:00
2022-08-11 15:42:32 +00:00
check_nodes_count(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_nodes
2022-08-11 15:42:32 +00:00
)
check_shard_num(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_shards
2022-08-11 15:42:32 +00:00
)
# test ON CLUSTER query
nodes["node0"].query(
"CREATE TABLE tbl ON CLUSTER 'test_auto_cluster' (x UInt64) ENGINE = MergeTree ORDER BY x"
)
nodes["node0"].query("INSERT INTO tbl VALUES (1)")
nodes["node1"].query("INSERT INTO tbl VALUES (2)")
assert (
int(
nodes["node_observer"]
.query(
"SELECT sum(x) FROM clusterAllReplicas(test_auto_cluster, default.tbl)"
)
.strip()
)
== 3
)
# Query SYSTEM DROP DNS CACHE may reload cluster configuration
# check that it does not affect cluster discovery
nodes["node1"].query("SYSTEM DROP DNS CACHE")
nodes["node0"].query("SYSTEM DROP DNS CACHE")
check_shard_num(
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_shards
)
2022-08-12 11:05:13 +00:00
nodes["node1"].stop_clickhouse(kill=True)
2022-08-11 15:42:32 +00:00
check_nodes_count(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_nodes - 1
2022-08-11 15:42:32 +00:00
)
2022-08-12 11:05:13 +00:00
# node1 was the only node in shard '1'
2022-08-11 15:42:32 +00:00
check_shard_num(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_shards - 1
2022-08-11 15:42:32 +00:00
)
2021-11-16 08:38:49 +00:00
2022-08-12 11:05:13 +00:00
nodes["node3"].stop_clickhouse()
2022-08-11 15:42:32 +00:00
check_nodes_count(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_nodes - 2
2022-08-11 15:42:32 +00:00
)
2021-11-16 08:38:49 +00:00
2022-08-12 11:05:13 +00:00
nodes["node1"].start_clickhouse()
2022-08-11 15:42:32 +00:00
check_nodes_count(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_nodes - 1
2022-08-11 15:42:32 +00:00
)
2021-11-16 08:38:49 +00:00
2022-08-12 11:05:13 +00:00
nodes["node3"].start_clickhouse()
2022-08-11 15:42:32 +00:00
check_nodes_count(
2022-08-12 11:05:13 +00:00
[nodes["node0"], nodes["node2"], nodes["node_observer"]], total_nodes
2022-08-11 15:42:32 +00:00
)
# regular cluster is not affected
2022-08-11 15:42:32 +00:00
check_nodes_count(
2022-08-12 11:05:13 +00:00
[nodes["node1"], nodes["node2"]], 2, cluster_name="two_shards", retries=1
2022-08-11 15:42:32 +00:00
)
2024-11-18 13:38:46 +00:00
# cleanup
2024-11-18 15:18:36 +00:00
nodes["node0"].query("DROP TABLE tbl ON CLUSTER 'test_auto_cluster' SYNC")