mirror of
https://github.com/juspay/services-flake.git
synced 2024-08-16 15:50:38 +03:00
Add Redis cluster service (#35)
This commit is contained in:
parent
d30b8afd55
commit
88a259d48e
@ -19,6 +19,7 @@ TODO
|
||||
- [x] PostgreSQL
|
||||
- [ ] MySQL
|
||||
- [x] Redis
|
||||
- [x] Redis Cluster
|
||||
- [ ] ...
|
||||
|
||||
## A note on process working directory
|
||||
|
@ -36,5 +36,6 @@ in
|
||||
imports = builtins.map multiService [
|
||||
./postgres.nix
|
||||
./redis.nix
|
||||
./redis-cluster.nix
|
||||
];
|
||||
}
|
||||
|
@ -343,7 +343,7 @@ in
|
||||
|
||||
initdbArgs =
|
||||
config.initdbArgs
|
||||
++ (lib.optionals (config.superuser != null) ["-U" config.superuser]);
|
||||
++ (lib.optionals (config.superuser != null) [ "-U" config.superuser ]);
|
||||
|
||||
setupScript = pkgs.writeShellScriptBin "setup-postgres" ''
|
||||
set -euo pipefail
|
||||
|
133
nix/redis-cluster.nix
Normal file
133
nix/redis-cluster.nix
Normal file
@ -0,0 +1,133 @@
|
||||
{ pkgs, lib, name, config, ... }:
|
||||
let
|
||||
inherit (lib) types;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
enable = lib.mkEnableOption name;
|
||||
|
||||
package = lib.mkPackageOption pkgs "redis" { };
|
||||
|
||||
dataDir = lib.mkOption {
|
||||
type = types.str;
|
||||
default = "./data/${name}";
|
||||
description = "The redis-cluster data directory (common for all nodes).";
|
||||
};
|
||||
|
||||
nodes = lib.mkOption {
|
||||
type = types.attrsOf (types.submodule {
|
||||
options = {
|
||||
port = lib.mkOption {
|
||||
type = types.int;
|
||||
description = "The TCP port to accept connections. If port is set to `0`, redis will not listen on a TCP socket.";
|
||||
};
|
||||
extraConfig = lib.mkOption {
|
||||
type = types.lines;
|
||||
description = "Extra configuration for this node. To be appended to `redis.conf`.";
|
||||
default = "";
|
||||
};
|
||||
};
|
||||
});
|
||||
default = {
|
||||
"n1" = { port = 30001; };
|
||||
"n2" = { port = 30002; };
|
||||
"n3" = { port = 30003; };
|
||||
"n4" = { port = 30004; };
|
||||
"n5" = { port = 30005; };
|
||||
"n6" = { port = 30006; };
|
||||
};
|
||||
};
|
||||
|
||||
bind = lib.mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = "127.0.0.1";
|
||||
description = ''
|
||||
The IP interface to bind to.
|
||||
`null` means "all interfaces".
|
||||
|
||||
All the nodes will follow the same bind address.
|
||||
'';
|
||||
example = "127.0.0.1";
|
||||
};
|
||||
|
||||
timeout = lib.mkOption {
|
||||
type = types.int;
|
||||
default = 2000;
|
||||
description = ''
|
||||
Time (in milliseconds) after which the node is considered to be down.
|
||||
|
||||
If master node is down, a replica node will take over.
|
||||
'';
|
||||
};
|
||||
|
||||
replicas = lib.mkOption {
|
||||
type = types.int;
|
||||
default = 1;
|
||||
description = ''
|
||||
Number of replicas per Master node.
|
||||
'';
|
||||
};
|
||||
|
||||
outputs.settings = lib.mkOption {
|
||||
type = types.deferredModule;
|
||||
internal = true;
|
||||
readOnly = true;
|
||||
default =
|
||||
let
|
||||
mkNodeProcess = nodeName: cfg:
|
||||
let
|
||||
port = builtins.toString cfg.port;
|
||||
redisConfig = pkgs.writeText "redis.conf" ''
|
||||
port ${port}
|
||||
cluster-enabled yes
|
||||
cluster-config-file nodes-${port}.conf
|
||||
cluster-node-timeout ${builtins.toString config.timeout}
|
||||
appendonly yes
|
||||
appendfilename "appendonly-${port}.aof"
|
||||
dbfilename "dump-${port}.rdb"
|
||||
|
||||
${lib.optionalString (config.bind != null) "bind ${config.bind}"}
|
||||
${cfg.extraConfig}
|
||||
'';
|
||||
|
||||
startScript = pkgs.writeShellScriptBin "start-redis" ''
|
||||
set -euo pipefail
|
||||
|
||||
export REDISDATA=${config.dataDir}
|
||||
|
||||
if [[ ! -d "$REDISDATA" ]]; then
|
||||
mkdir -p "$REDISDATA"
|
||||
fi
|
||||
|
||||
exec ${config.package}/bin/redis-server ${redisConfig} --dir "$REDISDATA"
|
||||
'';
|
||||
in
|
||||
lib.nameValuePair "${name}-${nodeName}" {
|
||||
command = "${startScript}/bin/start-redis";
|
||||
shutdown.command = "${config.package}/bin/redis-cli -p ${port} shutdown nosave";
|
||||
|
||||
readiness_probe = {
|
||||
exec.command = "${config.package}/bin/redis-cli -p ${port} ping";
|
||||
initial_delay_seconds = 2;
|
||||
period_seconds = 10;
|
||||
timeout_seconds = 4;
|
||||
success_threshold = 1;
|
||||
failure_threshold = 5;
|
||||
};
|
||||
|
||||
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
|
||||
availability.restart = "on_failure";
|
||||
};
|
||||
hosts = lib.mapAttrsToList (_: cfg: "${config.bind}:${builtins.toString cfg.port}") config.nodes;
|
||||
in
|
||||
{
|
||||
processes = (lib.mapAttrs' mkNodeProcess config.nodes) // {
|
||||
"${name}-cluster-create" = {
|
||||
depends_on = lib.mapAttrs' (nodeName: cfg: lib.nameValuePair "${name}-${nodeName}" { condition = "process_healthy"; }) config.nodes;
|
||||
command = "${config.package}/bin/redis-cli --cluster create ${lib.concatStringsSep " " hosts} --cluster-replicas ${builtins.toString config.replicas} --cluster-yes";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
16
nix/redis-cluster_test.nix
Normal file
16
nix/redis-cluster_test.nix
Normal file
@ -0,0 +1,16 @@
|
||||
{ config, ... }: {
|
||||
services.redis-cluster."c1".enable = true;
|
||||
testScript = ''
|
||||
process_compose.wait_until(lambda procs:
|
||||
# TODO: Check for 'is_ready' of `c1-cluster-create` instead of `c1-n1` (status of `c1-cluster-create` determines whether the hashslots are assigned).
|
||||
# This should be easy after https://github.com/juspay/services-flake/issues/32
|
||||
procs["c1-n1"]["status"] == "Running"
|
||||
)
|
||||
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30001 ping | grep -q 'PONG'")
|
||||
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30002 ping | grep -q 'PONG'")
|
||||
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30003 ping | grep -q 'PONG'")
|
||||
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30004 ping | grep -q 'PONG'")
|
||||
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30005 ping | grep -q 'PONG'")
|
||||
machine.succeed("${config.services.redis-cluster.c1.package}/bin/redis-cli -p 30006 ping | grep -q 'PONG'")
|
||||
'';
|
||||
}
|
@ -26,6 +26,12 @@
|
||||
../nix/redis_test.nix
|
||||
];
|
||||
};
|
||||
redis-cluster = {
|
||||
imports = [
|
||||
inputs.services-flake.processComposeModules.default
|
||||
../nix/redis-cluster_test.nix
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user