summary refs log tree commit diff
path: root/nixos/modules/services/databases/cassandra.nix
blob: b36cac35e7c29820ea7dc8b8d7988a802143527b (plain) (blame)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
{ config, lib, pkgs, ... }:

let
  inherit (lib)
    concatStringsSep
    flip
    literalDocBook
    literalExpression
    optionalAttrs
    optionals
    recursiveUpdate
    mkEnableOption
    mkIf
    mkOption
    types
    versionAtLeast
    ;

  cfg = config.services.cassandra;

  defaultUser = "cassandra";

  cassandraConfig = flip recursiveUpdate cfg.extraConfig (
    {
      commitlog_sync = "batch";
      commitlog_sync_batch_window_in_ms = 2;
      start_native_transport = cfg.allowClients;
      cluster_name = cfg.clusterName;
      partitioner = "org.apache.cassandra.dht.Murmur3Partitioner";
      endpoint_snitch = "SimpleSnitch";
      data_file_directories = [ "${cfg.homeDir}/data" ];
      commitlog_directory = "${cfg.homeDir}/commitlog";
      saved_caches_directory = "${cfg.homeDir}/saved_caches";
    } // optionalAttrs (cfg.seedAddresses != [ ]) {
      seed_provider = [
        {
          class_name = "org.apache.cassandra.locator.SimpleSeedProvider";
          parameters = [{ seeds = concatStringsSep "," cfg.seedAddresses; }];
        }
      ];
    } // optionalAttrs (versionAtLeast cfg.package.version "3") {
      hints_directory = "${cfg.homeDir}/hints";
    }
  );

  cassandraConfigWithAddresses = cassandraConfig // (
    if cfg.listenAddress == null
    then { listen_interface = cfg.listenInterface; }
    else { listen_address = cfg.listenAddress; }
  ) // (
    if cfg.rpcAddress == null
    then { rpc_interface = cfg.rpcInterface; }
    else { rpc_address = cfg.rpcAddress; }
  );

  cassandraEtc = pkgs.stdenv.mkDerivation {
    name = "cassandra-etc";

    cassandraYaml = builtins.toJSON cassandraConfigWithAddresses;
    cassandraEnvPkg = "${cfg.package}/conf/cassandra-env.sh";
    cassandraLogbackConfig = pkgs.writeText "logback.xml" cfg.logbackConfig;

    passAsFile = [ "extraEnvSh" ];
    inherit (cfg) extraEnvSh;

    buildCommand = ''
      mkdir -p "$out"

      echo "$cassandraYaml" > "$out/cassandra.yaml"
      ln -s "$cassandraLogbackConfig" "$out/logback.xml"

      ( cat "$cassandraEnvPkg"
        echo "# lines from services.cassandra.extraEnvSh: "
        cat "$extraEnvShPath"
      ) > "$out/cassandra-env.sh"

      # Delete default JMX Port, otherwise we can't set it using env variable
      sed -i '/JMX_PORT="7199"/d' "$out/cassandra-env.sh"

      # Delete default password file
      sed -i '/-Dcom.sun.management.jmxremote.password.file=\/etc\/cassandra\/jmxremote.password/d' "$out/cassandra-env.sh"
    '';
  };

  defaultJmxRolesFile =
    builtins.foldl'
      (left: right: left + right) ""
      (map (role: "${role.username} ${role.password}") cfg.jmxRoles);

  fullJvmOptions =
    cfg.jvmOpts
    ++ optionals (cfg.jmxRoles != [ ]) [
      "-Dcom.sun.management.jmxremote.authenticate=true"
      "-Dcom.sun.management.jmxremote.password.file=${cfg.jmxRolesFile}"
    ] ++ optionals cfg.remoteJmx [
      "-Djava.rmi.server.hostname=${cfg.rpcAddress}"
    ];

