ClickHouse/tests/integration/test_keeper_persistent_log/test.py

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

215 lines
6.0 KiB
Python
Raw Normal View History

2021-02-17 11:53:47 +00:00
#!/usr/bin/env python3
2024-09-27 10:19:39 +00:00
import os
2021-02-17 11:53:47 +00:00
import random
import string
import time
2024-09-27 10:19:39 +00:00
import pytest
2021-02-17 11:53:47 +00:00
from kazoo.client import KazooClient, KazooState
2024-09-27 10:19:39 +00:00
from helpers.cluster import ClickHouseCluster
2021-02-17 11:53:47 +00:00
cluster = ClickHouseCluster(__file__)
node = cluster.add_instance(
"node",
main_configs=["configs/enable_keeper.xml", "configs/use_keeper.xml"],
stay_alive=True,
)
2021-02-17 11:53:47 +00:00
def random_string(length):
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))
2021-02-17 11:53:47 +00:00
def create_random_path(prefix="", depth=1):
if depth == 0:
return prefix
return create_random_path(os.path.join(prefix, random_string(3)), depth - 1)
2021-02-17 11:53:47 +00:00
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
2021-02-17 11:53:47 +00:00
def get_connection_zk(nodename, timeout=30.0):
_fake_zk_instance = KazooClient(
hosts=cluster.get_instance_ip(nodename) + ":9181", timeout=timeout
)
_fake_zk_instance.start()
return _fake_zk_instance
2022-09-06 10:58:14 +00:00
def restart_clickhouse():
node.restart_clickhouse(kill=True)
2021-02-17 11:53:47 +00:00
def test_state_after_restart(started_cluster):
try:
node_zk = None
node_zk2 = None
node_zk = get_connection_zk("node")
node_zk.create("/test_state_after_restart", b"somevalue")
strs = []
for i in range(100):
strs.append(random_string(123).encode())
node_zk.create("/test_state_after_restart/node" + str(i), strs[i])
for i in range(100):
if i % 7 == 0:
node_zk.delete("/test_state_after_restart/node" + str(i))
2022-09-06 10:58:14 +00:00
restart_clickhouse()
2021-02-17 11:53:47 +00:00
node_zk2 = get_connection_zk("node")
assert node_zk2.get("/test_state_after_restart")[0] == b"somevalue"
for i in range(100):
if i % 7 == 0:
assert (
node_zk2.exists("/test_state_after_restart/node" + str(i)) is None
)
2021-02-17 11:53:47 +00:00
else:
assert (
len(node_zk2.get("/test_state_after_restart/node" + str(i))[0])
== 123
)
2021-02-17 11:53:47 +00:00
assert (
node_zk2.get("/test_state_after_restart/node" + str(i))[0]
== strs[i]
)
2021-02-17 11:53:47 +00:00
finally:
try:
if node_zk is not None:
node_zk.stop()
node_zk.close()
if node_zk2 is not None:
node_zk2.stop()
node_zk2.close()
except:
pass
2021-09-21 14:29:05 +00:00
def test_state_duplicate_restart(started_cluster):
try:
node_zk = None
node_zk2 = None
node_zk3 = None
node_zk = get_connection_zk("node")
node_zk.create("/test_state_duplicated_restart", b"somevalue")
strs = []
for i in range(100):
strs.append(random_string(123).encode())
node_zk.create("/test_state_duplicated_restart/node" + str(i), strs[i])
for i in range(100):
if i % 7 == 0:
node_zk.delete("/test_state_duplicated_restart/node" + str(i))
2022-09-06 10:58:14 +00:00
restart_clickhouse()
2021-09-21 14:29:05 +00:00
node_zk2 = get_connection_zk("node")
node_zk2.create("/test_state_duplicated_restart/just_test1")
node_zk2.create("/test_state_duplicated_restart/just_test2")
node_zk2.create("/test_state_duplicated_restart/just_test3")
2022-09-06 10:58:14 +00:00
restart_clickhouse()
2021-09-21 14:29:05 +00:00
node_zk3 = get_connection_zk("node")
assert node_zk3.get("/test_state_duplicated_restart")[0] == b"somevalue"
for i in range(100):
if i % 7 == 0:
assert (
node_zk3.exists("/test_state_duplicated_restart/node" + str(i))
is None
)
2021-09-21 14:29:05 +00:00
else:
assert (
len(node_zk3.get("/test_state_duplicated_restart/node" + str(i))[0])
== 123
)
2021-09-21 14:29:05 +00:00
assert (
node_zk3.get("/test_state_duplicated_restart/node" + str(i))[0]
== strs[i]
)
2021-09-21 14:29:05 +00:00
finally:
try:
if node_zk is not None:
node_zk.stop()
node_zk.close()
if node_zk2 is not None:
node_zk2.stop()
node_zk2.close()
if node_zk3 is not None:
node_zk3.stop()
node_zk3.close()
except:
pass
2021-02-17 11:53:47 +00:00
# http://zookeeper-user.578899.n2.nabble.com/Why-are-ephemeral-nodes-written-to-disk-tp7583403p7583418.html
def test_ephemeral_after_restart(started_cluster):
try:
node_zk = None
node_zk2 = None
node_zk = get_connection_zk("node")
node_zk.create("/test_ephemeral_after_restart", b"somevalue")
strs = []
for i in range(100):
strs.append(random_string(123).encode())
node_zk.create(
"/test_ephemeral_after_restart/node" + str(i), strs[i], ephemeral=True
)
for i in range(100):
if i % 7 == 0:
node_zk.delete("/test_ephemeral_after_restart/node" + str(i))
2022-09-06 10:58:14 +00:00
restart_clickhouse()
2021-02-17 11:53:47 +00:00
node_zk2 = get_connection_zk("node")
assert node_zk2.get("/test_ephemeral_after_restart")[0] == b"somevalue"
for i in range(100):
if i % 7 == 0:
assert (
node_zk2.exists("/test_ephemeral_after_restart/node" + str(i))
is None
)
2021-02-17 11:53:47 +00:00
else:
assert (
len(node_zk2.get("/test_ephemeral_after_restart/node" + str(i))[0])
== 123
)
2021-02-17 11:53:47 +00:00
assert (
node_zk2.get("/test_ephemeral_after_restart/node" + str(i))[0]
== strs[i]
)
2021-02-17 11:53:47 +00:00
finally:
try:
if node_zk is not None:
node_zk.stop()
node_zk.close()
if node_zk2 is not None:
node_zk2.stop()
node_zk2.close()
except:
pass