Skip to content

Commit

Permalink
redis-cli --bigkeys ,--hotkeys and --memkeys to replica in cluster mo…
Browse files Browse the repository at this point in the history
…de (redis#12735)

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 <[email protected]>
Co-authored-by: Oran Agra <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent a1f91ff commit 4278ed8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/redis-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 4278ed8

Please sign in to comment.