|
1 | 1 | import pytest |
2 | 2 | import urllib.parse |
3 | 3 | from helpers.cluster import ClickHouseCluster |
| 4 | +import os |
4 | 5 |
|
5 | 6 | cluster = ClickHouseCluster(__file__) |
6 | 7 |
|
|
21 | 22 | ) |
22 | 23 |
|
23 | 24 |
|
24 | | -@pytest.fixture(scope="module") |
25 | | -def started_cluster(): |
26 | | - try: |
27 | | - cluster.start() |
| 25 | +def update_interserver_http_address(node, new_address): |
| 26 | + config_path = os.path.join(os.path.dirname(__file__), "configs/config.xml") |
28 | 27 |
|
29 | | - # Replace NODE_NAME placeholder with actual IP addresses in config |
30 | | - import os |
31 | | - config_path = os.path.join( |
32 | | - os.path.dirname(__file__), "configs/config.xml" |
33 | | - ) |
| 28 | + config_content = open(config_path).read() |
| 29 | + config_content = config_content.replace("NODE_NAME", new_address) |
34 | 30 |
|
35 | | - for node in [node1, node2]: |
36 | | - config_content = open(config_path).read() |
37 | | - config_content = config_content.replace("NODE_NAME", node.ip_address) |
| 31 | + # Debug: print the config and IP address |
| 32 | + print(f"Configuring {node.name} with address {new_address}") |
| 33 | + print(f"New config:\n{config_content}") |
38 | 34 |
|
39 | | - # Debug: print the config and IP address |
40 | | - print(f"Configuring {node.name} with IP {node.ip_address}") |
41 | | - print(f"Config content:\n{config_content}") |
| 35 | + node.exec_in_container( |
| 36 | + [ |
| 37 | + "bash", |
| 38 | + "-c", |
| 39 | + 'echo "${NEW_CONFIG}" > /etc/clickhouse-server/config.d/interserver_host.xml', |
| 40 | + ], |
| 41 | + environment={"NEW_CONFIG": config_content}, |
| 42 | + ) |
42 | 43 |
|
43 | | - node.exec_in_container( |
44 | | - ["bash", "-c", 'echo "${NEW_CONFIG}" > /etc/clickhouse-server/config.d/interserver_host.xml'], |
45 | | - environment={"NEW_CONFIG": config_content}, |
46 | | - ) |
| 44 | + # Verify the file was written |
| 45 | + result = node.exec_in_container( |
| 46 | + ["cat", "/etc/clickhouse-server/config.d/interserver_host.xml"] |
| 47 | + ) |
| 48 | + print(f"Verification - Config file on {node.name}:\n{result}") |
47 | 49 |
|
48 | | - # Verify the file was written |
49 | | - result = node.exec_in_container( |
50 | | - ["cat", "/etc/clickhouse-server/config.d/interserver_host.xml"] |
51 | | - ) |
52 | | - print(f"Verification - Config file on {node.name}:\n{result}") |
| 50 | + # IMPORTANT: interserver_http_host is only loaded at startup, not by SYSTEM RELOAD CONFIG |
| 51 | + # So we need to restart ClickHouse |
| 52 | + print( |
| 53 | + f"Restarting ClickHouse on {node.name} to apply interserver_http_host config..." |
| 54 | + ) |
| 55 | + node.restart_clickhouse() |
53 | 56 |
|
54 | | - # IMPORTANT: interserver_http_host is only loaded at startup, not by SYSTEM RELOAD CONFIG |
55 | | - # So we need to restart ClickHouse |
56 | | - print(f"Restarting ClickHouse on {node.name} to apply interserver_http_host config...") |
57 | | - node.restart_clickhouse() |
| 57 | + # Verify the setting was applied |
| 58 | + interserver_host = node.query( |
| 59 | + "SELECT value FROM system.server_settings WHERE name = 'interserver_http_host'" |
| 60 | + ) |
| 61 | + print( |
| 62 | + f"Verification - interserver_http_host setting on {node.name}: {interserver_host}" |
| 63 | + ) |
58 | 64 |
|
59 | | - # Verify the setting was applied |
60 | | - interserver_host = node.query("SELECT value FROM system.server_settings WHERE name = 'interserver_http_host'") |
61 | | - print(f"Verification - interserver_http_host setting on {node.name}: {interserver_host}") |
62 | 65 |
|
| 66 | +@pytest.fixture(scope="module") |
| 67 | +def started_cluster(): |
| 68 | + try: |
| 69 | + cluster.start() |
63 | 70 | yield cluster |
| 71 | + |
64 | 72 | finally: |
65 | 73 | cluster.shutdown() |
66 | 74 |
|
67 | 75 |
|
68 | 76 | def test_replicated_database_uses_interserver_host(started_cluster): |
69 | 77 | """Test that DatabaseReplicated uses interserver_http_host for replica registration.""" |
70 | 78 |
|
| 79 | + for node in [node1, node2]: |
| 80 | + update_interserver_http_address(node, node.ip_address) |
| 81 | + |
71 | 82 | node1.query( |
72 | 83 | "CREATE DATABASE test_db ENGINE = Replicated('/clickhouse/databases/test_db', 'shard1', 'node1')" |
73 | 84 | ) |
@@ -106,3 +117,36 @@ def test_replicated_database_uses_interserver_host(started_cluster): |
106 | 117 |
|
107 | 118 | node1.query("DROP DATABASE test_db SYNC") |
108 | 119 | node2.query("DROP DATABASE test_db SYNC") |
| 120 | + |
| 121 | + |
| 122 | +def test_replicated_database_uses_interserver_host_changed(started_cluster): |
| 123 | + """Test that interserver_http_host changed, and Replicated database is still able to be attached after restarting""" |
| 124 | + |
| 125 | + node1.query( |
| 126 | + "CREATE DATABASE test_db ENGINE = Replicated('/clickhouse/databases/test_db', 'shard1', 'node1')" |
| 127 | + ) |
| 128 | + node2.query( |
| 129 | + "CREATE DATABASE test_db ENGINE = Replicated('/clickhouse/databases/test_db', 'shard1', 'node2')" |
| 130 | + ) |
| 131 | + |
| 132 | + node1.query("CREATE TABLE test_db.t (x INT, y INT) ENGINE=MergeTree ORDER BY x") |
| 133 | + |
| 134 | + for node in [node1, node2]: |
| 135 | + update_interserver_http_address(node, node.name) |
| 136 | + |
| 137 | + node.query("SYSTEM FLUSH LOGS") |
| 138 | + assert ( |
| 139 | + node.query( |
| 140 | + """ |
| 141 | + SELECT count() |
| 142 | + FROM system.text_log |
| 143 | + WHERE (level='Error') |
| 144 | + AND (logger_name='DatabaseReplicated (test_db)') |
| 145 | + AND (message LIKE '%replicated database at /clickhouse/databases/test_db already exists%') |
| 146 | + """ |
| 147 | + ).strip() |
| 148 | + == "0" |
| 149 | + ) |
| 150 | + |
| 151 | + node1.query("DROP DATABASE test_db SYNC") |
| 152 | + node2.query("DROP DATABASE test_db SYNC") |
0 commit comments