|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from helpers.cluster import ClickHouseCluster |
| 8 | +from helpers.test_tools import TSV |
| 9 | + |
| 10 | +cluster = ClickHouseCluster(__file__) |
| 11 | + |
| 12 | + |
| 13 | +def wait_table_sync(zk_client, zk_table_path): |
| 14 | + _, parts_stat = zk_client.get(f"{zk_table_path}/parts") |
| 15 | + nodes = zk_client.get_children(f"{zk_table_path}/replicas") |
| 16 | + |
| 17 | + for _ in range(60): |
| 18 | + nodes_in_sync = 0 |
| 19 | + for node in nodes: |
| 20 | + node_epoch, _ = zk_client.get(f"{zk_table_path}/replicas/{node}/epoch") |
| 21 | + if int(node_epoch) >= parts_stat.pzxid: |
| 22 | + nodes_in_sync += 1 |
| 23 | + |
| 24 | + if nodes_in_sync == len(nodes): |
| 25 | + return |
| 26 | + |
| 27 | + time.sleep(0.5) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="module") |
| 31 | +def started_cluster(): |
| 32 | + try: |
| 33 | + cluster.add_instance( |
| 34 | + "node1", |
| 35 | + main_configs=["configs/config.yaml"], |
| 36 | + with_minio=True, |
| 37 | + with_zookeeper=True, |
| 38 | + stay_alive=True, |
| 39 | + use_keeper=False, |
| 40 | + cpu_limit=3, |
| 41 | + ) |
| 42 | + cluster.add_instance( |
| 43 | + "node2", |
| 44 | + main_configs=["configs/config.yaml"], |
| 45 | + with_minio=True, |
| 46 | + with_zookeeper=True, |
| 47 | + stay_alive=True, |
| 48 | + use_keeper=False, |
| 49 | + cpu_limit=3 |
| 50 | + ) |
| 51 | + cluster.start() |
| 52 | + yield cluster |
| 53 | + finally: |
| 54 | + cluster.shutdown() |
| 55 | + |
| 56 | + |
| 57 | +def test_insert_select(started_cluster): |
| 58 | + """ |
| 59 | + Basic scenario with inserts and replica synchronization |
| 60 | + """ |
| 61 | + |
| 62 | + for node_name, node in cluster.instances.items(): |
| 63 | + data = node.query(f""" |
| 64 | + CREATE TABLE test_basic (key UInt32, val String) |
| 65 | + ENGINE = AmateurMergeTree('/clickhouse/tables/default/test_basic', '{node_name}') |
| 66 | + ORDER BY key |
| 67 | + SETTINGS storage_policy = 's3' |
| 68 | + """) |
| 69 | + |
| 70 | + cluster.instances["node1"].query(""" |
| 71 | + INSERT INTO test_basic SELECT number, 'val-' || number FROM numbers(10) |
| 72 | + """) |
| 73 | + |
| 74 | + cluster.instances["node2"].query(""" |
| 75 | + INSERT INTO test_basic SELECT number + 10, 'val-' || (number + 10) FROM numbers(10) |
| 76 | + """) |
| 77 | + |
| 78 | + zk_client = cluster.get_kazoo_client("zoo1") |
| 79 | + wait_table_sync(zk_client, "/clickhouse/tables/default/test_basic") |
| 80 | + |
| 81 | + expected = [[0, 'val-0', 19, 'val-19']] |
| 82 | + for node_name, node in cluster.instances.items(): |
| 83 | + actual = node.query(""" |
| 84 | + SELECT min(key), argMin(val, key), max(key), argMax(val, key) FROM test_basic |
| 85 | + """) |
| 86 | + |
| 87 | + assert TSV(actual) == TSV(expected), f"Received an unexpected result from node {node_name}" |
0 commit comments