|
| 1 | +import logging |
| 2 | +import random |
| 3 | +import string |
| 4 | +import uuid |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from helpers.cluster import ClickHouseCluster |
| 9 | +from helpers.config_cluster import minio_secret_key |
| 10 | + |
| 11 | +cluster = ClickHouseCluster(__file__) |
| 12 | + |
| 13 | + |
| 14 | +def gen_insert_values(size): |
| 15 | + return ",".join( |
| 16 | + f"({i},'{''.join(random.choices(string.ascii_lowercase, k=5))}')" |
| 17 | + for i in range(size) |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def randomize_name(table_name, random_suffix_length=8): |
| 22 | + letters = string.ascii_letters |
| 23 | + return f"{table_name}_{''.join(random.choice(letters) for _ in range(random_suffix_length))}" |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="module", autouse=True) |
| 27 | +def start_cluster(): |
| 28 | + cluster.add_instance( |
| 29 | + "node1", |
| 30 | + main_configs=["configs/storage_conf.xml"], |
| 31 | + with_minio=True, |
| 32 | + env_variables={"ENDPOINT_SUBPATH": "node1"}, |
| 33 | + stay_alive=True, |
| 34 | + ) |
| 35 | + cluster.add_instance( |
| 36 | + "node2", |
| 37 | + main_configs=["configs/storage_conf.xml"], |
| 38 | + with_minio=True, |
| 39 | + env_variables={ |
| 40 | + "ENDPOINT_SUBPATH": "node2", |
| 41 | + "ROTATED_REPLICA_ENDPOINT_SUBPATH": "node1", |
| 42 | + }, |
| 43 | + stay_alive=True, |
| 44 | + instance_env_variables=True, |
| 45 | + ) |
| 46 | + |
| 47 | + try: |
| 48 | + cluster.start() |
| 49 | + yield cluster |
| 50 | + finally: |
| 51 | + cluster.shutdown() |
| 52 | + |
| 53 | + |
| 54 | +def test_alter_partition_after_table_rotation(): |
| 55 | + node1 = cluster.instances["node1"] |
| 56 | + |
| 57 | + def create_insert(node, table_name, insert_values): |
| 58 | + node1.query(f"DROP TABLE IF EXISTS {table_name} SYNC") |
| 59 | + node.query( |
| 60 | + f"""CREATE TABLE {table_name} ( |
| 61 | + id Int64, |
| 62 | + data String |
| 63 | + ) ENGINE=MergeTree() |
| 64 | + ORDER BY id |
| 65 | + PARTITION BY id%10 |
| 66 | + SETTINGS storage_policy='s3_plain_rewritable' |
| 67 | + """ |
| 68 | + ) |
| 69 | + |
| 70 | + node.query("INSERT INTO {} VALUES {}".format(table_name, insert_values)) |
| 71 | + |
| 72 | + table1 = randomize_name("t1") |
| 73 | + create_insert(node1, table1, gen_insert_values(1000)) |
| 74 | + |
| 75 | + assert int(node1.query(f"SELECT count(*) FROM {table1}")) == 1000 |
| 76 | + |
| 77 | + uuid1 = node1.query( |
| 78 | + f"SELECT uuid FROM system.tables WHERE table='{table1}'" |
| 79 | + ).strip() |
| 80 | + |
| 81 | + node1.query(f"DETACH TABLE {table1} PERMANENTLY SYNC") |
| 82 | + |
| 83 | + disk_name = randomize_name("disk") |
| 84 | + node2 = cluster.instances["node2"] |
| 85 | + table2 = randomize_name("table2") |
| 86 | + node2.query(f"DROP TABLE IF EXISTS {table2} SYNC") |
| 87 | + node2.query( |
| 88 | + f"""CREATE TABLE {table2} (id Int64, data String) |
| 89 | + ENGINE=MergeTree() |
| 90 | + ORDER BY id |
| 91 | + PARTITION BY id%10 |
| 92 | + SETTINGS disk=disk( |
| 93 | + name={disk_name}, |
| 94 | + type='s3_plain_rewritable', |
| 95 | + endpoint='http://minio1:9001/root/data/node1', |
| 96 | + access_key_id='minio', |
| 97 | + secret_access_key='ClickHouse_Minio_P@ssw0rd') |
| 98 | + """ |
| 99 | + ) |
| 100 | + |
| 101 | + rotated_table = f"{table1}_rotated" |
| 102 | + node2.query(f"""DROP TABLE IF EXISTS {rotated_table} SYNC""") |
| 103 | + node2.query( |
| 104 | + f"""ATTACH TABLE {rotated_table} UUID '{uuid1}' (id Int64, data String) |
| 105 | + ENGINE=MergeTree() |
| 106 | + ORDER BY id |
| 107 | + PARTITION BY id%10 |
| 108 | + SETTINGS disk=disk( |
| 109 | + name={disk_name}, |
| 110 | + type='s3_plain_rewritable', |
| 111 | + endpoint='http://minio1:9001/root/data/node1', |
| 112 | + access_key_id='minio', |
| 113 | + secret_access_key='ClickHouse_Minio_P@ssw0rd') |
| 114 | + """ |
| 115 | + ) |
| 116 | + |
| 117 | + assert ( |
| 118 | + int( |
| 119 | + node2.query( |
| 120 | + f"SELECT count(*) FROM {rotated_table} WHERE _partition_id = '0'" |
| 121 | + ) |
| 122 | + ) |
| 123 | + == 100 |
| 124 | + ) |
| 125 | + |
| 126 | + assert int(node2.query(f"SELECT count(*) FROM {rotated_table}")) == 1000 |
| 127 | + |
| 128 | + node2.query(f"""ALTER TABLE {rotated_table} MOVE PARTITION '0' TO TABLE {table2}""") |
| 129 | + |
| 130 | + assert int(node2.query(f"SELECT count(*) FROM {rotated_table}")) == 900 |
| 131 | + assert int(node2.query(f"SELECT count(*) FROM {table2}")) == 100 |
| 132 | + |
| 133 | + node2.query(f"DROP TABLE {rotated_table} SYNC") |
| 134 | + node2.query(f"DROP TABLE {table2} SYNC") |
0 commit comments