in
{
  options.services.cassandra = {

    enable = mkEnableOption ''
      Apache Cassandra – Scalable and highly available database.
    '';

    clusterName = mkOption {
      type = types.str;
      default = "Test Cluster";
      description = ''
        The name of the cluster.
        This setting prevents nodes in one logical cluster from joining
        another. All nodes in a cluster must have the same value.
      '';
    };

    user = mkOption {
      type = types.str;
      default = defaultUser;
      description = "Run Apache Cassandra under this user.";
    };

    group = mkOption {
      type = types.str;
      default = defaultUser;
      description = "Run Apache Cassandra under this group.";
    };

    homeDir = mkOption {
      type = types.path;
      default = "/var/lib/cassandra";
      description = ''
        Home directory for Apache Cassandra.
      '';
    };

    package = mkOption {
      type = types.package;
      default = pkgs.cassandra;
      defaultText = literalExpression "pkgs.cassandra";
      example = literalExpression "pkgs.cassandra_3_11";
      description = ''
        The Apache Cassandra package to use.
      '';
    };

    jvmOpts = mkOption {
      type = types.listOf types.str;
      default = [ ];
      description = ''
        Populate the JVM_OPT environment variable.
      '';
    };

    listenAddress = mkOption {
      type = types.nullOr types.str;
      default = "127.0.0.1";
      example = null;
      description = ''
        Address or interface to bind to and tell other Cassandra nodes
        to connect to. You _must_ change this if you want multiple
        nodes to be able to communicate!

        Set listenAddress OR listenInterface, not both.

        Leaving it blank leaves it up to
        InetAddress.getLocalHost(). This will always do the Right
        Thing _if_ the node is properly configured (hostname, name
        resolution, etc), and the Right Thing is to use the address
        associated with the hostname (it might not be).

        Setting listen_address to 0.0.0.0 is always wrong.
      '';
    };

    listenInterface = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "eth1";
      description = ''
        Set listenAddress OR listenInterface, not both. Interfaces
        must correspond to a single address, IP aliasing is not
        supported.
      '';
    };

    rpcAddress = mkOption {
      type = types.nullOr types.str;
      default = "127.0.0.1";
      example = null;
      description = ''
        The address or interface to bind the native transport server to.

        Set rpcAddress OR rpcInterface, not both.

        Leaving rpcAddress blank has the same effect as on
        listenAddress (i.e. it will be based on the configured hostname
        of the node).

        Note that unlike listenAddress, you can specify 0.0.0.0, but you
        must also set extraConfig.broadcast_rpc_address to a value other
        than 0.0.0.0.

        For security reasons, you should not expose this port to the
        internet. Firewall it if needed.
      '';
    };

    rpcInterface = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "eth1";
      description = ''
        Set rpcAddress OR rpcInterface, not both. Interfaces must
        correspond to a single address, IP aliasing is not supported.
      '';
    };

    logbackConfig = mkOption {
      type = types.lines;
      default = ''
        <configuration scan="false">
          <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
              <pattern>%-5level %date{HH:mm:ss,SSS} %msg%n</pattern>
            </encoder>
          </appender>

          <root level="INFO">
            <appender-ref ref="STDOUT" />
          </root>

          <logger name="com.thinkaurelius.thrift" level="ERROR"/>
        </configuration>
      '';
      description = ''
        XML logback configuration for cassandra
      '';
    };

    seedAddresses = mkOption {
      type = types.listOf types.str;
      default = [ "127.0.0.1" ];
      description = ''
        The addresses of hosts designated as contact points in the cluster. A
        joining node contacts one of the nodes in the seeds list to learn the
        topology of the ring.
        Set to 127.0.0.1 for a single node cluster.
      '';
    };

    allowClients = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Enables or disables the native transport server (CQL binary protocol).
        This server uses the same address as the <literal>rpcAddress</literal>,
        but the port it uses is not <literal>rpc_port</literal> but
        <literal>native_transport_port</literal>. See the official Cassandra
        docs for more information on these variables and set them using
        <literal>extraConfig</literal>.
      '';
    };

    extraConfig = mkOption {
      type = types.attrs;
      default = { };
      example =
        {
          commitlog_sync_batch_window_in_ms = 3;
        };
      description = ''
        Extra options to be merged into cassandra.yaml as nix attribute set.
      '';
    };

    extraEnvSh = mkOption {
      type = types.lines;
      default = "";
      example = literalExpression ''"CLASSPATH=$CLASSPATH:''${extraJar}"'';
      description = ''
        Extra shell lines to be appended onto cassandra-env.sh.
      '';
    };

    fullRepairInterval = mkOption {
      type = types.nullOr types.str;
      default = "3w";
      example = null;
      description = ''
        Set the interval how often full repairs are run, i.e.
        <literal>nodetool repair --full</literal> is executed. See
        https://cassandra.apache.org/doc/latest/operating/repair.html
        for more information.

        Set to <literal>null</literal> to disable full repairs.
      '';
    };

    fullRepairOptions = mkOption {
      type = types.listOf types.str;
      default = [ ];
      example = [ "--partitioner-range" ];
      description = ''
        Options passed through to the full repair command.
      '';
    };

    incrementalRepairInterval = mkOption {
      type = types.nullOr types.str;
      default = "3d";
      example = null;
      description = ''
        Set the interval how often incremental repairs are run, i.e.
        <literal>nodetool repair</literal> is executed. See
        https://cassandra.apache.org/doc/latest/operating/repair.html
        for more information.

        Set to <literal>null</literal> to disable incremental repairs.
      '';
    };

    incrementalRepairOptions = mkOption {
      type = types.listOf types.str;
      default = [ ];
      example = [ "--partitioner-range" ];
      description = ''
        Options passed through to the incremental repair command.
      '';
    };

    maxHeapSize = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "4G";
      description = ''
        Must be left blank or set together with heapNewSize.
        If left blank a sensible value for the available amount of RAM and CPU
        cores is calculated.

        Override to set the amount of memory to allocate to the JVM at
        start-up. For production use you may wish to adjust this for your
        environment. MAX_HEAP_SIZE is the total amount of memory dedicated
        to the Java heap. HEAP_NEWSIZE refers to the size of the young
        generation.

        The main trade-off for the young generation is that the larger it
        is, the longer GC pause times will be. The shorter it is, the more
        expensive GC will be (usually).
      '';
    };

    heapNewSize = mkOption {
      type = types.nullOr types.str;
      default = null;
      example = "800M";
      description = ''
        Must be left blank or set together with heapNewSize.
        If left blank a sensible value for the available amount of RAM and CPU
        cores is calculated.

        Override to set the amount of memory to allocate to the JVM at
        start-up. For production use you may wish to adjust this for your
        environment. HEAP_NEWSIZE refers to the size of the young
        generation.

        The main trade-off for the young generation is that the larger it
        is, the longer GC pause times will be. The shorter it is, the more
        expensive GC will be (usually).

        The example HEAP_NEWSIZE assumes a modern 8-core+ machine for decent pause
        times. If in doubt, and if you do not particularly want to tweak, go with
        100 MB per physical CPU core.
      '';
    };

    mallocArenaMax = mkOption {
      type = types.nullOr types.int;
      default = null;
      example = 4;
      description = ''
        Set this to control the amount of arenas per-thread in glibc.
      '';
    };

    remoteJmx = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Cassandra ships with JMX accessible *only* from localhost.
        To enable remote JMX connections set to true.

        Be sure to also enable authentication and/or TLS.
        See: https://wiki.apache.org/cassandra/JmxSecurity
      '';
    };

    jmxPort = mkOption {
      type = types.int;
      default = 7199;
      description = ''
        Specifies the default port over which Cassandra will be available for
        JMX connections.
        For security reasons, you should not expose this port to the internet.
        Firewall it if needed.
      '';
    };

    jmxRoles = mkOption {
      default = [ ];
      description = ''
        Roles that are allowed to access the JMX (e.g. nodetool)
        BEWARE: The passwords will be stored world readable in the nix-store.
                It's recommended to use your own protected file using
                <literal>jmxRolesFile</literal>

        Doesn't work in versions older than 3.11 because they don't like that
        it's world readable.
      '';
      type = types.listOf (types.submodule {
        options = {
          username = mkOption {
            type = types.str;
            description = "Username for JMX";
          };
          password = mkOption {
            type = types.str;
            description = "Password for JMX";
          };
        };
      });
    };

    jmxRolesFile = mkOption {
      type = types.nullOr types.path;
      default =
        if versionAtLeast cfg.package.version "3.11"
        then pkgs.writeText "jmx-roles-file" defaultJmxRolesFile
        else null;
      defaultText = literalDocBook ''generated configuration file if version is at least 3.11, otherwise <literal>null</literal>'';
      example = "/var/lib/cassandra/jmx.password";
      description = ''
        Specify your own jmx roles file.

        Make sure the permissions forbid "others" from reading the file if
        you're using Cassandra below version 3.11.
      '';
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = (cfg.listenAddress == null) != (cfg.listenInterface == null);
        message = "You have to set either listenAddress or listenInterface";
      }
      {
        assertion = (cfg.rpcAddress == null) != (cfg.rpcInterface == null);
        message = "You have to set either rpcAddress or rpcInterface";
      }
      {
        assertion = (cfg.maxHeapSize == null) == (cfg.heapNewSize == null);
        message = "If you set either of maxHeapSize or heapNewSize you have to set both";
      }
      {
        assertion = cfg.remoteJmx -> cfg.jmxRolesFile != null;
        message = ''
          If you want JMX available remotely you need to set a password using
          <literal>jmxRoles</literal> or <literal>jmxRolesFile</literal> if
          using Cassandra older than v3.11.
        '';
      }
    ];
    users = mkIf (cfg.user == defaultUser) {
      users.${defaultUser} = {
        group = cfg.group;
        home = cfg.homeDir;
        createHome = true;
        uid = config.ids.uids.cassandra;
        description = "Cassandra service user";
      };
      groups.${defaultUser}.gid = config.ids.gids.cassandra;
    };

    systemd.services.cassandra = {
      description = "Apache Cassandra service";
      after = [ "network.target" ];
      environment = {
        CASSANDRA_CONF = "${cassandraEtc}";
        JVM_OPTS = builtins.concatStringsSep " " fullJvmOptions;
        MAX_HEAP_SIZE = toString cfg.maxHeapSize;
        HEAP_NEWSIZE = toString cfg.heapNewSize;
        MALLOC_ARENA_MAX = toString cfg.mallocArenaMax;
        LOCAL_JMX = if cfg.remoteJmx then "no" else "yes";
        JMX_PORT = toString cfg.jmxPort;
      };
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        ExecStart = "${cfg.package}/bin/cassandra -f";
        SuccessExitStatus = 143;
      };
    };

    systemd.services.cassandra-full-repair = {
      description = "Perform a full repair on this Cassandra node";
      after = [ "cassandra.service" ];
      requires = [ "cassandra.service" ];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        ExecStart =
          concatStringsSep " "
            ([
              "${cfg.package}/bin/nodetool"
              "repair"
              "--full"
            ] ++ cfg.fullRepairOptions);
      };
    };

    systemd.timers.cassandra-full-repair =
      mkIf (cfg.fullRepairInterval != null) {
        description = "Schedule full repairs on Cassandra";
        wantedBy = [ "timers.target" ];
        timerConfig = {
          OnBootSec = cfg.fullRepairInterval;
          OnUnitActiveSec = cfg.fullRepairInterval;
          Persistent = true;
        };
      };

    systemd.services.cassandra-incremental-repair = {
      description = "Perform an incremental repair on this cassandra node.";
      after = [ "cassandra.service" ];
      requires = [ "cassandra.service" ];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        ExecStart =
          concatStringsSep " "
            ([
              "${cfg.package}/bin/nodetool"
              "repair"
            ] ++ cfg.incrementalRepairOptions);
      };
    };

    systemd.timers.cassandra-incremental-repair =
      mkIf (cfg.incrementalRepairInterval != null) {
        description = "Schedule incremental repairs on Cassandra";
        wantedBy = [ "timers.target" ];
        timerConfig = {
          OnBootSec = cfg.incrementalRepairInterval;
          OnUnitActiveSec = cfg.incrementalRepairInterval;
          Persistent = true;
        };
      };
  };

  meta.maintainers = with lib.maintainers; [ roberth ];
}