summary refs log tree commit diff
path: root/nixos/modules/services/cluster/hadoop/hdfs.nix
blob: 1725dc62d0cc1be796ab1b2b2a603df873bde160 (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
{ config, lib, pkgs, ... }:
with lib;
let
  cfg = config.services.hadoop;

  # Config files for hadoop services
  hadoopConf = "${import ./conf.nix { inherit cfg pkgs lib; }}/";

  # Generator for HDFS service options
  hadoopServiceOption = { serviceName, firewallOption ? true }: {
    enable = mkEnableOption serviceName;
    restartIfChanged = mkOption {
      type = types.bool;
      description = ''
        Automatically restart the service on config change.
        This can be set to false to defer restarts on clusters running critical applications.
        Please consider the security implications of inadvertently running an older version,
        and the possibility of unexpected behavior caused by inconsistent versions across a cluster when disabling this option.
      '';
      default = false;
    };
  } // (optionalAttrs firewallOption {
    openFirewall = mkOption {
      type = types.bool;
      default = false;
      description = "Open firewall ports for ${serviceName}.";
    };
  });

  # Generator for HDFS service configs
  hadoopServiceConfig =
    { name
    , serviceOptions ? cfg.hdfs."${toLower name}"
    , description ? "Hadoop HDFS ${name}"
    , User ? "hdfs"
    , allowedTCPPorts ? [ ]
    , preStart ? ""
    , environment ? { }
    }: (

      mkIf serviceOptions.enable {
        systemd.services."hdfs-${toLower name}" = {
          inherit description preStart environment;
          wantedBy = [ "multi-user.target" ];
          inherit (serviceOptions) restartIfChanged;
          serviceConfig = {
            inherit User;
            SyslogIdentifier = "hdfs-${toLower name}";
            ExecStart = "${cfg.package}/bin/hdfs --config ${hadoopConf} ${toLower name}";
            Restart = "always";
          };
        };

        services.hadoop.gatewayRole.enable = true;

        networking.firewall.allowedTCPPorts = mkIf
          ((builtins.hasAttr "openFirewall" serviceOptions) && serviceOptions.openFirewall)
          allowedTCPPorts;
      }
    );

in
{
  options.services.hadoop.hdfs = {

    namenode = hadoopServiceOption { serviceName = "HDFS NameNode"; } // {
      formatOnInit = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Format HDFS namenode on first start. This is useful for quickly spinning up
          ephemeral HDFS clusters with a single namenode.
          For HA clusters, initialization involves multiple steps across multiple nodes.
          Follow this guide to initialize an HA cluster manually:
          <link xlink:href="https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HDFSHighAvailabilityWithQJM.html"/>
        '';
      };
    };

    datanode = hadoopServiceOption { serviceName = "HDFS DataNode"; };

    journalnode = hadoopServiceOption { serviceName = "HDFS JournalNode"; };

    zkfc = hadoopServiceOption {
      serviceName = "HDFS ZooKeeper failover controller";
      firewallOption = false;
    };

    httpfs = hadoopServiceOption { serviceName = "HDFS JournalNode"; } // {
      tempPath = mkOption {
        type = types.path;
        default = "/tmp/hadoop/httpfs";
        description = "HTTPFS_TEMP path used by HTTPFS";
      };
    };

  };

  config = mkMerge [
    (hadoopServiceConfig {
      name = "NameNode";
      allowedTCPPorts = [
        9870 # namenode.http-address
        8020 # namenode.rpc-address
        8022 # namenode.servicerpc-address
        8019 # dfs.ha.zkfc.port
      ];
      preStart = (mkIf cfg.hdfs.namenode.formatOnInit
        "${cfg.package}/bin/hdfs --config ${hadoopConf} namenode -format -nonInteractive || true"
      );
    })

    (hadoopServiceConfig {
      name = "DataNode";
      # port numbers for datanode changed between hadoop 2 and 3
      allowedTCPPorts = if versionAtLeast cfg.package.version "3" then [
        9864 # datanode.http.address
        9866 # datanode.address
        9867 # datanode.ipc.address
      ] else [
        50075 # datanode.http.address
        50010 # datanode.address
        50020 # datanode.ipc.address
      ];
    })

    (hadoopServiceConfig {
      name = "JournalNode";
      allowedTCPPorts = [
        8480 # dfs.journalnode.http-address
        8485 # dfs.journalnode.rpc-address
      ];
    })

    (hadoopServiceConfig {
      name = "zkfc";
      description = "Hadoop HDFS ZooKeeper failover controller";
    })

    (hadoopServiceConfig {
      name = "HTTPFS";
      environment.HTTPFS_TEMP = cfg.hdfs.httpfs.tempPath;
      preStart = "mkdir -p $HTTPFS_TEMP";
      User = "httpfs";
      allowedTCPPorts = [
        14000 # httpfs.http.port
      ];
    })

    (mkIf cfg.gatewayRole.enable {
      users.users.hdfs = {
        description = "Hadoop HDFS user";
        group = "hadoop";
        uid = config.ids.uids.hdfs;
      };
    })
    (mkIf cfg.hdfs.httpfs.enable {
      users.users.httpfs = {
        description = "Hadoop HTTPFS user";
        group = "hadoop";
        isSystemUser = true;
      };
    })

  ];
}