[misc] add scripts for testing redis cluster

This commit is contained in:
Jakob Ackermann 2020-11-09 17:04:51 +00:00
parent 7b69cc42c9
commit 830d6275b3
3 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1,4 @@
while true; do
seq 0 8 \
| xargs -I% redis-cli -p 700% FLUSHALL > /dev/null
done

View file

@ -0,0 +1,25 @@
/*
execute this script with a redis cluster running to test the health check.
starting and stopping shards with this script running is a good test.
to create a new cluster, use $ ./create-redis-cluster.sh
to run a chaos monkey, use $ ./clear-dbs.sh
*/
const redis = require('../../../')
const rclient = redis.createClient({
cluster: Array.from({ length: 9 }).map((value, index) => {
return { host: '127.0.0.1', port: 7000 + index }
}),
})
setInterval(() => {
rclient.healthCheck((err) => {
if (err) {
console.log('HEALTH CHECK FAILED', err)
} else {
console.log('HEALTH CHECK OK')
}
})
}, 1000)

View file

@ -0,0 +1,73 @@
#!/bin/bash
# USAGE: $0 [NUMBER_OF_NODES, default: 9] [DATA_DIR, default: a new temp dir]
#
# ports are assigned from 7000 on
#
# NOTE: the cluster setup requires redis 5+
set -ex
COUNT=${1:-9}
DATA=$2
if [[ -z "$DATA" ]]; then
IS_TEMP=1
TEMP=`mktemp -d`
DATA="$TEMP"
fi
HAS_DATA=
if [[ -e "$DATA/7000/node.conf" ]]; then
HAS_DATA=1
fi
PIDs=""
cleanup() {
# ensure that we delete the temp dir, no matter how the kill cmd exists
set +e
# invoke kill with at least one PID
echo "$PIDs" | xargs -r kill
if [[ ! -z "$IS_TEMP" ]]; then
rm -rf "$TEMP"
fi
}
trap cleanup exit
for NUM in `seq "$COUNT"`; do
PORT=`expr 6999 + "$NUM"`
CWD="$DATA/$PORT"
mkdir -p "$CWD"
pushd "$CWD"
redis-server \
--appendonly no \
--cluster-enabled yes \
--cluster-config-file node.conf \
--port "$PORT" \
--save "" \
> /dev/null \
&
PIDs="$PIDs $!"
popd
done
# initial nodes
if [[ -z "$HAS_DATA" ]]; then
# confirm the setup
echo yes \
| redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
fi
# scale up as requested
for NUM in `seq 4 "$COUNT"`; do
PORT=`expr 6999 + "$NUM"`
GUARD="$DATA/$PORT/.joined"
if [[ ! -e "$GUARD" ]]; then
redis-cli --cluster add-node "127.0.0.1:$PORT" 127.0.0.1:7000 --cluster-slave
touch "$GUARD"
fi
done
echo "CLUSTER IS READY" >&2
wait