2018-12-28 13:39:44 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from helpers.cluster import ClickHouseCluster
|
|
|
|
from helpers.test_tools import assert_eq_with_retry
|
|
|
|
|
|
|
|
cluster = ClickHouseCluster(__file__)
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
node1 = cluster.add_instance(
|
|
|
|
"node1",
|
|
|
|
main_configs=["configs/remote_servers.xml"],
|
|
|
|
user_configs=["configs/user_good_restricted.xml"],
|
|
|
|
with_zookeeper=True,
|
|
|
|
)
|
|
|
|
node2 = cluster.add_instance(
|
|
|
|
"node2",
|
|
|
|
main_configs=["configs/remote_servers.xml"],
|
|
|
|
user_configs=["configs/user_good_restricted.xml"],
|
|
|
|
with_zookeeper=True,
|
|
|
|
)
|
|
|
|
node3 = cluster.add_instance(
|
|
|
|
"node3",
|
|
|
|
main_configs=["configs/remote_servers.xml"],
|
|
|
|
user_configs=["configs/user_good_allowed.xml"],
|
|
|
|
with_zookeeper=True,
|
|
|
|
)
|
|
|
|
node4 = cluster.add_instance(
|
|
|
|
"node4",
|
|
|
|
main_configs=["configs/remote_servers.xml"],
|
|
|
|
user_configs=["configs/user_good_allowed.xml"],
|
|
|
|
with_zookeeper=True,
|
|
|
|
)
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2018-12-28 13:39:44 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
|
|
|
|
|
|
|
for node in [node1, node2]:
|
2022-03-22 16:39:58 +00:00
|
|
|
node.query(
|
|
|
|
"""
|
2018-12-28 13:39:44 +00:00
|
|
|
CREATE TABLE sometable(date Date, id UInt32, value Int32)
|
|
|
|
ENGINE = ReplicatedMergeTree('/clickhouse/tables/0/sometable', '{replica}', date, id, 8192);
|
2022-03-22 16:39:58 +00:00
|
|
|
""".format(
|
|
|
|
replica=node.name
|
|
|
|
),
|
|
|
|
user="awesome",
|
|
|
|
)
|
2018-12-28 13:39:44 +00:00
|
|
|
|
2018-12-28 17:11:52 +00:00
|
|
|
for node in [node3, node4]:
|
2022-03-22 16:39:58 +00:00
|
|
|
node.query(
|
|
|
|
"""
|
2018-12-28 17:11:52 +00:00
|
|
|
CREATE TABLE someothertable(date Date, id UInt32, value Int32)
|
|
|
|
ENGINE = ReplicatedMergeTree('/clickhouse/tables/0/someothertable', '{replica}', date, id, 8192);
|
2022-03-22 16:39:58 +00:00
|
|
|
""".format(
|
|
|
|
replica=node.name
|
|
|
|
),
|
|
|
|
user="good",
|
|
|
|
)
|
2018-12-28 17:11:52 +00:00
|
|
|
|
2018-12-28 13:39:44 +00:00
|
|
|
yield cluster
|
|
|
|
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"table,query,expected,n1,n2",
|
|
|
|
[
|
|
|
|
pytest.param(
|
|
|
|
"sometable",
|
|
|
|
"ALTER TABLE sometable DROP PARTITION 201706",
|
|
|
|
"1",
|
|
|
|
node1,
|
|
|
|
node2,
|
|
|
|
id="case1",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"sometable", "TRUNCATE TABLE sometable", "0", node1, node2, id="case2"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"sometable", "OPTIMIZE TABLE sometable", "4", node1, node2, id="case3"
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"someothertable",
|
|
|
|
"ALTER TABLE someothertable DROP PARTITION 201706",
|
|
|
|
"1",
|
|
|
|
node3,
|
|
|
|
node4,
|
|
|
|
id="case4",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"someothertable",
|
|
|
|
"TRUNCATE TABLE someothertable",
|
|
|
|
"0",
|
|
|
|
node3,
|
|
|
|
node4,
|
|
|
|
id="case5",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"someothertable",
|
|
|
|
"OPTIMIZE TABLE someothertable",
|
|
|
|
"4",
|
|
|
|
node3,
|
|
|
|
node4,
|
|
|
|
id="case6",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-12-28 17:11:52 +00:00
|
|
|
def test_alter_table_drop_partition(started_cluster, table, query, expected, n1, n2):
|
2022-03-22 16:39:58 +00:00
|
|
|
to_insert = """\
|
2018-12-28 13:39:44 +00:00
|
|
|
2017-06-16 111 0
|
|
|
|
2017-06-16 222 1
|
|
|
|
2017-06-16 333 2
|
|
|
|
2017-07-16 444 3
|
2022-03-22 16:39:58 +00:00
|
|
|
"""
|
|
|
|
n1.query("INSERT INTO {} FORMAT TSV".format(table), stdin=to_insert, user="good")
|
2018-12-28 13:39:44 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
assert_eq_with_retry(n1, "SELECT COUNT(*) from {}".format(table), "4", user="good")
|
|
|
|
assert_eq_with_retry(n2, "SELECT COUNT(*) from {}".format(table), "4", user="good")
|
2018-12-28 13:39:44 +00:00
|
|
|
|
|
|
|
### It maybe leader and everything will be ok
|
2022-03-22 16:39:58 +00:00
|
|
|
n1.query(query, user="good")
|
2018-12-28 13:39:44 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
assert_eq_with_retry(
|
|
|
|
n1, "SELECT COUNT(*) from {}".format(table), expected, user="good"
|
|
|
|
)
|
|
|
|
assert_eq_with_retry(
|
|
|
|
n2, "SELECT COUNT(*) from {}".format(table), expected, user="good"
|
|
|
|
)
|
2018-12-28 13:39:44 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
n1.query("INSERT INTO {} FORMAT TSV".format(table), stdin=to_insert, user="good")
|
2018-12-28 13:39:44 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
assert_eq_with_retry(n1, "SELECT COUNT(*) from {}".format(table), "4", user="good")
|
|
|
|
assert_eq_with_retry(n2, "SELECT COUNT(*) from {}".format(table), "4", user="good")
|
2018-12-28 13:39:44 +00:00
|
|
|
|
|
|
|
### If node1 is leader than node2 will be slave
|
2022-03-22 16:39:58 +00:00
|
|
|
n2.query(query, user="good")
|
|
|
|
|
|
|
|
assert_eq_with_retry(
|
|
|
|
n1, "SELECT COUNT(*) from {}".format(table), expected, user="good"
|
|
|
|
)
|
|
|
|
assert_eq_with_retry(
|
|
|
|
n2, "SELECT COUNT(*) from {}".format(table), expected, user="good"
|
|
|
|
)
|