about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/cluster/hadoop/hbase.nix
blob: 6801e505db64d27d777f03c7e60fa4e11e4f38e5 (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
{ config, lib, pkgs, ...}:

with lib;
let
  cfg = config.services.hadoop;
  hadoopConf = "${import ./conf.nix { inherit cfg pkgs lib; }}/";
  mkIfNotNull = x: mkIf (x != null) x;
  # generic hbase role options
  hbaseRoleOption = name: extraOpts: {
    enable = mkEnableOption (mdDoc "HBase ${name}");

    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = mdDoc "Open firewall ports for HBase ${name}.";
    };

    restartIfChanged = mkOption {
      type = types.bool;
      default = false;
      description = mdDoc "Restart ${name} con config change.";
    };

    extraFlags = mkOption {
      type = with types; listOf str;
      default = [];
      example = literalExpression ''[ "--backup" ]'';
      description = mdDoc "Extra flags for the ${name} service.";
    };

    environment = mkOption {
      type = with types; attrsOf str;
      default = {};
      example = literalExpression ''
        {
          HBASE_MASTER_OPTS = "-Dcom.sun.management.jmxremote.ssl=true";
        }
      '';
      description = mdDoc "Environment variables passed to ${name}.";
    };
  } // extraOpts;
  # generic hbase role configs
  hbaseRoleConfig = name: ports: (mkIf cfg.hbase."${name}".enable {
    services.hadoop.gatewayRole = {
      enable = true;
      enableHbaseCli = mkDefault true;
    };

    systemd.services."hbase-${toLower name}" = {
      description = "HBase ${name}";
      wantedBy = [ "multi-user.target" ];
      path = with cfg; [ hbase.package ] ++ optional
        (with cfg.hbase.master; enable && initHDFS) package;
      preStart = mkIf (with cfg.hbase.master; enable && initHDFS)
        (concatStringsSep "\n" (
          map (x: "HADOOP_USER_NAME=hdfs hdfs --config /etc/hadoop-conf ${x}")[
            "dfsadmin -safemode wait"
            "dfs -mkdir -p ${cfg.hbase.rootdir}"
            "dfs -chown hbase ${cfg.hbase.rootdir}"
          ]
        ));

      inherit (cfg.hbase."${name}") environment;
      script = concatStringsSep " " (
        [
          "hbase --config /etc/hadoop-conf/"
          "${toLower name} start"
        ]
        ++ cfg.hbase."${name}".extraFlags
        ++ map (x: "--${toLower x} ${toString cfg.hbase.${name}.${x}}")
          (filter (x: hasAttr x cfg.hbase.${name}) ["port" "infoPort"])
      );

      serviceConfig = {
        User = "hbase";
        SyslogIdentifier = "hbase-${toLower name}";
        Restart = "always";
      };
    };

    services.hadoop.hbaseSiteInternal."hbase.rootdir" = cfg.hbase.rootdir;

    networking = {
      firewall.allowedTCPPorts = mkIf cfg.hbase."${name}".openFirewall ports;
      hosts = mkIf (with cfg.hbase.regionServer; enable && overrideHosts) {
        "127.0.0.2" = mkForce [ ];
        "::1" = mkForce [ ];
      };
    };

  });
in
{
  options.services.hadoop = {

    gatewayRole.enableHbaseCli = mkEnableOption (mdDoc "HBase CLI tools");

    hbaseSiteDefault = mkOption {
      default = {
        "hbase.regionserver.ipc.address" = "0.0.0.0";
        "hbase.master.ipc.address" = "0.0.0.0";
        "hbase.master.info.bindAddress" = "0.0.0.0";
        "hbase.regionserver.info.bindAddress" = "0.0.0.0";

        "hbase.cluster.distributed" = "true";
      };
      type = types.attrsOf types.anything;
      description = mdDoc ''
        Default options for hbase-site.xml
      '';
    };
    hbaseSite = mkOption {
      default = {};
      type = with types; attrsOf anything;
      example = literalExpression ''
        {
          "hbase.hregion.max.filesize" = 20*1024*1024*1024;
          "hbase.table.normalization.enabled" = "true";
        }
      '';
      description = mdDoc ''
        Additional options and overrides for hbase-site.xml
        <https://github.com/apache/hbase/blob/rel/2.4.11/hbase-common/src/main/resources/hbase-default.xml>
      '';
    };
    hbaseSiteInternal = mkOption {
      default = {};
      type = with types; attrsOf anything;
      internal = true;
      description = mdDoc ''
        Internal option to add configs to hbase-site.xml based on module options
      '';
    };

    hbase = {

      package = mkPackageOption pkgs "hbase" { };

      rootdir = mkOption {
        description = mdDoc ''
          This option will set "hbase.rootdir" in hbase-site.xml and determine
          the directory shared by region servers and into which HBase persists.
          The URL should be 'fully-qualified' to include the filesystem scheme.
          If a core-site.xml is provided, the FS scheme defaults to the value
          of "fs.defaultFS".

          Filesystems other than HDFS (like S3, QFS, Swift) are also supported.
        '';
        type = types.str;
        example = "hdfs://nameservice1/hbase";
        default = "/hbase";
      };
      zookeeperQuorum = mkOption {
        description = mdDoc ''
          This option will set "hbase.zookeeper.quorum" in hbase-site.xml.
          Comma separated list of servers in the ZooKeeper ensemble.
        '';
        type = with types; nullOr commas;
        example = "zk1.internal,zk2.internal,zk3.internal";
        default = null;
      };
    } // (let
      ports = port: infoPort: {
        port = mkOption {
          type = types.int;
          default = port;
          description = mdDoc "RPC port";
        };
        infoPort = mkOption {
          type = types.int;
          default = infoPort;
          description = mdDoc "web UI port";
        };
      };
    in mapAttrs hbaseRoleOption {
      master.initHDFS = mkEnableOption (mdDoc "initialization of the hbase directory on HDFS");
      regionServer.overrideHosts = mkOption {
        type = types.bool;
        default = true;
        description = mdDoc ''
          Remove /etc/hosts entries for "127.0.0.2" and "::1" defined in nixos/modules/config/networking.nix
          Regionservers must be able to resolve their hostnames to their IP addresses, through PTR records
          or /etc/hosts entries.
        '';
      };
      thrift = ports 9090 9095;
      rest = ports 8080 8085;
    });
  };

  config = mkMerge ([

    (mkIf cfg.gatewayRole.enable {

      environment.systemPackages = mkIf cfg.gatewayRole.enableHbaseCli [ cfg.hbase.package ];

      services.hadoop.hbaseSiteInternal = with cfg.hbase; {
        "hbase.zookeeper.quorum" = mkIfNotNull zookeeperQuorum;
      };

      users.users.hbase = {
        description = "Hadoop HBase user";
        group = "hadoop";
        isSystemUser = true;
      };
    })
  ] ++ (mapAttrsToList hbaseRoleConfig {
    master = [ 16000 16010 ];
    regionServer = [ 16020 16030 ];
    thrift = with cfg.hbase.thrift; [ port infoPort ];
    rest = with cfg.hbase.rest; [ port infoPort ];
  }));
}