git.iko.soy/typeable/nixpkgs
on master help sign in
cassandra: rewrote service from scratch

Adds a replacement for the previously broken `services.database.cassandra` with tests for a multi-node setup.

commit31e11bdd60fd164bc7f52c0a4f59024382a58851 authorThomas Bach <hacking@babibo.de> dateTue 7 Nov 2017, 14:11 (9 years ago) parent291018b
6 files changed+307−443
nixos/doc/manual/release-notes/rl-1809.xml+14 nixos/modules/misc/ids.nix+2 nixos/modules/module-list.nix+1 nixos/modules/services/databases/cassandra.nix+243−400 nixos/tests/cassandra.nix+46−43 pkgs/servers/nosql/cassandra/generic.nix+1
nixos/doc/manual/release-notes/rl-1809.xml+14
@@ -73,6 +73,14 @@ $ nix-instantiate -E '(import &lt;nixpkgsunstable&gt; {}).gitFull'
7373 </para>
7474
7575 <itemizedlist>
76+ <listitem>
77+ <para>
78+ The <varname>services.cassandra</varname> module has been reworked and
79+ was rewritten from scratch. The service has succeeding tests for
80+ the versions 2.1, 2.2, 3.0 and 3.11 of <link
81+ xlink:href="https://cassandra.apache.org/">Apache Cassandra</link>.
82+ </para>
83+ </listitem>
7684 <listitem>
7785 <para>
7886 There is a new <varname>services.foundationdb</varname> module for deploying
@@ -119,6 +127,12 @@ $ nix-instantiate -E '(import &lt;nixpkgsunstable&gt; {}).gitFull'
119127 </para>
120128
121129 <itemizedlist>
130+ <listitem>
131+ <para>
132+ The deprecated <varname>services.cassandra</varname> module has
133+ seen a complete rewrite. (See above.)
134+ </para>
135+ </listitem>
122136 <listitem>
123137 <para>
124138 <literal>lib.strict</literal> is removed. Use
nixos/modules/misc/ids.nix+2
@@ -324,6 +324,7 @@
324324 hadoop = 297;
325325 hydron = 298;
326326 cfssl = 299;
327+ cassandra = 300;
327328
328329 # When adding a uid, make sure it doesn't match an existing gid. And don't use uids above 399!
329330
@@ -608,6 +609,7 @@
608609 hadoop = 297;
609610 hydron = 298;
610611 cfssl = 299;
612+ cassandra = 300;
611613
612614 # When adding a gid, make sure it doesn't match an existing
613615 # uid. Users and groups with the same name should have equal
nixos/modules/module-list.nix+1
@@ -201,6 +201,7 @@
201201 ./services/databases/4store-endpoint.nix
202202 ./services/databases/4store.nix
203203 ./services/databases/aerospike.nix
204+ ./services/databases/cassandra.nix
204205 ./services/databases/clickhouse.nix
205206 ./services/databases/couchdb.nix
206207 ./services/databases/firebird.nix
nixos/modules/services/databases/cassandra.nix+243−400
@@ -4,445 +4,288 @@ with lib;
44
55let
66 cfg = config.services.cassandra;
7− cassandraPackage = cfg.package.override {
8− jre = cfg.jre;
9− };
10− cassandraUser = {
11− name = cfg.user;
12− home = "/var/lib/cassandra";
13− description = "Cassandra role user";
14− };
15−
16− cassandraRackDcProperties = ''
17− dc=${cfg.dc}
18− rack=${cfg.rack}
19− '';
20−
21− cassandraConf = ''
22− cluster_name: ${cfg.clusterName}
23− num_tokens: 256
24− auto_bootstrap: ${boolToString cfg.autoBootstrap}
25− hinted_handoff_enabled: ${boolToString cfg.hintedHandOff}
26− hinted_handoff_throttle_in_kb: ${builtins.toString cfg.hintedHandOffThrottle}
27− max_hints_delivery_threads: 2
28− max_hint_window_in_ms: 10800000 # 3 hours
29− authenticator: ${cfg.authenticator}
30− authorizer: ${cfg.authorizer}
31− permissions_validity_in_ms: 2000
32− partitioner: org.apache.cassandra.dht.Murmur3Partitioner
33− data_file_directories:
34− ${builtins.concatStringsSep "\n" (map (v: " - "+v) cfg.dataDirs)}
35− commitlog_directory: ${cfg.commitLogDirectory}
36− disk_failure_policy: stop
37− key_cache_size_in_mb:
38− key_cache_save_period: 14400
39− row_cache_size_in_mb: 0
40− row_cache_save_period: 0
41− saved_caches_directory: ${cfg.savedCachesDirectory}
42− commitlog_sync: ${cfg.commitLogSync}
43− commitlog_sync_period_in_ms: ${builtins.toString cfg.commitLogSyncPeriod}
44− commitlog_segment_size_in_mb: 32
45− seed_provider:
46− - class_name: org.apache.cassandra.locator.SimpleSeedProvider
47− parameters:
48− - seeds: "${builtins.concatStringsSep "," cfg.seeds}"
49− concurrent_reads: ${builtins.toString cfg.concurrentReads}
50− concurrent_writes: ${builtins.toString cfg.concurrentWrites}
51− memtable_flush_queue_size: 4
52− trickle_fsync: false
53− trickle_fsync_interval_in_kb: 10240
54− storage_port: 7000
55− ssl_storage_port: 7001
56− listen_address: ${cfg.listenAddress}
57− start_native_transport: true
58− native_transport_port: 9042
59− start_rpc: true
60− rpc_address: ${cfg.rpcAddress}
61− rpc_port: 9160
62− rpc_keepalive: true
63− rpc_server_type: sync
64− thrift_framed_transport_size_in_mb: 15
65− incremental_backups: ${boolToString cfg.incrementalBackups}
66− snapshot_before_compaction: false
67− auto_snapshot: true
68− column_index_size_in_kb: 64
69− in_memory_compaction_limit_in_mb: 64
70− multithreaded_compaction: false
71− compaction_throughput_mb_per_sec: 16
72− compaction_preheat_key_cache: true
73− read_request_timeout_in_ms: 10000
74− range_request_timeout_in_ms: 10000
75− write_request_timeout_in_ms: 10000
76− cas_contention_timeout_in_ms: 1000
77− truncate_request_timeout_in_ms: 60000
78− request_timeout_in_ms: 10000
79− cross_node_timeout: false
80− endpoint_snitch: ${cfg.snitch}
81− dynamic_snitch_update_interval_in_ms: 100
82− dynamic_snitch_reset_interval_in_ms: 600000
83− dynamic_snitch_badness_threshold: 0.1
84− request_scheduler: org.apache.cassandra.scheduler.NoScheduler
85− server_encryption_options:
86− internode_encryption: ${cfg.internodeEncryption}
87− keystore: ${cfg.keyStorePath}
88− keystore_password: ${cfg.keyStorePassword}
89− truststore: ${cfg.trustStorePath}
90− truststore_password: ${cfg.trustStorePassword}
91− client_encryption_options:
92− enabled: ${boolToString cfg.clientEncryption}
93− keystore: ${cfg.keyStorePath}
94− keystore_password: ${cfg.keyStorePassword}
95− internode_compression: all
96− inter_dc_tcp_nodelay: false
97− preheat_kernel_page_cache: false
98− streaming_socket_timeout_in_ms: ${toString cfg.streamingSocketTimoutInMS}
99− '';
100−
101− cassandraLog = ''
102− log4j.rootLogger=${cfg.logLevel},stdout
103− log4j.appender.stdout=org.apache.log4j.ConsoleAppender
104− log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
105− log4j.appender.stdout.layout.ConversionPattern=%5p [%t] %d{HH:mm:ss,SSS} %m%n
106− '';
107−
108− cassandraConfFile = pkgs.writeText "cassandra.yaml" cassandraConf;
109− cassandraLogFile = pkgs.writeText "log4j-server.properties" cassandraLog;
110− cassandraRackFile = pkgs.writeText "cassandra-rackdc.properties" cassandraRackDcProperties;
111−
112− cassandraEnvironment = {
113− CASSANDRA_HOME = cassandraPackage;
114− JAVA_HOME = cfg.jre;
115− CASSANDRA_CONF = "/etc/cassandra";
116− };
7+ defaultUser = "cassandra";
8+ cassandraConfig = flip recursiveUpdate cfg.extraConfig
9+ ({ commitlog_sync = "batch";
10+ commitlog_sync_batch_window_in_ms = 2;
11+ partitioner = "org.apache.cassandra.dht.Murmur3Partitioner";
12+ endpoint_snitch = "SimpleSnitch";
13+ seed_provider =
14+ [{ class_name = "org.apache.cassandra.locator.SimpleSeedProvider";
15+ parameters = [ { seeds = "127.0.0.1"; } ];
16+ }];
17+ data_file_directories = [ "${cfg.homeDir}/data" ];
18+ commitlog_directory = "${cfg.homeDir}/commitlog";
19+ saved_caches_directory = "${cfg.homeDir}/saved_caches";
20+ } // (if builtins.compareVersions cfg.package.version "3" >= 0
21+ then { hints_directory = "${cfg.homeDir}/hints"; }
22+ else {})
23+ );
24+ cassandraConfigWithAddresses = cassandraConfig //
25+ ( if isNull cfg.listenAddress
26+ then { listen_interface = cfg.listenInterface; }
27+ else { listen_address = cfg.listenAddress; }
28+ ) // (
29+ if isNull cfg.rpcAddress
30+ then { rpc_interface = cfg.rpcInterface; }
31+ else { rpc_address = cfg.rpcAddress; }
32+ );
33+ cassandraEtc = pkgs.stdenv.mkDerivation
34+ { name = "cassandra-etc";
35+ cassandraYaml = builtins.toJSON cassandraConfigWithAddresses;
36+ cassandraEnvPkg = "${cfg.package}/conf/cassandra-env.sh";
37+ buildCommand = ''
38+ mkdir -p "$out"
11739
40+ echo "$cassandraYaml" > "$out/cassandra.yaml"
41+ ln -s "$cassandraEnvPkg" "$out/cassandra-env.sh"
42+ '';
43+ };
11844in {
119−
120− ###### interface
121−
12245 options.services.cassandra = {
123− enable = mkOption {
124− description = "Whether to enable cassandra.";
125− default = false;
126− type = types.bool;
127− };
128− package = mkOption {
129− description = "Cassandra package to use.";
130− default = pkgs.cassandra;
131− defaultText = "pkgs.cassandra";
132− type = types.package;
133− };
134− jre = mkOption {
135− description = "JRE package to run cassandra service.";
136− default = pkgs.jre;
137− defaultText = "pkgs.jre";
138− type = types.package;
139− };
46+ enable = mkEnableOption ''
47+ Apache Cassandra – Scalable and highly available database.
48+ '';
14049 user = mkOption {
141− description = "User that runs cassandra service.";
142− default = "cassandra";
143− type = types.string;
50+ type = types.str;
51+ default = defaultUser;
52+ description = "Run Apache Cassandra under this user.";
14453 };
14554 group = mkOption {
146− description = "Group that runs cassandra service.";
147− default = "cassandra";
148− type = types.string;
149− };
150− envFile = mkOption {
151− description = "path to cassandra-env.sh";
152− default = "${cassandraPackage}/conf/cassandra-env.sh";
153− defaultText = "\${cassandraPackage}/conf/cassandra-env.sh";
154− type = types.path;
155− };
156− clusterName = mkOption {
157− description = "set cluster name";
158− default = "cassandra";
159− example = "prod-cluster0";
160− type = types.string;
161− };
162− commitLogDirectory = mkOption {
163− description = "directory for commit logs";
164− default = "/var/lib/cassandra/commit_log";
165− type = types.string;
166− };
167− savedCachesDirectory = mkOption {
168− description = "directory for saved caches";
169− default = "/var/lib/cassandra/saved_caches";
170− type = types.string;
171− };
172− hintedHandOff = mkOption {
173− description = "enable hinted handoff";
174− default = true;
175− type = types.bool;
176− };
177− hintedHandOffThrottle = mkOption {
178− description = "hinted hand off throttle rate in kb";
179− default = 1024;
180− type = types.int;
181− };
182− commitLogSync = mkOption {
183− description = "commitlog sync method";
184− default = "periodic";
18555 type = types.str;
186− example = "batch";
187− };
188− commitLogSyncPeriod = mkOption {
189− description = "commitlog sync period in ms ";
190− default = 10000;
191− type = types.int;
56+ default = defaultUser;
57+ description = "Run Apache Cassandra under this group.";
19258 };
193− envScript = mkOption {
194− default = "${cassandraPackage}/conf/cassandra-env.sh";
195− defaultText = "\${cassandraPackage}/conf/cassandra-env.sh";
59+ homeDir = mkOption {
19660 type = types.path;
197− description = "Supply your own cassandra-env.sh rather than using the default";
198− };
199− extraParams = mkOption {
200− description = "add additional lines to cassandra-env.sh";
201− default = [];
202− example = [''JVM_OPTS="$JVM_OPTS -Dcassandra.available_processors=1"''];
203− type = types.listOf types.str;
204− };
205− dataDirs = mkOption {
206− type = types.listOf types.path;
207− default = [ "/var/lib/cassandra/data" ];
208− description = "Data directories for cassandra";
209− };
210− logLevel = mkOption {
211− type = types.str;
212− default = "INFO";
213− description = "default logging level for log4j";
214− };
215− internodeEncryption = mkOption {
216− description = "enable internode encryption";
217− default = "none";
218− example = "all";
219− type = types.str;
220− };
221− clientEncryption = mkOption {
222− description = "enable client encryption";
223− default = false;
224− type = types.bool;
225− };
226− trustStorePath = mkOption {
227− description = "path to truststore";
228− default = ".conf/truststore";
229− type = types.str;
230− };
231− keyStorePath = mkOption {
232− description = "path to keystore";
233− default = ".conf/keystore";
234− type = types.str;
235− };
236− keyStorePassword = mkOption {
237− description = "password to keystore";
238− default = "cassandra";
239− type = types.str;
61+ default = "/var/lib/cassandra";
62+ description = ''
63+ Home directory for Apache Cassandra.
64+ '';
24065 };
241− trustStorePassword = mkOption {
242− description = "password to truststore";
243− default = "cassandra";
244− type = types.str;
66+ package = mkOption {
67+ type = types.package;
68+ default = pkgs.cassandra;
69+ defaultText = "pkgs.cassandra";
70+ example = literalExample "pkgs.cassandra_3_11";
71+ description = ''
72+ The Apache Cassandra package to use.
73+ '';
24574 };
246− seeds = mkOption {
247− description = "password to truststore";
248− default = [ "127.0.0.1" ];
75+ jvmOpts = mkOption {
24976 type = types.listOf types.str;
250− };
251− concurrentWrites = mkOption {
252− description = "number of concurrent writes allowed";
253− default = 32;
254− type = types.int;
255− };
256− concurrentReads = mkOption {
257− description = "number of concurrent reads allowed";
258− default = 32;
259− type = types.int;
77+ default = [];
78+ description = ''
79+ Populate the JVM_OPT environment variable.
80+ '';
26081 };
26182 listenAddress = mkOption {
262− description = "listen address";
263− default = "localhost";
264− type = types.str;
265− };
266− rpcAddress = mkOption {
267− description = "rpc listener address";
268− default = "localhost";
269− type = types.str;
270− };
271− incrementalBackups = mkOption {
272− description = "enable incremental backups";
273− default = false;
274− type = types.bool;
275− };
276− snitch = mkOption {
277− description = "snitch to use for topology discovery";
278− default = "GossipingPropertyFileSnitch";
279− example = "Ec2Snitch";
280− type = types.str;
281− };
282− dc = mkOption {
283− description = "datacenter for use in topology configuration";
284− default = "DC1";
285− example = "DC1";
286− type = types.str;
287− };
288− rack = mkOption {
289− description = "rack for use in topology configuration";
290− default = "RAC1";
291− example = "RAC1";
292− type = types.str;
293− };
294− authorizer = mkOption {
295− description = "
296− Authorization backend, implementing IAuthorizer; used to limit access/provide permissions
297− ";
298− default = "AllowAllAuthorizer";
299− example = "CassandraAuthorizer";
300− type = types.str;
301− };
302− authenticator = mkOption {
303− description = "
304− Authentication backend, implementing IAuthenticator; used to identify users
305− ";
306− default = "AllowAllAuthenticator";
307− example = "PasswordAuthenticator";
308− type = types.str;
309− };
310− autoBootstrap = mkOption {
311− description = "It makes new (non-seed) nodes automatically migrate the right data to themselves.";
312− default = true;
313− type = types.bool;
314− };
315− streamingSocketTimoutInMS = mkOption {
316− description = "Enable or disable socket timeout for streaming operations";
317− default = 3600000; #CASSANDRA-8611
318− example = 120;
319− type = types.int;
320− };
321− repairStartAt = mkOption {
322− default = "Sun";
323− type = types.string;
83+ type = types.nullOr types.str;
84+ default = "127.0.0.1";
85+ example = literalExample "null";
32486 description = ''
325− Defines realtime (i.e. wallclock) timers with calendar event
326− expressions. For more details re: systemd OnCalendar at
327− https://www.freedesktop.org/software/systemd/man/systemd.time.html#Displaying%20Time%20Spans
87+ Address or interface to bind to and tell other Cassandra nodes
88+ to connect to. You _must_ change this if you want multiple
89+ nodes to be able to communicate!
90+
91+ Set listenAddress OR listenInterface, not both.
92+
93+ Leaving it blank leaves it up to
94+ InetAddress.getLocalHost(). This will always do the Right
95+ Thing _if_ the node is properly configured (hostname, name
96+ resolution, etc), and the Right Thing is to use the address
97+ associated with the hostname (it might not be).
98+
99+ Setting listen_address to 0.0.0.0 is always wrong.
328100 '';
329− example = ["weekly" "daily" "08:05:40" "mon,fri *-1/2-1,3 *:30:45"];
330101 };
331− repairRandomizedDelayInSec = mkOption {
332− default = 0;
333− type = types.int;
334− description = ''Delay the timer by a randomly selected, evenly distributed
335− amount of time between 0 and the specified time value. re: systemd timer
336− RandomizedDelaySec for more details
102+ listenInterface = mkOption {
103+ type = types.nullOr types.str;
104+ default = null;
105+ example = "eth1";
106+ description = ''
107+ Set listenAddress OR listenInterface, not both. Interfaces
108+ must correspond to a single address, IP aliasing is not
109+ supported.
337110 '';
338111 };
339− repairPostStop = mkOption {
340− default = null;
341− type = types.nullOr types.string;
112+ rpcAddress = mkOption {
113+ type = types.nullOr types.str;
114+ default = "127.0.0.1";
115+ example = literalExample "null";
342116 description = ''
343− Run a script when repair is over. One can use it to send statsd events, email, etc.
117+ The address or interface to bind the native transport server to.
118+
119+ Set rpcAddress OR rpcInterface, not both.
120+
121+ Leaving rpcAddress blank has the same effect as on
122+ listenAddress (i.e. it will be based on the configured hostname
123+ of the node).
124+
125+ Note that unlike listenAddress, you can specify 0.0.0.0, but you
126+ must also set extraConfig.broadcast_rpc_address to a value other
127+ than 0.0.0.0.
128+
129+ For security reasons, you should not expose this port to the
130+ internet. Firewall it if needed.
344131 '';
345132 };
346− repairPostStart = mkOption {
133+ rpcInterface = mkOption {
134+ type = types.nullOr types.str;
347135 default = null;
348− type = types.nullOr types.string;
136+ example = "eth1";
349137 description = ''
350− Run a script when repair starts. One can use it to send statsd events, email, etc.
351− It has same semantics as systemd ExecStopPost; So, if it fails, unit is consisdered
352− failed.
138+ Set rpcAddress OR rpcInterface, not both. Interfaces must
139+ correspond to a single address, IP aliasing is not supported.
353140 '';
354141 };
355− };
356142
357− ###### implementation
358−
359− config = mkIf cfg.enable {
143+ extraConfig = mkOption {
144+ type = types.attrs;
145+ default = {};
146+ example =
147+ { commitlog_sync_batch_window_in_ms = 3;
148+ };
149+ description = ''
150+ Extra options to be merged into cassandra.yaml as nix attribute set.
151+ '';
152+ };
153+ fullRepairInterval = mkOption {
154+ type = types.nullOr types.str;
155+ default = "3w";
156+ example = literalExample "null";
157+ description = ''
158+ Set the interval how often full repairs are run, i.e.
159+ `nodetool repair --full` is executed. See
160+ https://cassandra.apache.org/doc/latest/operating/repair.html
161+ for more information.
360162
361− environment.etc."cassandra/cassandra-rackdc.properties" = {
362− source = cassandraRackFile;
163+ Set to `null` to disable full repairs.
164+ '';
363165 };
364− environment.etc."cassandra/cassandra.yaml" = {
365− source = cassandraConfFile;
166+ fullRepairOptions = mkOption {
167+ type = types.listOf types.str;
168+ default = [];
169+ example = [ "--partitioner-range" ];
170+ description = ''
171+ Options passed through to the full repair command.
172+ '';
366173 };
367− environment.etc."cassandra/log4j-server.properties" = {
368− source = cassandraLogFile;
174+ incrementalRepairInterval = mkOption {
175+ type = types.nullOr types.str;
176+ default = "3d";
177+ example = literalExample "null";
178+ description = ''
179+ Set the interval how often incremental repairs are run, i.e.
180+ `nodetool repair` is executed. See
181+ https://cassandra.apache.org/doc/latest/operating/repair.html
182+ for more information.
183+
184+ Set to `null` to disable incremental repairs.
185+ '';
369186 };
370− environment.etc."cassandra/cassandra-env.sh" = {
371− text = ''
372− ${builtins.readFile cfg.envFile}
373− ${concatStringsSep "\n" cfg.extraParams}
374− '';
187+ incrementalRepairOptions = mkOption {
188+ type = types.listOf types.string;
189+ default = [];
190+ example = [ "--partitioner-range" ];
191+ description = ''
192+ Options passed through to the incremental repair command.
193+ '';
375194 };
376− systemd.services.cassandra = {
377− description = "Cassandra Daemon";
378− wantedBy = [ "multi-user.target" ];
379− after = [ "network.target" ];
380− environment = cassandraEnvironment;
381− restartTriggers = [ cassandraConfFile cassandraLogFile cassandraRackFile ];
382− serviceConfig = {
383−
384− User = cfg.user;
385− PermissionsStartOnly = true;
386− LimitAS = "infinity";
387− LimitNOFILE = "100000";
388− LimitNPROC = "32768";
389− LimitMEMLOCK = "infinity";
195+ };
390196
391− };
392− script = ''
393− ${cassandraPackage}/bin/cassandra -f
394− '';
395− path = [
396− cfg.jre
397− cassandraPackage
398− pkgs.coreutils
197+ config = mkIf cfg.enable {
198+ assertions =
199+ [ { assertion =
200+ ((isNull cfg.listenAddress)
201+ || (isNull cfg.listenInterface)
202+ ) && !((isNull cfg.listenAddress)
203+ && (isNull cfg.listenInterface)
204+ );
205+ message = "You have to set either listenAddress or listenInterface";
206+ }
207+ { assertion =
208+ ((isNull cfg.rpcAddress)
209+ || (isNull cfg.rpcInterface)
210+ ) && !((isNull cfg.rpcAddress)
211+ && (isNull cfg.rpcInterface)
212+ );
213+ message = "You have to set either rpcAddress or rpcInterface";
214+ }
399215 ];
400− preStart = ''
401− mkdir -m 0700 -p /etc/cassandra/triggers
402− mkdir -m 0700 -p /var/lib/cassandra /var/log/cassandra
403− chown ${cfg.user} /var/lib/cassandra /var/log/cassandra /etc/cassandra/triggers
404− '';
405− postStart = ''
406− sleep 2
407− while ! nodetool status >/dev/null 2>&1; do
408− sleep 2
409− done
410− nodetool status
411− '';
216+ users = mkIf (cfg.user == defaultUser) {
217+ extraUsers."${defaultUser}" =
218+ { group = cfg.group;
219+ home = cfg.homeDir;
220+ createHome = true;
221+ uid = config.ids.uids.cassandra;
222+ description = "Cassandra service user";
223+ };
224+ extraGroups."${defaultUser}".gid = config.ids.gids.cassandra;
412225 };
413226
414− environment.systemPackages = [ cassandraPackage ];
415−
416− networking.firewall.allowedTCPPorts = [
417− 7000
418− 7001
419− 9042
420− 9160
421− ];
422−
423− users.users.cassandra =
424− if config.ids.uids ? "cassandra"
425− then { uid = config.ids.uids.cassandra; } // cassandraUser
426− else cassandraUser ;
427−
428− boot.kernel.sysctl."vm.swappiness" = pkgs.lib.mkOptionDefault 0;
227+ systemd.services.cassandra =
228+ { description = "Apache Cassandra service";
229+ after = [ "network.target" ];
230+ environment =
231+ { CASSANDRA_CONF = "${cassandraEtc}";
232+ JVM_OPTS = builtins.concatStringsSep " " cfg.jvmOpts;
233+ };
234+ wantedBy = [ "multi-user.target" ];
235+ serviceConfig =
236+ { User = cfg.user;
237+ Group = cfg.group;
238+ ExecStart = "${cfg.package}/bin/cassandra -f";
239+ SuccessExitStatus = 143;
240+ };
241+ };
429242
430− systemd.timers."cassandra-repair" = {
431− timerConfig = {
432− OnCalendar = "${toString cfg.repairStartAt}";
433− RandomizedDelaySec = cfg.repairRandomizedDelayInSec;
243+ systemd.services.cassandra-full-repair =
244+ { description = "Perform a full repair on this Cassandra node";
245+ after = [ "cassandra.service" ];
246+ requires = [ "cassandra.service" ];
247+ serviceConfig =
248+ { User = cfg.user;
249+ Group = cfg.group;
250+ ExecStart =
251+ lib.concatStringsSep " "
252+ ([ "${cfg.package}/bin/nodetool" "repair" "--full"
253+ ] ++ cfg.fullRepairOptions);
254+ };
255+ };
256+ systemd.timers.cassandra-full-repair =
257+ mkIf (!isNull cfg.fullRepairInterval) {
258+ description = "Schedule full repairs on Cassandra";
259+ wantedBy = [ "timers.target" ];
260+ timerConfig =
261+ { OnBootSec = cfg.fullRepairInterval;
262+ OnUnitActiveSec = cfg.fullRepairInterval;
263+ Persistent = true;
264+ };
434265 };
435− };
436266
437− systemd.services."cassandra-repair" = {
438− description = "Cassandra repair daemon";
439− environment = cassandraEnvironment;
440− script = "${cassandraPackage}/bin/nodetool repair -pr";
441− postStop = mkIf (cfg.repairPostStop != null) cfg.repairPostStop;
442− postStart = mkIf (cfg.repairPostStart != null) cfg.repairPostStart;
443− serviceConfig = {
444− User = cfg.user;
267+ systemd.services.cassandra-incremental-repair =
268+ { description = "Perform an incremental repair on this cassandra node.";
269+ after = [ "cassandra.service" ];
270+ requires = [ "cassandra.service" ];
271+ serviceConfig =
272+ { User = cfg.user;
273+ Group = cfg.group;
274+ ExecStart =
275+ lib.concatStringsSep " "
276+ ([ "${cfg.package}/bin/nodetool" "repair"
277+ ] ++ cfg.incrementalRepairOptions);
278+ };
279+ };
280+ systemd.timers.cassandra-incremental-repair =
281+ mkIf (!isNull cfg.incrementalRepairInterval) {
282+ description = "Schedule incremental repairs on Cassandra";
283+ wantedBy = [ "timers.target" ];
284+ timerConfig =
285+ { OnBootSec = cfg.incrementalRepairInterval;
286+ OnUnitActiveSec = cfg.incrementalRepairInterval;
287+ Persistent = true;
288+ };
445289 };
446− };
447290 };
448291}
nixos/tests/cassandra.nix+46−43
@@ -1,68 +1,71 @@
11import ./make-test.nix ({ pkgs, ...}:
22let
3− user = "cassandra";
4− nodeCfg = nodes: selfIP: cassandraOpts:
5− {
6− services.cassandra = {
7− enable = true;
8− listenAddress = selfIP;
9− rpcAddress = "0.0.0.0";
10− seeds = [ "192.168.1.1" ];
11− package = pkgs.cassandra_2_0;
12− jre = pkgs.openjdk;
13− clusterName = "ci ahoy";
14− authenticator = "PasswordAuthenticator";
15− authorizer = "CassandraAuthorizer";
16− user = user;
17− } // cassandraOpts;
18− nixpkgs.config.allowUnfree = true;
3+ # Change this to test a different version of Cassandra:
4+ testPackage = pkgs.cassandra;
5+ cassandraCfg =
6+ { enable = true;
7+ listenAddress = null;
8+ listenInterface = "eth1";
9+ rpcAddress = null;
10+ rpcInterface = "eth1";
11+ extraConfig =
12+ { start_native_transport = true;
13+ seed_provider =
14+ [{ class_name = "org.apache.cassandra.locator.SimpleSeedProvider";
15+ parameters = [ { seeds = "cass0"; } ];
16+ }];
17+ };
18+ package = testPackage;
19+ };
20+ nodeCfg = extra: {pkgs, config, ...}:
21+ { environment.systemPackages = [ testPackage ];
22+ networking.firewall.enable = false;
23+ services.cassandra = cassandraCfg // extra;
1924 virtualisation.memorySize = 1024;
2025 };
21−
2226in
2327{
2428 name = "cassandra-ci";
2529
2630 nodes = {
27− cass0 = { nodes, ... }: nodeCfg nodes "192.168.1.1" {};
28− cass1 = { nodes, ... }: nodeCfg nodes "192.168.1.2" {};
29− cass2 = { nodes, ... }: nodeCfg nodes "192.168.1.3" {
30− extraParams = [
31− ''JVM_OPTS="$JVM_OPTS -Dcassandra.replace_address=192.168.1.2"''
32− ];
33− listenAddress = "192.168.1.3";
34− };
31+ cass0 = nodeCfg {};
32+ cass1 = nodeCfg {};
33+ cass2 = nodeCfg { jvmOpts = [ "-Dcassandra.replace_address=cass1" ]; };
3534 };
3635
3736 testScript = ''
38− subtest "start seed", sub {
37+ subtest "timers exist", sub {
38+ $cass0->succeed("systemctl list-timers | grep cassandra-full-repair.timer");
39+ $cass0->succeed("systemctl list-timers | grep cassandra-incremental-repair.timer");
40+ };
41+ subtest "can connect via cqlsh", sub {
3942 $cass0->waitForUnit("cassandra.service");
40− $cass0->waitForOpenPort(9160);
41− $cass0->execute("echo show version | cqlsh localhost -u cassandra -p cassandra");
42− sleep 2;
43− $cass0->succeed("echo show version | cqlsh localhost -u cassandra -p cassandra");
44− $cass1->start;
43+ $cass0->waitUntilSucceeds("nc -z cass0 9042");
44+ $cass0->succeed("echo 'show version;' | cqlsh cass0");
4545 };
46− subtest "cassandra user/group", sub {
47− $cass0->succeed("id \"${user}\" >/dev/null");
48− $cass1->succeed("id \"${user}\" >/dev/null");
46+ subtest "nodetool is operational", sub {
47+ $cass0->waitForUnit("cassandra.service");
48+ $cass0->waitUntilSucceeds("nc -z localhost 7199");
49+ $cass0->succeed("nodetool status --resolve-ip | egrep '^UN[[:space:]]+cass0'");
4950 };
50− subtest "bring up cassandra cluster", sub {
51+ subtest "bring up cluster", sub {
5152 $cass1->waitForUnit("cassandra.service");
52− $cass0->waitUntilSucceeds("nodetool status | grep -c UN | grep 2");
53+ $cass1->waitUntilSucceeds("nodetool status | egrep -c '^UN' | grep 2");
54+ $cass0->succeed("nodetool status --resolve-ip | egrep '^UN[[:space:]]+cass1'");
5355 };
5456 subtest "break and fix node", sub {
55− $cass0->block;
56− $cass0->waitUntilSucceeds("nodetool status | grep -c DN | grep 1");
57− $cass0->unblock;
58− $cass0->waitUntilSucceeds("nodetool status | grep -c UN | grep 2");
57+ $cass1->block;
58+ $cass0->waitUntilSucceeds("nodetool status --resolve-ip | egrep -c '^DN[[:space:]]+cass1'");
59+ $cass0->succeed("nodetool status | egrep -c '^UN' | grep 1");
60+ $cass1->unblock;
61+ $cass1->waitUntilSucceeds("nodetool status | egrep -c '^UN' | grep 2");
62+ $cass0->succeed("nodetool status | egrep -c '^UN' | grep 2");
5963 };
6064 subtest "replace crashed node", sub {
6165 $cass1->crash;
62− $cass2->start;
6366 $cass2->waitForUnit("cassandra.service");
64− $cass0->waitUntilFails("nodetool status | grep UN | grep 192.168.1.2");
65− $cass0->waitUntilSucceeds("nodetool status | grep UN | grep 192.168.1.3");
67+ $cass0->waitUntilFails("nodetool status --resolve-ip | egrep '^UN[[:space:]]+cass1'");
68+ $cass0->waitUntilSucceeds("nodetool status --resolve-ip | egrep '^UN[[:space:]]+cass2'");
6669 };
6770 '';
6871})
pkgs/servers/nosql/cassandra/generic.nix+1
@@ -16,6 +16,7 @@ in
1616
1717stdenv.mkDerivation rec {
1818 name = "cassandra-${version}";
19+ inherit version;
1920
2021 src = fetchurl {
2122 inherit sha256;