2023-04-20 13:26:02 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-11-16 09:02:11 +00:00
|
|
|
import time
|
2023-09-07 00:36:39 +00:00
|
|
|
import typing as tp
|
2024-09-27 10:19:39 +00:00
|
|
|
from os.path import dirname, join, realpath
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
import helpers.keeper_utils as ku
|
|
|
|
from helpers.cluster import ClickHouseCluster, ClickHouseInstance
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
|
|
|
CONFIG_DIR = join(dirname(realpath(__file__)), "configs")
|
|
|
|
|
|
|
|
node1 = cluster.add_instance("node1", main_configs=["configs/keeper1.xml"])
|
|
|
|
node2 = cluster.add_instance("node2", main_configs=["configs/keeper2.xml"])
|
|
|
|
node3 = cluster.add_instance("node3", main_configs=["configs/keeper3.xml"])
|
|
|
|
node4 = cluster.add_instance("node4", stay_alive=True)
|
|
|
|
zk1, zk2, zk3, zk4 = None, None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
|
|
|
|
|
|
|
node4.stop_clickhouse()
|
|
|
|
node4.copy_file_to_container(
|
|
|
|
join(CONFIG_DIR, "keeper4.xml"),
|
|
|
|
"/etc/clickhouse-server/config.d/keeper.xml",
|
|
|
|
)
|
|
|
|
|
|
|
|
yield cluster
|
|
|
|
|
|
|
|
finally:
|
2023-09-07 00:36:39 +00:00
|
|
|
conn: tp.Optional[ku.KeeperClient]
|
2023-04-20 13:26:02 +00:00
|
|
|
for conn in [zk1, zk2, zk3, zk4]:
|
|
|
|
if conn:
|
|
|
|
conn.stop()
|
|
|
|
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
2023-09-07 00:36:39 +00:00
|
|
|
def create_client(node: ClickHouseInstance):
|
|
|
|
return ku.KeeperClient(
|
|
|
|
cluster.server_bin_path, cluster.get_instance_ip(node.name), 9181
|
|
|
|
)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_reconfig_replace_leader(started_cluster):
|
|
|
|
"""
|
|
|
|
Remove leader from a cluster of 3 and add a new node via two commands.
|
|
|
|
"""
|
2023-09-07 00:36:39 +00:00
|
|
|
global zk1, zk2, zk3, zk4
|
|
|
|
zk1 = create_client(node1)
|
2023-04-20 13:26:02 +00:00
|
|
|
config = ku.get_config_str(zk1)
|
|
|
|
|
|
|
|
assert len(config.split("\n")) == 3
|
|
|
|
assert "node1" in config
|
|
|
|
assert "node2" in config
|
|
|
|
assert "node3" in config
|
|
|
|
assert "node4" not in config
|
|
|
|
|
|
|
|
for i in range(100):
|
2023-09-07 00:36:39 +00:00
|
|
|
zk1.create(f"/test_four_{i}", "somedata")
|
2023-04-20 13:26:02 +00:00
|
|
|
|
2023-09-07 00:36:39 +00:00
|
|
|
zk2 = create_client(node2)
|
2023-04-20 13:26:02 +00:00
|
|
|
zk2.sync("/test_four_0")
|
2023-07-03 16:38:26 +00:00
|
|
|
ku.wait_configs_equal(config, zk2)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
2023-09-07 00:36:39 +00:00
|
|
|
zk3 = create_client(node3)
|
2023-04-20 13:26:02 +00:00
|
|
|
zk3.sync("/test_four_0")
|
2023-07-03 16:38:26 +00:00
|
|
|
ku.wait_configs_equal(config, zk3)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
for i in range(100):
|
2023-09-07 00:36:39 +00:00
|
|
|
assert zk2.exists(f"/test_four_{i}")
|
|
|
|
assert zk3.exists(f"/test_four_{i}")
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
assert ku.is_leader(cluster, node1)
|
2023-09-07 00:36:39 +00:00
|
|
|
config = zk2.reconfig(joining=None, leaving="1", new_members=None)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
print("After removing 1 (leader)", config)
|
|
|
|
assert len(config.split("\n")) == 2
|
|
|
|
assert "node1" not in config
|
|
|
|
assert "node2" in config
|
|
|
|
assert "node3" in config
|
|
|
|
assert "node4" not in config
|
|
|
|
|
2023-11-16 09:02:11 +00:00
|
|
|
# wait until cluster stabilizes with a new leader
|
|
|
|
while not ku.is_leader(started_cluster, node2) and not ku.is_leader(
|
|
|
|
started_cluster, node3
|
|
|
|
):
|
|
|
|
time.sleep(1)
|
|
|
|
|
2023-08-16 14:37:30 +00:00
|
|
|
# additional 20s wait before removing leader
|
|
|
|
ku.wait_configs_equal(config, zk2, timeout=50)
|
2023-07-27 10:36:02 +00:00
|
|
|
|
2023-04-20 13:26:02 +00:00
|
|
|
node4.start_clickhouse()
|
2023-09-07 00:44:54 +00:00
|
|
|
config = zk2.reconfig(joining="server.4=node4:9234", leaving=None, new_members=None)
|
2023-04-20 13:26:02 +00:00
|
|
|
ku.wait_until_connected(cluster, node4)
|
|
|
|
|
|
|
|
print("After adding 4", config)
|
|
|
|
assert len(config.split("\n")) == 3
|
|
|
|
assert "node1" not in config
|
|
|
|
assert "node2" in config
|
|
|
|
assert "node3" in config
|
|
|
|
assert "node4" in config
|
|
|
|
|
2023-09-07 00:36:39 +00:00
|
|
|
zk4 = create_client(node4)
|
2023-07-03 16:38:26 +00:00
|
|
|
ku.wait_configs_equal(config, zk4)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
for i in range(100):
|
2023-09-07 00:36:39 +00:00
|
|
|
assert zk4.exists(f"test_four_{i}")
|
|
|
|
zk4.create(f"/test_four_{100 + i}", "somedata")
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
zk2.stop()
|
2023-09-07 00:36:39 +00:00
|
|
|
zk2 = create_client(node2)
|
2023-04-20 13:26:02 +00:00
|
|
|
zk2.sync("/test_four_0")
|
2023-07-03 16:38:26 +00:00
|
|
|
ku.wait_configs_equal(config, zk2)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
zk3.stop()
|
2023-09-07 00:36:39 +00:00
|
|
|
zk3 = create_client(node3)
|
2023-04-20 13:26:02 +00:00
|
|
|
zk3.sync("/test_four_0")
|
2023-07-03 16:38:26 +00:00
|
|
|
ku.wait_configs_equal(config, zk3)
|
2023-04-20 13:26:02 +00:00
|
|
|
|
|
|
|
for i in range(200):
|
|
|
|
assert zk2.exists(f"test_four_{i}") is not None
|
|
|
|
assert zk3.exists(f"test_four_{i}") is not None
|