From 4278ed8de5c5c3c023b28c34330ef45aaab75446 Mon Sep 17 00:00:00 2001 From: iKun <110004933+xuliang0317@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:33:28 +0800 Subject: [PATCH] redis-cli --bigkeys ,--hotkeys and --memkeys to replica in cluster mode (#12735) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make redis-cli --bigkeys and --memkeys usable on a replicas in cluster mode, by sending the READONLY command. This is only if -c is also given. We don't detect if a node is a master or a replica so we send READONLY in both cases. The READONLY has no effect on masters. Release notes: Make redis-cli --bigkeys and --memkeys usable on cluster replicas --------- Co-authored-by: Viktor Söderqvist Co-authored-by: Oran Agra --- src/redis-cli.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/redis-cli.c b/src/redis-cli.c index b1b651ea68a..96d667c86de 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -9087,6 +9087,21 @@ static void longStatLoopModeStop(int s) { force_cancel_loop = 1; } +/* In cluster mode we may need to send the READONLY command. + Ignore the error in case the server isn't using cluster mode. */ +static void sendReadOnly(void) { + redisReply *read_reply; + read_reply = redisCommand(context, "READONLY"); + if (read_reply == NULL){ + fprintf(stderr, "\nI/O error\n"); + exit(1); + } else if (read_reply->type == REDIS_REPLY_ERROR && strcmp(read_reply->str, "ERR This instance has cluster support disabled") != 0) { + fprintf(stderr, "Error: %s\n", read_reply->str); + exit(1); + } + freeReplyObject(read_reply); +} + static void findBigKeys(int memkeys, unsigned memkeys_samples) { unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0, scan_loops = 0; redisReply *reply, *keys; @@ -9112,6 +9127,9 @@ static void findBigKeys(int memkeys, unsigned memkeys_samples) { printf("\n# Scanning the entire keyspace to find biggest keys as well as\n"); printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n"); printf("# per 100 SCAN commands (not usually needed).\n\n"); + + /* Use readonly in cluster */ + sendReadOnly(); /* SCAN loop */ do { @@ -9278,6 +9296,9 @@ static void findHotKeys(void) { printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n"); printf("# per 100 SCAN commands (not usually needed).\n\n"); + /* Use readonly in cluster */ + sendReadOnly(); + /* SCAN loop */ do { /* Calculate approximate percentage completion */