forked from ClickHouse/ClickHouse
/
test.py
253 lines (219 loc) · 7.61 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import threading
import time
import pytest
from helpers.cluster import ClickHouseCluster
from helpers.test_tools import assert_eq_with_retry
cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance(
"node1",
main_configs=["configs/logs_config.xml"],
with_zookeeper=True,
macros={"shard": 0, "replica": 1},
)
node2 = cluster.add_instance(
"node2",
main_configs=["configs/logs_config.xml"],
with_zookeeper=True,
macros={"shard": 0, "replica": 2},
)
settings = {
"mutations_sync": 2,
"replication_alter_partitions_sync": 2,
"optimize_throw_if_noop": 1,
}
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster.start()
node1.query(
"CREATE DATABASE test ENGINE=Ordinary",
settings={"allow_deprecated_database_ordinary": 1},
) # Different paths with Atomic
node2.query(
"CREATE DATABASE test ENGINE=Ordinary",
settings={"allow_deprecated_database_ordinary": 1},
)
yield cluster
finally:
cluster.shutdown()
def split_tsv(data):
return [x.split("\t") for x in data.splitlines()]
@pytest.mark.parametrize("replicated", ["", "replicated"])
def test_merge_simple(started_cluster, replicated):
clickhouse_path = "/var/lib/clickhouse"
db_name = "test"
table_name = "merge_simple"
name = db_name + "." + table_name
table_path = "data/" + db_name + "/" + table_name
nodes = [node1, node2] if replicated else [node1]
engine = (
"ReplicatedMergeTree('/clickhouse/test_merge_simple', '{replica}')"
if replicated
else "MergeTree()"
)
node_check = nodes[-1]
starting_block = 0 if replicated else 1
try:
for node in nodes:
node.query(
f"create table {name} (a Int64) engine={engine} order by tuple()"
)
node1.query(f"INSERT INTO {name} VALUES (1)")
node1.query(f"INSERT INTO {name} VALUES (2)")
node1.query(f"INSERT INTO {name} VALUES (3)")
node1.query(
f"alter table {name} add column b int materialized sleepEachRow(3)",
settings=settings,
)
parts = [
"all_{}_{}_0".format(x, x)
for x in range(starting_block, starting_block + 3)
]
result_part = "all_{}_{}_1".format(starting_block, starting_block + 2)
# OPTIMIZE will sleep for 3s * 3 (parts) = 9s
def optimize():
node1.query("OPTIMIZE TABLE {name}".format(name=name), settings=settings)
t = threading.Thread(target=optimize)
t.start()
# Wait for OPTIMIZE to actually start
assert_eq_with_retry(
node_check,
f"select count() from system.merges where table='{table_name}'",
"1\n",
retry_count=30,
sleep_time=0.1,
)
assert (
split_tsv(
node_check.query(
"""
SELECT database, table, num_parts, source_part_names, source_part_paths, result_part_name, result_part_path, partition_id, is_mutation
FROM system.merges
WHERE table = '{name}'
""".format(
name=table_name
)
)
)
== [
[
db_name,
table_name,
"3",
"['{}','{}','{}']".format(*parts),
"['{clickhouse}/{table_path}/{}/','{clickhouse}/{table_path}/{}/','{clickhouse}/{table_path}/{}/']".format(
*parts, clickhouse=clickhouse_path, table_path=table_path
),
result_part,
"{clickhouse}/{table_path}/{}/".format(
result_part, clickhouse=clickhouse_path, table_path=table_path
),
"all",
"0",
]
]
)
t.join()
# It still can show a row with progress=1, because OPTIMIZE returns before the entry is removed from MergeList
assert (
node_check.query(
f"SELECT * FROM system.merges WHERE table = '{table_name}' and progress < 1"
)
== ""
)
# It will eventually disappear
assert_eq_with_retry(
node_check,
f"SELECT * FROM system.merges WHERE table = '{table_name}' and progress < 1",
"\n",
)
finally:
for node in nodes:
node.query("DROP TABLE {name}".format(name=name))
@pytest.mark.parametrize("replicated", ["", "replicated"])
def test_mutation_simple(started_cluster, replicated):
clickhouse_path = "/var/lib/clickhouse"
db_name = "test"
table_name = "mutation_simple"
name = db_name + "." + table_name
table_path = "data/" + db_name + "/" + table_name
nodes = [node1, node2] if replicated else [node1]
engine = (
"ReplicatedMergeTree('/clickhouse/test_mutation_simple', '{replica}')"
if replicated
else "MergeTree()"
)
node_check = nodes[-1]
starting_block = 0 if replicated else 1
try:
for node in nodes:
node.query(
f"create table {name} (a Int64) engine={engine} order by tuple()"
)
node1.query(f"INSERT INTO {name} VALUES (1), (2), (3)")
part = "all_{}_{}_0".format(starting_block, starting_block)
result_part = "all_{}_{}_0_{}".format(
starting_block, starting_block, starting_block + 1
)
# ALTER will sleep for 3s * 3 (rows) = 9s
def alter():
node1.query(
f"ALTER TABLE {name} UPDATE a = 42 WHERE sleep(9) = 0",
settings=settings,
)
t = threading.Thread(target=alter)
t.start()
# Wait for the mutation to actually start
assert_eq_with_retry(
node_check,
f"select count() from system.merges where table='{table_name}'",
"1\n",
retry_count=30,
sleep_time=0.1,
)
assert (
split_tsv(
node_check.query(
"""
SELECT database, table, num_parts, source_part_names, source_part_paths, result_part_name, result_part_path, partition_id, is_mutation
FROM system.merges
WHERE table = '{name}'
""".format(
name=table_name
)
)
)
== [
[
db_name,
table_name,
"1",
"['{}']".format(part),
"['{clickhouse}/{table_path}/{}/']".format(
part, clickhouse=clickhouse_path, table_path=table_path
),
result_part,
"{clickhouse}/{table_path}/{}/".format(
result_part, clickhouse=clickhouse_path, table_path=table_path
),
"all",
"1",
],
]
)
t.join()
assert (
node_check.query(
f"SELECT * FROM system.merges WHERE table = '{table_name}' and progress < 1"
)
== ""
)
# It will eventually disappear
assert_eq_with_retry(
node_check,
f"SELECT * FROM system.merges WHERE table = '{table_name}' and progress < 1",
"\n",
)
finally:
for node in nodes:
node.query("DROP TABLE {name}".format(name=name))