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
|
{
lib,
pkgs,
package,
...
}:
rec {
name = "clickhouse-${package.version}-keeper";
meta.maintainers = with pkgs.lib.maintainers; [
jpds
thevar1able
];
nodes =
let
node = i: {
environment.etc = {
"clickhouse-server/config.d/cluster.xml".text = ''
<clickhouse>
<remote_servers>
<perftest_2shards_1replicas>
${lib.concatStrings (
lib.imap0 (j: name: ''
<shard>
<replica>
<host>${name}</host>
<port>9000</port>
</replica>
</shard>
'') (builtins.attrNames nodes)
)}
</perftest_2shards_1replicas>
</remote_servers>
</clickhouse>
'';
"clickhouse-server/config.d/keeper.xml".text = ''
<clickhouse>
<keeper_server>
<server_id>${toString i}</server_id>
<tcp_port>9181</tcp_port>
<log_storage_path>/var/lib/clickhouse/coordination/log</log_storage_path>
<snapshot_storage_path>/var/lib/clickhouse/coordination/snapshots</snapshot_storage_path>
<coordination_settings>
<operation_timeout_ms>10000</operation_timeout_ms>
<session_timeout_ms>30000</session_timeout_ms>
<raft_logs_level>trace</raft_logs_level>
<rotate_log_storage_interval>10000</rotate_log_storage_interval>
</coordination_settings>
<raft_configuration>
${lib.concatStrings (
lib.imap1 (j: name: ''
<server>
<id>${toString j}</id>
<hostname>${name}</hostname>
<port>9444</port>
</server>
'') (builtins.attrNames nodes)
)}
</raft_configuration>
</keeper_server>
<zookeeper>
${lib.concatStrings (
lib.imap0 (j: name: ''
<node>
<host>${name}</host>
<port>9181</port>
</node>
'') (builtins.attrNames nodes)
)}
</zookeeper>
<distributed_ddl>
<path>/clickhouse/testcluster/task_queue/ddl</path>
</distributed_ddl>
</clickhouse>
'';
"clickhouse-server/config.d/listen.xml".text = ''
<clickhouse>
<listen_host>::</listen_host>
</clickhouse>
'';
"clickhouse-server/config.d/macros.xml".text = ''
<clickhouse>
<macros>
<replica>${toString i}</replica>
<cluster>perftest_2shards_1replicas</cluster>
</macros>
</clickhouse>
'';
};
networking.firewall.allowedTCPPorts = [
9009
9181
9444
];
services.clickhouse = {
enable = true;
inherit package;
};
systemd.services.clickhouse = {
after = [ "network-online.target" ];
requires = [ "network-online.target" ];
};
virtualisation.memorySize = 1024 * 4;
virtualisation.diskSize = 1024 * 10;
};
in
{
clickhouse1 = node 1;
clickhouse2 = node 2;
};
testScript =
let
# work around quote/substitution complexity by Nix, Perl, bash and SQL.
clustersQuery = pkgs.writeText "clusters.sql" "SHOW clusters";
keeperQuery = pkgs.writeText "keeper.sql" "SELECT * FROM system.zookeeper WHERE path IN ('/', '/clickhouse') FORMAT VERTICAL";
systemClustersQuery = pkgs.writeText "system-clusters.sql" "SELECT host_name, host_address, replica_num FROM system.clusters WHERE cluster = 'perftest_2shards_1replicas'";
tableDDL = pkgs.writeText "table.sql" ''
CREATE TABLE test ON cluster 'perftest_2shards_1replicas' ( A Int64, S String)
Engine = ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/{table}', '{replica}')
ORDER BY A;
'';
insertDDL = pkgs.writeText "insert.sql" "
INSERT INTO test SELECT number, '' FROM numbers(100000000);
";
selectCountQuery = pkgs.writeText "select-count.sql" "
select count() from test;
";
in
''
clickhouse1.start()
clickhouse2.start()
for machine in clickhouse1, clickhouse2:
machine.wait_for_unit("clickhouse.service")
machine.wait_for_open_port(9000)
machine.wait_for_open_port(9009)
machine.wait_for_open_port(9181)
machine.wait_for_open_port(9444)
machine.wait_until_succeeds(
"""
journalctl -o cat -u clickhouse.service | grep "Merging configuration file '/etc/clickhouse-server/config.d/keeper.xml'"
"""
)
machine.log(machine.succeed(
"cat ${clustersQuery} | clickhouse-client | grep perftest_2shards_1replicas"
))
machine.log(machine.succeed(
"cat ${keeperQuery} | clickhouse-client"
))
machine.succeed(
"cat ${systemClustersQuery} | clickhouse-client | grep clickhouse1"
)
machine.succeed(
"cat ${systemClustersQuery} | clickhouse-client | grep clickhouse2"
)
machine.succeed(
"ls /var/lib/clickhouse/coordination/log | grep changelog"
)
clickhouse2.succeed(
"cat ${tableDDL} | clickhouse-client"
)
clickhouse2.succeed(
"cat ${insertDDL} | clickhouse-client"
)
for machine in clickhouse1, clickhouse2:
machine.wait_until_succeeds(
"cat ${selectCountQuery} | clickhouse-client | grep 100000000"
)
'';
}
|