1 /* Redis CLI (command line interface)
2  *
3  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "fmacros.h"
32 #include "version.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <signal.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <assert.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <math.h>
48 
49 #include <hiredis.h>
50 #include <sds.h> /* use sds.h from hiredis, so that only one set of sds functions will be present in the binary */
51 #include "dict.h"
52 #include "adlist.h"
53 #include "zmalloc.h"
54 #include "linenoise.h"
55 #include "help.h"
56 #include "anet.h"
57 #include "ae.h"
58 
59 #define UNUSED(V) ((void) V)
60 
61 #define OUTPUT_STANDARD 0
62 #define OUTPUT_RAW 1
63 #define OUTPUT_CSV 2
64 #define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */
65 #define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */
66 #define REDIS_CLI_HISTFILE_ENV "REDISCLI_HISTFILE"
67 #define REDIS_CLI_HISTFILE_DEFAULT ".rediscli_history"
68 #define REDIS_CLI_RCFILE_ENV "REDISCLI_RCFILE"
69 #define REDIS_CLI_RCFILE_DEFAULT ".redisclirc"
70 #define REDIS_CLI_AUTH_ENV "REDISCLI_AUTH"
71 #define REDIS_CLI_CLUSTER_YES_ENV "REDISCLI_CLUSTER_YES"
72 
73 #define CLUSTER_MANAGER_SLOTS               16384
74 #define CLUSTER_MANAGER_MIGRATE_TIMEOUT     60000
75 #define CLUSTER_MANAGER_MIGRATE_PIPELINE    10
76 #define CLUSTER_MANAGER_REBALANCE_THRESHOLD 2
77 
78 #define CLUSTER_MANAGER_INVALID_HOST_ARG \
79     "[ERR] Invalid arguments: you need to pass either a valid " \
80     "address (ie. 120.0.0.1:7000) or space separated IP " \
81     "and port (ie. 120.0.0.1 7000)\n"
82 #define CLUSTER_MANAGER_MODE() (config.cluster_manager_command.name != NULL)
83 #define CLUSTER_MANAGER_MASTERS_COUNT(nodes, replicas) (nodes/(replicas + 1))
84 #define CLUSTER_MANAGER_COMMAND(n,...) \
85         (redisCommand(n->context, __VA_ARGS__))
86 
87 #define CLUSTER_MANAGER_NODE_ARRAY_FREE(array) zfree(array->alloc)
88 
89 #define CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, err) \
90     clusterManagerLogErr("Node %s:%d replied with error:\n%s\n", \
91                          n->ip, n->port, err);
92 
93 #define clusterManagerLogInfo(...) \
94     clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_INFO,__VA_ARGS__)
95 
96 #define clusterManagerLogErr(...) \
97     clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_ERR,__VA_ARGS__)
98 
99 #define clusterManagerLogWarn(...) \
100     clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_WARN,__VA_ARGS__)
101 
102 #define clusterManagerLogOk(...) \
103     clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_SUCCESS,__VA_ARGS__)
104 
105 #define CLUSTER_MANAGER_FLAG_MYSELF     1 << 0
106 #define CLUSTER_MANAGER_FLAG_SLAVE      1 << 1
107 #define CLUSTER_MANAGER_FLAG_FRIEND     1 << 2
108 #define CLUSTER_MANAGER_FLAG_NOADDR     1 << 3
109 #define CLUSTER_MANAGER_FLAG_DISCONNECT 1 << 4
110 #define CLUSTER_MANAGER_FLAG_FAIL       1 << 5
111 
112 #define CLUSTER_MANAGER_CMD_FLAG_FIX            1 << 0
113 #define CLUSTER_MANAGER_CMD_FLAG_SLAVE          1 << 1
114 #define CLUSTER_MANAGER_CMD_FLAG_YES            1 << 2
115 #define CLUSTER_MANAGER_CMD_FLAG_AUTOWEIGHTS    1 << 3
116 #define CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER    1 << 4
117 #define CLUSTER_MANAGER_CMD_FLAG_SIMULATE       1 << 5
118 #define CLUSTER_MANAGER_CMD_FLAG_REPLACE        1 << 6
119 #define CLUSTER_MANAGER_CMD_FLAG_COPY           1 << 7
120 #define CLUSTER_MANAGER_CMD_FLAG_COLOR          1 << 8
121 #define CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS   1 << 9
122 
123 #define CLUSTER_MANAGER_OPT_GETFRIENDS  1 << 0
124 #define CLUSTER_MANAGER_OPT_COLD        1 << 1
125 #define CLUSTER_MANAGER_OPT_UPDATE      1 << 2
126 #define CLUSTER_MANAGER_OPT_QUIET       1 << 6
127 #define CLUSTER_MANAGER_OPT_VERBOSE     1 << 7
128 
129 #define CLUSTER_MANAGER_LOG_LVL_INFO    1
130 #define CLUSTER_MANAGER_LOG_LVL_WARN    2
131 #define CLUSTER_MANAGER_LOG_LVL_ERR     3
132 #define CLUSTER_MANAGER_LOG_LVL_SUCCESS 4
133 
134 #define LOG_COLOR_BOLD      "29;1m"
135 #define LOG_COLOR_RED       "31;1m"
136 #define LOG_COLOR_GREEN     "32;1m"
137 #define LOG_COLOR_YELLOW    "33;1m"
138 #define LOG_COLOR_RESET     "0m"
139 
140 /* cliConnect() flags. */
141 #define CC_FORCE (1<<0)         /* Re-connect if already connected. */
142 #define CC_QUIET (1<<1)         /* Don't log connecting errors. */
143 
144 /* --latency-dist palettes. */
145 int spectrum_palette_color_size = 19;
146 int spectrum_palette_color[] = {0,233,234,235,237,239,241,243,245,247,144,143,142,184,226,214,208,202,196};
147 
148 int spectrum_palette_mono_size = 13;
149 int spectrum_palette_mono[] = {0,233,234,235,237,239,241,243,245,247,249,251,253};
150 
151 /* The actual palette in use. */
152 int *spectrum_palette;
153 int spectrum_palette_size;
154 
155 /* Dict Helpers */
156 
157 static uint64_t dictSdsHash(const void *key);
158 static int dictSdsKeyCompare(void *privdata, const void *key1,
159     const void *key2);
160 static void dictSdsDestructor(void *privdata, void *val);
161 static void dictListDestructor(void *privdata, void *val);
162 
163 /* Cluster Manager Command Info */
164 typedef struct clusterManagerCommand {
165     char *name;
166     int argc;
167     char **argv;
168     int flags;
169     int replicas;
170     char *from;
171     char *to;
172     char **weight;
173     int weight_argc;
174     char *master_id;
175     int slots;
176     int timeout;
177     int pipeline;
178     float threshold;
179 } clusterManagerCommand;
180 
181 static void createClusterManagerCommand(char *cmdname, int argc, char **argv);
182 
183 
184 static redisContext *context;
185 static struct config {
186     char *hostip;
187     int hostport;
188     char *hostsocket;
189     long repeat;
190     long interval;
191     int dbnum;
192     int interactive;
193     int shutdown;
194     int monitor_mode;
195     int pubsub_mode;
196     int latency_mode;
197     int latency_dist_mode;
198     int latency_history;
199     int lru_test_mode;
200     long long lru_test_sample_size;
201     int cluster_mode;
202     int cluster_reissue_command;
203     int slave_mode;
204     int pipe_mode;
205     int pipe_timeout;
206     int getrdb_mode;
207     int stat_mode;
208     int scan_mode;
209     int intrinsic_latency_mode;
210     int intrinsic_latency_duration;
211     char *pattern;
212     char *rdb_filename;
213     int bigkeys;
214     int memkeys;
215     unsigned memkeys_samples;
216     int hotkeys;
217     int stdinarg; /* get last arg from stdin. (-x option) */
218     char *auth;
219     int output; /* output mode, see OUTPUT_* defines */
220     sds mb_delim;
221     char prompt[128];
222     char *eval;
223     int eval_ldb;
224     int eval_ldb_sync;  /* Ask for synchronous mode of the Lua debugger. */
225     int eval_ldb_end;   /* Lua debugging session ended. */
226     int enable_ldb_on_eval; /* Handle manual SCRIPT DEBUG + EVAL commands. */
227     int last_cmd_type;
228     int verbose;
229     clusterManagerCommand cluster_manager_command;
230     int no_auth_warning;
231 } config;
232 
233 /* User preferences. */
234 static struct pref {
235     int hints;
236 } pref;
237 
238 static volatile sig_atomic_t force_cancel_loop = 0;
239 static void usage(void);
240 static void slaveMode(void);
241 char *redisGitSHA1(void);
242 char *redisGitDirty(void);
243 static int cliConnect(int force);
244 
245 static char *getInfoField(char *info, char *field);
246 static long getLongInfoField(char *info, char *field);
247 
248 /*------------------------------------------------------------------------------
249  * Utility functions
250  *--------------------------------------------------------------------------- */
251 
252 uint16_t crc16(const char *buf, int len);
253 
ustime(void)254 static long long ustime(void) {
255     struct timeval tv;
256     long long ust;
257 
258     gettimeofday(&tv, NULL);
259     ust = ((long long)tv.tv_sec)*1000000;
260     ust += tv.tv_usec;
261     return ust;
262 }
263 
mstime(void)264 static long long mstime(void) {
265     return ustime()/1000;
266 }
267 
cliRefreshPrompt(void)268 static void cliRefreshPrompt(void) {
269     if (config.eval_ldb) return;
270 
271     sds prompt = sdsempty();
272     if (config.hostsocket != NULL) {
273         prompt = sdscatfmt(prompt,"redis %s",config.hostsocket);
274     } else {
275         char addr[256];
276         anetFormatAddr(addr, sizeof(addr), config.hostip, config.hostport);
277         prompt = sdscatlen(prompt,addr,strlen(addr));
278     }
279 
280     /* Add [dbnum] if needed */
281     if (config.dbnum != 0)
282         prompt = sdscatfmt(prompt,"[%i]",config.dbnum);
283 
284     /* Copy the prompt in the static buffer. */
285     prompt = sdscatlen(prompt,"> ",2);
286     snprintf(config.prompt,sizeof(config.prompt),"%s",prompt);
287     sdsfree(prompt);
288 }
289 
290 /* Return the name of the dotfile for the specified 'dotfilename'.
291  * Normally it just concatenates user $HOME to the file specified
292  * in 'dotfilename'. However if the environment varialbe 'envoverride'
293  * is set, its value is taken as the path.
294  *
295  * The function returns NULL (if the file is /dev/null or cannot be
296  * obtained for some error), or an SDS string that must be freed by
297  * the user. */
getDotfilePath(char * envoverride,char * dotfilename)298 static sds getDotfilePath(char *envoverride, char *dotfilename) {
299     char *path = NULL;
300     sds dotPath = NULL;
301 
302     /* Check the env for a dotfile override. */
303     path = getenv(envoverride);
304     if (path != NULL && *path != '\0') {
305         if (!strcmp("/dev/null", path)) {
306             return NULL;
307         }
308 
309         /* If the env is set, return it. */
310         dotPath = sdsnew(path);
311     } else {
312         char *home = getenv("HOME");
313         if (home != NULL && *home != '\0') {
314             /* If no override is set use $HOME/<dotfilename>. */
315             dotPath = sdscatprintf(sdsempty(), "%s/%s", home, dotfilename);
316         }
317     }
318     return dotPath;
319 }
320 
321 /* URL-style percent decoding. */
322 #define isHexChar(c) (isdigit(c) || (c >= 'a' && c <= 'f'))
323 #define decodeHexChar(c) (isdigit(c) ? c - '0' : c - 'a' + 10)
324 #define decodeHex(h, l) ((decodeHexChar(h) << 4) + decodeHexChar(l))
325 
percentDecode(const char * pe,size_t len)326 static sds percentDecode(const char *pe, size_t len) {
327     const char *end = pe + len;
328     sds ret = sdsempty();
329     const char *curr = pe;
330 
331     while (curr < end) {
332         if (*curr == '%') {
333             if ((end - curr) < 2) {
334                 fprintf(stderr, "Incomplete URI encoding\n");
335                 exit(1);
336             }
337 
338             char h = tolower(*(++curr));
339             char l = tolower(*(++curr));
340             if (!isHexChar(h) || !isHexChar(l)) {
341                 fprintf(stderr, "Illegal character in URI encoding\n");
342                 exit(1);
343             }
344             char c = decodeHex(h, l);
345             ret = sdscatlen(ret, &c, 1);
346             curr++;
347         } else {
348             ret = sdscatlen(ret, curr++, 1);
349         }
350     }
351 
352     return ret;
353 }
354 
355 /* Parse a URI and extract the server connection information.
356  * URI scheme is based on the the provisional specification[1] excluding support
357  * for query parameters. Valid URIs are:
358  *   scheme:    "redis://"
359  *   authority: [<username> ":"] <password> "@"] [<hostname> [":" <port>]]
360  *   path:      ["/" [<db>]]
361  *
362  *  [1]: https://www.iana.org/assignments/uri-schemes/prov/redis */
parseRedisUri(const char * uri)363 static void parseRedisUri(const char *uri) {
364 
365     const char *scheme = "redis://";
366     const char *curr = uri;
367     const char *end = uri + strlen(uri);
368     const char *userinfo, *username, *port, *host, *path;
369 
370     /* URI must start with a valid scheme. */
371     if (strncasecmp(scheme, curr, strlen(scheme))) {
372         fprintf(stderr,"Invalid URI scheme\n");
373         exit(1);
374     }
375     curr += strlen(scheme);
376     if (curr == end) return;
377 
378     /* Extract user info. */
379     if ((userinfo = strchr(curr,'@'))) {
380         if ((username = strchr(curr, ':')) && username < userinfo) {
381             /* If provided, username is ignored. */
382             curr = username + 1;
383         }
384 
385         config.auth = percentDecode(curr, userinfo - curr);
386         curr = userinfo + 1;
387     }
388     if (curr == end) return;
389 
390     /* Extract host and port. */
391     path = strchr(curr, '/');
392     if (*curr != '/') {
393         host = path ? path - 1 : end;
394         if ((port = strchr(curr, ':'))) {
395             config.hostport = atoi(port + 1);
396             host = port - 1;
397         }
398         config.hostip = sdsnewlen(curr, host - curr + 1);
399     }
400     curr = path ? path + 1 : end;
401     if (curr == end) return;
402 
403     /* Extract database number. */
404     config.dbnum = atoi(curr);
405 }
406 
dictSdsHash(const void * key)407 static uint64_t dictSdsHash(const void *key) {
408     return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
409 }
410 
dictSdsKeyCompare(void * privdata,const void * key1,const void * key2)411 static int dictSdsKeyCompare(void *privdata, const void *key1,
412         const void *key2)
413 {
414     int l1,l2;
415     DICT_NOTUSED(privdata);
416 
417     l1 = sdslen((sds)key1);
418     l2 = sdslen((sds)key2);
419     if (l1 != l2) return 0;
420     return memcmp(key1, key2, l1) == 0;
421 }
422 
dictSdsDestructor(void * privdata,void * val)423 static void dictSdsDestructor(void *privdata, void *val)
424 {
425     DICT_NOTUSED(privdata);
426     sdsfree(val);
427 }
428 
dictListDestructor(void * privdata,void * val)429 void dictListDestructor(void *privdata, void *val)
430 {
431     DICT_NOTUSED(privdata);
432     listRelease((list*)val);
433 }
434 
435 /* _serverAssert is needed by dict */
_serverAssert(const char * estr,const char * file,int line)436 void _serverAssert(const char *estr, const char *file, int line) {
437     fprintf(stderr, "=== ASSERTION FAILED ===");
438     fprintf(stderr, "==> %s:%d '%s' is not true",file,line,estr);
439     *((char*)-1) = 'x';
440 }
441 
442 /*------------------------------------------------------------------------------
443  * Help functions
444  *--------------------------------------------------------------------------- */
445 
446 #define CLI_HELP_COMMAND 1
447 #define CLI_HELP_GROUP 2
448 
449 typedef struct {
450     int type;
451     int argc;
452     sds *argv;
453     sds full;
454 
455     /* Only used for help on commands */
456     struct commandHelp *org;
457 } helpEntry;
458 
459 static helpEntry *helpEntries;
460 static int helpEntriesLen;
461 
cliVersion(void)462 static sds cliVersion(void) {
463     sds version;
464     version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
465 
466     /* Add git commit and working tree status when available */
467     if (strtoll(redisGitSHA1(),NULL,16)) {
468         version = sdscatprintf(version, " (git:%s", redisGitSHA1());
469         if (strtoll(redisGitDirty(),NULL,10))
470             version = sdscatprintf(version, "-dirty");
471         version = sdscat(version, ")");
472     }
473     return version;
474 }
475 
cliInitHelp(void)476 static void cliInitHelp(void) {
477     int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
478     int groupslen = sizeof(commandGroups)/sizeof(char*);
479     int i, len, pos = 0;
480     helpEntry tmp;
481 
482     helpEntriesLen = len = commandslen+groupslen;
483     helpEntries = zmalloc(sizeof(helpEntry)*len);
484 
485     for (i = 0; i < groupslen; i++) {
486         tmp.argc = 1;
487         tmp.argv = zmalloc(sizeof(sds));
488         tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
489         tmp.full = tmp.argv[0];
490         tmp.type = CLI_HELP_GROUP;
491         tmp.org = NULL;
492         helpEntries[pos++] = tmp;
493     }
494 
495     for (i = 0; i < commandslen; i++) {
496         tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
497         tmp.full = sdsnew(commandHelp[i].name);
498         tmp.type = CLI_HELP_COMMAND;
499         tmp.org = &commandHelp[i];
500         helpEntries[pos++] = tmp;
501     }
502 }
503 
504 /* cliInitHelp() setups the helpEntries array with the command and group
505  * names from the help.h file. However the Redis instance we are connecting
506  * to may support more commands, so this function integrates the previous
507  * entries with additional entries obtained using the COMMAND command
508  * available in recent versions of Redis. */
cliIntegrateHelp(void)509 static void cliIntegrateHelp(void) {
510     if (cliConnect(CC_QUIET) == REDIS_ERR) return;
511 
512     redisReply *reply = redisCommand(context, "COMMAND");
513     if(reply == NULL || reply->type != REDIS_REPLY_ARRAY) return;
514 
515     /* Scan the array reported by COMMAND and fill only the entries that
516      * don't already match what we have. */
517     for (size_t j = 0; j < reply->elements; j++) {
518         redisReply *entry = reply->element[j];
519         if (entry->type != REDIS_REPLY_ARRAY || entry->elements < 4 ||
520             entry->element[0]->type != REDIS_REPLY_STRING ||
521             entry->element[1]->type != REDIS_REPLY_INTEGER ||
522             entry->element[3]->type != REDIS_REPLY_INTEGER) return;
523         char *cmdname = entry->element[0]->str;
524         int i;
525 
526         for (i = 0; i < helpEntriesLen; i++) {
527             helpEntry *he = helpEntries+i;
528             if (!strcasecmp(he->argv[0],cmdname))
529                 break;
530         }
531         if (i != helpEntriesLen) continue;
532 
533         helpEntriesLen++;
534         helpEntries = zrealloc(helpEntries,sizeof(helpEntry)*helpEntriesLen);
535         helpEntry *new = helpEntries+(helpEntriesLen-1);
536 
537         new->argc = 1;
538         new->argv = zmalloc(sizeof(sds));
539         new->argv[0] = sdsnew(cmdname);
540         new->full = new->argv[0];
541         new->type = CLI_HELP_COMMAND;
542         sdstoupper(new->argv[0]);
543 
544         struct commandHelp *ch = zmalloc(sizeof(*ch));
545         ch->name = new->argv[0];
546         ch->params = sdsempty();
547         int args = llabs(entry->element[1]->integer);
548         args--; /* Remove the command name itself. */
549         if (entry->element[3]->integer == 1) {
550             ch->params = sdscat(ch->params,"key ");
551             args--;
552         }
553         while(args-- > 0) ch->params = sdscat(ch->params,"arg ");
554         if (entry->element[1]->integer < 0)
555             ch->params = sdscat(ch->params,"...options...");
556         ch->summary = "Help not available";
557         ch->group = 0;
558         ch->since = "not known";
559         new->org = ch;
560     }
561     freeReplyObject(reply);
562 }
563 
564 /* Output command help to stdout. */
cliOutputCommandHelp(struct commandHelp * help,int group)565 static void cliOutputCommandHelp(struct commandHelp *help, int group) {
566     printf("\r\n  \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
567     printf("  \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
568     printf("  \x1b[33msince:\x1b[0m %s\r\n", help->since);
569     if (group) {
570         printf("  \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
571     }
572 }
573 
574 /* Print generic help. */
cliOutputGenericHelp(void)575 static void cliOutputGenericHelp(void) {
576     sds version = cliVersion();
577     printf(
578         "redis-cli %s\n"
579         "To get help about Redis commands type:\n"
580         "      \"help @<group>\" to get a list of commands in <group>\n"
581         "      \"help <command>\" for help on <command>\n"
582         "      \"help <tab>\" to get a list of possible help topics\n"
583         "      \"quit\" to exit\n"
584         "\n"
585         "To set redis-cli preferences:\n"
586         "      \":set hints\" enable online hints\n"
587         "      \":set nohints\" disable online hints\n"
588         "Set your preferences in ~/.redisclirc\n",
589         version
590     );
591     sdsfree(version);
592 }
593 
594 /* Output all command help, filtering by group or command name. */
cliOutputHelp(int argc,char ** argv)595 static void cliOutputHelp(int argc, char **argv) {
596     int i, j, len;
597     int group = -1;
598     helpEntry *entry;
599     struct commandHelp *help;
600 
601     if (argc == 0) {
602         cliOutputGenericHelp();
603         return;
604     } else if (argc > 0 && argv[0][0] == '@') {
605         len = sizeof(commandGroups)/sizeof(char*);
606         for (i = 0; i < len; i++) {
607             if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
608                 group = i;
609                 break;
610             }
611         }
612     }
613 
614     assert(argc > 0);
615     for (i = 0; i < helpEntriesLen; i++) {
616         entry = &helpEntries[i];
617         if (entry->type != CLI_HELP_COMMAND) continue;
618 
619         help = entry->org;
620         if (group == -1) {
621             /* Compare all arguments */
622             if (argc == entry->argc) {
623                 for (j = 0; j < argc; j++) {
624                     if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
625                 }
626                 if (j == argc) {
627                     cliOutputCommandHelp(help,1);
628                 }
629             }
630         } else {
631             if (group == help->group) {
632                 cliOutputCommandHelp(help,0);
633             }
634         }
635     }
636     printf("\r\n");
637 }
638 
639 /* Linenoise completion callback. */
completionCallback(const char * buf,linenoiseCompletions * lc)640 static void completionCallback(const char *buf, linenoiseCompletions *lc) {
641     size_t startpos = 0;
642     int mask;
643     int i;
644     size_t matchlen;
645     sds tmp;
646 
647     if (strncasecmp(buf,"help ",5) == 0) {
648         startpos = 5;
649         while (isspace(buf[startpos])) startpos++;
650         mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
651     } else {
652         mask = CLI_HELP_COMMAND;
653     }
654 
655     for (i = 0; i < helpEntriesLen; i++) {
656         if (!(helpEntries[i].type & mask)) continue;
657 
658         matchlen = strlen(buf+startpos);
659         if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
660             tmp = sdsnewlen(buf,startpos);
661             tmp = sdscat(tmp,helpEntries[i].full);
662             linenoiseAddCompletion(lc,tmp);
663             sdsfree(tmp);
664         }
665     }
666 }
667 
668 /* Linenoise hints callback. */
hintsCallback(const char * buf,int * color,int * bold)669 static char *hintsCallback(const char *buf, int *color, int *bold) {
670     if (!pref.hints) return NULL;
671 
672     int i, argc, buflen = strlen(buf);
673     sds *argv = sdssplitargs(buf,&argc);
674     int endspace = buflen && isspace(buf[buflen-1]);
675 
676     /* Check if the argument list is empty and return ASAP. */
677     if (argc == 0) {
678         sdsfreesplitres(argv,argc);
679         return NULL;
680     }
681 
682     for (i = 0; i < helpEntriesLen; i++) {
683         if (!(helpEntries[i].type & CLI_HELP_COMMAND)) continue;
684 
685         if (strcasecmp(argv[0],helpEntries[i].full) == 0)
686         {
687             *color = 90;
688             *bold = 0;
689             sds hint = sdsnew(helpEntries[i].org->params);
690 
691             /* Remove arguments from the returned hint to show only the
692              * ones the user did not yet typed. */
693             int toremove = argc-1;
694             while(toremove > 0 && sdslen(hint)) {
695                 if (hint[0] == '[') break;
696                 if (hint[0] == ' ') toremove--;
697                 sdsrange(hint,1,-1);
698             }
699 
700             /* Add an initial space if needed. */
701             if (!endspace) {
702                 sds newhint = sdsnewlen(" ",1);
703                 newhint = sdscatsds(newhint,hint);
704                 sdsfree(hint);
705                 hint = newhint;
706             }
707 
708             sdsfreesplitres(argv,argc);
709             return hint;
710         }
711     }
712     sdsfreesplitres(argv,argc);
713     return NULL;
714 }
715 
freeHintsCallback(void * ptr)716 static void freeHintsCallback(void *ptr) {
717     sdsfree(ptr);
718 }
719 
720 /*------------------------------------------------------------------------------
721  * Networking / parsing
722  *--------------------------------------------------------------------------- */
723 
724 /* Send AUTH command to the server */
cliAuth(void)725 static int cliAuth(void) {
726     redisReply *reply;
727     if (config.auth == NULL) return REDIS_OK;
728 
729     reply = redisCommand(context,"AUTH %s",config.auth);
730     if (reply != NULL) {
731         freeReplyObject(reply);
732         return REDIS_OK;
733     }
734     return REDIS_ERR;
735 }
736 
737 /* Send SELECT dbnum to the server */
cliSelect(void)738 static int cliSelect(void) {
739     redisReply *reply;
740     if (config.dbnum == 0) return REDIS_OK;
741 
742     reply = redisCommand(context,"SELECT %d",config.dbnum);
743     if (reply != NULL) {
744         int result = REDIS_OK;
745         if (reply->type == REDIS_REPLY_ERROR) result = REDIS_ERR;
746         freeReplyObject(reply);
747         return result;
748     }
749     return REDIS_ERR;
750 }
751 
752 /* Connect to the server. It is possible to pass certain flags to the function:
753  *      CC_FORCE: The connection is performed even if there is already
754  *                a connected socket.
755  *      CC_QUIET: Don't print errors if connection fails. */
cliConnect(int flags)756 static int cliConnect(int flags) {
757     if (context == NULL || flags & CC_FORCE) {
758         if (context != NULL) {
759             redisFree(context);
760         }
761 
762         if (config.hostsocket == NULL) {
763             context = redisConnect(config.hostip,config.hostport);
764         } else {
765             context = redisConnectUnix(config.hostsocket);
766         }
767 
768         if (context->err) {
769             if (!(flags & CC_QUIET)) {
770                 fprintf(stderr,"Could not connect to Redis at ");
771                 if (config.hostsocket == NULL)
772                     fprintf(stderr,"%s:%d: %s\n",
773                         config.hostip,config.hostport,context->errstr);
774                 else
775                     fprintf(stderr,"%s: %s\n",
776                         config.hostsocket,context->errstr);
777             }
778             redisFree(context);
779             context = NULL;
780             return REDIS_ERR;
781         }
782 
783         /* Set aggressive KEEP_ALIVE socket option in the Redis context socket
784          * in order to prevent timeouts caused by the execution of long
785          * commands. At the same time this improves the detection of real
786          * errors. */
787         anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);
788 
789         /* Do AUTH and select the right DB. */
790         if (cliAuth() != REDIS_OK)
791             return REDIS_ERR;
792         if (cliSelect() != REDIS_OK)
793             return REDIS_ERR;
794     }
795     return REDIS_OK;
796 }
797 
cliPrintContextError(void)798 static void cliPrintContextError(void) {
799     if (context == NULL) return;
800     fprintf(stderr,"Error: %s\n",context->errstr);
801 }
802 
cliFormatReplyTTY(redisReply * r,char * prefix)803 static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
804     sds out = sdsempty();
805     switch (r->type) {
806     case REDIS_REPLY_ERROR:
807         out = sdscatprintf(out,"(error) %s\n", r->str);
808     break;
809     case REDIS_REPLY_STATUS:
810         out = sdscat(out,r->str);
811         out = sdscat(out,"\n");
812     break;
813     case REDIS_REPLY_INTEGER:
814         out = sdscatprintf(out,"(integer) %lld\n",r->integer);
815     break;
816     case REDIS_REPLY_STRING:
817         /* If you are producing output for the standard output we want
818         * a more interesting output with quoted characters and so forth */
819         out = sdscatrepr(out,r->str,r->len);
820         out = sdscat(out,"\n");
821     break;
822     case REDIS_REPLY_NIL:
823         out = sdscat(out,"(nil)\n");
824     break;
825     case REDIS_REPLY_ARRAY:
826         if (r->elements == 0) {
827             out = sdscat(out,"(empty list or set)\n");
828         } else {
829             unsigned int i, idxlen = 0;
830             char _prefixlen[16];
831             char _prefixfmt[16];
832             sds _prefix;
833             sds tmp;
834 
835             /* Calculate chars needed to represent the largest index */
836             i = r->elements;
837             do {
838                 idxlen++;
839                 i /= 10;
840             } while(i);
841 
842             /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
843             memset(_prefixlen,' ',idxlen+2);
844             _prefixlen[idxlen+2] = '\0';
845             _prefix = sdscat(sdsnew(prefix),_prefixlen);
846 
847             /* Setup prefix format for every entry */
848             snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%ud) ",idxlen);
849 
850             for (i = 0; i < r->elements; i++) {
851                 /* Don't use the prefix for the first element, as the parent
852                  * caller already prepended the index number. */
853                 out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
854 
855                 /* Format the multi bulk entry */
856                 tmp = cliFormatReplyTTY(r->element[i],_prefix);
857                 out = sdscatlen(out,tmp,sdslen(tmp));
858                 sdsfree(tmp);
859             }
860             sdsfree(_prefix);
861         }
862     break;
863     default:
864         fprintf(stderr,"Unknown reply type: %d\n", r->type);
865         exit(1);
866     }
867     return out;
868 }
869 
isColorTerm(void)870 int isColorTerm(void) {
871     char *t = getenv("TERM");
872     return t != NULL && strstr(t,"xterm") != NULL;
873 }
874 
875 /* Helper  function for sdsCatColorizedLdbReply() appending colorize strings
876  * to an SDS string. */
sdscatcolor(sds o,char * s,size_t len,char * color)877 sds sdscatcolor(sds o, char *s, size_t len, char *color) {
878     if (!isColorTerm()) return sdscatlen(o,s,len);
879 
880     int bold = strstr(color,"bold") != NULL;
881     int ccode = 37; /* Defaults to white. */
882     if (strstr(color,"red")) ccode = 31;
883     else if (strstr(color,"green")) ccode = 32;
884     else if (strstr(color,"yellow")) ccode = 33;
885     else if (strstr(color,"blue")) ccode = 34;
886     else if (strstr(color,"magenta")) ccode = 35;
887     else if (strstr(color,"cyan")) ccode = 36;
888     else if (strstr(color,"white")) ccode = 37;
889 
890     o = sdscatfmt(o,"\033[%i;%i;49m",bold,ccode);
891     o = sdscatlen(o,s,len);
892     o = sdscat(o,"\033[0m");
893     return o;
894 }
895 
896 /* Colorize Lua debugger status replies according to the prefix they
897  * have. */
sdsCatColorizedLdbReply(sds o,char * s,size_t len)898 sds sdsCatColorizedLdbReply(sds o, char *s, size_t len) {
899     char *color = "white";
900 
901     if (strstr(s,"<debug>")) color = "bold";
902     if (strstr(s,"<redis>")) color = "green";
903     if (strstr(s,"<reply>")) color = "cyan";
904     if (strstr(s,"<error>")) color = "red";
905     if (strstr(s,"<hint>")) color = "bold";
906     if (strstr(s,"<value>") || strstr(s,"<retval>")) color = "magenta";
907     if (len > 4 && isdigit(s[3])) {
908         if (s[1] == '>') color = "yellow"; /* Current line. */
909         else if (s[2] == '#') color = "bold"; /* Break point. */
910     }
911     return sdscatcolor(o,s,len,color);
912 }
913 
cliFormatReplyRaw(redisReply * r)914 static sds cliFormatReplyRaw(redisReply *r) {
915     sds out = sdsempty(), tmp;
916     size_t i;
917 
918     switch (r->type) {
919     case REDIS_REPLY_NIL:
920         /* Nothing... */
921         break;
922     case REDIS_REPLY_ERROR:
923         out = sdscatlen(out,r->str,r->len);
924         out = sdscatlen(out,"\n",1);
925         break;
926     case REDIS_REPLY_STATUS:
927     case REDIS_REPLY_STRING:
928         if (r->type == REDIS_REPLY_STATUS && config.eval_ldb) {
929             /* The Lua debugger replies with arrays of simple (status)
930              * strings. We colorize the output for more fun if this
931              * is a debugging session. */
932 
933             /* Detect the end of a debugging session. */
934             if (strstr(r->str,"<endsession>") == r->str) {
935                 config.enable_ldb_on_eval = 0;
936                 config.eval_ldb = 0;
937                 config.eval_ldb_end = 1; /* Signal the caller session ended. */
938                 config.output = OUTPUT_STANDARD;
939                 cliRefreshPrompt();
940             } else {
941                 out = sdsCatColorizedLdbReply(out,r->str,r->len);
942             }
943         } else {
944             out = sdscatlen(out,r->str,r->len);
945         }
946         break;
947     case REDIS_REPLY_INTEGER:
948         out = sdscatprintf(out,"%lld",r->integer);
949         break;
950     case REDIS_REPLY_ARRAY:
951         for (i = 0; i < r->elements; i++) {
952             if (i > 0) out = sdscat(out,config.mb_delim);
953             tmp = cliFormatReplyRaw(r->element[i]);
954             out = sdscatlen(out,tmp,sdslen(tmp));
955             sdsfree(tmp);
956         }
957         break;
958     default:
959         fprintf(stderr,"Unknown reply type: %d\n", r->type);
960         exit(1);
961     }
962     return out;
963 }
964 
cliFormatReplyCSV(redisReply * r)965 static sds cliFormatReplyCSV(redisReply *r) {
966     unsigned int i;
967 
968     sds out = sdsempty();
969     switch (r->type) {
970     case REDIS_REPLY_ERROR:
971         out = sdscat(out,"ERROR,");
972         out = sdscatrepr(out,r->str,strlen(r->str));
973     break;
974     case REDIS_REPLY_STATUS:
975         out = sdscatrepr(out,r->str,r->len);
976     break;
977     case REDIS_REPLY_INTEGER:
978         out = sdscatprintf(out,"%lld",r->integer);
979     break;
980     case REDIS_REPLY_STRING:
981         out = sdscatrepr(out,r->str,r->len);
982     break;
983     case REDIS_REPLY_NIL:
984         out = sdscat(out,"NIL");
985     break;
986     case REDIS_REPLY_ARRAY:
987         for (i = 0; i < r->elements; i++) {
988             sds tmp = cliFormatReplyCSV(r->element[i]);
989             out = sdscatlen(out,tmp,sdslen(tmp));
990             if (i != r->elements-1) out = sdscat(out,",");
991             sdsfree(tmp);
992         }
993     break;
994     default:
995         fprintf(stderr,"Unknown reply type: %d\n", r->type);
996         exit(1);
997     }
998     return out;
999 }
1000 
cliReadReply(int output_raw_strings)1001 static int cliReadReply(int output_raw_strings) {
1002     void *_reply;
1003     redisReply *reply;
1004     sds out = NULL;
1005     int output = 1;
1006 
1007     if (redisGetReply(context,&_reply) != REDIS_OK) {
1008         if (config.shutdown) {
1009             redisFree(context);
1010             context = NULL;
1011             return REDIS_OK;
1012         }
1013         if (config.interactive) {
1014             /* Filter cases where we should reconnect */
1015             if (context->err == REDIS_ERR_IO &&
1016                 (errno == ECONNRESET || errno == EPIPE))
1017                 return REDIS_ERR;
1018             if (context->err == REDIS_ERR_EOF)
1019                 return REDIS_ERR;
1020         }
1021         cliPrintContextError();
1022         exit(1);
1023         return REDIS_ERR; /* avoid compiler warning */
1024     }
1025 
1026     reply = (redisReply*)_reply;
1027 
1028     config.last_cmd_type = reply->type;
1029 
1030     /* Check if we need to connect to a different node and reissue the
1031      * request. */
1032     if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR &&
1033         (!strncmp(reply->str,"MOVED",5) || !strcmp(reply->str,"ASK")))
1034     {
1035         char *p = reply->str, *s;
1036         int slot;
1037 
1038         output = 0;
1039         /* Comments show the position of the pointer as:
1040          *
1041          * [S] for pointer 's'
1042          * [P] for pointer 'p'
1043          */
1044         s = strchr(p,' ');      /* MOVED[S]3999 127.0.0.1:6381 */
1045         p = strchr(s+1,' ');    /* MOVED[S]3999[P]127.0.0.1:6381 */
1046         *p = '\0';
1047         slot = atoi(s+1);
1048         s = strrchr(p+1,':');    /* MOVED 3999[P]127.0.0.1[S]6381 */
1049         *s = '\0';
1050         sdsfree(config.hostip);
1051         config.hostip = sdsnew(p+1);
1052         config.hostport = atoi(s+1);
1053         if (config.interactive)
1054             printf("-> Redirected to slot [%d] located at %s:%d\n",
1055                 slot, config.hostip, config.hostport);
1056         config.cluster_reissue_command = 1;
1057         cliRefreshPrompt();
1058     }
1059 
1060     if (output) {
1061         if (output_raw_strings) {
1062             out = cliFormatReplyRaw(reply);
1063         } else {
1064             if (config.output == OUTPUT_RAW) {
1065                 out = cliFormatReplyRaw(reply);
1066                 out = sdscat(out,"\n");
1067             } else if (config.output == OUTPUT_STANDARD) {
1068                 out = cliFormatReplyTTY(reply,"");
1069             } else if (config.output == OUTPUT_CSV) {
1070                 out = cliFormatReplyCSV(reply);
1071                 out = sdscat(out,"\n");
1072             }
1073         }
1074         fwrite(out,sdslen(out),1,stdout);
1075         sdsfree(out);
1076     }
1077     freeReplyObject(reply);
1078     return REDIS_OK;
1079 }
1080 
cliSendCommand(int argc,char ** argv,long repeat)1081 static int cliSendCommand(int argc, char **argv, long repeat) {
1082     char *command = argv[0];
1083     size_t *argvlen;
1084     int j, output_raw;
1085 
1086     if (!config.eval_ldb && /* In debugging mode, let's pass "help" to Redis. */
1087         (!strcasecmp(command,"help") || !strcasecmp(command,"?"))) {
1088         cliOutputHelp(--argc, ++argv);
1089         return REDIS_OK;
1090     }
1091 
1092     if (context == NULL) return REDIS_ERR;
1093 
1094     output_raw = 0;
1095     if (!strcasecmp(command,"info") ||
1096         !strcasecmp(command,"lolwut") ||
1097         (argc >= 2 && !strcasecmp(command,"debug") &&
1098                        !strcasecmp(argv[1],"htstats")) ||
1099         (argc >= 2 && !strcasecmp(command,"debug") &&
1100                        !strcasecmp(argv[1],"htstats-key")) ||
1101         (argc >= 2 && !strcasecmp(command,"memory") &&
1102                       (!strcasecmp(argv[1],"malloc-stats") ||
1103                        !strcasecmp(argv[1],"doctor"))) ||
1104         (argc == 2 && !strcasecmp(command,"cluster") &&
1105                       (!strcasecmp(argv[1],"nodes") ||
1106                        !strcasecmp(argv[1],"info"))) ||
1107         (argc >= 2 && !strcasecmp(command,"client") &&
1108                        !strcasecmp(argv[1],"list")) ||
1109         (argc == 3 && !strcasecmp(command,"latency") &&
1110                        !strcasecmp(argv[1],"graph")) ||
1111         (argc == 2 && !strcasecmp(command,"latency") &&
1112                        !strcasecmp(argv[1],"doctor")))
1113     {
1114         output_raw = 1;
1115     }
1116 
1117     if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
1118     if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
1119     if (!strcasecmp(command,"subscribe") ||
1120         !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
1121     if (!strcasecmp(command,"sync") ||
1122         !strcasecmp(command,"psync")) config.slave_mode = 1;
1123 
1124     /* When the user manually calls SCRIPT DEBUG, setup the activation of
1125      * debugging mode on the next eval if needed. */
1126     if (argc == 3 && !strcasecmp(argv[0],"script") &&
1127                      !strcasecmp(argv[1],"debug"))
1128     {
1129         if (!strcasecmp(argv[2],"yes") || !strcasecmp(argv[2],"sync")) {
1130             config.enable_ldb_on_eval = 1;
1131         } else {
1132             config.enable_ldb_on_eval = 0;
1133         }
1134     }
1135 
1136     /* Actually activate LDB on EVAL if needed. */
1137     if (!strcasecmp(command,"eval") && config.enable_ldb_on_eval) {
1138         config.eval_ldb = 1;
1139         config.output = OUTPUT_RAW;
1140     }
1141 
1142     /* Setup argument length */
1143     argvlen = zmalloc(argc*sizeof(size_t));
1144     for (j = 0; j < argc; j++)
1145         argvlen[j] = sdslen(argv[j]);
1146 
1147     while(repeat < 0 || repeat-- > 0) {
1148         redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
1149         while (config.monitor_mode) {
1150             if (cliReadReply(output_raw) != REDIS_OK) exit(1);
1151             fflush(stdout);
1152         }
1153 
1154         if (config.pubsub_mode) {
1155             if (config.output != OUTPUT_RAW)
1156                 printf("Reading messages... (press Ctrl-C to quit)\n");
1157             while (1) {
1158                 if (cliReadReply(output_raw) != REDIS_OK) exit(1);
1159             }
1160         }
1161 
1162         if (config.slave_mode) {
1163             printf("Entering replica output mode...  (press Ctrl-C to quit)\n");
1164             slaveMode();
1165             config.slave_mode = 0;
1166             zfree(argvlen);
1167             return REDIS_ERR;  /* Error = slaveMode lost connection to master */
1168         }
1169 
1170         if (cliReadReply(output_raw) != REDIS_OK) {
1171             zfree(argvlen);
1172             return REDIS_ERR;
1173         } else {
1174             /* Store database number when SELECT was successfully executed. */
1175             if (!strcasecmp(command,"select") && argc == 2 && config.last_cmd_type != REDIS_REPLY_ERROR) {
1176                 config.dbnum = atoi(argv[1]);
1177                 cliRefreshPrompt();
1178             } else if (!strcasecmp(command,"auth") && argc == 2) {
1179                 cliSelect();
1180             }
1181         }
1182         if (config.cluster_reissue_command){
1183             /* If we need to reissue the command, break to prevent a
1184                further 'repeat' number of dud interations */
1185             break;
1186         }
1187         if (config.interval) usleep(config.interval);
1188         fflush(stdout); /* Make it grep friendly */
1189     }
1190 
1191     zfree(argvlen);
1192     return REDIS_OK;
1193 }
1194 
1195 /* Send a command reconnecting the link if needed. */
reconnectingRedisCommand(redisContext * c,const char * fmt,...)1196 static redisReply *reconnectingRedisCommand(redisContext *c, const char *fmt, ...) {
1197     redisReply *reply = NULL;
1198     int tries = 0;
1199     va_list ap;
1200 
1201     assert(!c->err);
1202     while(reply == NULL) {
1203         while (c->err & (REDIS_ERR_IO | REDIS_ERR_EOF)) {
1204             printf("\r\x1b[0K"); /* Cursor to left edge + clear line. */
1205             printf("Reconnecting... %d\r", ++tries);
1206             fflush(stdout);
1207 
1208             redisFree(c);
1209             c = redisConnect(config.hostip,config.hostport);
1210             usleep(1000000);
1211         }
1212 
1213         va_start(ap,fmt);
1214         reply = redisvCommand(c,fmt,ap);
1215         va_end(ap);
1216 
1217         if (c->err && !(c->err & (REDIS_ERR_IO | REDIS_ERR_EOF))) {
1218             fprintf(stderr, "Error: %s\n", c->errstr);
1219             exit(1);
1220         } else if (tries > 0) {
1221             printf("\r\x1b[0K"); /* Cursor to left edge + clear line. */
1222         }
1223     }
1224 
1225     context = c;
1226     return reply;
1227 }
1228 
1229 /*------------------------------------------------------------------------------
1230  * User interface
1231  *--------------------------------------------------------------------------- */
1232 
parseOptions(int argc,char ** argv)1233 static int parseOptions(int argc, char **argv) {
1234     int i;
1235 
1236     for (i = 1; i < argc; i++) {
1237         int lastarg = i==argc-1;
1238 
1239         if (!strcmp(argv[i],"-h") && !lastarg) {
1240             sdsfree(config.hostip);
1241             config.hostip = sdsnew(argv[++i]);
1242         } else if (!strcmp(argv[i],"-h") && lastarg) {
1243             usage();
1244         } else if (!strcmp(argv[i],"--help")) {
1245             usage();
1246         } else if (!strcmp(argv[i],"-x")) {
1247             config.stdinarg = 1;
1248         } else if (!strcmp(argv[i],"-p") && !lastarg) {
1249             config.hostport = atoi(argv[++i]);
1250         } else if (!strcmp(argv[i],"-s") && !lastarg) {
1251             config.hostsocket = argv[++i];
1252         } else if (!strcmp(argv[i],"-r") && !lastarg) {
1253             config.repeat = strtoll(argv[++i],NULL,10);
1254         } else if (!strcmp(argv[i],"-i") && !lastarg) {
1255             double seconds = atof(argv[++i]);
1256             config.interval = seconds*1000000;
1257         } else if (!strcmp(argv[i],"-n") && !lastarg) {
1258             config.dbnum = atoi(argv[++i]);
1259         } else if (!strcmp(argv[i], "--no-auth-warning")) {
1260             config.no_auth_warning = 1;
1261         } else if (!strcmp(argv[i],"-a") && !lastarg) {
1262             config.auth = argv[++i];
1263         } else if (!strcmp(argv[i],"-u") && !lastarg) {
1264             parseRedisUri(argv[++i]);
1265         } else if (!strcmp(argv[i],"--raw")) {
1266             config.output = OUTPUT_RAW;
1267         } else if (!strcmp(argv[i],"--no-raw")) {
1268             config.output = OUTPUT_STANDARD;
1269         } else if (!strcmp(argv[i],"--csv")) {
1270             config.output = OUTPUT_CSV;
1271         } else if (!strcmp(argv[i],"--latency")) {
1272             config.latency_mode = 1;
1273         } else if (!strcmp(argv[i],"--latency-dist")) {
1274             config.latency_dist_mode = 1;
1275         } else if (!strcmp(argv[i],"--mono")) {
1276             spectrum_palette = spectrum_palette_mono;
1277             spectrum_palette_size = spectrum_palette_mono_size;
1278         } else if (!strcmp(argv[i],"--latency-history")) {
1279             config.latency_mode = 1;
1280             config.latency_history = 1;
1281         } else if (!strcmp(argv[i],"--lru-test") && !lastarg) {
1282             config.lru_test_mode = 1;
1283             config.lru_test_sample_size = strtoll(argv[++i],NULL,10);
1284         } else if (!strcmp(argv[i],"--slave")) {
1285             config.slave_mode = 1;
1286         } else if (!strcmp(argv[i],"--replica")) {
1287             config.slave_mode = 1;
1288         } else if (!strcmp(argv[i],"--stat")) {
1289             config.stat_mode = 1;
1290         } else if (!strcmp(argv[i],"--scan")) {
1291             config.scan_mode = 1;
1292         } else if (!strcmp(argv[i],"--pattern") && !lastarg) {
1293             config.pattern = argv[++i];
1294         } else if (!strcmp(argv[i],"--intrinsic-latency") && !lastarg) {
1295             config.intrinsic_latency_mode = 1;
1296             config.intrinsic_latency_duration = atoi(argv[++i]);
1297         } else if (!strcmp(argv[i],"--rdb") && !lastarg) {
1298             config.getrdb_mode = 1;
1299             config.rdb_filename = argv[++i];
1300         } else if (!strcmp(argv[i],"--pipe")) {
1301             config.pipe_mode = 1;
1302         } else if (!strcmp(argv[i],"--pipe-timeout") && !lastarg) {
1303             config.pipe_timeout = atoi(argv[++i]);
1304         } else if (!strcmp(argv[i],"--bigkeys")) {
1305             config.bigkeys = 1;
1306         } else if (!strcmp(argv[i],"--memkeys")) {
1307             config.memkeys = 1;
1308             config.memkeys_samples = 0; /* use redis default */
1309         } else if (!strcmp(argv[i],"--memkeys-samples")) {
1310             config.memkeys = 1;
1311             config.memkeys_samples = atoi(argv[++i]);
1312         } else if (!strcmp(argv[i],"--hotkeys")) {
1313             config.hotkeys = 1;
1314         } else if (!strcmp(argv[i],"--eval") && !lastarg) {
1315             config.eval = argv[++i];
1316         } else if (!strcmp(argv[i],"--ldb")) {
1317             config.eval_ldb = 1;
1318             config.output = OUTPUT_RAW;
1319         } else if (!strcmp(argv[i],"--ldb-sync-mode")) {
1320             config.eval_ldb = 1;
1321             config.eval_ldb_sync = 1;
1322             config.output = OUTPUT_RAW;
1323         } else if (!strcmp(argv[i],"-c")) {
1324             config.cluster_mode = 1;
1325         } else if (!strcmp(argv[i],"-d") && !lastarg) {
1326             sdsfree(config.mb_delim);
1327             config.mb_delim = sdsnew(argv[++i]);
1328         } else if (!strcmp(argv[i],"--verbose")) {
1329             config.verbose = 1;
1330         } else if (!strcmp(argv[i],"--cluster") && !lastarg) {
1331             if (CLUSTER_MANAGER_MODE()) usage();
1332             char *cmd = argv[++i];
1333             int j = i;
1334             while (j < argc && argv[j][0] != '-') j++;
1335             if (j > i) j--;
1336             createClusterManagerCommand(cmd, j - i, argv + i + 1);
1337             i = j;
1338         } else if (!strcmp(argv[i],"--cluster") && lastarg) {
1339             usage();
1340         } else if (!strcmp(argv[i],"--cluster-replicas") && !lastarg) {
1341             config.cluster_manager_command.replicas = atoi(argv[++i]);
1342         } else if (!strcmp(argv[i],"--cluster-master-id") && !lastarg) {
1343             config.cluster_manager_command.master_id = argv[++i];
1344         } else if (!strcmp(argv[i],"--cluster-from") && !lastarg) {
1345             config.cluster_manager_command.from = argv[++i];
1346         } else if (!strcmp(argv[i],"--cluster-to") && !lastarg) {
1347             config.cluster_manager_command.to = argv[++i];
1348         } else if (!strcmp(argv[i],"--cluster-weight") && !lastarg) {
1349             if (config.cluster_manager_command.weight != NULL) {
1350                 fprintf(stderr, "WARNING: you cannot use --cluster-weight "
1351                                 "more than once.\n"
1352                                 "You can set more weights by adding them "
1353                                 "as a space-separated list, ie:\n"
1354                                 "--cluster-weight n1=w n2=w\n");
1355                 exit(1);
1356             }
1357             int widx = i + 1;
1358             char **weight = argv + widx;
1359             int wargc = 0;
1360             for (; widx < argc; widx++) {
1361                 if (strstr(argv[widx], "--") == argv[widx]) break;
1362                 if (strchr(argv[widx], '=') == NULL) break;
1363                 wargc++;
1364             }
1365             if (wargc > 0) {
1366                 config.cluster_manager_command.weight = weight;
1367                 config.cluster_manager_command.weight_argc = wargc;
1368                 i += wargc;
1369             }
1370         } else if (!strcmp(argv[i],"--cluster-slots") && !lastarg) {
1371             config.cluster_manager_command.slots = atoi(argv[++i]);
1372         } else if (!strcmp(argv[i],"--cluster-timeout") && !lastarg) {
1373             config.cluster_manager_command.timeout = atoi(argv[++i]);
1374         } else if (!strcmp(argv[i],"--cluster-pipeline") && !lastarg) {
1375             config.cluster_manager_command.pipeline = atoi(argv[++i]);
1376         } else if (!strcmp(argv[i],"--cluster-threshold") && !lastarg) {
1377             config.cluster_manager_command.threshold = atof(argv[++i]);
1378         } else if (!strcmp(argv[i],"--cluster-yes")) {
1379             config.cluster_manager_command.flags |=
1380                 CLUSTER_MANAGER_CMD_FLAG_YES;
1381         } else if (!strcmp(argv[i],"--cluster-simulate")) {
1382             config.cluster_manager_command.flags |=
1383                 CLUSTER_MANAGER_CMD_FLAG_SIMULATE;
1384         } else if (!strcmp(argv[i],"--cluster-replace")) {
1385             config.cluster_manager_command.flags |=
1386                 CLUSTER_MANAGER_CMD_FLAG_REPLACE;
1387         } else if (!strcmp(argv[i],"--cluster-copy")) {
1388             config.cluster_manager_command.flags |=
1389                 CLUSTER_MANAGER_CMD_FLAG_COPY;
1390         } else if (!strcmp(argv[i],"--cluster-slave")) {
1391             config.cluster_manager_command.flags |=
1392                 CLUSTER_MANAGER_CMD_FLAG_SLAVE;
1393         } else if (!strcmp(argv[i],"--cluster-use-empty-masters")) {
1394             config.cluster_manager_command.flags |=
1395                 CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER;
1396         } else if (!strcmp(argv[i],"--cluster-search-multiple-owners")) {
1397             config.cluster_manager_command.flags |=
1398                 CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS;
1399         } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
1400             sds version = cliVersion();
1401             printf("redis-cli %s\n", version);
1402             sdsfree(version);
1403             exit(0);
1404         } else if (CLUSTER_MANAGER_MODE() && argv[i][0] != '-') {
1405             if (config.cluster_manager_command.argc == 0) {
1406                 int j = i + 1;
1407                 while (j < argc && argv[j][0] != '-') j++;
1408                 int cmd_argc = j - i;
1409                 config.cluster_manager_command.argc = cmd_argc;
1410                 config.cluster_manager_command.argv = argv + i;
1411                 if (cmd_argc > 1) i = j - 1;
1412             }
1413         } else {
1414             if (argv[i][0] == '-') {
1415                 fprintf(stderr,
1416                     "Unrecognized option or bad number of args for: '%s'\n",
1417                     argv[i]);
1418                 exit(1);
1419             } else {
1420                 /* Likely the command name, stop here. */
1421                 break;
1422             }
1423         }
1424     }
1425 
1426     /* --ldb requires --eval. */
1427     if (config.eval_ldb && config.eval == NULL) {
1428         fprintf(stderr,"Options --ldb and --ldb-sync-mode require --eval.\n");
1429         fprintf(stderr,"Try %s --help for more information.\n", argv[0]);
1430         exit(1);
1431     }
1432 
1433     if (!config.no_auth_warning && config.auth != NULL) {
1434         fputs("Warning: Using a password with '-a' or '-u' option on the command"
1435               " line interface may not be safe.\n", stderr);
1436     }
1437 
1438     return i;
1439 }
1440 
parseEnv()1441 static void parseEnv() {
1442     /* Set auth from env, but do not overwrite CLI arguments if passed */
1443     char *auth = getenv(REDIS_CLI_AUTH_ENV);
1444     if (auth != NULL && config.auth == NULL) {
1445         config.auth = auth;
1446     }
1447 
1448     char *cluster_yes = getenv(REDIS_CLI_CLUSTER_YES_ENV);
1449     if (cluster_yes != NULL && !strcmp(cluster_yes, "1")) {
1450         config.cluster_manager_command.flags |= CLUSTER_MANAGER_CMD_FLAG_YES;
1451     }
1452 }
1453 
readArgFromStdin(void)1454 static sds readArgFromStdin(void) {
1455     char buf[1024];
1456     sds arg = sdsempty();
1457 
1458     while(1) {
1459         int nread = read(fileno(stdin),buf,1024);
1460 
1461         if (nread == 0) break;
1462         else if (nread == -1) {
1463             perror("Reading from standard input");
1464             exit(1);
1465         }
1466         arg = sdscatlen(arg,buf,nread);
1467     }
1468     return arg;
1469 }
1470 
usage(void)1471 static void usage(void) {
1472     sds version = cliVersion();
1473     fprintf(stderr,
1474 "redis-cli %s\n"
1475 "\n"
1476 "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
1477 "  -h <hostname>      Server hostname (default: 127.0.0.1).\n"
1478 "  -p <port>          Server port (default: 6379).\n"
1479 "  -s <socket>        Server socket (overrides hostname and port).\n"
1480 "  -a <password>      Password to use when connecting to the server.\n"
1481 "                     You can also use the " REDIS_CLI_AUTH_ENV " environment\n"
1482 "                     variable to pass this password more safely\n"
1483 "                     (if both are used, this argument takes predecence).\n"
1484 "  -u <uri>           Server URI.\n"
1485 "  -r <repeat>        Execute specified command N times.\n"
1486 "  -i <interval>      When -r is used, waits <interval> seconds per command.\n"
1487 "                     It is possible to specify sub-second times like -i 0.1.\n"
1488 "  -n <db>            Database number.\n"
1489 "  -x                 Read last argument from STDIN.\n"
1490 "  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \\n).\n"
1491 "  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).\n"
1492 "  --raw              Use raw formatting for replies (default when STDOUT is\n"
1493 "                     not a tty).\n"
1494 "  --no-raw           Force formatted output even when STDOUT is not a tty.\n"
1495 "  --csv              Output in CSV format.\n"
1496 "  --stat             Print rolling stats about server: mem, clients, ...\n"
1497 "  --latency          Enter a special mode continuously sampling latency.\n"
1498 "                     If you use this mode in an interactive session it runs\n"
1499 "                     forever displaying real-time stats. Otherwise if --raw or\n"
1500 "                     --csv is specified, or if you redirect the output to a non\n"
1501 "                     TTY, it samples the latency for 1 second (you can use\n"
1502 "                     -i to change the interval), then produces a single output\n"
1503 "                     and exits.\n"
1504 "  --latency-history  Like --latency but tracking latency changes over time.\n"
1505 "                     Default time interval is 15 sec. Change it using -i.\n"
1506 "  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.\n"
1507 "                     Default time interval is 1 sec. Change it using -i.\n"
1508 "  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.\n"
1509 "  --replica          Simulate a replica showing commands received from the master.\n"
1510 "  --rdb <filename>   Transfer an RDB dump from remote server to local file.\n"
1511 "  --pipe             Transfer raw Redis protocol from stdin to server.\n"
1512 "  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.\n"
1513 "                     no reply is received within <n> seconds.\n"
1514 "                     Default timeout: %d. Use 0 to wait forever.\n"
1515 "  --bigkeys          Sample Redis keys looking for keys with many elements (complexity).\n"
1516 "  --memkeys          Sample Redis keys looking for keys consuming a lot of memory.\n"
1517 "  --memkeys-samples <n> Sample Redis keys looking for keys consuming a lot of memory.\n"
1518 "                     And define number of key elements to sample\n"
1519 "  --hotkeys          Sample Redis keys looking for hot keys.\n"
1520 "                     only works when maxmemory-policy is *lfu.\n"
1521 "  --scan             List all keys using the SCAN command.\n"
1522 "  --pattern <pat>    Useful with --scan to specify a SCAN pattern.\n"
1523 "  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n"
1524 "                     The test will run for the specified amount of seconds.\n"
1525 "  --eval <file>      Send an EVAL command using the Lua script at <file>.\n"
1526 "  --ldb              Used with --eval enable the Redis Lua debugger.\n"
1527 "  --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in\n"
1528 "                     this mode the server is blocked and script changes are\n"
1529 "                     not rolled back from the server memory.\n"
1530 "  --cluster <command> [args...] [opts...]\n"
1531 "                     Cluster Manager command and arguments (see below).\n"
1532 "  --verbose          Verbose mode.\n"
1533 "  --no-auth-warning  Don't show warning message when using password on command\n"
1534 "                     line interface.\n"
1535 "  --help             Output this help and exit.\n"
1536 "  --version          Output version and exit.\n"
1537 "\n",
1538     version, REDIS_CLI_DEFAULT_PIPE_TIMEOUT);
1539     /* Using another fprintf call to avoid -Woverlength-strings compile warning */
1540     fprintf(stderr,
1541 "Cluster Manager Commands:\n"
1542 "  Use --cluster help to list all available cluster manager commands.\n"
1543 "\n"
1544 "Examples:\n"
1545 "  cat /etc/passwd | redis-cli -x set mypasswd\n"
1546 "  redis-cli get mypasswd\n"
1547 "  redis-cli -r 100 lpush mylist x\n"
1548 "  redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
1549 "  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n"
1550 "  redis-cli --scan --pattern '*:12345*'\n"
1551 "\n"
1552 "  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n"
1553 "\n"
1554 "When no command is given, redis-cli starts in interactive mode.\n"
1555 "Type \"help\" in interactive mode for information on available commands\n"
1556 "and settings.\n"
1557 "\n");
1558     sdsfree(version);
1559     exit(1);
1560 }
1561 
confirmWithYes(char * msg)1562 static int confirmWithYes(char *msg) {
1563     if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_YES) {
1564         return 1;
1565     }
1566     printf("%s (type 'yes' to accept): ", msg);
1567     fflush(stdout);
1568     char buf[4];
1569     int nread = read(fileno(stdin),buf,4);
1570     buf[3] = '\0';
1571     return (nread != 0 && !strcmp("yes", buf));
1572 }
1573 
1574 /* Turn the plain C strings into Sds strings */
convertToSds(int count,char ** args)1575 static char **convertToSds(int count, char** args) {
1576   int j;
1577   char **sds = zmalloc(sizeof(char*)*count);
1578 
1579   for(j = 0; j < count; j++)
1580     sds[j] = sdsnew(args[j]);
1581 
1582   return sds;
1583 }
1584 
issueCommandRepeat(int argc,char ** argv,long repeat)1585 static int issueCommandRepeat(int argc, char **argv, long repeat) {
1586     while (1) {
1587         config.cluster_reissue_command = 0;
1588         if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
1589             cliConnect(CC_FORCE);
1590 
1591             /* If we still cannot send the command print error.
1592              * We'll try to reconnect the next time. */
1593             if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
1594                 cliPrintContextError();
1595                 return REDIS_ERR;
1596             }
1597         }
1598         /* Issue the command again if we got redirected in cluster mode */
1599         if (config.cluster_mode && config.cluster_reissue_command) {
1600             cliConnect(CC_FORCE);
1601         } else {
1602             break;
1603         }
1604     }
1605     return REDIS_OK;
1606 }
1607 
issueCommand(int argc,char ** argv)1608 static int issueCommand(int argc, char **argv) {
1609     return issueCommandRepeat(argc, argv, config.repeat);
1610 }
1611 
1612 /* Split the user provided command into multiple SDS arguments.
1613  * This function normally uses sdssplitargs() from sds.c which is able
1614  * to understand "quoted strings", escapes and so forth. However when
1615  * we are in Lua debugging mode and the "eval" command is used, we want
1616  * the remaining Lua script (after "e " or "eval ") to be passed verbatim
1617  * as a single big argument. */
cliSplitArgs(char * line,int * argc)1618 static sds *cliSplitArgs(char *line, int *argc) {
1619     if (config.eval_ldb && (strstr(line,"eval ") == line ||
1620                             strstr(line,"e ") == line))
1621     {
1622         sds *argv = sds_malloc(sizeof(sds)*2);
1623         *argc = 2;
1624         int len = strlen(line);
1625         int elen = line[1] == ' ' ? 2 : 5; /* "e " or "eval "? */
1626         argv[0] = sdsnewlen(line,elen-1);
1627         argv[1] = sdsnewlen(line+elen,len-elen);
1628         return argv;
1629     } else {
1630         return sdssplitargs(line,argc);
1631     }
1632 }
1633 
1634 /* Set the CLI preferences. This function is invoked when an interactive
1635  * ":command" is called, or when reading ~/.redisclirc file, in order to
1636  * set user preferences. */
cliSetPreferences(char ** argv,int argc,int interactive)1637 void cliSetPreferences(char **argv, int argc, int interactive) {
1638     if (!strcasecmp(argv[0],":set") && argc >= 2) {
1639         if (!strcasecmp(argv[1],"hints")) pref.hints = 1;
1640         else if (!strcasecmp(argv[1],"nohints")) pref.hints = 0;
1641         else {
1642             printf("%sunknown redis-cli preference '%s'\n",
1643                 interactive ? "" : ".redisclirc: ",
1644                 argv[1]);
1645         }
1646     } else {
1647         printf("%sunknown redis-cli internal command '%s'\n",
1648             interactive ? "" : ".redisclirc: ",
1649             argv[0]);
1650     }
1651 }
1652 
1653 /* Load the ~/.redisclirc file if any. */
cliLoadPreferences(void)1654 void cliLoadPreferences(void) {
1655     sds rcfile = getDotfilePath(REDIS_CLI_RCFILE_ENV,REDIS_CLI_RCFILE_DEFAULT);
1656     if (rcfile == NULL) return;
1657     FILE *fp = fopen(rcfile,"r");
1658     char buf[1024];
1659 
1660     if (fp) {
1661         while(fgets(buf,sizeof(buf),fp) != NULL) {
1662             sds *argv;
1663             int argc;
1664 
1665             argv = sdssplitargs(buf,&argc);
1666             if (argc > 0) cliSetPreferences(argv,argc,0);
1667             sdsfreesplitres(argv,argc);
1668         }
1669         fclose(fp);
1670     }
1671     sdsfree(rcfile);
1672 }
1673 
repl(void)1674 static void repl(void) {
1675     sds historyfile = NULL;
1676     int history = 0;
1677     char *line;
1678     int argc;
1679     sds *argv;
1680 
1681     /* Initialize the help and, if possible, use the COMMAND command in order
1682      * to retrieve missing entries. */
1683     cliInitHelp();
1684     cliIntegrateHelp();
1685 
1686     config.interactive = 1;
1687     linenoiseSetMultiLine(1);
1688     linenoiseSetCompletionCallback(completionCallback);
1689     linenoiseSetHintsCallback(hintsCallback);
1690     linenoiseSetFreeHintsCallback(freeHintsCallback);
1691 
1692     /* Only use history and load the rc file when stdin is a tty. */
1693     if (isatty(fileno(stdin))) {
1694         historyfile = getDotfilePath(REDIS_CLI_HISTFILE_ENV,REDIS_CLI_HISTFILE_DEFAULT);
1695         //keep in-memory history always regardless if history file can be determined
1696         history = 1;
1697         if (historyfile != NULL) {
1698             linenoiseHistoryLoad(historyfile);
1699         }
1700         cliLoadPreferences();
1701     }
1702 
1703     cliRefreshPrompt();
1704     while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
1705         if (line[0] != '\0') {
1706             long repeat = 1;
1707             int skipargs = 0;
1708             char *endptr = NULL;
1709 
1710             argv = cliSplitArgs(line,&argc);
1711 
1712             /* check if we have a repeat command option and
1713              * need to skip the first arg */
1714             if (argv && argc > 0) {
1715                 errno = 0;
1716                 repeat = strtol(argv[0], &endptr, 10);
1717                 if (argc > 1 && *endptr == '\0') {
1718                     if (errno == ERANGE || errno == EINVAL || repeat <= 0) {
1719                         fputs("Invalid redis-cli repeat command option value.\n", stdout);
1720                         sdsfreesplitres(argv, argc);
1721                         linenoiseFree(line);
1722                         continue;
1723                     }
1724                     skipargs = 1;
1725                 } else {
1726                     repeat = 1;
1727                 }
1728             }
1729 
1730             /* Won't save auth command in history file */
1731             if (!(argv && argc > 0 && !strcasecmp(argv[0+skipargs], "auth"))) {
1732                 if (history) linenoiseHistoryAdd(line);
1733                 if (historyfile) linenoiseHistorySave(historyfile);
1734             }
1735 
1736             if (argv == NULL) {
1737                 printf("Invalid argument(s)\n");
1738                 linenoiseFree(line);
1739                 continue;
1740             } else if (argc > 0) {
1741                 if (strcasecmp(argv[0],"quit") == 0 ||
1742                     strcasecmp(argv[0],"exit") == 0)
1743                 {
1744                     exit(0);
1745                 } else if (argv[0][0] == ':') {
1746                     cliSetPreferences(argv,argc,1);
1747                     sdsfreesplitres(argv,argc);
1748                     linenoiseFree(line);
1749                     continue;
1750                 } else if (strcasecmp(argv[0],"restart") == 0) {
1751                     if (config.eval) {
1752                         config.eval_ldb = 1;
1753                         config.output = OUTPUT_RAW;
1754                         return; /* Return to evalMode to restart the session. */
1755                     } else {
1756                         printf("Use 'restart' only in Lua debugging mode.");
1757                     }
1758                 } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
1759                     sdsfree(config.hostip);
1760                     config.hostip = sdsnew(argv[1]);
1761                     config.hostport = atoi(argv[2]);
1762                     cliRefreshPrompt();
1763                     cliConnect(CC_FORCE);
1764                 } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
1765                     linenoiseClearScreen();
1766                 } else {
1767                     long long start_time = mstime(), elapsed;
1768 
1769                     issueCommandRepeat(argc-skipargs, argv+skipargs, repeat);
1770 
1771                     /* If our debugging session ended, show the EVAL final
1772                      * reply. */
1773                     if (config.eval_ldb_end) {
1774                         config.eval_ldb_end = 0;
1775                         cliReadReply(0);
1776                         printf("\n(Lua debugging session ended%s)\n\n",
1777                             config.eval_ldb_sync ? "" :
1778                             " -- dataset changes rolled back");
1779                     }
1780 
1781                     elapsed = mstime()-start_time;
1782                     if (elapsed >= 500 &&
1783                         config.output == OUTPUT_STANDARD)
1784                     {
1785                         printf("(%.2fs)\n",(double)elapsed/1000);
1786                     }
1787                 }
1788             }
1789             /* Free the argument vector */
1790             sdsfreesplitres(argv,argc);
1791         }
1792         /* linenoise() returns malloc-ed lines like readline() */
1793         linenoiseFree(line);
1794     }
1795     exit(0);
1796 }
1797 
noninteractive(int argc,char ** argv)1798 static int noninteractive(int argc, char **argv) {
1799     int retval = 0;
1800     if (config.stdinarg) {
1801         argv = zrealloc(argv, (argc+1)*sizeof(char*));
1802         argv[argc] = readArgFromStdin();
1803         retval = issueCommand(argc+1, argv);
1804     } else {
1805         retval = issueCommand(argc, argv);
1806     }
1807     return retval;
1808 }
1809 
1810 /*------------------------------------------------------------------------------
1811  * Eval mode
1812  *--------------------------------------------------------------------------- */
1813 
evalMode(int argc,char ** argv)1814 static int evalMode(int argc, char **argv) {
1815     sds script = NULL;
1816     FILE *fp;
1817     char buf[1024];
1818     size_t nread;
1819     char **argv2;
1820     int j, got_comma, keys;
1821     int retval = REDIS_OK;
1822 
1823     while(1) {
1824         if (config.eval_ldb) {
1825             printf(
1826             "Lua debugging session started, please use:\n"
1827             "quit    -- End the session.\n"
1828             "restart -- Restart the script in debug mode again.\n"
1829             "help    -- Show Lua script debugging commands.\n\n"
1830             );
1831         }
1832 
1833         sdsfree(script);
1834         script = sdsempty();
1835         got_comma = 0;
1836         keys = 0;
1837 
1838         /* Load the script from the file, as an sds string. */
1839         fp = fopen(config.eval,"r");
1840         if (!fp) {
1841             fprintf(stderr,
1842                 "Can't open file '%s': %s\n", config.eval, strerror(errno));
1843             exit(1);
1844         }
1845         while((nread = fread(buf,1,sizeof(buf),fp)) != 0) {
1846             script = sdscatlen(script,buf,nread);
1847         }
1848         fclose(fp);
1849 
1850         /* If we are debugging a script, enable the Lua debugger. */
1851         if (config.eval_ldb) {
1852             redisReply *reply = redisCommand(context,
1853                     config.eval_ldb_sync ?
1854                     "SCRIPT DEBUG sync": "SCRIPT DEBUG yes");
1855             if (reply) freeReplyObject(reply);
1856         }
1857 
1858         /* Create our argument vector */
1859         argv2 = zmalloc(sizeof(sds)*(argc+3));
1860         argv2[0] = sdsnew("EVAL");
1861         argv2[1] = script;
1862         for (j = 0; j < argc; j++) {
1863             if (!got_comma && argv[j][0] == ',' && argv[j][1] == 0) {
1864                 got_comma = 1;
1865                 continue;
1866             }
1867             argv2[j+3-got_comma] = sdsnew(argv[j]);
1868             if (!got_comma) keys++;
1869         }
1870         argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
1871 
1872         /* Call it */
1873         int eval_ldb = config.eval_ldb; /* Save it, may be reverteed. */
1874         retval = issueCommand(argc+3-got_comma, argv2);
1875         if (eval_ldb) {
1876             if (!config.eval_ldb) {
1877                 /* If the debugging session ended immediately, there was an
1878                  * error compiling the script. Show it and they don't enter
1879                  * the REPL at all. */
1880                 printf("Eval debugging session can't start:\n");
1881                 cliReadReply(0);
1882                 break; /* Return to the caller. */
1883             } else {
1884                 strncpy(config.prompt,"lua debugger> ",sizeof(config.prompt));
1885                 repl();
1886                 /* Restart the session if repl() returned. */
1887                 cliConnect(CC_FORCE);
1888                 printf("\n");
1889             }
1890         } else {
1891             break; /* Return to the caller. */
1892         }
1893     }
1894     return retval;
1895 }
1896 
1897 /*------------------------------------------------------------------------------
1898  * Cluster Manager
1899  *--------------------------------------------------------------------------- */
1900 
1901 /* The Cluster Manager global structure */
1902 static struct clusterManager {
1903     list *nodes;    /* List of nodes in the configuration. */
1904     list *errors;
1905 } cluster_manager;
1906 
1907 /* Used by clusterManagerFixSlotsCoverage */
1908 dict *clusterManagerUncoveredSlots = NULL;
1909 
1910 typedef struct clusterManagerNode {
1911     redisContext *context;
1912     sds name;
1913     char *ip;
1914     int port;
1915     uint64_t current_epoch;
1916     time_t ping_sent;
1917     time_t ping_recv;
1918     int flags;
1919     list *flags_str; /* Flags string representations */
1920     sds replicate;  /* Master ID if node is a slave */
1921     int dirty;      /* Node has changes that can be flushed */
1922     uint8_t slots[CLUSTER_MANAGER_SLOTS];
1923     int slots_count;
1924     int replicas_count;
1925     list *friends;
1926     sds *migrating; /* An array of sds where even strings are slots and odd
1927                      * strings are the destination node IDs. */
1928     sds *importing; /* An array of sds where even strings are slots and odd
1929                      * strings are the source node IDs. */
1930     int migrating_count; /* Length of the migrating array (migrating slots*2) */
1931     int importing_count; /* Length of the importing array (importing slots*2) */
1932     float weight;   /* Weight used by rebalance */
1933     int balance;    /* Used by rebalance */
1934 } clusterManagerNode;
1935 
1936 /* Data structure used to represent a sequence of cluster nodes. */
1937 typedef struct clusterManagerNodeArray {
1938     clusterManagerNode **nodes; /* Actual nodes array */
1939     clusterManagerNode **alloc; /* Pointer to the allocated memory */
1940     int len;                    /* Actual length of the array */
1941     int count;                  /* Non-NULL nodes count */
1942 } clusterManagerNodeArray;
1943 
1944 /* Used for the reshard table. */
1945 typedef struct clusterManagerReshardTableItem {
1946     clusterManagerNode *source;
1947     int slot;
1948 } clusterManagerReshardTableItem;
1949 
1950 static dictType clusterManagerDictType = {
1951     dictSdsHash,               /* hash function */
1952     NULL,                      /* key dup */
1953     NULL,                      /* val dup */
1954     dictSdsKeyCompare,         /* key compare */
1955     NULL,                      /* key destructor */
1956     dictSdsDestructor          /* val destructor */
1957 };
1958 
1959 typedef int clusterManagerCommandProc(int argc, char **argv);
1960 typedef int (*clusterManagerOnReplyError)(redisReply *reply, int bulk_idx);
1961 
1962 /* Cluster Manager helper functions */
1963 
1964 static clusterManagerNode *clusterManagerNewNode(char *ip, int port);
1965 static clusterManagerNode *clusterManagerNodeByName(const char *name);
1966 static clusterManagerNode *clusterManagerNodeByAbbreviatedName(const char *n);
1967 static void clusterManagerNodeResetSlots(clusterManagerNode *node);
1968 static int clusterManagerNodeIsCluster(clusterManagerNode *node, char **err);
1969 static void clusterManagerPrintNotClusterNodeError(clusterManagerNode *node,
1970                                                    char *err);
1971 static int clusterManagerNodeLoadInfo(clusterManagerNode *node, int opts,
1972                                       char **err);
1973 static int clusterManagerLoadInfoFromNode(clusterManagerNode *node, int opts);
1974 static int clusterManagerNodeIsEmpty(clusterManagerNode *node, char **err);
1975 static int clusterManagerGetAntiAffinityScore(clusterManagerNodeArray *ipnodes,
1976     int ip_count, clusterManagerNode ***offending, int *offending_len);
1977 static void clusterManagerOptimizeAntiAffinity(clusterManagerNodeArray *ipnodes,
1978     int ip_count);
1979 static sds clusterManagerNodeInfo(clusterManagerNode *node, int indent);
1980 static void clusterManagerShowNodes(void);
1981 static void clusterManagerShowClusterInfo(void);
1982 static int clusterManagerFlushNodeConfig(clusterManagerNode *node, char **err);
1983 static void clusterManagerWaitForClusterJoin(void);
1984 static int clusterManagerCheckCluster(int quiet);
1985 static void clusterManagerLog(int level, const char* fmt, ...);
1986 static int clusterManagerIsConfigConsistent(void);
1987 static void clusterManagerOnError(sds err);
1988 static void clusterManagerNodeArrayInit(clusterManagerNodeArray *array,
1989                                         int len);
1990 static void clusterManagerNodeArrayReset(clusterManagerNodeArray *array);
1991 static void clusterManagerNodeArrayShift(clusterManagerNodeArray *array,
1992                                          clusterManagerNode **nodeptr);
1993 static void clusterManagerNodeArrayAdd(clusterManagerNodeArray *array,
1994                                        clusterManagerNode *node);
1995 
1996 /* Cluster Manager commands. */
1997 
1998 static int clusterManagerCommandCreate(int argc, char **argv);
1999 static int clusterManagerCommandAddNode(int argc, char **argv);
2000 static int clusterManagerCommandDeleteNode(int argc, char **argv);
2001 static int clusterManagerCommandInfo(int argc, char **argv);
2002 static int clusterManagerCommandCheck(int argc, char **argv);
2003 static int clusterManagerCommandFix(int argc, char **argv);
2004 static int clusterManagerCommandReshard(int argc, char **argv);
2005 static int clusterManagerCommandRebalance(int argc, char **argv);
2006 static int clusterManagerCommandSetTimeout(int argc, char **argv);
2007 static int clusterManagerCommandImport(int argc, char **argv);
2008 static int clusterManagerCommandCall(int argc, char **argv);
2009 static int clusterManagerCommandHelp(int argc, char **argv);
2010 
2011 typedef struct clusterManagerCommandDef {
2012     char *name;
2013     clusterManagerCommandProc *proc;
2014     int arity;
2015     char *args;
2016     char *options;
2017 } clusterManagerCommandDef;
2018 
2019 clusterManagerCommandDef clusterManagerCommands[] = {
2020     {"create", clusterManagerCommandCreate, -2, "host1:port1 ... hostN:portN",
2021      "replicas <arg>"},
2022     {"check", clusterManagerCommandCheck, -1, "host:port",
2023      "search-multiple-owners"},
2024     {"info", clusterManagerCommandInfo, -1, "host:port", NULL},
2025     {"fix", clusterManagerCommandFix, -1, "host:port",
2026      "search-multiple-owners"},
2027     {"reshard", clusterManagerCommandReshard, -1, "host:port",
2028      "from <arg>,to <arg>,slots <arg>,yes,timeout <arg>,pipeline <arg>,"
2029      "replace"},
2030     {"rebalance", clusterManagerCommandRebalance, -1, "host:port",
2031      "weight <node1=w1...nodeN=wN>,use-empty-masters,"
2032      "timeout <arg>,simulate,pipeline <arg>,threshold <arg>,replace"},
2033     {"add-node", clusterManagerCommandAddNode, 2,
2034      "new_host:new_port existing_host:existing_port", "slave,master-id <arg>"},
2035     {"del-node", clusterManagerCommandDeleteNode, 2, "host:port node_id",NULL},
2036     {"call", clusterManagerCommandCall, -2,
2037         "host:port command arg arg .. arg", NULL},
2038     {"set-timeout", clusterManagerCommandSetTimeout, 2,
2039      "host:port milliseconds", NULL},
2040     {"import", clusterManagerCommandImport, 1, "host:port",
2041      "from <arg>,copy,replace"},
2042     {"help", clusterManagerCommandHelp, 0, NULL, NULL}
2043 };
2044 
2045 
createClusterManagerCommand(char * cmdname,int argc,char ** argv)2046 static void createClusterManagerCommand(char *cmdname, int argc, char **argv) {
2047     clusterManagerCommand *cmd = &config.cluster_manager_command;
2048     cmd->name = cmdname;
2049     cmd->argc = argc;
2050     cmd->argv = argc ? argv : NULL;
2051     if (isColorTerm()) cmd->flags |= CLUSTER_MANAGER_CMD_FLAG_COLOR;
2052 }
2053 
2054 
validateClusterManagerCommand(void)2055 static clusterManagerCommandProc *validateClusterManagerCommand(void) {
2056     int i, commands_count = sizeof(clusterManagerCommands) /
2057                             sizeof(clusterManagerCommandDef);
2058     clusterManagerCommandProc *proc = NULL;
2059     char *cmdname = config.cluster_manager_command.name;
2060     int argc = config.cluster_manager_command.argc;
2061     for (i = 0; i < commands_count; i++) {
2062         clusterManagerCommandDef cmddef = clusterManagerCommands[i];
2063         if (!strcmp(cmddef.name, cmdname)) {
2064             if ((cmddef.arity > 0 && argc != cmddef.arity) ||
2065                 (cmddef.arity < 0 && argc < (cmddef.arity * -1))) {
2066                 fprintf(stderr, "[ERR] Wrong number of arguments for "
2067                                 "specified --cluster sub command\n");
2068                 return NULL;
2069             }
2070             proc = cmddef.proc;
2071         }
2072     }
2073     if (!proc) fprintf(stderr, "Unknown --cluster subcommand\n");
2074     return proc;
2075 }
2076 
2077 /* Get host ip and port from command arguments. If only one argument has
2078  * been provided it must be in the form of 'ip:port', elsewhere
2079  * the first argument must be the ip and the second one the port.
2080  * If host and port can be detected, it returns 1 and it stores host and
2081  * port into variables referenced by'ip_ptr' and 'port_ptr' pointers,
2082  * elsewhere it returns 0. */
getClusterHostFromCmdArgs(int argc,char ** argv,char ** ip_ptr,int * port_ptr)2083 static int getClusterHostFromCmdArgs(int argc, char **argv,
2084                                      char **ip_ptr, int *port_ptr) {
2085     int port = 0;
2086     char *ip = NULL;
2087     if (argc == 1) {
2088         char *addr = argv[0];
2089         char *c = strrchr(addr, '@');
2090         if (c != NULL) *c = '\0';
2091         c = strrchr(addr, ':');
2092         if (c != NULL) {
2093             *c = '\0';
2094             ip = addr;
2095             port = atoi(++c);
2096         } else return 0;
2097     } else {
2098         ip = argv[0];
2099         port = atoi(argv[1]);
2100     }
2101     if (!ip || !port) return 0;
2102     else {
2103         *ip_ptr = ip;
2104         *port_ptr = port;
2105     }
2106     return 1;
2107 }
2108 
freeClusterManagerNodeFlags(list * flags)2109 static void freeClusterManagerNodeFlags(list *flags) {
2110     listIter li;
2111     listNode *ln;
2112     listRewind(flags, &li);
2113     while ((ln = listNext(&li)) != NULL) {
2114         sds flag = ln->value;
2115         sdsfree(flag);
2116     }
2117     listRelease(flags);
2118 }
2119 
freeClusterManagerNode(clusterManagerNode * node)2120 static void freeClusterManagerNode(clusterManagerNode *node) {
2121     if (node->context != NULL) redisFree(node->context);
2122     if (node->friends != NULL) {
2123         listIter li;
2124         listNode *ln;
2125         listRewind(node->friends,&li);
2126         while ((ln = listNext(&li)) != NULL) {
2127             clusterManagerNode *fn = ln->value;
2128             freeClusterManagerNode(fn);
2129         }
2130         listRelease(node->friends);
2131         node->friends = NULL;
2132     }
2133     if (node->name != NULL) sdsfree(node->name);
2134     if (node->replicate != NULL) sdsfree(node->replicate);
2135     if ((node->flags & CLUSTER_MANAGER_FLAG_FRIEND) && node->ip)
2136         sdsfree(node->ip);
2137     int i;
2138     if (node->migrating != NULL) {
2139         for (i = 0; i < node->migrating_count; i++) sdsfree(node->migrating[i]);
2140         zfree(node->migrating);
2141     }
2142     if (node->importing != NULL) {
2143         for (i = 0; i < node->importing_count; i++) sdsfree(node->importing[i]);
2144         zfree(node->importing);
2145     }
2146     if (node->flags_str != NULL) {
2147         freeClusterManagerNodeFlags(node->flags_str);
2148         node->flags_str = NULL;
2149     }
2150     zfree(node);
2151 }
2152 
freeClusterManager(void)2153 static void freeClusterManager(void) {
2154     listIter li;
2155     listNode *ln;
2156     if (cluster_manager.nodes != NULL) {
2157         listRewind(cluster_manager.nodes,&li);
2158         while ((ln = listNext(&li)) != NULL) {
2159             clusterManagerNode *n = ln->value;
2160             freeClusterManagerNode(n);
2161         }
2162         listRelease(cluster_manager.nodes);
2163         cluster_manager.nodes = NULL;
2164     }
2165     if (cluster_manager.errors != NULL) {
2166         listRewind(cluster_manager.errors,&li);
2167         while ((ln = listNext(&li)) != NULL) {
2168             sds err = ln->value;
2169             sdsfree(err);
2170         }
2171         listRelease(cluster_manager.errors);
2172         cluster_manager.errors = NULL;
2173     }
2174     if (clusterManagerUncoveredSlots != NULL)
2175         dictRelease(clusterManagerUncoveredSlots);
2176 }
2177 
clusterManagerNewNode(char * ip,int port)2178 static clusterManagerNode *clusterManagerNewNode(char *ip, int port) {
2179     clusterManagerNode *node = zmalloc(sizeof(*node));
2180     node->context = NULL;
2181     node->name = NULL;
2182     node->ip = ip;
2183     node->port = port;
2184     node->current_epoch = 0;
2185     node->ping_sent = 0;
2186     node->ping_recv = 0;
2187     node->flags = 0;
2188     node->flags_str = NULL;
2189     node->replicate = NULL;
2190     node->dirty = 0;
2191     node->friends = NULL;
2192     node->migrating = NULL;
2193     node->importing = NULL;
2194     node->migrating_count = 0;
2195     node->importing_count = 0;
2196     node->replicas_count = 0;
2197     node->weight = 1.0f;
2198     node->balance = 0;
2199     clusterManagerNodeResetSlots(node);
2200     return node;
2201 }
2202 
2203 /* Check whether reply is NULL or its type is REDIS_REPLY_ERROR. In the
2204  * latest case, if the 'err' arg is not NULL, it gets allocated with a copy
2205  * of reply error (it's up to the caller function to free it), elsewhere
2206  * the error is directly printed. */
clusterManagerCheckRedisReply(clusterManagerNode * n,redisReply * r,char ** err)2207 static int clusterManagerCheckRedisReply(clusterManagerNode *n,
2208                                          redisReply *r, char **err)
2209 {
2210     int is_err = 0;
2211     if (!r || (is_err = (r->type == REDIS_REPLY_ERROR))) {
2212         if (is_err) {
2213             if (err != NULL) {
2214                 *err = zmalloc((r->len + 1) * sizeof(char));
2215                 strcpy(*err, r->str);
2216             } else CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, r->str);
2217         }
2218         return 0;
2219     }
2220     return 1;
2221 }
2222 
2223 /* Execute MULTI command on a cluster node. */
clusterManagerStartTransaction(clusterManagerNode * node)2224 static int clusterManagerStartTransaction(clusterManagerNode *node) {
2225     redisReply *reply = CLUSTER_MANAGER_COMMAND(node, "MULTI");
2226     int success = clusterManagerCheckRedisReply(node, reply, NULL);
2227     if (reply) freeReplyObject(reply);
2228     return success;
2229 }
2230 
2231 /* Execute EXEC command on a cluster node. */
clusterManagerExecTransaction(clusterManagerNode * node,clusterManagerOnReplyError onerror)2232 static int clusterManagerExecTransaction(clusterManagerNode *node,
2233                                          clusterManagerOnReplyError onerror)
2234 {
2235     redisReply *reply = CLUSTER_MANAGER_COMMAND(node, "EXEC");
2236     int success = clusterManagerCheckRedisReply(node, reply, NULL);
2237     if (success) {
2238         if (reply->type != REDIS_REPLY_ARRAY) {
2239             success = 0;
2240             goto cleanup;
2241         }
2242         size_t i;
2243         for (i = 0; i < reply->elements; i++) {
2244             redisReply *r = reply->element[i];
2245             char *err = NULL;
2246             success = clusterManagerCheckRedisReply(node, r, &err);
2247             if (!success && onerror) success = onerror(r, i);
2248             if (err) {
2249                 if (!success)
2250                     CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);
2251                 zfree(err);
2252             }
2253             if (!success) break;
2254         }
2255     }
2256 cleanup:
2257     if (reply) freeReplyObject(reply);
2258     return success;
2259 }
2260 
clusterManagerNodeConnect(clusterManagerNode * node)2261 static int clusterManagerNodeConnect(clusterManagerNode *node) {
2262     if (node->context) redisFree(node->context);
2263     node->context = redisConnect(node->ip, node->port);
2264     if (node->context->err) {
2265         fprintf(stderr,"Could not connect to Redis at ");
2266         fprintf(stderr,"%s:%d: %s\n", node->ip, node->port,
2267                 node->context->errstr);
2268         redisFree(node->context);
2269         node->context = NULL;
2270         return 0;
2271     }
2272     /* Set aggressive KEEP_ALIVE socket option in the Redis context socket
2273      * in order to prevent timeouts caused by the execution of long
2274      * commands. At the same time this improves the detection of real
2275      * errors. */
2276     anetKeepAlive(NULL, node->context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);
2277     if (config.auth) {
2278         redisReply *reply = redisCommand(node->context,"AUTH %s",config.auth);
2279         int ok = clusterManagerCheckRedisReply(node, reply, NULL);
2280         if (reply != NULL) freeReplyObject(reply);
2281         if (!ok) return 0;
2282     }
2283     return 1;
2284 }
2285 
clusterManagerRemoveNodeFromList(list * nodelist,clusterManagerNode * node)2286 static void clusterManagerRemoveNodeFromList(list *nodelist,
2287                                              clusterManagerNode *node) {
2288     listIter li;
2289     listNode *ln;
2290     listRewind(nodelist, &li);
2291     while ((ln = listNext(&li)) != NULL) {
2292         if (node == ln->value) {
2293             listDelNode(nodelist, ln);
2294             break;
2295         }
2296     }
2297 }
2298 
2299 /* Return the node with the specified name (ID) or NULL. */
clusterManagerNodeByName(const char * name)2300 static clusterManagerNode *clusterManagerNodeByName(const char *name) {
2301     if (cluster_manager.nodes == NULL) return NULL;
2302     clusterManagerNode *found = NULL;
2303     sds lcname = sdsempty();
2304     lcname = sdscpy(lcname, name);
2305     sdstolower(lcname);
2306     listIter li;
2307     listNode *ln;
2308     listRewind(cluster_manager.nodes, &li);
2309     while ((ln = listNext(&li)) != NULL) {
2310         clusterManagerNode *n = ln->value;
2311         if (n->name && !sdscmp(n->name, lcname)) {
2312             found = n;
2313             break;
2314         }
2315     }
2316     sdsfree(lcname);
2317     return found;
2318 }
2319 
2320 /* Like clusterManagerNodeByName but the specified name can be just the first
2321  * part of the node ID as long as the prefix in unique across the
2322  * cluster.
2323  */
clusterManagerNodeByAbbreviatedName(const char * name)2324 static clusterManagerNode *clusterManagerNodeByAbbreviatedName(const char*name)
2325 {
2326     if (cluster_manager.nodes == NULL) return NULL;
2327     clusterManagerNode *found = NULL;
2328     sds lcname = sdsempty();
2329     lcname = sdscpy(lcname, name);
2330     sdstolower(lcname);
2331     listIter li;
2332     listNode *ln;
2333     listRewind(cluster_manager.nodes, &li);
2334     while ((ln = listNext(&li)) != NULL) {
2335         clusterManagerNode *n = ln->value;
2336         if (n->name &&
2337             strstr(n->name, lcname) == n->name) {
2338             found = n;
2339             break;
2340         }
2341     }
2342     sdsfree(lcname);
2343     return found;
2344 }
2345 
clusterManagerNodeResetSlots(clusterManagerNode * node)2346 static void clusterManagerNodeResetSlots(clusterManagerNode *node) {
2347     memset(node->slots, 0, sizeof(node->slots));
2348     node->slots_count = 0;
2349 }
2350 
2351 /* Call "INFO" redis command on the specified node and return the reply. */
clusterManagerGetNodeRedisInfo(clusterManagerNode * node,char ** err)2352 static redisReply *clusterManagerGetNodeRedisInfo(clusterManagerNode *node,
2353                                                   char **err)
2354 {
2355     redisReply *info = CLUSTER_MANAGER_COMMAND(node, "INFO");
2356     if (err != NULL) *err = NULL;
2357     if (info == NULL) return NULL;
2358     if (info->type == REDIS_REPLY_ERROR) {
2359         if (err != NULL) {
2360             *err = zmalloc((info->len + 1) * sizeof(char));
2361             strcpy(*err, info->str);
2362         }
2363         freeReplyObject(info);
2364         return  NULL;
2365     }
2366     return info;
2367 }
2368 
clusterManagerNodeIsCluster(clusterManagerNode * node,char ** err)2369 static int clusterManagerNodeIsCluster(clusterManagerNode *node, char **err) {
2370     redisReply *info = clusterManagerGetNodeRedisInfo(node, err);
2371     if (info == NULL) return 0;
2372     int is_cluster = (int) getLongInfoField(info->str, "cluster_enabled");
2373     freeReplyObject(info);
2374     return is_cluster;
2375 }
2376 
2377 /* Checks whether the node is empty. Node is considered not-empty if it has
2378  * some key or if it already knows other nodes */
clusterManagerNodeIsEmpty(clusterManagerNode * node,char ** err)2379 static int clusterManagerNodeIsEmpty(clusterManagerNode *node, char **err) {
2380     redisReply *info = clusterManagerGetNodeRedisInfo(node, err);
2381     int is_empty = 1;
2382     if (info == NULL) return 0;
2383     if (strstr(info->str, "db0:") != NULL) {
2384         is_empty = 0;
2385         goto result;
2386     }
2387     freeReplyObject(info);
2388     info = CLUSTER_MANAGER_COMMAND(node, "CLUSTER INFO");
2389     if (err != NULL) *err = NULL;
2390     if (!clusterManagerCheckRedisReply(node, info, err)) {
2391         is_empty = 0;
2392         goto result;
2393     }
2394     long known_nodes = getLongInfoField(info->str, "cluster_known_nodes");
2395     is_empty = (known_nodes == 1);
2396 result:
2397     freeReplyObject(info);
2398     return is_empty;
2399 }
2400 
2401 /* Return the anti-affinity score, which is a measure of the amount of
2402  * violations of anti-affinity in the current cluster layout, that is, how
2403  * badly the masters and slaves are distributed in the different IP
2404  * addresses so that slaves of the same master are not in the master
2405  * host and are also in different hosts.
2406  *
2407  * The score is calculated as follows:
2408  *
2409  * SAME_AS_MASTER = 10000 * each slave in the same IP of its master.
2410  * SAME_AS_SLAVE  = 1 * each slave having the same IP as another slave
2411                       of the same master.
2412  * FINAL_SCORE = SAME_AS_MASTER + SAME_AS_SLAVE
2413  *
2414  * So a greater score means a worse anti-affinity level, while zero
2415  * means perfect anti-affinity.
2416  *
2417  * The anti affinity optimizator will try to get a score as low as
2418  * possible. Since we do not want to sacrifice the fact that slaves should
2419  * not be in the same host as the master, we assign 10000 times the score
2420  * to this violation, so that we'll optimize for the second factor only
2421  * if it does not impact the first one.
2422  *
2423  * The ipnodes argument is an array of clusterManagerNodeArray, one for
2424  * each IP, while ip_count is the total number of IPs in the configuration.
2425  *
2426  * The function returns the above score, and the list of
2427  * offending slaves can be stored into the 'offending' argument,
2428  * so that the optimizer can try changing the configuration of the
2429  * slaves violating the anti-affinity goals. */
clusterManagerGetAntiAffinityScore(clusterManagerNodeArray * ipnodes,int ip_count,clusterManagerNode *** offending,int * offending_len)2430 static int clusterManagerGetAntiAffinityScore(clusterManagerNodeArray *ipnodes,
2431     int ip_count, clusterManagerNode ***offending, int *offending_len)
2432 {
2433     int score = 0, i, j;
2434     int node_len = cluster_manager.nodes->len;
2435     clusterManagerNode **offending_p = NULL;
2436     if (offending != NULL) {
2437         *offending = zcalloc(node_len * sizeof(clusterManagerNode*));
2438         offending_p = *offending;
2439     }
2440     /* For each set of nodes in the same host, split by
2441      * related nodes (masters and slaves which are involved in
2442      * replication of each other) */
2443     for (i = 0; i < ip_count; i++) {
2444         clusterManagerNodeArray *node_array = &(ipnodes[i]);
2445         dict *related = dictCreate(&clusterManagerDictType, NULL);
2446         char *ip = NULL;
2447         for (j = 0; j < node_array->len; j++) {
2448             clusterManagerNode *node = node_array->nodes[j];
2449             if (node == NULL) continue;
2450             if (!ip) ip = node->ip;
2451             sds types;
2452             /* We always use the Master ID as key. */
2453             sds key = (!node->replicate ? node->name : node->replicate);
2454             assert(key != NULL);
2455             dictEntry *entry = dictFind(related, key);
2456             if (entry) types = sdsdup((sds) dictGetVal(entry));
2457             else types = sdsempty();
2458             /* Master type 'm' is always set as the first character of the
2459              * types string. */
2460             if (!node->replicate) types = sdscatprintf(types, "m%s", types);
2461             else types = sdscat(types, "s");
2462             dictReplace(related, key, types);
2463         }
2464         /* Now it's trivial to check, for each related group having the
2465          * same host, what is their local score. */
2466         dictIterator *iter = dictGetIterator(related);
2467         dictEntry *entry;
2468         while ((entry = dictNext(iter)) != NULL) {
2469             sds types = (sds) dictGetVal(entry);
2470             sds name = (sds) dictGetKey(entry);
2471             int typeslen = sdslen(types);
2472             if (typeslen < 2) continue;
2473             if (types[0] == 'm') score += (10000 * (typeslen - 1));
2474             else score += (1 * typeslen);
2475             if (offending == NULL) continue;
2476             /* Populate the list of offending nodes. */
2477             listIter li;
2478             listNode *ln;
2479             listRewind(cluster_manager.nodes, &li);
2480             while ((ln = listNext(&li)) != NULL) {
2481                 clusterManagerNode *n = ln->value;
2482                 if (n->replicate == NULL) continue;
2483                 if (!strcmp(n->replicate, name) && !strcmp(n->ip, ip)) {
2484                     *(offending_p++) = n;
2485                     if (offending_len != NULL) (*offending_len)++;
2486                     break;
2487                 }
2488             }
2489         }
2490         //if (offending_len != NULL) *offending_len = offending_p - *offending;
2491         dictReleaseIterator(iter);
2492         dictRelease(related);
2493     }
2494     return score;
2495 }
2496 
clusterManagerOptimizeAntiAffinity(clusterManagerNodeArray * ipnodes,int ip_count)2497 static void clusterManagerOptimizeAntiAffinity(clusterManagerNodeArray *ipnodes,
2498     int ip_count)
2499 {
2500     clusterManagerNode **offenders = NULL;
2501     int score = clusterManagerGetAntiAffinityScore(ipnodes, ip_count,
2502                                                    NULL, NULL);
2503     if (score == 0) goto cleanup;
2504     clusterManagerLogInfo(">>> Trying to optimize slaves allocation "
2505                           "for anti-affinity\n");
2506     int node_len = cluster_manager.nodes->len;
2507     int maxiter = 500 * node_len; // Effort is proportional to cluster size...
2508     srand(time(NULL));
2509     while (maxiter > 0) {
2510         int offending_len = 0;
2511         if (offenders != NULL) {
2512             zfree(offenders);
2513             offenders = NULL;
2514         }
2515         score = clusterManagerGetAntiAffinityScore(ipnodes,
2516                                                    ip_count,
2517                                                    &offenders,
2518                                                    &offending_len);
2519         if (score == 0) break; // Optimal anti affinity reached
2520         /* We'll try to randomly swap a slave's assigned master causing
2521          * an affinity problem with another random slave, to see if we
2522          * can improve the affinity. */
2523         int rand_idx = rand() % offending_len;
2524         clusterManagerNode *first = offenders[rand_idx],
2525                            *second = NULL;
2526         clusterManagerNode **other_replicas = zcalloc((node_len - 1) *
2527                                                       sizeof(*other_replicas));
2528         int other_replicas_count = 0;
2529         listIter li;
2530         listNode *ln;
2531         listRewind(cluster_manager.nodes, &li);
2532         while ((ln = listNext(&li)) != NULL) {
2533             clusterManagerNode *n = ln->value;
2534             if (n != first && n->replicate != NULL)
2535                 other_replicas[other_replicas_count++] = n;
2536         }
2537         if (other_replicas_count == 0) {
2538             zfree(other_replicas);
2539             break;
2540         }
2541         rand_idx = rand() % other_replicas_count;
2542         second = other_replicas[rand_idx];
2543         char *first_master = first->replicate,
2544              *second_master = second->replicate;
2545         first->replicate = second_master, first->dirty = 1;
2546         second->replicate = first_master, second->dirty = 1;
2547         int new_score = clusterManagerGetAntiAffinityScore(ipnodes,
2548                                                            ip_count,
2549                                                            NULL, NULL);
2550         /* If the change actually makes thing worse, revert. Otherwise
2551          * leave as it is because the best solution may need a few
2552          * combined swaps. */
2553         if (new_score > score) {
2554             first->replicate = first_master;
2555             second->replicate = second_master;
2556         }
2557         zfree(other_replicas);
2558         maxiter--;
2559     }
2560     score = clusterManagerGetAntiAffinityScore(ipnodes, ip_count, NULL, NULL);
2561     char *msg;
2562     int perfect = (score == 0);
2563     int log_level = (perfect ? CLUSTER_MANAGER_LOG_LVL_SUCCESS :
2564                                CLUSTER_MANAGER_LOG_LVL_WARN);
2565     if (perfect) msg = "[OK] Perfect anti-affinity obtained!";
2566     else if (score >= 10000)
2567         msg = ("[WARNING] Some slaves are in the same host as their master");
2568     else
2569         msg=("[WARNING] Some slaves of the same master are in the same host");
2570     clusterManagerLog(log_level, "%s\n", msg);
2571 cleanup:
2572     zfree(offenders);
2573 }
2574 
2575 /* Return a representable string of the node's flags */
clusterManagerNodeFlagString(clusterManagerNode * node)2576 static sds clusterManagerNodeFlagString(clusterManagerNode *node) {
2577     sds flags = sdsempty();
2578     if (!node->flags_str) return flags;
2579     int empty = 1;
2580     listIter li;
2581     listNode *ln;
2582     listRewind(node->flags_str, &li);
2583     while ((ln = listNext(&li)) != NULL) {
2584         sds flag = ln->value;
2585         if (strcmp(flag, "myself") == 0) continue;
2586         if (!empty) flags = sdscat(flags, ",");
2587         flags = sdscatfmt(flags, "%S", flag);
2588         empty = 0;
2589     }
2590     return flags;
2591 }
2592 
2593 /* Return a representable string of the node's slots */
clusterManagerNodeSlotsString(clusterManagerNode * node)2594 static sds clusterManagerNodeSlotsString(clusterManagerNode *node) {
2595     sds slots = sdsempty();
2596     int first_range_idx = -1, last_slot_idx = -1, i;
2597     for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {
2598         int has_slot = node->slots[i];
2599         if (has_slot) {
2600             if (first_range_idx == -1) {
2601                 if (sdslen(slots)) slots = sdscat(slots, ",");
2602                 first_range_idx = i;
2603                 slots = sdscatfmt(slots, "[%u", i);
2604             }
2605             last_slot_idx = i;
2606         } else {
2607             if (last_slot_idx >= 0) {
2608                 if (first_range_idx == last_slot_idx)
2609                     slots = sdscat(slots, "]");
2610                 else slots = sdscatfmt(slots, "-%u]", last_slot_idx);
2611             }
2612             last_slot_idx = -1;
2613             first_range_idx = -1;
2614         }
2615     }
2616     if (last_slot_idx >= 0) {
2617         if (first_range_idx == last_slot_idx) slots = sdscat(slots, "]");
2618         else slots = sdscatfmt(slots, "-%u]", last_slot_idx);
2619     }
2620     return slots;
2621 }
2622 
2623 /* -----------------------------------------------------------------------------
2624  * Key space handling
2625  * -------------------------------------------------------------------------- */
2626 
2627 /* We have 16384 hash slots. The hash slot of a given key is obtained
2628  * as the least significant 14 bits of the crc16 of the key.
2629  *
2630  * However if the key contains the {...} pattern, only the part between
2631  * { and } is hashed. This may be useful in the future to force certain
2632  * keys to be in the same node (assuming no resharding is in progress). */
clusterManagerKeyHashSlot(char * key,int keylen)2633 static unsigned int clusterManagerKeyHashSlot(char *key, int keylen) {
2634     int s, e; /* start-end indexes of { and } */
2635 
2636     for (s = 0; s < keylen; s++)
2637         if (key[s] == '{') break;
2638 
2639     /* No '{' ? Hash the whole key. This is the base case. */
2640     if (s == keylen) return crc16(key,keylen) & 0x3FFF;
2641 
2642     /* '{' found? Check if we have the corresponding '}'. */
2643     for (e = s+1; e < keylen; e++)
2644         if (key[e] == '}') break;
2645 
2646     /* No '}' or nothing between {} ? Hash the whole key. */
2647     if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF;
2648 
2649     /* If we are here there is both a { and a } on its right. Hash
2650      * what is in the middle between { and }. */
2651     return crc16(key+s+1,e-s-1) & 0x3FFF;
2652 }
2653 
2654 /* Return a string representation of the cluster node. */
clusterManagerNodeInfo(clusterManagerNode * node,int indent)2655 static sds clusterManagerNodeInfo(clusterManagerNode *node, int indent) {
2656     sds info = sdsempty();
2657     sds spaces = sdsempty();
2658     int i;
2659     for (i = 0; i < indent; i++) spaces = sdscat(spaces, " ");
2660     if (indent) info = sdscat(info, spaces);
2661     int is_master = !(node->flags & CLUSTER_MANAGER_FLAG_SLAVE);
2662     char *role = (is_master ? "M" : "S");
2663     sds slots = NULL;
2664     if (node->dirty && node->replicate != NULL)
2665         info = sdscatfmt(info, "S: %S %s:%u", node->name, node->ip, node->port);
2666     else {
2667         slots = clusterManagerNodeSlotsString(node);
2668         sds flags = clusterManagerNodeFlagString(node);
2669         info = sdscatfmt(info, "%s: %S %s:%u\n"
2670                                "%s   slots:%S (%u slots) "
2671                                "%S",
2672                                role, node->name, node->ip, node->port, spaces,
2673                                slots, node->slots_count, flags);
2674         sdsfree(slots);
2675         sdsfree(flags);
2676     }
2677     if (node->replicate != NULL)
2678         info = sdscatfmt(info, "\n%s   replicates %S", spaces, node->replicate);
2679     else if (node->replicas_count)
2680         info = sdscatfmt(info, "\n%s   %U additional replica(s)",
2681                          spaces, node->replicas_count);
2682     sdsfree(spaces);
2683     return info;
2684 }
2685 
clusterManagerShowNodes(void)2686 static void clusterManagerShowNodes(void) {
2687     listIter li;
2688     listNode *ln;
2689     listRewind(cluster_manager.nodes, &li);
2690     while ((ln = listNext(&li)) != NULL) {
2691         clusterManagerNode *node = ln->value;
2692         sds info = clusterManagerNodeInfo(node, 0);
2693         printf("%s\n", (char *) info);
2694         sdsfree(info);
2695     }
2696 }
2697 
clusterManagerShowClusterInfo(void)2698 static void clusterManagerShowClusterInfo(void) {
2699     int masters = 0;
2700     int keys = 0;
2701     listIter li;
2702     listNode *ln;
2703     listRewind(cluster_manager.nodes, &li);
2704     while ((ln = listNext(&li)) != NULL) {
2705         clusterManagerNode *node = ln->value;
2706         if (!(node->flags & CLUSTER_MANAGER_FLAG_SLAVE)) {
2707             if (!node->name) continue;
2708             int replicas = 0;
2709             int dbsize = -1;
2710             char name[9];
2711             memcpy(name, node->name, 8);
2712             name[8] = '\0';
2713             listIter ri;
2714             listNode *rn;
2715             listRewind(cluster_manager.nodes, &ri);
2716             while ((rn = listNext(&ri)) != NULL) {
2717                 clusterManagerNode *n = rn->value;
2718                 if (n == node || !(n->flags & CLUSTER_MANAGER_FLAG_SLAVE))
2719                     continue;
2720                 if (n->replicate && !strcmp(n->replicate, node->name))
2721                     replicas++;
2722             }
2723             redisReply *reply = CLUSTER_MANAGER_COMMAND(node, "DBSIZE");
2724             if (reply != NULL || reply->type == REDIS_REPLY_INTEGER)
2725                 dbsize = reply->integer;
2726             if (dbsize < 0) {
2727                 char *err = "";
2728                 if (reply != NULL && reply->type == REDIS_REPLY_ERROR)
2729                     err = reply->str;
2730                 CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);
2731                 if (reply != NULL) freeReplyObject(reply);
2732                 return;
2733             };
2734             if (reply != NULL) freeReplyObject(reply);
2735             printf("%s:%d (%s...) -> %d keys | %d slots | %d slaves.\n",
2736                    node->ip, node->port, name, dbsize,
2737                    node->slots_count, replicas);
2738             masters++;
2739             keys += dbsize;
2740         }
2741     }
2742     clusterManagerLogOk("[OK] %d keys in %d masters.\n", keys, masters);
2743     float keys_per_slot = keys / (float) CLUSTER_MANAGER_SLOTS;
2744     printf("%.2f keys per slot on average.\n", keys_per_slot);
2745 }
2746 
2747 /* Flush dirty slots configuration of the node by calling CLUSTER ADDSLOTS */
clusterManagerAddSlots(clusterManagerNode * node,char ** err)2748 static int clusterManagerAddSlots(clusterManagerNode *node, char**err)
2749 {
2750     redisReply *reply = NULL;
2751     void *_reply = NULL;
2752     int success = 1;
2753     /* First two args are used for the command itself. */
2754     int argc = node->slots_count + 2;
2755     sds *argv = zmalloc(argc * sizeof(*argv));
2756     size_t *argvlen = zmalloc(argc * sizeof(*argvlen));
2757     argv[0] = "CLUSTER";
2758     argv[1] = "ADDSLOTS";
2759     argvlen[0] = 7;
2760     argvlen[1] = 8;
2761     *err = NULL;
2762     int i, argv_idx = 2;
2763     for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {
2764         if (argv_idx >= argc) break;
2765         if (node->slots[i]) {
2766             argv[argv_idx] = sdsfromlonglong((long long) i);
2767             argvlen[argv_idx] = sdslen(argv[argv_idx]);
2768             argv_idx++;
2769         }
2770     }
2771     if (!argv_idx) {
2772         success = 0;
2773         goto cleanup;
2774     }
2775     redisAppendCommandArgv(node->context,argc,(const char**)argv,argvlen);
2776     if (redisGetReply(node->context, &_reply) != REDIS_OK) {
2777         success = 0;
2778         goto cleanup;
2779     }
2780     reply = (redisReply*) _reply;
2781     success = clusterManagerCheckRedisReply(node, reply, err);
2782 cleanup:
2783     zfree(argvlen);
2784     if (argv != NULL) {
2785         for (i = 2; i < argc; i++) sdsfree(argv[i]);
2786         zfree(argv);
2787     }
2788     if (reply != NULL) freeReplyObject(reply);
2789     return success;
2790 }
2791 
2792 /* Set slot status to "importing" or "migrating" */
clusterManagerSetSlot(clusterManagerNode * node1,clusterManagerNode * node2,int slot,const char * status,char ** err)2793 static int clusterManagerSetSlot(clusterManagerNode *node1,
2794                                  clusterManagerNode *node2,
2795                                  int slot, const char *status, char **err) {
2796     redisReply *reply = CLUSTER_MANAGER_COMMAND(node1, "CLUSTER "
2797                                                 "SETSLOT %d %s %s",
2798                                                 slot, status,
2799                                                 (char *) node2->name);
2800     if (err != NULL) *err = NULL;
2801     if (!reply) return 0;
2802     int success = 1;
2803     if (reply->type == REDIS_REPLY_ERROR) {
2804         success = 0;
2805         if (err != NULL) {
2806             *err = zmalloc((reply->len + 1) * sizeof(char));
2807             strcpy(*err, reply->str);
2808         } else CLUSTER_MANAGER_PRINT_REPLY_ERROR(node1, reply->str);
2809         goto cleanup;
2810     }
2811 cleanup:
2812     freeReplyObject(reply);
2813     return success;
2814 }
2815 
clusterManagerClearSlotStatus(clusterManagerNode * node,int slot)2816 static int clusterManagerClearSlotStatus(clusterManagerNode *node, int slot) {
2817     redisReply *reply = CLUSTER_MANAGER_COMMAND(node,
2818         "CLUSTER SETSLOT %d %s", slot, "STABLE");
2819     int success = clusterManagerCheckRedisReply(node, reply, NULL);
2820     if (reply) freeReplyObject(reply);
2821     return success;
2822 }
2823 
clusterManagerDelSlot(clusterManagerNode * node,int slot,int ignore_unassigned_err)2824 static int clusterManagerDelSlot(clusterManagerNode *node, int slot,
2825                                  int ignore_unassigned_err)
2826 {
2827     redisReply *reply = CLUSTER_MANAGER_COMMAND(node,
2828         "CLUSTER DELSLOTS %d", slot);
2829     char *err = NULL;
2830     int success = clusterManagerCheckRedisReply(node, reply, &err);
2831     if (!success && reply && reply->type == REDIS_REPLY_ERROR &&
2832         ignore_unassigned_err &&
2833         strstr(reply->str, "already unassigned") != NULL) success = 1;
2834     if (!success && err != NULL) {
2835         CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);
2836         zfree(err);
2837     }
2838     if (reply) freeReplyObject(reply);
2839     return success;
2840 }
2841 
clusterManagerAddSlot(clusterManagerNode * node,int slot)2842 static int clusterManagerAddSlot(clusterManagerNode *node, int slot) {
2843     redisReply *reply = CLUSTER_MANAGER_COMMAND(node,
2844         "CLUSTER ADDSLOTS %d", slot);
2845     int success = clusterManagerCheckRedisReply(node, reply, NULL);
2846     if (reply) freeReplyObject(reply);
2847     return success;
2848 }
2849 
clusterManagerCountKeysInSlot(clusterManagerNode * node,int slot)2850 static signed int clusterManagerCountKeysInSlot(clusterManagerNode *node,
2851                                                 int slot)
2852 {
2853     redisReply *reply = CLUSTER_MANAGER_COMMAND(node,
2854         "CLUSTER COUNTKEYSINSLOT %d", slot);
2855     int count = -1;
2856     int success = clusterManagerCheckRedisReply(node, reply, NULL);
2857     if (success && reply->type == REDIS_REPLY_INTEGER) count = reply->integer;
2858     if (reply) freeReplyObject(reply);
2859     return count;
2860 }
2861 
clusterManagerBumpEpoch(clusterManagerNode * node)2862 static int clusterManagerBumpEpoch(clusterManagerNode *node) {
2863     redisReply *reply = CLUSTER_MANAGER_COMMAND(node, "CLUSTER BUMPEPOCH");
2864     int success = clusterManagerCheckRedisReply(node, reply, NULL);
2865     if (reply) freeReplyObject(reply);
2866     return success;
2867 }
2868 
clusterManagerIgnoreUnassignedErr(redisReply * reply,int bulk_idx)2869 static int clusterManagerIgnoreUnassignedErr(redisReply *reply, int bulk_idx) {
2870     if (bulk_idx == 0 && reply) {
2871         if (reply->type == REDIS_REPLY_ERROR)
2872             return strstr(reply->str, "already unassigned") != NULL;
2873     }
2874     return 0;
2875 }
2876 
clusterManagerSetSlotOwner(clusterManagerNode * owner,int slot,int do_clear)2877 static int clusterManagerSetSlotOwner(clusterManagerNode *owner,
2878                                       int slot,
2879                                       int do_clear)
2880 {
2881     int success = clusterManagerStartTransaction(owner);
2882     if (!success) return 0;
2883     /* Ensure the slot is not already assigned. */
2884     clusterManagerDelSlot(owner, slot, 1);
2885     /* Add the slot and bump epoch. */
2886     clusterManagerAddSlot(owner, slot);
2887     if (do_clear) clusterManagerClearSlotStatus(owner, slot);
2888     clusterManagerBumpEpoch(owner);
2889     success = clusterManagerExecTransaction(owner,
2890         clusterManagerIgnoreUnassignedErr);
2891     return success;
2892 }
2893 
2894 /* Migrate keys taken from reply->elements. It returns the reply from the
2895  * MIGRATE command, or NULL if something goes wrong. If the argument 'dots'
2896  * is not NULL, a dot will be printed for every migrated key. */
clusterManagerMigrateKeysInReply(clusterManagerNode * source,clusterManagerNode * target,redisReply * reply,int replace,int timeout,char * dots)2897 static redisReply *clusterManagerMigrateKeysInReply(clusterManagerNode *source,
2898                                                     clusterManagerNode *target,
2899                                                     redisReply *reply,
2900                                                     int replace, int timeout,
2901                                                     char *dots)
2902 {
2903     redisReply *migrate_reply = NULL;
2904     char **argv = NULL;
2905     size_t *argv_len = NULL;
2906     int c = (replace ? 8 : 7);
2907     if (config.auth) c += 2;
2908     size_t argc = c + reply->elements;
2909     size_t i, offset = 6; // Keys Offset
2910     argv = zcalloc(argc * sizeof(char *));
2911     argv_len = zcalloc(argc * sizeof(size_t));
2912     char portstr[255];
2913     char timeoutstr[255];
2914     snprintf(portstr, 10, "%d", target->port);
2915     snprintf(timeoutstr, 10, "%d", timeout);
2916     argv[0] = "MIGRATE";
2917     argv_len[0] = 7;
2918     argv[1] = target->ip;
2919     argv_len[1] = strlen(target->ip);
2920     argv[2] = portstr;
2921     argv_len[2] = strlen(portstr);
2922     argv[3] = "";
2923     argv_len[3] = 0;
2924     argv[4] = "0";
2925     argv_len[4] = 1;
2926     argv[5] = timeoutstr;
2927     argv_len[5] = strlen(timeoutstr);
2928     if (replace) {
2929         argv[offset] = "REPLACE";
2930         argv_len[offset] = 7;
2931         offset++;
2932     }
2933     if (config.auth) {
2934         argv[offset] = "AUTH";
2935         argv_len[offset] = 4;
2936         offset++;
2937         argv[offset] = config.auth;
2938         argv_len[offset] = strlen(config.auth);
2939         offset++;
2940     }
2941     argv[offset] = "KEYS";
2942     argv_len[offset] = 4;
2943     offset++;
2944     for (i = 0; i < reply->elements; i++) {
2945         redisReply *entry = reply->element[i];
2946         size_t idx = i + offset;
2947         assert(entry->type == REDIS_REPLY_STRING);
2948         argv[idx] = (char *) sdsnew(entry->str);
2949         argv_len[idx] = entry->len;
2950         if (dots) dots[i] = '.';
2951     }
2952     if (dots) dots[reply->elements] = '\0';
2953     void *_reply = NULL;
2954     redisAppendCommandArgv(source->context,argc,
2955                            (const char**)argv,argv_len);
2956     int success = (redisGetReply(source->context, &_reply) == REDIS_OK);
2957     for (i = 0; i < reply->elements; i++) sdsfree(argv[i + offset]);
2958     if (!success) goto cleanup;
2959     migrate_reply = (redisReply *) _reply;
2960 cleanup:
2961     zfree(argv);
2962     zfree(argv_len);
2963     return migrate_reply;
2964 }
2965 
2966 /* Migrate all keys in the given slot from source to target.*/
clusterManagerMigrateKeysInSlot(clusterManagerNode * source,clusterManagerNode * target,int slot,int timeout,int pipeline,int verbose,char ** err)2967 static int clusterManagerMigrateKeysInSlot(clusterManagerNode *source,
2968                                            clusterManagerNode *target,
2969                                            int slot, int timeout,
2970                                            int pipeline, int verbose,
2971                                            char **err)
2972 {
2973     int success = 1;
2974     int replace_existing_keys = (config.cluster_manager_command.flags &
2975             (CLUSTER_MANAGER_CMD_FLAG_FIX | CLUSTER_MANAGER_CMD_FLAG_REPLACE));
2976     while (1) {
2977         char *dots = NULL;
2978         redisReply *reply = NULL, *migrate_reply = NULL;
2979         reply = CLUSTER_MANAGER_COMMAND(source, "CLUSTER "
2980                                         "GETKEYSINSLOT %d %d", slot,
2981                                         pipeline);
2982         success = (reply != NULL);
2983         if (!success) return 0;
2984         if (reply->type == REDIS_REPLY_ERROR) {
2985             success = 0;
2986             if (err != NULL) {
2987                 *err = zmalloc((reply->len + 1) * sizeof(char));
2988                 strcpy(*err, reply->str);
2989                 CLUSTER_MANAGER_PRINT_REPLY_ERROR(source, *err);
2990             }
2991             goto next;
2992         }
2993         assert(reply->type == REDIS_REPLY_ARRAY);
2994         size_t count = reply->elements;
2995         if (count == 0) {
2996             freeReplyObject(reply);
2997             break;
2998         }
2999         if (verbose) dots = zmalloc((count+1) * sizeof(char));
3000         /* Calling MIGRATE command. */
3001         migrate_reply = clusterManagerMigrateKeysInReply(source, target,
3002                                                          reply, 0, timeout,
3003                                                          dots);
3004         if (migrate_reply == NULL) goto next;
3005         if (migrate_reply->type == REDIS_REPLY_ERROR) {
3006             int is_busy = strstr(migrate_reply->str, "BUSYKEY") != NULL;
3007             int not_served = strstr(migrate_reply->str, "slot not served") != NULL;
3008             if (replace_existing_keys && (is_busy || not_served)) {
3009                 /* If the key already exists, try to migrate keys
3010                  * adding REPLACE option.
3011                  * If the key's slot is not served, try to assign slot
3012                  * to the target node. */
3013                 if (not_served)
3014                     clusterManagerSetSlot(source, target, slot, "node", NULL);
3015                 clusterManagerLogWarn("*** Target key exists. "
3016                                       "Replacing it for FIX.\n");
3017                 freeReplyObject(migrate_reply);
3018                 migrate_reply = clusterManagerMigrateKeysInReply(source,
3019                                                                  target,
3020                                                                  reply,
3021                                                                  is_busy,
3022                                                                  timeout,
3023                                                                  NULL);
3024                 success = (migrate_reply != NULL &&
3025                            migrate_reply->type != REDIS_REPLY_ERROR);
3026             } else success = 0;
3027             if (!success) {
3028                 if (migrate_reply != NULL) {
3029                     if (err) {
3030                         *err = zmalloc((migrate_reply->len + 1) * sizeof(char));
3031                         strcpy(*err, migrate_reply->str);
3032                     }
3033                     printf("\n");
3034                     CLUSTER_MANAGER_PRINT_REPLY_ERROR(source,
3035                                                       migrate_reply->str);
3036                 }
3037                 goto next;
3038             }
3039         }
3040         if (verbose) {
3041             printf("%s", dots);
3042             fflush(stdout);
3043         }
3044 next:
3045         if (reply != NULL) freeReplyObject(reply);
3046         if (migrate_reply != NULL) freeReplyObject(migrate_reply);
3047         if (dots) zfree(dots);
3048         if (!success) break;
3049     }
3050     return success;
3051 }
3052 
3053 /* Move slots between source and target nodes using MIGRATE.
3054  *
3055  * Options:
3056  * CLUSTER_MANAGER_OPT_VERBOSE -- Print a dot for every moved key.
3057  * CLUSTER_MANAGER_OPT_COLD    -- Move keys without opening slots /
3058  *                                reconfiguring the nodes.
3059  * CLUSTER_MANAGER_OPT_UPDATE  -- Update node->slots for source/target nodes.
3060  * CLUSTER_MANAGER_OPT_QUIET   -- Don't print info messages.
3061 */
clusterManagerMoveSlot(clusterManagerNode * source,clusterManagerNode * target,int slot,int opts,char ** err)3062 static int clusterManagerMoveSlot(clusterManagerNode *source,
3063                                   clusterManagerNode *target,
3064                                   int slot, int opts,  char**err)
3065 {
3066     if (!(opts & CLUSTER_MANAGER_OPT_QUIET)) {
3067         printf("Moving slot %d from %s:%d to %s:%d: ", slot, source->ip,
3068                source->port, target->ip, target->port);
3069         fflush(stdout);
3070     }
3071     if (err != NULL) *err = NULL;
3072     int pipeline = config.cluster_manager_command.pipeline,
3073         timeout = config.cluster_manager_command.timeout,
3074         print_dots = (opts & CLUSTER_MANAGER_OPT_VERBOSE),
3075         option_cold = (opts & CLUSTER_MANAGER_OPT_COLD),
3076         success = 1;
3077     if (!option_cold) {
3078         success = clusterManagerSetSlot(target, source, slot,
3079                                         "importing", err);
3080         if (!success) return 0;
3081         success = clusterManagerSetSlot(source, target, slot,
3082                                         "migrating", err);
3083         if (!success) return 0;
3084     }
3085     success = clusterManagerMigrateKeysInSlot(source, target, slot, timeout,
3086                                               pipeline, print_dots, err);
3087     if (!(opts & CLUSTER_MANAGER_OPT_QUIET)) printf("\n");
3088     if (!success) return 0;
3089     /* Set the new node as the owner of the slot in all the known nodes. */
3090     if (!option_cold) {
3091         listIter li;
3092         listNode *ln;
3093         listRewind(cluster_manager.nodes, &li);
3094         while ((ln = listNext(&li)) != NULL) {
3095             clusterManagerNode *n = ln->value;
3096             if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
3097             redisReply *r = CLUSTER_MANAGER_COMMAND(n, "CLUSTER "
3098                                                     "SETSLOT %d %s %s",
3099                                                     slot, "node",
3100                                                     target->name);
3101             success = (r != NULL);
3102             if (!success) return 0;
3103             if (r->type == REDIS_REPLY_ERROR) {
3104                 success = 0;
3105                 if (err != NULL) {
3106                     *err = zmalloc((r->len + 1) * sizeof(char));
3107                     strcpy(*err, r->str);
3108                     CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, *err);
3109                 }
3110             }
3111             freeReplyObject(r);
3112             if (!success) return 0;
3113         }
3114     }
3115     /* Update the node logical config */
3116     if (opts & CLUSTER_MANAGER_OPT_UPDATE) {
3117         source->slots[slot] = 0;
3118         target->slots[slot] = 1;
3119     }
3120     return 1;
3121 }
3122 
3123 /* Flush the dirty node configuration by calling replicate for slaves or
3124  * adding the slots defined in the masters. */
clusterManagerFlushNodeConfig(clusterManagerNode * node,char ** err)3125 static int clusterManagerFlushNodeConfig(clusterManagerNode *node, char **err) {
3126     if (!node->dirty) return 0;
3127     redisReply *reply = NULL;
3128     int is_err = 0, success = 1;
3129     if (err != NULL) *err = NULL;
3130     if (node->replicate != NULL) {
3131         reply = CLUSTER_MANAGER_COMMAND(node, "CLUSTER REPLICATE %s",
3132                                         node->replicate);
3133         if (reply == NULL || (is_err = (reply->type == REDIS_REPLY_ERROR))) {
3134             if (is_err && err != NULL) {
3135                 *err = zmalloc((reply->len + 1) * sizeof(char));
3136                 strcpy(*err, reply->str);
3137             }
3138             success = 0;
3139             /* If the cluster did not already joined it is possible that
3140              * the slave does not know the master node yet. So on errors
3141              * we return ASAP leaving the dirty flag set, to flush the
3142              * config later. */
3143             goto cleanup;
3144         }
3145     } else {
3146         int added = clusterManagerAddSlots(node, err);
3147         if (!added || *err != NULL) success = 0;
3148     }
3149     node->dirty = 0;
3150 cleanup:
3151     if (reply != NULL) freeReplyObject(reply);
3152     return success;
3153 }
3154 
3155 /* Wait until the cluster configuration is consistent. */
clusterManagerWaitForClusterJoin(void)3156 static void clusterManagerWaitForClusterJoin(void) {
3157     printf("Waiting for the cluster to join\n");
3158     while(!clusterManagerIsConfigConsistent()) {
3159         printf(".");
3160         fflush(stdout);
3161         sleep(1);
3162     }
3163     printf("\n");
3164 }
3165 
3166 /* Load node's cluster configuration by calling "CLUSTER NODES" command.
3167  * Node's configuration (name, replicate, slots, ...) is then updated.
3168  * If CLUSTER_MANAGER_OPT_GETFRIENDS flag is set into 'opts' argument,
3169  * and node already knows other nodes, the node's friends list is populated
3170  * with the other nodes info. */
clusterManagerNodeLoadInfo(clusterManagerNode * node,int opts,char ** err)3171 static int clusterManagerNodeLoadInfo(clusterManagerNode *node, int opts,
3172                                       char **err)
3173 {
3174     redisReply *reply = CLUSTER_MANAGER_COMMAND(node, "CLUSTER NODES");
3175     int success = 1;
3176     *err = NULL;
3177     if (!clusterManagerCheckRedisReply(node, reply, err)) {
3178         success = 0;
3179         goto cleanup;
3180     }
3181     int getfriends = (opts & CLUSTER_MANAGER_OPT_GETFRIENDS);
3182     char *lines = reply->str, *p, *line;
3183     while ((p = strstr(lines, "\n")) != NULL) {
3184         *p = '\0';
3185         line = lines;
3186         lines = p + 1;
3187         char *name = NULL, *addr = NULL, *flags = NULL, *master_id = NULL,
3188              *ping_sent = NULL, *ping_recv = NULL, *config_epoch = NULL,
3189              *link_status = NULL;
3190         UNUSED(link_status);
3191         int i = 0;
3192         while ((p = strchr(line, ' ')) != NULL) {
3193             *p = '\0';
3194             char *token = line;
3195             line = p + 1;
3196             switch(i++){
3197             case 0: name = token; break;
3198             case 1: addr = token; break;
3199             case 2: flags = token; break;
3200             case 3: master_id = token; break;
3201             case 4: ping_sent = token; break;
3202             case 5: ping_recv = token; break;
3203             case 6: config_epoch = token; break;
3204             case 7: link_status = token; break;
3205             }
3206             if (i == 8) break; // Slots
3207         }
3208         if (!flags) {
3209             success = 0;
3210             goto cleanup;
3211         }
3212         int myself = (strstr(flags, "myself") != NULL);
3213         clusterManagerNode *currentNode = NULL;
3214         if (myself) {
3215             node->flags |= CLUSTER_MANAGER_FLAG_MYSELF;
3216             currentNode = node;
3217             clusterManagerNodeResetSlots(node);
3218             if (i == 8) {
3219                 int remaining = strlen(line);
3220                 while (remaining > 0) {
3221                     p = strchr(line, ' ');
3222                     if (p == NULL) p = line + remaining;
3223                     remaining -= (p - line);
3224 
3225                     char *slotsdef = line;
3226                     *p = '\0';
3227                     if (remaining) {
3228                         line = p + 1;
3229                         remaining--;
3230                     } else line = p;
3231                     char *dash = NULL;
3232                     if (slotsdef[0] == '[') {
3233                         slotsdef++;
3234                         if ((p = strstr(slotsdef, "->-"))) { // Migrating
3235                             *p = '\0';
3236                             p += 3;
3237                             char *closing_bracket = strchr(p, ']');
3238                             if (closing_bracket) *closing_bracket = '\0';
3239                             sds slot = sdsnew(slotsdef);
3240                             sds dst = sdsnew(p);
3241                             node->migrating_count += 2;
3242                             node->migrating = zrealloc(node->migrating,
3243                                 (node->migrating_count * sizeof(sds)));
3244                             node->migrating[node->migrating_count - 2] =
3245                                 slot;
3246                             node->migrating[node->migrating_count - 1] =
3247                                 dst;
3248                         }  else if ((p = strstr(slotsdef, "-<-"))) {//Importing
3249                             *p = '\0';
3250                             p += 3;
3251                             char *closing_bracket = strchr(p, ']');
3252                             if (closing_bracket) *closing_bracket = '\0';
3253                             sds slot = sdsnew(slotsdef);
3254                             sds src = sdsnew(p);
3255                             node->importing_count += 2;
3256                             node->importing = zrealloc(node->importing,
3257                                 (node->importing_count * sizeof(sds)));
3258                             node->importing[node->importing_count - 2] =
3259                                 slot;
3260                             node->importing[node->importing_count - 1] =
3261                                 src;
3262                         }
3263                     } else if ((dash = strchr(slotsdef, '-')) != NULL) {
3264                         p = dash;
3265                         int start, stop;
3266                         *p = '\0';
3267                         start = atoi(slotsdef);
3268                         stop = atoi(p + 1);
3269                         node->slots_count += (stop - (start - 1));
3270                         while (start <= stop) node->slots[start++] = 1;
3271                     } else if (p > slotsdef) {
3272                         node->slots[atoi(slotsdef)] = 1;
3273                         node->slots_count++;
3274                     }
3275                 }
3276             }
3277             node->dirty = 0;
3278         } else if (!getfriends) {
3279             if (!(node->flags & CLUSTER_MANAGER_FLAG_MYSELF)) continue;
3280             else break;
3281         } else {
3282             if (addr == NULL) {
3283                 fprintf(stderr, "Error: invalid CLUSTER NODES reply\n");
3284                 success = 0;
3285                 goto cleanup;
3286             }
3287             char *c = strrchr(addr, '@');
3288             if (c != NULL) *c = '\0';
3289             c = strrchr(addr, ':');
3290             if (c == NULL) {
3291                 fprintf(stderr, "Error: invalid CLUSTER NODES reply\n");
3292                 success = 0;
3293                 goto cleanup;
3294             }
3295             *c = '\0';
3296             int port = atoi(++c);
3297             currentNode = clusterManagerNewNode(sdsnew(addr), port);
3298             currentNode->flags |= CLUSTER_MANAGER_FLAG_FRIEND;
3299             if (node->friends == NULL) node->friends = listCreate();
3300             listAddNodeTail(node->friends, currentNode);
3301         }
3302         if (name != NULL) {
3303             if (currentNode->name) sdsfree(currentNode->name);
3304             currentNode->name = sdsnew(name);
3305         }
3306         if (currentNode->flags_str != NULL)
3307             freeClusterManagerNodeFlags(currentNode->flags_str);
3308         currentNode->flags_str = listCreate();
3309         int flag_len;
3310         while ((flag_len = strlen(flags)) > 0) {
3311             sds flag = NULL;
3312             char *fp = strchr(flags, ',');
3313             if (fp) {
3314                 *fp = '\0';
3315                 flag = sdsnew(flags);
3316                 flags = fp + 1;
3317             } else {
3318                 flag = sdsnew(flags);
3319                 flags += flag_len;
3320             }
3321             if (strcmp(flag, "noaddr") == 0)
3322                 currentNode->flags |= CLUSTER_MANAGER_FLAG_NOADDR;
3323             else if (strcmp(flag, "disconnected") == 0)
3324                 currentNode->flags |= CLUSTER_MANAGER_FLAG_DISCONNECT;
3325             else if (strcmp(flag, "fail") == 0)
3326                 currentNode->flags |= CLUSTER_MANAGER_FLAG_FAIL;
3327             else if (strcmp(flag, "slave") == 0) {
3328                 currentNode->flags |= CLUSTER_MANAGER_FLAG_SLAVE;
3329                 if (master_id != NULL) {
3330                     if (currentNode->replicate) sdsfree(currentNode->replicate);
3331                     currentNode->replicate = sdsnew(master_id);
3332                 }
3333             }
3334             listAddNodeTail(currentNode->flags_str, flag);
3335         }
3336         if (config_epoch != NULL)
3337             currentNode->current_epoch = atoll(config_epoch);
3338         if (ping_sent != NULL) currentNode->ping_sent = atoll(ping_sent);
3339         if (ping_recv != NULL) currentNode->ping_recv = atoll(ping_recv);
3340         if (!getfriends && myself) break;
3341     }
3342 cleanup:
3343     if (reply) freeReplyObject(reply);
3344     return success;
3345 }
3346 
3347 /* Retrieves info about the cluster using argument 'node' as the starting
3348  * point. All nodes will be loaded inside the cluster_manager.nodes list.
3349  * Warning: if something goes wrong, it will free the starting node before
3350  * returning 0. */
clusterManagerLoadInfoFromNode(clusterManagerNode * node,int opts)3351 static int clusterManagerLoadInfoFromNode(clusterManagerNode *node, int opts) {
3352     if (node->context == NULL && !clusterManagerNodeConnect(node)) {
3353         freeClusterManagerNode(node);
3354         return 0;
3355     }
3356     opts |= CLUSTER_MANAGER_OPT_GETFRIENDS;
3357     char *e = NULL;
3358     if (!clusterManagerNodeIsCluster(node, &e)) {
3359         clusterManagerPrintNotClusterNodeError(node, e);
3360         if (e) zfree(e);
3361         freeClusterManagerNode(node);
3362         return 0;
3363     }
3364     e = NULL;
3365     if (!clusterManagerNodeLoadInfo(node, opts, &e)) {
3366         if (e) {
3367             CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, e);
3368             zfree(e);
3369         }
3370         freeClusterManagerNode(node);
3371         return 0;
3372     }
3373     listIter li;
3374     listNode *ln;
3375     if (cluster_manager.nodes != NULL) {
3376         listRewind(cluster_manager.nodes, &li);
3377         while ((ln = listNext(&li)) != NULL)
3378             freeClusterManagerNode((clusterManagerNode *) ln->value);
3379         listRelease(cluster_manager.nodes);
3380     }
3381     cluster_manager.nodes = listCreate();
3382     listAddNodeTail(cluster_manager.nodes, node);
3383     if (node->friends != NULL) {
3384         listRewind(node->friends, &li);
3385         while ((ln = listNext(&li)) != NULL) {
3386             clusterManagerNode *friend = ln->value;
3387             if (!friend->ip || !friend->port) goto invalid_friend;
3388             if (!friend->context && !clusterManagerNodeConnect(friend))
3389                 goto invalid_friend;
3390             e = NULL;
3391             if (clusterManagerNodeLoadInfo(friend, 0, &e)) {
3392                 if (friend->flags & (CLUSTER_MANAGER_FLAG_NOADDR |
3393                                      CLUSTER_MANAGER_FLAG_DISCONNECT |
3394                                      CLUSTER_MANAGER_FLAG_FAIL))
3395                     goto invalid_friend;
3396                 listAddNodeTail(cluster_manager.nodes, friend);
3397             } else {
3398                 clusterManagerLogErr("[ERR] Unable to load info for "
3399                                      "node %s:%d\n",
3400                                      friend->ip, friend->port);
3401                 goto invalid_friend;
3402             }
3403             continue;
3404 invalid_friend:
3405             freeClusterManagerNode(friend);
3406         }
3407         listRelease(node->friends);
3408         node->friends = NULL;
3409     }
3410     // Count replicas for each node
3411     listRewind(cluster_manager.nodes, &li);
3412     while ((ln = listNext(&li)) != NULL) {
3413         clusterManagerNode *n = ln->value;
3414         if (n->replicate != NULL) {
3415             clusterManagerNode *master = clusterManagerNodeByName(n->replicate);
3416             if (master == NULL) {
3417                 clusterManagerLogWarn("*** WARNING: %s:%d claims to be "
3418                                       "slave of unknown node ID %s.\n",
3419                                       n->ip, n->port, n->replicate);
3420             } else master->replicas_count++;
3421         }
3422     }
3423     return 1;
3424 }
3425 
3426 /* Compare functions used by various sorting operations. */
clusterManagerSlotCompare(const void * slot1,const void * slot2)3427 int clusterManagerSlotCompare(const void *slot1, const void *slot2) {
3428     const char **i1 = (const char **)slot1;
3429     const char **i2 = (const char **)slot2;
3430     return strcmp(*i1, *i2);
3431 }
3432 
clusterManagerSlotCountCompareDesc(const void * n1,const void * n2)3433 int clusterManagerSlotCountCompareDesc(const void *n1, const void *n2) {
3434     clusterManagerNode *node1 = *((clusterManagerNode **) n1);
3435     clusterManagerNode *node2 = *((clusterManagerNode **) n2);
3436     return node2->slots_count - node1->slots_count;
3437 }
3438 
clusterManagerCompareNodeBalance(const void * n1,const void * n2)3439 int clusterManagerCompareNodeBalance(const void *n1, const void *n2) {
3440     clusterManagerNode *node1 = *((clusterManagerNode **) n1);
3441     clusterManagerNode *node2 = *((clusterManagerNode **) n2);
3442     return node1->balance - node2->balance;
3443 }
3444 
clusterManagerGetConfigSignature(clusterManagerNode * node)3445 static sds clusterManagerGetConfigSignature(clusterManagerNode *node) {
3446     sds signature = NULL;
3447     int node_count = 0, i = 0, name_len = 0;
3448     char **node_configs = NULL;
3449     redisReply *reply = CLUSTER_MANAGER_COMMAND(node, "CLUSTER NODES");
3450     if (reply == NULL || reply->type == REDIS_REPLY_ERROR)
3451         goto cleanup;
3452     char *lines = reply->str, *p, *line;
3453     while ((p = strstr(lines, "\n")) != NULL) {
3454         i = 0;
3455         *p = '\0';
3456         line = lines;
3457         lines = p + 1;
3458         char *nodename = NULL;
3459         int tot_size = 0;
3460         while ((p = strchr(line, ' ')) != NULL) {
3461             *p = '\0';
3462             char *token = line;
3463             line = p + 1;
3464             if (i == 0) {
3465                 nodename = token;
3466                 tot_size = (p - token);
3467                 name_len = tot_size++; // Make room for ':' in tot_size
3468             }
3469             if (++i == 8) break;
3470         }
3471         if (i != 8) continue;
3472         if (nodename == NULL) continue;
3473         int remaining = strlen(line);
3474         if (remaining == 0) continue;
3475         char **slots = NULL;
3476         int c = 0;
3477         while (remaining > 0) {
3478             p = strchr(line, ' ');
3479             if (p == NULL) p = line + remaining;
3480             int size = (p - line);
3481             remaining -= size;
3482             tot_size += size;
3483             char *slotsdef = line;
3484             *p = '\0';
3485             if (remaining) {
3486                 line = p + 1;
3487                 remaining--;
3488             } else line = p;
3489             if (slotsdef[0] != '[') {
3490                 c++;
3491                 slots = zrealloc(slots, (c * sizeof(char *)));
3492                 slots[c - 1] = slotsdef;
3493             }
3494         }
3495         if (c > 0) {
3496             if (c > 1)
3497                 qsort(slots, c, sizeof(char *), clusterManagerSlotCompare);
3498             node_count++;
3499             node_configs =
3500                 zrealloc(node_configs, (node_count * sizeof(char *)));
3501             /* Make room for '|' separators. */
3502             tot_size += (sizeof(char) * (c - 1));
3503             char *cfg = zmalloc((sizeof(char) * tot_size) + 1);
3504             memcpy(cfg, nodename, name_len);
3505             char *sp = cfg + name_len;
3506             *(sp++) = ':';
3507             for (i = 0; i < c; i++) {
3508                 if (i > 0) *(sp++) = ',';
3509                 int slen = strlen(slots[i]);
3510                 memcpy(sp, slots[i], slen);
3511                 sp += slen;
3512             }
3513             *(sp++) = '\0';
3514             node_configs[node_count - 1] = cfg;
3515         }
3516         zfree(slots);
3517     }
3518     if (node_count > 0) {
3519         if (node_count > 1) {
3520             qsort(node_configs, node_count, sizeof(char *),
3521                   clusterManagerSlotCompare);
3522         }
3523         signature = sdsempty();
3524         for (i = 0; i < node_count; i++) {
3525             if (i > 0) signature = sdscatprintf(signature, "%c", '|');
3526             signature = sdscatfmt(signature, "%s", node_configs[i]);
3527         }
3528     }
3529 cleanup:
3530     if (reply != NULL) freeReplyObject(reply);
3531     if (node_configs != NULL) {
3532         for (i = 0; i < node_count; i++) zfree(node_configs[i]);
3533         zfree(node_configs);
3534     }
3535     return signature;
3536 }
3537 
clusterManagerIsConfigConsistent(void)3538 static int clusterManagerIsConfigConsistent(void) {
3539     if (cluster_manager.nodes == NULL) return 0;
3540     int consistent = (listLength(cluster_manager.nodes) <= 1);
3541     // If the Cluster has only one node, it's always consistent
3542     if (consistent) return 1;
3543     sds first_cfg = NULL;
3544     listIter li;
3545     listNode *ln;
3546     listRewind(cluster_manager.nodes, &li);
3547     while ((ln = listNext(&li)) != NULL) {
3548         clusterManagerNode *node = ln->value;
3549         sds cfg = clusterManagerGetConfigSignature(node);
3550         if (cfg == NULL) {
3551             consistent = 0;
3552             break;
3553         }
3554         if (first_cfg == NULL) first_cfg = cfg;
3555         else {
3556             consistent = !sdscmp(first_cfg, cfg);
3557             sdsfree(cfg);
3558             if (!consistent) break;
3559         }
3560     }
3561     if (first_cfg != NULL) sdsfree(first_cfg);
3562     return consistent;
3563 }
3564 
3565 /* Add the error string to cluster_manager.errors and print it. */
clusterManagerOnError(sds err)3566 static void clusterManagerOnError(sds err) {
3567     if (cluster_manager.errors == NULL)
3568         cluster_manager.errors = listCreate();
3569     listAddNodeTail(cluster_manager.errors, err);
3570     clusterManagerLogErr("%s\n", (char *) err);
3571 }
3572 
3573 /* Check the slots coverage of the cluster. The 'all_slots' argument must be
3574  * and array of 16384 bytes. Every covered slot will be set to 1 in the
3575  * 'all_slots' array. The function returns the total number if covered slots.*/
clusterManagerGetCoveredSlots(char * all_slots)3576 static int clusterManagerGetCoveredSlots(char *all_slots) {
3577     if (cluster_manager.nodes == NULL) return 0;
3578     listIter li;
3579     listNode *ln;
3580     listRewind(cluster_manager.nodes, &li);
3581     int totslots = 0, i;
3582     while ((ln = listNext(&li)) != NULL) {
3583         clusterManagerNode *node = ln->value;
3584         for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {
3585             if (node->slots[i] && !all_slots[i]) {
3586                 all_slots[i] = 1;
3587                 totslots++;
3588             }
3589         }
3590     }
3591     return totslots;
3592 }
3593 
clusterManagerPrintSlotsList(list * slots)3594 static void clusterManagerPrintSlotsList(list *slots) {
3595     listIter li;
3596     listNode *ln;
3597     listRewind(slots, &li);
3598     sds first = NULL;
3599     while ((ln = listNext(&li)) != NULL) {
3600         sds slot = ln->value;
3601         if (!first) first = slot;
3602         else printf(", ");
3603         printf("%s", slot);
3604     }
3605     printf("\n");
3606 }
3607 
3608 /* Return the node, among 'nodes' with the greatest number of keys
3609  * in the specified slot. */
clusterManagerGetNodeWithMostKeysInSlot(list * nodes,int slot,char ** err)3610 static clusterManagerNode * clusterManagerGetNodeWithMostKeysInSlot(list *nodes,
3611                                                                     int slot,
3612                                                                     char **err)
3613 {
3614     clusterManagerNode *node = NULL;
3615     int numkeys = 0;
3616     listIter li;
3617     listNode *ln;
3618     listRewind(nodes, &li);
3619     if (err) *err = NULL;
3620     while ((ln = listNext(&li)) != NULL) {
3621         clusterManagerNode *n = ln->value;
3622         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)
3623             continue;
3624         redisReply *r =
3625             CLUSTER_MANAGER_COMMAND(n, "CLUSTER COUNTKEYSINSLOT %d", slot);
3626         int success = clusterManagerCheckRedisReply(n, r, err);
3627         if (success) {
3628             if (r->integer > numkeys || node == NULL) {
3629                 numkeys = r->integer;
3630                 node = n;
3631             }
3632         }
3633         if (r != NULL) freeReplyObject(r);
3634         /* If the reply contains errors */
3635         if (!success) {
3636             if (err != NULL && *err != NULL)
3637                 CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, err);
3638             node = NULL;
3639             break;
3640         }
3641     }
3642     return node;
3643 }
3644 
3645 /* This function returns the master that has the least number of replicas
3646  * in the cluster. If there are multiple masters with the same smaller
3647  * number of replicas, one at random is returned. */
3648 
clusterManagerNodeWithLeastReplicas()3649 static clusterManagerNode *clusterManagerNodeWithLeastReplicas() {
3650     clusterManagerNode *node = NULL;
3651     int lowest_count = 0;
3652     listIter li;
3653     listNode *ln;
3654     listRewind(cluster_manager.nodes, &li);
3655     while ((ln = listNext(&li)) != NULL) {
3656         clusterManagerNode *n = ln->value;
3657         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
3658         if (node == NULL || n->replicas_count < lowest_count) {
3659             node = n;
3660             lowest_count = n->replicas_count;
3661         }
3662     }
3663     return node;
3664 }
3665 
3666 /* This fucntion returns a random master node, return NULL if none */
3667 
clusterManagerNodeMasterRandom()3668 static clusterManagerNode *clusterManagerNodeMasterRandom() {
3669     int master_count = 0;
3670     int idx;
3671     listIter li;
3672     listNode *ln;
3673     listRewind(cluster_manager.nodes, &li);
3674     while ((ln = listNext(&li)) != NULL) {
3675         clusterManagerNode *n = ln->value;
3676         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
3677         master_count++;
3678     }
3679 
3680     srand(time(NULL));
3681     idx = rand() % master_count;
3682     listRewind(cluster_manager.nodes, &li);
3683     while ((ln = listNext(&li)) != NULL) {
3684         clusterManagerNode *n = ln->value;
3685         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
3686         if (!idx--) {
3687             return n;
3688         }
3689     }
3690     /* Can not be reached */
3691     return NULL;
3692 }
3693 
clusterManagerFixSlotsCoverage(char * all_slots)3694 static int clusterManagerFixSlotsCoverage(char *all_slots) {
3695     int i, fixed = 0;
3696     list *none = NULL, *single = NULL, *multi = NULL;
3697     clusterManagerLogInfo(">>> Fixing slots coverage...\n");
3698     printf("List of not covered slots: \n");
3699     int uncovered_count = 0;
3700     sds log = sdsempty();
3701     for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {
3702         int covered = all_slots[i];
3703         if (!covered) {
3704             sds key = sdsfromlonglong((long long) i);
3705             if (uncovered_count++ > 0) printf(",");
3706             printf("%s", (char *) key);
3707             list *slot_nodes = listCreate();
3708             sds slot_nodes_str = sdsempty();
3709             listIter li;
3710             listNode *ln;
3711             listRewind(cluster_manager.nodes, &li);
3712             while ((ln = listNext(&li)) != NULL) {
3713                 clusterManagerNode *n = ln->value;
3714                 if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)
3715                     continue;
3716                 redisReply *reply = CLUSTER_MANAGER_COMMAND(n,
3717                     "CLUSTER GETKEYSINSLOT %d %d", i, 1);
3718                 if (!clusterManagerCheckRedisReply(n, reply, NULL)) {
3719                     fixed = -1;
3720                     if (reply) freeReplyObject(reply);
3721                     goto cleanup;
3722                 }
3723                 assert(reply->type == REDIS_REPLY_ARRAY);
3724                 if (reply->elements > 0) {
3725                     listAddNodeTail(slot_nodes, n);
3726                     if (listLength(slot_nodes) > 1)
3727                         slot_nodes_str = sdscat(slot_nodes_str, ", ");
3728                     slot_nodes_str = sdscatfmt(slot_nodes_str,
3729                                                "%s:%u", n->ip, n->port);
3730                 }
3731                 freeReplyObject(reply);
3732             }
3733             log = sdscatfmt(log, "\nSlot %S has keys in %u nodes: %S",
3734                             key, listLength(slot_nodes), slot_nodes_str);
3735             sdsfree(slot_nodes_str);
3736             dictAdd(clusterManagerUncoveredSlots, key, slot_nodes);
3737         }
3738     }
3739     printf("\n%s\n", log);
3740     /* For every slot, take action depending on the actual condition:
3741      * 1) No node has keys for this slot.
3742      * 2) A single node has keys for this slot.
3743      * 3) Multiple nodes have keys for this slot. */
3744     none = listCreate();
3745     single = listCreate();
3746     multi = listCreate();
3747     dictIterator *iter = dictGetIterator(clusterManagerUncoveredSlots);
3748     dictEntry *entry;
3749     while ((entry = dictNext(iter)) != NULL) {
3750         sds slot = (sds) dictGetKey(entry);
3751         list *nodes = (list *) dictGetVal(entry);
3752         switch (listLength(nodes)){
3753         case 0: listAddNodeTail(none, slot); break;
3754         case 1: listAddNodeTail(single, slot); break;
3755         default: listAddNodeTail(multi, slot); break;
3756         }
3757     }
3758     dictReleaseIterator(iter);
3759 
3760     /*  Handle case "1": keys in no node. */
3761     if (listLength(none) > 0) {
3762         printf("The following uncovered slots have no keys "
3763                "across the cluster:\n");
3764         clusterManagerPrintSlotsList(none);
3765         if (confirmWithYes("Fix these slots by covering with a random node?")){
3766             listIter li;
3767             listNode *ln;
3768             listRewind(none, &li);
3769             while ((ln = listNext(&li)) != NULL) {
3770                 sds slot = ln->value;
3771                 int s = atoi(slot);
3772                 clusterManagerNode *n = clusterManagerNodeMasterRandom();
3773                 clusterManagerLogInfo(">>> Covering slot %s with %s:%d\n",
3774                                       slot, n->ip, n->port);
3775                 if (!clusterManagerSetSlotOwner(n, s, 0)) {
3776                     fixed = -1;
3777                     goto cleanup;
3778                 }
3779                 /* Since CLUSTER ADDSLOTS succeeded, we also update the slot
3780                  * info into the node struct, in order to keep it synced */
3781                 n->slots[s] = 1;
3782                 fixed++;
3783             }
3784         }
3785     }
3786 
3787     /*  Handle case "2": keys only in one node. */
3788     if (listLength(single) > 0) {
3789         printf("The following uncovered slots have keys in just one node:\n");
3790         clusterManagerPrintSlotsList(single);
3791         if (confirmWithYes("Fix these slots by covering with those nodes?")){
3792             listIter li;
3793             listNode *ln;
3794             listRewind(single, &li);
3795             while ((ln = listNext(&li)) != NULL) {
3796                 sds slot = ln->value;
3797                 int s = atoi(slot);
3798                 dictEntry *entry = dictFind(clusterManagerUncoveredSlots, slot);
3799                 assert(entry != NULL);
3800                 list *nodes = (list *) dictGetVal(entry);
3801                 listNode *fn = listFirst(nodes);
3802                 assert(fn != NULL);
3803                 clusterManagerNode *n = fn->value;
3804                 clusterManagerLogInfo(">>> Covering slot %s with %s:%d\n",
3805                                       slot, n->ip, n->port);
3806                 if (!clusterManagerSetSlotOwner(n, s, 0)) {
3807                     fixed = -1;
3808                     goto cleanup;
3809                 }
3810                 /* Since CLUSTER ADDSLOTS succeeded, we also update the slot
3811                  * info into the node struct, in order to keep it synced */
3812                 n->slots[atoi(slot)] = 1;
3813                 fixed++;
3814             }
3815         }
3816     }
3817 
3818     /* Handle case "3": keys in multiple nodes. */
3819     if (listLength(multi) > 0) {
3820         printf("The following uncovered slots have keys in multiple nodes:\n");
3821         clusterManagerPrintSlotsList(multi);
3822         if (confirmWithYes("Fix these slots by moving keys "
3823                            "into a single node?")) {
3824             listIter li;
3825             listNode *ln;
3826             listRewind(multi, &li);
3827             while ((ln = listNext(&li)) != NULL) {
3828                 sds slot = ln->value;
3829                 dictEntry *entry = dictFind(clusterManagerUncoveredSlots, slot);
3830                 assert(entry != NULL);
3831                 list *nodes = (list *) dictGetVal(entry);
3832                 int s = atoi(slot);
3833                 clusterManagerNode *target =
3834                     clusterManagerGetNodeWithMostKeysInSlot(nodes, s, NULL);
3835                 if (target == NULL) {
3836                     fixed = -1;
3837                     goto cleanup;
3838                 }
3839                 clusterManagerLogInfo(">>> Covering slot %s moving keys "
3840                                       "to %s:%d\n", slot,
3841                                       target->ip, target->port);
3842                 if (!clusterManagerSetSlotOwner(target, s, 1)) {
3843                     fixed = -1;
3844                     goto cleanup;
3845                 }
3846                 /* Since CLUSTER ADDSLOTS succeeded, we also update the slot
3847                  * info into the node struct, in order to keep it synced */
3848                 target->slots[atoi(slot)] = 1;
3849                 listIter nli;
3850                 listNode *nln;
3851                 listRewind(nodes, &nli);
3852                 while ((nln = listNext(&nli)) != NULL) {
3853                     clusterManagerNode *src = nln->value;
3854                     if (src == target) continue;
3855                     /* Assign the slot to target node in the source node. */
3856                     if (!clusterManagerSetSlot(src, target, s, "NODE", NULL))
3857                         fixed = -1;
3858                     if (fixed < 0) goto cleanup;
3859                     /* Set the source node in 'importing' state
3860                      * (even if we will actually migrate keys away)
3861                      * in order to avoid receiving redirections
3862                      * for MIGRATE. */
3863                     if (!clusterManagerSetSlot(src, target, s,
3864                                                "IMPORTING", NULL)) fixed = -1;
3865                     if (fixed < 0) goto cleanup;
3866                     int opts = CLUSTER_MANAGER_OPT_VERBOSE |
3867                                CLUSTER_MANAGER_OPT_COLD;
3868                     if (!clusterManagerMoveSlot(src, target, s, opts, NULL)) {
3869                         fixed = -1;
3870                         goto cleanup;
3871                     }
3872                     if (!clusterManagerClearSlotStatus(src, s))
3873                         fixed = -1;
3874                     if (fixed < 0) goto cleanup;
3875                 }
3876                 fixed++;
3877             }
3878         }
3879     }
3880 cleanup:
3881     sdsfree(log);
3882     if (none) listRelease(none);
3883     if (single) listRelease(single);
3884     if (multi) listRelease(multi);
3885     return fixed;
3886 }
3887 
3888 /* Slot 'slot' was found to be in importing or migrating state in one or
3889  * more nodes. This function fixes this condition by migrating keys where
3890  * it seems more sensible. */
clusterManagerFixOpenSlot(int slot)3891 static int clusterManagerFixOpenSlot(int slot) {
3892     clusterManagerLogInfo(">>> Fixing open slot %d\n", slot);
3893     /* Try to obtain the current slot owner, according to the current
3894      * nodes configuration. */
3895     int success = 1;
3896     list *owners = listCreate();
3897     list *migrating = listCreate();
3898     list *importing = listCreate();
3899     sds migrating_str = sdsempty();
3900     sds importing_str = sdsempty();
3901     clusterManagerNode *owner = NULL;
3902     listIter li;
3903     listNode *ln;
3904     listRewind(cluster_manager.nodes, &li);
3905     while ((ln = listNext(&li)) != NULL) {
3906         clusterManagerNode *n = ln->value;
3907         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
3908         if (n->slots[slot]) listAddNodeTail(owners, n);
3909         else {
3910             redisReply *r = CLUSTER_MANAGER_COMMAND(n,
3911                 "CLUSTER COUNTKEYSINSLOT %d", slot);
3912             success = clusterManagerCheckRedisReply(n, r, NULL);
3913             if (success && r->integer > 0) {
3914                 clusterManagerLogWarn("*** Found keys about slot %d "
3915                                       "in non-owner node %s:%d!\n", slot,
3916                                       n->ip, n->port);
3917                 listAddNodeTail(owners, n);
3918             }
3919             if (r) freeReplyObject(r);
3920             if (!success) goto cleanup;
3921         }
3922     }
3923     if (listLength(owners) == 1) owner = listFirst(owners)->value;
3924     listRewind(cluster_manager.nodes, &li);
3925     while ((ln = listNext(&li)) != NULL) {
3926         clusterManagerNode *n = ln->value;
3927         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
3928         int is_migrating = 0, is_importing = 0;
3929         if (n->migrating) {
3930             for (int i = 0; i < n->migrating_count; i += 2) {
3931                 sds migrating_slot = n->migrating[i];
3932                 if (atoi(migrating_slot) == slot) {
3933                     char *sep = (listLength(migrating) == 0 ? "" : ",");
3934                     migrating_str = sdscatfmt(migrating_str, "%s%s:%u",
3935                                               sep, n->ip, n->port);
3936                     listAddNodeTail(migrating, n);
3937                     is_migrating = 1;
3938                     break;
3939                 }
3940             }
3941         }
3942         if (!is_migrating && n->importing) {
3943             for (int i = 0; i < n->importing_count; i += 2) {
3944                 sds importing_slot = n->importing[i];
3945                 if (atoi(importing_slot) == slot) {
3946                     char *sep = (listLength(importing) == 0 ? "" : ",");
3947                     importing_str = sdscatfmt(importing_str, "%s%s:%u",
3948                                               sep, n->ip, n->port);
3949                     listAddNodeTail(importing, n);
3950                     is_importing = 1;
3951                     break;
3952                 }
3953             }
3954         }
3955         /* If the node is neither migrating nor importing and it's not
3956          * the owner, then is added to the importing list in case
3957          * it has keys in the slot. */
3958         if (!is_migrating && !is_importing && n != owner) {
3959             redisReply *r = CLUSTER_MANAGER_COMMAND(n,
3960                 "CLUSTER COUNTKEYSINSLOT %d", slot);
3961             success = clusterManagerCheckRedisReply(n, r, NULL);
3962             if (success && r->integer > 0) {
3963                 clusterManagerLogWarn("*** Found keys about slot %d "
3964                                       "in node %s:%d!\n", slot, n->ip,
3965                                       n->port);
3966                 char *sep = (listLength(importing) == 0 ? "" : ",");
3967                 importing_str = sdscatfmt(importing_str, "%s%S:%u",
3968                                           sep, n->ip, n->port);
3969                 listAddNodeTail(importing, n);
3970             }
3971             if (r) freeReplyObject(r);
3972             if (!success) goto cleanup;
3973         }
3974     }
3975     if (sdslen(migrating_str) > 0)
3976         printf("Set as migrating in: %s\n", migrating_str);
3977     if (sdslen(importing_str) > 0)
3978         printf("Set as importing in: %s\n", importing_str);
3979     /* If there is no slot owner, set as owner the node with the biggest
3980      * number of keys, among the set of migrating / importing nodes. */
3981     if (owner == NULL) {
3982         clusterManagerLogInfo(">>> Nobody claims ownership, "
3983                               "selecting an owner...\n");
3984         owner = clusterManagerGetNodeWithMostKeysInSlot(cluster_manager.nodes,
3985                                                         slot, NULL);
3986         // If we still don't have an owner, we can't fix it.
3987         if (owner == NULL) {
3988             clusterManagerLogErr("[ERR] Can't select a slot owner. "
3989                                  "Impossible to fix.\n");
3990             success = 0;
3991             goto cleanup;
3992         }
3993 
3994         // Use ADDSLOTS to assign the slot.
3995         clusterManagerLogWarn("*** Configuring %s:%d as the slot owner\n",
3996                               owner->ip, owner->port);
3997         success = clusterManagerClearSlotStatus(owner, slot);
3998         if (!success) goto cleanup;
3999         success = clusterManagerSetSlotOwner(owner, slot, 0);
4000         if (!success) goto cleanup;
4001         /* Since CLUSTER ADDSLOTS succeeded, we also update the slot
4002          * info into the node struct, in order to keep it synced */
4003         owner->slots[slot] = 1;
4004         /* Make sure this information will propagate. Not strictly needed
4005          * since there is no past owner, so all the other nodes will accept
4006          * whatever epoch this node will claim the slot with. */
4007         success = clusterManagerBumpEpoch(owner);
4008         if (!success) goto cleanup;
4009         /* Remove the owner from the list of migrating/importing
4010          * nodes. */
4011         clusterManagerRemoveNodeFromList(migrating, owner);
4012         clusterManagerRemoveNodeFromList(importing, owner);
4013     }
4014     /* If there are multiple owners of the slot, we need to fix it
4015      * so that a single node is the owner and all the other nodes
4016      * are in importing state. Later the fix can be handled by one
4017      * of the base cases above.
4018      *
4019      * Note that this case also covers multiple nodes having the slot
4020      * in migrating state, since migrating is a valid state only for
4021      * slot owners. */
4022     if (listLength(owners) > 1) {
4023         /* Owner cannot be NULL at this point, since if there are more owners,
4024          * the owner has been set in the previous condition (owner == NULL). */
4025         assert(owner != NULL);
4026         listRewind(owners, &li);
4027         while ((ln = listNext(&li)) != NULL) {
4028             clusterManagerNode *n = ln->value;
4029             if (n == owner) continue;
4030             success = clusterManagerDelSlot(n, slot, 1);
4031             if (!success) goto cleanup;
4032             n->slots[slot] = 0;
4033             /* Assign the slot to the owner in the node 'n' configuration.' */
4034             success = clusterManagerSetSlot(n, owner, slot, "node", NULL);
4035             if (!success) goto cleanup;
4036             success = clusterManagerSetSlot(n, owner, slot, "importing", NULL);
4037             if (!success) goto cleanup;
4038             /* Avoid duplicates. */
4039             clusterManagerRemoveNodeFromList(importing, n);
4040             listAddNodeTail(importing, n);
4041             /* Ensure that the node is not in the migrating list. */
4042             clusterManagerRemoveNodeFromList(migrating, n);
4043         }
4044     }
4045     int move_opts = CLUSTER_MANAGER_OPT_VERBOSE;
4046     /* Case 1: The slot is in migrating state in one node, and in
4047      *         importing state in 1 node. That's trivial to address. */
4048     if (listLength(migrating) == 1 && listLength(importing) == 1) {
4049         clusterManagerNode *src = listFirst(migrating)->value;
4050         clusterManagerNode *dst = listFirst(importing)->value;
4051         clusterManagerLogInfo(">>> Case 1: Moving slot %d from "
4052                               "%s:%d to %s:%d\n", slot,
4053                               src->ip, src->port, dst->ip, dst->port);
4054         move_opts |= CLUSTER_MANAGER_OPT_UPDATE;
4055         success = clusterManagerMoveSlot(src, dst, slot, move_opts, NULL);
4056     }
4057     /* Case 2: There are multiple nodes that claim the slot as importing,
4058      * they probably got keys about the slot after a restart so opened
4059      * the slot. In this case we just move all the keys to the owner
4060      * according to the configuration. */
4061     else if (listLength(migrating) == 0 && listLength(importing) > 0) {
4062         clusterManagerLogInfo(">>> Case 2: Moving all the %d slot keys to its "
4063                               "owner %s:%d\n", slot, owner->ip, owner->port);
4064         move_opts |= CLUSTER_MANAGER_OPT_COLD;
4065         listRewind(importing, &li);
4066         while ((ln = listNext(&li)) != NULL) {
4067             clusterManagerNode *n = ln->value;
4068             if (n == owner) continue;
4069             success = clusterManagerMoveSlot(n, owner, slot, move_opts, NULL);
4070             if (!success) goto cleanup;
4071             clusterManagerLogInfo(">>> Setting %d as STABLE in "
4072                                   "%s:%d\n", slot, n->ip, n->port);
4073             success = clusterManagerClearSlotStatus(n, slot);
4074             if (!success) goto cleanup;
4075         }
4076         /* Since the slot has been moved in "cold" mode, ensure that all the
4077          * other nodes update their own configuration about the slot itself. */
4078         listRewind(cluster_manager.nodes, &li);
4079         while ((ln = listNext(&li)) != NULL) {
4080             clusterManagerNode *n = ln->value;
4081             if (n == owner) continue;
4082             if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
4083             success = clusterManagerSetSlot(n, owner, slot, "NODE", NULL);
4084             if (!success) goto cleanup;
4085         }
4086     }
4087     /* Case 3: The slot is in migrating state in one node but multiple
4088      * other nodes claim to be in importing state and don't have any key in
4089      * the slot. We search for the importing node having the same ID as
4090      * the destination node of the migrating node.
4091      * In that case we move the slot from the migrating node to this node and
4092      * we close the importing states on all the other importing nodes.
4093      * If no importing node has the same ID as the destination node of the
4094      * migrating node, the slot's state is closed on both the migrating node
4095      * and the importing nodes. */
4096     else if (listLength(migrating) == 1 && listLength(importing) > 1) {
4097         int try_to_fix = 1;
4098         clusterManagerNode *src = listFirst(migrating)->value;
4099         clusterManagerNode *dst = NULL;
4100         sds target_id = NULL;
4101         for (int i = 0; i < src->migrating_count; i += 2) {
4102             sds migrating_slot = src->migrating[i];
4103             if (atoi(migrating_slot) == slot) {
4104                 target_id = src->migrating[i + 1];
4105                 break;
4106             }
4107         }
4108         assert(target_id != NULL);
4109         listIter li;
4110         listNode *ln;
4111         listRewind(importing, &li);
4112         while ((ln = listNext(&li)) != NULL) {
4113             clusterManagerNode *n = ln->value;
4114             int count = clusterManagerCountKeysInSlot(n, slot);
4115             if (count > 0) {
4116                 try_to_fix = 0;
4117                 break;
4118             }
4119             if (strcmp(n->name, target_id) == 0) dst = n;
4120         }
4121         if (!try_to_fix) goto unhandled_case;
4122         if (dst != NULL) {
4123             clusterManagerLogInfo(">>> Case 3: Moving slot %d from %s:%d to "
4124                                   "%s:%d and closing it on all the other "
4125                                   "importing nodes.\n",
4126                                   slot, src->ip, src->port,
4127                                   dst->ip, dst->port);
4128             /* Move the slot to the destination node. */
4129             success = clusterManagerMoveSlot(src, dst, slot, move_opts, NULL);
4130             if (!success) goto cleanup;
4131             /* Close slot on all the other importing nodes. */
4132             listRewind(importing, &li);
4133             while ((ln = listNext(&li)) != NULL) {
4134                 clusterManagerNode *n = ln->value;
4135                 if (dst == n) continue;
4136                 success = clusterManagerClearSlotStatus(n, slot);
4137                 if (!success) goto cleanup;
4138             }
4139         } else {
4140             clusterManagerLogInfo(">>> Case 3: Closing slot %d on both "
4141                                   "migrating and importing nodes.\n", slot);
4142             /* Close the slot on both the migrating node and the importing
4143              * nodes. */
4144             success = clusterManagerClearSlotStatus(src, slot);
4145             if (!success) goto cleanup;
4146             listRewind(importing, &li);
4147             while ((ln = listNext(&li)) != NULL) {
4148                 clusterManagerNode *n = ln->value;
4149                 success = clusterManagerClearSlotStatus(n, slot);
4150                 if (!success) goto cleanup;
4151             }
4152         }
4153     } else {
4154         int try_to_close_slot = (listLength(importing) == 0 &&
4155                                  listLength(migrating) == 1);
4156         if (try_to_close_slot) {
4157             clusterManagerNode *n = listFirst(migrating)->value;
4158             if (!owner || owner != n) {
4159                 redisReply *r = CLUSTER_MANAGER_COMMAND(n,
4160                     "CLUSTER GETKEYSINSLOT %d %d", slot, 10);
4161                 success = clusterManagerCheckRedisReply(n, r, NULL);
4162                 if (r) {
4163                     if (success) try_to_close_slot = (r->elements == 0);
4164                     freeReplyObject(r);
4165                 }
4166                 if (!success) goto cleanup;
4167             }
4168         }
4169         /* Case 4: There are no slots claiming to be in importing state, but
4170          * there is a migrating node that actually don't have any key or is the
4171          * slot owner. We can just close the slot, probably a reshard
4172          * interrupted in the middle. */
4173         if (try_to_close_slot) {
4174             clusterManagerNode *n = listFirst(migrating)->value;
4175             clusterManagerLogInfo(">>> Case 4: Closing slot %d on %s:%d\n",
4176                                   slot, n->ip, n->port);
4177             redisReply *r = CLUSTER_MANAGER_COMMAND(n, "CLUSTER SETSLOT %d %s",
4178                                                     slot, "STABLE");
4179             success = clusterManagerCheckRedisReply(n, r, NULL);
4180             if (r) freeReplyObject(r);
4181             if (!success) goto cleanup;
4182         } else {
4183 unhandled_case:
4184             success = 0;
4185             clusterManagerLogErr("[ERR] Sorry, redis-cli can't fix this slot "
4186                                  "yet (work in progress). Slot is set as "
4187                                  "migrating in %s, as importing in %s, "
4188                                  "owner is %s:%d\n", migrating_str,
4189                                  importing_str, owner->ip, owner->port);
4190         }
4191     }
4192 cleanup:
4193     listRelease(owners);
4194     listRelease(migrating);
4195     listRelease(importing);
4196     sdsfree(migrating_str);
4197     sdsfree(importing_str);
4198     return success;
4199 }
4200 
clusterManagerFixMultipleSlotOwners(int slot,list * owners)4201 static int clusterManagerFixMultipleSlotOwners(int slot, list *owners) {
4202     clusterManagerLogInfo(">>> Fixing multiple owners for slot %d...\n", slot);
4203     int success = 0;
4204     assert(listLength(owners) > 1);
4205     clusterManagerNode *owner = clusterManagerGetNodeWithMostKeysInSlot(owners,
4206                                                                         slot,
4207                                                                         NULL);
4208     if (!owner) owner = listFirst(owners)->value;
4209     clusterManagerLogInfo(">>> Setting slot %d owner: %s:%d\n",
4210                           slot, owner->ip, owner->port);
4211     /* Set the slot owner. */
4212     if (!clusterManagerSetSlotOwner(owner, slot, 0)) return 0;
4213     listIter li;
4214     listNode *ln;
4215     listRewind(cluster_manager.nodes, &li);
4216     /* Update configuration in all the other master nodes by assigning the slot
4217      * itself to the new owner, and by eventually migrating keys if the node
4218      * has keys for the slot. */
4219     while ((ln = listNext(&li)) != NULL) {
4220         clusterManagerNode *n = ln->value;
4221         if (n == owner) continue;
4222         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
4223         int count = clusterManagerCountKeysInSlot(n, slot);
4224         success = (count >= 0);
4225         if (!success) break;
4226         clusterManagerDelSlot(n, slot, 1);
4227         if (!clusterManagerSetSlot(n, owner, slot, "node", NULL)) return 0;
4228         if (count > 0) {
4229             int opts = CLUSTER_MANAGER_OPT_VERBOSE |
4230                        CLUSTER_MANAGER_OPT_COLD;
4231             success = clusterManagerMoveSlot(n, owner, slot, opts, NULL);
4232             if (!success) break;
4233         }
4234     }
4235     return success;
4236 }
4237 
clusterManagerCheckCluster(int quiet)4238 static int clusterManagerCheckCluster(int quiet) {
4239     listNode *ln = listFirst(cluster_manager.nodes);
4240     if (!ln) return 0;
4241     clusterManagerNode *node = ln->value;
4242     clusterManagerLogInfo(">>> Performing Cluster Check (using node %s:%d)\n",
4243                           node->ip, node->port);
4244     int result = 1, consistent = 0;
4245     int do_fix = config.cluster_manager_command.flags &
4246                  CLUSTER_MANAGER_CMD_FLAG_FIX;
4247     if (!quiet) clusterManagerShowNodes();
4248     consistent = clusterManagerIsConfigConsistent();
4249     if (!consistent) {
4250         sds err = sdsnew("[ERR] Nodes don't agree about configuration!");
4251         clusterManagerOnError(err);
4252         result = 0;
4253     } else {
4254         clusterManagerLogOk("[OK] All nodes agree about slots "
4255                             "configuration.\n");
4256     }
4257     /* Check open slots */
4258     clusterManagerLogInfo(">>> Check for open slots...\n");
4259     listIter li;
4260     listRewind(cluster_manager.nodes, &li);
4261     int i;
4262     dict *open_slots = NULL;
4263     while ((ln = listNext(&li)) != NULL) {
4264         clusterManagerNode *n = ln->value;
4265         if (n->migrating != NULL) {
4266             if (open_slots == NULL)
4267                 open_slots = dictCreate(&clusterManagerDictType, NULL);
4268             sds errstr = sdsempty();
4269             errstr = sdscatprintf(errstr,
4270                                 "[WARNING] Node %s:%d has slots in "
4271                                 "migrating state ",
4272                                 n->ip,
4273                                 n->port);
4274             for (i = 0; i < n->migrating_count; i += 2) {
4275                 sds slot = n->migrating[i];
4276                 dictAdd(open_slots, slot, sdsdup(n->migrating[i + 1]));
4277                 char *fmt = (i > 0 ? ",%S" : "%S");
4278                 errstr = sdscatfmt(errstr, fmt, slot);
4279             }
4280             errstr = sdscat(errstr, ".");
4281             clusterManagerOnError(errstr);
4282         }
4283         if (n->importing != NULL) {
4284             if (open_slots == NULL)
4285                 open_slots = dictCreate(&clusterManagerDictType, NULL);
4286             sds errstr = sdsempty();
4287             errstr = sdscatprintf(errstr,
4288                                 "[WARNING] Node %s:%d has slots in "
4289                                 "importing state ",
4290                                 n->ip,
4291                                 n->port);
4292             for (i = 0; i < n->importing_count; i += 2) {
4293                 sds slot = n->importing[i];
4294                 dictAdd(open_slots, slot, sdsdup(n->importing[i + 1]));
4295                 char *fmt = (i > 0 ? ",%S" : "%S");
4296                 errstr = sdscatfmt(errstr, fmt, slot);
4297             }
4298             errstr = sdscat(errstr, ".");
4299             clusterManagerOnError(errstr);
4300         }
4301     }
4302     if (open_slots != NULL) {
4303         result = 0;
4304         dictIterator *iter = dictGetIterator(open_slots);
4305         dictEntry *entry;
4306         sds errstr = sdsnew("[WARNING] The following slots are open: ");
4307         i = 0;
4308         while ((entry = dictNext(iter)) != NULL) {
4309             sds slot = (sds) dictGetKey(entry);
4310             char *fmt = (i++ > 0 ? ",%S" : "%S");
4311             errstr = sdscatfmt(errstr, fmt, slot);
4312         }
4313         clusterManagerLogErr("%s.\n", (char *) errstr);
4314         sdsfree(errstr);
4315         if (do_fix) {
4316             /* Fix open slots. */
4317             dictReleaseIterator(iter);
4318             iter = dictGetIterator(open_slots);
4319             while ((entry = dictNext(iter)) != NULL) {
4320                 sds slot = (sds) dictGetKey(entry);
4321                 result = clusterManagerFixOpenSlot(atoi(slot));
4322                 if (!result) break;
4323             }
4324         }
4325         dictReleaseIterator(iter);
4326         dictRelease(open_slots);
4327     }
4328     clusterManagerLogInfo(">>> Check slots coverage...\n");
4329     char slots[CLUSTER_MANAGER_SLOTS];
4330     memset(slots, 0, CLUSTER_MANAGER_SLOTS);
4331     int coverage = clusterManagerGetCoveredSlots(slots);
4332     if (coverage == CLUSTER_MANAGER_SLOTS) {
4333         clusterManagerLogOk("[OK] All %d slots covered.\n",
4334                             CLUSTER_MANAGER_SLOTS);
4335     } else {
4336         sds err = sdsempty();
4337         err = sdscatprintf(err, "[ERR] Not all %d slots are "
4338                                 "covered by nodes.\n",
4339                                 CLUSTER_MANAGER_SLOTS);
4340         clusterManagerOnError(err);
4341         result = 0;
4342         if (do_fix/* && result*/) {
4343             dictType dtype = clusterManagerDictType;
4344             dtype.keyDestructor = dictSdsDestructor;
4345             dtype.valDestructor = dictListDestructor;
4346             clusterManagerUncoveredSlots = dictCreate(&dtype, NULL);
4347             int fixed = clusterManagerFixSlotsCoverage(slots);
4348             if (fixed > 0) result = 1;
4349         }
4350     }
4351     int search_multiple_owners = config.cluster_manager_command.flags &
4352                                  CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS;
4353     if (search_multiple_owners) {
4354         /* Check whether there are multiple owners, even when slots are
4355          * fully covered and there are no open slots. */
4356         clusterManagerLogInfo(">>> Check for multiple slot owners...\n");
4357         int slot = 0;
4358         for (; slot < CLUSTER_MANAGER_SLOTS; slot++) {
4359             listIter li;
4360             listNode *ln;
4361             listRewind(cluster_manager.nodes, &li);
4362             list *owners = listCreate();
4363             while ((ln = listNext(&li)) != NULL) {
4364                 clusterManagerNode *n = ln->value;
4365                 if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
4366                 if (n->slots[slot]) listAddNodeTail(owners, n);
4367                 else {
4368                     /* Nodes having keys for the slot will be considered
4369                      * owners too. */
4370                     int count = clusterManagerCountKeysInSlot(n, slot);
4371                     if (count > 0) listAddNodeTail(owners, n);
4372                 }
4373             }
4374             if (listLength(owners) > 1) {
4375                 result = 0;
4376                 clusterManagerLogErr("[WARNING] Slot %d has %d owners:\n",
4377                                      slot, listLength(owners));
4378                 listRewind(owners, &li);
4379                 while ((ln = listNext(&li)) != NULL) {
4380                     clusterManagerNode *n = ln->value;
4381                     clusterManagerLogErr("    %s:%d\n", n->ip, n->port);
4382                 }
4383                 if (do_fix) {
4384                     result = clusterManagerFixMultipleSlotOwners(slot, owners);
4385                     if (!result) {
4386                         clusterManagerLogErr("Failed to fix multiple owners "
4387                                              "for slot %d\n", slot);
4388                         listRelease(owners);
4389                         break;
4390                     }
4391                 }
4392             }
4393             listRelease(owners);
4394         }
4395     }
4396     return result;
4397 }
4398 
clusterNodeForResharding(char * id,clusterManagerNode * target,int * raise_err)4399 static clusterManagerNode *clusterNodeForResharding(char *id,
4400                                                     clusterManagerNode *target,
4401                                                     int *raise_err)
4402 {
4403     clusterManagerNode *node = NULL;
4404     const char *invalid_node_msg = "*** The specified node (%s) is not known "
4405                                    "or not a master, please retry.\n";
4406     node = clusterManagerNodeByName(id);
4407     *raise_err = 0;
4408     if (!node || node->flags & CLUSTER_MANAGER_FLAG_SLAVE) {
4409         clusterManagerLogErr(invalid_node_msg, id);
4410         *raise_err = 1;
4411         return NULL;
4412     } else if (node != NULL && target != NULL) {
4413         if (!strcmp(node->name, target->name)) {
4414             clusterManagerLogErr( "*** It is not possible to use "
4415                                   "the target node as "
4416                                   "source node.\n");
4417             return NULL;
4418         }
4419     }
4420     return node;
4421 }
4422 
clusterManagerComputeReshardTable(list * sources,int numslots)4423 static list *clusterManagerComputeReshardTable(list *sources, int numslots) {
4424     list *moved = listCreate();
4425     int src_count = listLength(sources), i = 0, tot_slots = 0, j;
4426     clusterManagerNode **sorted = zmalloc(src_count * sizeof(*sorted));
4427     listIter li;
4428     listNode *ln;
4429     listRewind(sources, &li);
4430     while ((ln = listNext(&li)) != NULL) {
4431         clusterManagerNode *node = ln->value;
4432         tot_slots += node->slots_count;
4433         sorted[i++] = node;
4434     }
4435     qsort(sorted, src_count, sizeof(clusterManagerNode *),
4436           clusterManagerSlotCountCompareDesc);
4437     for (i = 0; i < src_count; i++) {
4438         clusterManagerNode *node = sorted[i];
4439         float n = ((float) numslots / tot_slots * node->slots_count);
4440         if (i == 0) n = ceil(n);
4441         else n = floor(n);
4442         int max = (int) n, count = 0;
4443         for (j = 0; j < CLUSTER_MANAGER_SLOTS; j++) {
4444             int slot = node->slots[j];
4445             if (!slot) continue;
4446             if (count >= max || (int)listLength(moved) >= numslots) break;
4447             clusterManagerReshardTableItem *item = zmalloc(sizeof(*item));
4448             item->source = node;
4449             item->slot = j;
4450             listAddNodeTail(moved, item);
4451             count++;
4452         }
4453     }
4454     zfree(sorted);
4455     return moved;
4456 }
4457 
clusterManagerShowReshardTable(list * table)4458 static void clusterManagerShowReshardTable(list *table) {
4459     listIter li;
4460     listNode *ln;
4461     listRewind(table, &li);
4462     while ((ln = listNext(&li)) != NULL) {
4463         clusterManagerReshardTableItem *item = ln->value;
4464         clusterManagerNode *n = item->source;
4465         printf("    Moving slot %d from %s\n", item->slot, (char *) n->name);
4466     }
4467 }
4468 
clusterManagerReleaseReshardTable(list * table)4469 static void clusterManagerReleaseReshardTable(list *table) {
4470     if (table != NULL) {
4471         listIter li;
4472         listNode *ln;
4473         listRewind(table, &li);
4474         while ((ln = listNext(&li)) != NULL) {
4475             clusterManagerReshardTableItem *item = ln->value;
4476             zfree(item);
4477         }
4478         listRelease(table);
4479     }
4480 }
4481 
clusterManagerLog(int level,const char * fmt,...)4482 static void clusterManagerLog(int level, const char* fmt, ...) {
4483     int use_colors =
4484         (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_COLOR);
4485     if (use_colors) {
4486         printf("\033[");
4487         switch (level) {
4488         case CLUSTER_MANAGER_LOG_LVL_INFO: printf(LOG_COLOR_BOLD); break;
4489         case CLUSTER_MANAGER_LOG_LVL_WARN: printf(LOG_COLOR_YELLOW); break;
4490         case CLUSTER_MANAGER_LOG_LVL_ERR: printf(LOG_COLOR_RED); break;
4491         case CLUSTER_MANAGER_LOG_LVL_SUCCESS: printf(LOG_COLOR_GREEN); break;
4492         default: printf(LOG_COLOR_RESET); break;
4493         }
4494     }
4495     va_list ap;
4496     va_start(ap, fmt);
4497     vprintf(fmt, ap);
4498     va_end(ap);
4499     if (use_colors) printf("\033[" LOG_COLOR_RESET);
4500 }
4501 
clusterManagerNodeArrayInit(clusterManagerNodeArray * array,int alloc_len)4502 static void clusterManagerNodeArrayInit(clusterManagerNodeArray *array,
4503                                         int alloc_len)
4504 {
4505     array->nodes = zcalloc(alloc_len * sizeof(clusterManagerNode*));
4506     array->alloc = array->nodes;
4507     array->len = alloc_len;
4508     array->count = 0;
4509 }
4510 
4511 /* Reset array->nodes to the original array allocation and re-count non-NULL
4512  * nodes. */
clusterManagerNodeArrayReset(clusterManagerNodeArray * array)4513 static void clusterManagerNodeArrayReset(clusterManagerNodeArray *array) {
4514     if (array->nodes > array->alloc) {
4515         array->len = array->nodes - array->alloc;
4516         array->nodes = array->alloc;
4517         array->count = 0;
4518         int i = 0;
4519         for(; i < array->len; i++) {
4520             if (array->nodes[i] != NULL) array->count++;
4521         }
4522     }
4523 }
4524 
4525 /* Shift array->nodes and store the shifted node into 'nodeptr'. */
clusterManagerNodeArrayShift(clusterManagerNodeArray * array,clusterManagerNode ** nodeptr)4526 static void clusterManagerNodeArrayShift(clusterManagerNodeArray *array,
4527                                          clusterManagerNode **nodeptr)
4528 {
4529     assert(array->nodes < (array->nodes + array->len));
4530     /* If the first node to be shifted is not NULL, decrement count. */
4531     if (*array->nodes != NULL) array->count--;
4532     /* Store the first node to be shifted into 'nodeptr'. */
4533     *nodeptr = *array->nodes;
4534     /* Shift the nodes array and decrement length. */
4535     array->nodes++;
4536     array->len--;
4537 }
4538 
clusterManagerNodeArrayAdd(clusterManagerNodeArray * array,clusterManagerNode * node)4539 static void clusterManagerNodeArrayAdd(clusterManagerNodeArray *array,
4540                                        clusterManagerNode *node)
4541 {
4542     assert(array->nodes < (array->nodes + array->len));
4543     assert(node != NULL);
4544     assert(array->count < array->len);
4545     array->nodes[array->count++] = node;
4546 }
4547 
clusterManagerPrintNotEmptyNodeError(clusterManagerNode * node,char * err)4548 static void clusterManagerPrintNotEmptyNodeError(clusterManagerNode *node,
4549                                                  char *err)
4550 {
4551     char *msg;
4552     if (err) msg = err;
4553     else {
4554         msg = "is not empty. Either the node already knows other "
4555               "nodes (check with CLUSTER NODES) or contains some "
4556               "key in database 0.";
4557     }
4558     clusterManagerLogErr("[ERR] Node %s:%d %s\n", node->ip, node->port, msg);
4559 }
4560 
clusterManagerPrintNotClusterNodeError(clusterManagerNode * node,char * err)4561 static void clusterManagerPrintNotClusterNodeError(clusterManagerNode *node,
4562                                                    char *err)
4563 {
4564     char *msg = (err ? err : "is not configured as a cluster node.");
4565     clusterManagerLogErr("[ERR] Node %s:%d %s\n", node->ip, node->port, msg);
4566 }
4567 
4568 /* Execute redis-cli in Cluster Manager mode */
clusterManagerMode(clusterManagerCommandProc * proc)4569 static void clusterManagerMode(clusterManagerCommandProc *proc) {
4570     int argc = config.cluster_manager_command.argc;
4571     char **argv = config.cluster_manager_command.argv;
4572     cluster_manager.nodes = NULL;
4573     if (!proc(argc, argv)) goto cluster_manager_err;
4574     freeClusterManager();
4575     exit(0);
4576 cluster_manager_err:
4577     freeClusterManager();
4578     sdsfree(config.hostip);
4579     sdsfree(config.mb_delim);
4580     exit(1);
4581 }
4582 
4583 /* Cluster Manager Commands */
4584 
clusterManagerCommandCreate(int argc,char ** argv)4585 static int clusterManagerCommandCreate(int argc, char **argv) {
4586     int i, j, success = 1;
4587     cluster_manager.nodes = listCreate();
4588     for (i = 0; i < argc; i++) {
4589         char *addr = argv[i];
4590         char *c = strrchr(addr, '@');
4591         if (c != NULL) *c = '\0';
4592         c = strrchr(addr, ':');
4593         if (c == NULL) {
4594             fprintf(stderr, "Invalid address format: %s\n", addr);
4595             return 0;
4596         }
4597         *c = '\0';
4598         char *ip = addr;
4599         int port = atoi(++c);
4600         clusterManagerNode *node = clusterManagerNewNode(ip, port);
4601         if (!clusterManagerNodeConnect(node)) {
4602             freeClusterManagerNode(node);
4603             return 0;
4604         }
4605         char *err = NULL;
4606         if (!clusterManagerNodeIsCluster(node, &err)) {
4607             clusterManagerPrintNotClusterNodeError(node, err);
4608             if (err) zfree(err);
4609             freeClusterManagerNode(node);
4610             return 0;
4611         }
4612         err = NULL;
4613         if (!clusterManagerNodeLoadInfo(node, 0, &err)) {
4614             if (err) {
4615                 CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);
4616                 zfree(err);
4617             }
4618             freeClusterManagerNode(node);
4619             return 0;
4620         }
4621         err = NULL;
4622         if (!clusterManagerNodeIsEmpty(node, &err)) {
4623             clusterManagerPrintNotEmptyNodeError(node, err);
4624             if (err) zfree(err);
4625             freeClusterManagerNode(node);
4626             return 0;
4627         }
4628         listAddNodeTail(cluster_manager.nodes, node);
4629     }
4630     int node_len = cluster_manager.nodes->len;
4631     int replicas = config.cluster_manager_command.replicas;
4632     int masters_count = CLUSTER_MANAGER_MASTERS_COUNT(node_len, replicas);
4633     if (masters_count < 3) {
4634         clusterManagerLogErr(
4635             "*** ERROR: Invalid configuration for cluster creation.\n"
4636             "*** Redis Cluster requires at least 3 master nodes.\n"
4637             "*** This is not possible with %d nodes and %d replicas per node.",
4638             node_len, replicas);
4639         clusterManagerLogErr("\n*** At least %d nodes are required.\n",
4640                              3 * (replicas + 1));
4641         return 0;
4642     }
4643     clusterManagerLogInfo(">>> Performing hash slots allocation "
4644                           "on %d nodes...\n", node_len);
4645     int interleaved_len = 0, ip_count = 0;
4646     clusterManagerNode **interleaved = zcalloc(node_len*sizeof(**interleaved));
4647     char **ips = zcalloc(node_len * sizeof(char*));
4648     clusterManagerNodeArray *ip_nodes = zcalloc(node_len * sizeof(*ip_nodes));
4649     listIter li;
4650     listNode *ln;
4651     listRewind(cluster_manager.nodes, &li);
4652     while ((ln = listNext(&li)) != NULL) {
4653         clusterManagerNode *n = ln->value;
4654         int found = 0;
4655         for (i = 0; i < ip_count; i++) {
4656             char *ip = ips[i];
4657             if (!strcmp(ip, n->ip)) {
4658                 found = 1;
4659                 break;
4660             }
4661         }
4662         if (!found) {
4663             ips[ip_count++] = n->ip;
4664         }
4665         clusterManagerNodeArray *node_array = &(ip_nodes[i]);
4666         if (node_array->nodes == NULL)
4667             clusterManagerNodeArrayInit(node_array, node_len);
4668         clusterManagerNodeArrayAdd(node_array, n);
4669     }
4670     while (interleaved_len < node_len) {
4671         for (i = 0; i < ip_count; i++) {
4672             clusterManagerNodeArray *node_array = &(ip_nodes[i]);
4673             if (node_array->count > 0) {
4674                 clusterManagerNode *n = NULL;
4675                 clusterManagerNodeArrayShift(node_array, &n);
4676                 interleaved[interleaved_len++] = n;
4677             }
4678         }
4679     }
4680     clusterManagerNode **masters = interleaved;
4681     interleaved += masters_count;
4682     interleaved_len -= masters_count;
4683     float slots_per_node = CLUSTER_MANAGER_SLOTS / (float) masters_count;
4684     long first = 0;
4685     float cursor = 0.0f;
4686     for (i = 0; i < masters_count; i++) {
4687         clusterManagerNode *master = masters[i];
4688         long last = lround(cursor + slots_per_node - 1);
4689         if (last > CLUSTER_MANAGER_SLOTS || i == (masters_count - 1))
4690             last = CLUSTER_MANAGER_SLOTS - 1;
4691         if (last < first) last = first;
4692         printf("Master[%d] -> Slots %lu - %lu\n", i, first, last);
4693         master->slots_count = 0;
4694         for (j = first; j <= last; j++) {
4695             master->slots[j] = 1;
4696             master->slots_count++;
4697         }
4698         master->dirty = 1;
4699         first = last + 1;
4700         cursor += slots_per_node;
4701     }
4702 
4703     /* Rotating the list sometimes helps to get better initial
4704      * anti-affinity before the optimizer runs. */
4705     clusterManagerNode *first_node = interleaved[0];
4706     for (i = 0; i < (interleaved_len - 1); i++)
4707         interleaved[i] = interleaved[i + 1];
4708     interleaved[interleaved_len - 1] = first_node;
4709     int assign_unused = 0, available_count = interleaved_len;
4710 assign_replicas:
4711     for (i = 0; i < masters_count; i++) {
4712         clusterManagerNode *master = masters[i];
4713         int assigned_replicas = 0;
4714         while (assigned_replicas < replicas) {
4715             if (available_count == 0) break;
4716             clusterManagerNode *found = NULL, *slave = NULL;
4717             int firstNodeIdx = -1;
4718             for (j = 0; j < interleaved_len; j++) {
4719                 clusterManagerNode *n = interleaved[j];
4720                 if (n == NULL) continue;
4721                 if (strcmp(n->ip, master->ip)) {
4722                     found = n;
4723                     interleaved[j] = NULL;
4724                     break;
4725                 }
4726                 if (firstNodeIdx < 0) firstNodeIdx = j;
4727             }
4728             if (found) slave = found;
4729             else if (firstNodeIdx >= 0) {
4730                 slave = interleaved[firstNodeIdx];
4731                 interleaved_len -= (interleaved - (interleaved + firstNodeIdx));
4732                 interleaved += (firstNodeIdx + 1);
4733             }
4734             if (slave != NULL) {
4735                 assigned_replicas++;
4736                 available_count--;
4737                 if (slave->replicate) sdsfree(slave->replicate);
4738                 slave->replicate = sdsnew(master->name);
4739                 slave->dirty = 1;
4740             } else break;
4741             printf("Adding replica %s:%d to %s:%d\n", slave->ip, slave->port,
4742                    master->ip, master->port);
4743             if (assign_unused) break;
4744         }
4745     }
4746     if (!assign_unused && available_count > 0) {
4747         assign_unused = 1;
4748         printf("Adding extra replicas...\n");
4749         goto assign_replicas;
4750     }
4751     for (i = 0; i < ip_count; i++) {
4752         clusterManagerNodeArray *node_array = ip_nodes + i;
4753         clusterManagerNodeArrayReset(node_array);
4754     }
4755     clusterManagerOptimizeAntiAffinity(ip_nodes, ip_count);
4756     clusterManagerShowNodes();
4757     if (confirmWithYes("Can I set the above configuration?")) {
4758         listRewind(cluster_manager.nodes, &li);
4759         while ((ln = listNext(&li)) != NULL) {
4760             clusterManagerNode *node = ln->value;
4761             char *err = NULL;
4762             int flushed = clusterManagerFlushNodeConfig(node, &err);
4763             if (!flushed && node->dirty && !node->replicate) {
4764                 if (err != NULL) {
4765                     CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);
4766                     zfree(err);
4767                 }
4768                 success = 0;
4769                 goto cleanup;
4770             } else if (err != NULL) zfree(err);
4771         }
4772         clusterManagerLogInfo(">>> Nodes configuration updated\n");
4773         clusterManagerLogInfo(">>> Assign a different config epoch to "
4774                               "each node\n");
4775         int config_epoch = 1;
4776         listRewind(cluster_manager.nodes, &li);
4777         while ((ln = listNext(&li)) != NULL) {
4778             clusterManagerNode *node = ln->value;
4779             redisReply *reply = NULL;
4780             reply = CLUSTER_MANAGER_COMMAND(node,
4781                                             "cluster set-config-epoch %d",
4782                                             config_epoch++);
4783             if (reply != NULL) freeReplyObject(reply);
4784         }
4785         clusterManagerLogInfo(">>> Sending CLUSTER MEET messages to join "
4786                               "the cluster\n");
4787         clusterManagerNode *first = NULL;
4788         listRewind(cluster_manager.nodes, &li);
4789         while ((ln = listNext(&li)) != NULL) {
4790             clusterManagerNode *node = ln->value;
4791             if (first == NULL) {
4792                 first = node;
4793                 continue;
4794             }
4795             redisReply *reply = NULL;
4796             reply = CLUSTER_MANAGER_COMMAND(node, "cluster meet %s %d",
4797                                             first->ip, first->port);
4798             int is_err = 0;
4799             if (reply != NULL) {
4800                 if ((is_err = reply->type == REDIS_REPLY_ERROR))
4801                     CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, reply->str);
4802                 freeReplyObject(reply);
4803             } else {
4804                 is_err = 1;
4805                 fprintf(stderr, "Failed to send CLUSTER MEET command.\n");
4806             }
4807             if (is_err) {
4808                 success = 0;
4809                 goto cleanup;
4810             }
4811         }
4812         /* Give one second for the join to start, in order to avoid that
4813          * waiting for cluster join will find all the nodes agree about
4814          * the config as they are still empty with unassigned slots. */
4815         sleep(1);
4816         clusterManagerWaitForClusterJoin();
4817         /* Useful for the replicas */
4818         listRewind(cluster_manager.nodes, &li);
4819         while ((ln = listNext(&li)) != NULL) {
4820             clusterManagerNode *node = ln->value;
4821             if (!node->dirty) continue;
4822             char *err = NULL;
4823             int flushed = clusterManagerFlushNodeConfig(node, &err);
4824             if (!flushed && !node->replicate) {
4825                 if (err != NULL) {
4826                     CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);
4827                     zfree(err);
4828                 }
4829                 success = 0;
4830                 goto cleanup;
4831             }
4832         }
4833         // Reset Nodes
4834         listRewind(cluster_manager.nodes, &li);
4835         clusterManagerNode *first_node = NULL;
4836         while ((ln = listNext(&li)) != NULL) {
4837             clusterManagerNode *node = ln->value;
4838             if (!first_node) first_node = node;
4839             else freeClusterManagerNode(node);
4840         }
4841         listEmpty(cluster_manager.nodes);
4842         if (!clusterManagerLoadInfoFromNode(first_node, 0)) {
4843             success = 0;
4844             goto cleanup;
4845         }
4846         clusterManagerCheckCluster(0);
4847     }
4848 cleanup:
4849     /* Free everything */
4850     zfree(masters);
4851     zfree(ips);
4852     for (i = 0; i < node_len; i++) {
4853         clusterManagerNodeArray *node_array = ip_nodes + i;
4854         CLUSTER_MANAGER_NODE_ARRAY_FREE(node_array);
4855     }
4856     zfree(ip_nodes);
4857     return success;
4858 }
4859 
clusterManagerCommandAddNode(int argc,char ** argv)4860 static int clusterManagerCommandAddNode(int argc, char **argv) {
4861     int success = 1;
4862     redisReply *reply = NULL;
4863     char *ref_ip = NULL, *ip = NULL;
4864     int ref_port = 0, port = 0;
4865     if (!getClusterHostFromCmdArgs(argc - 1, argv + 1, &ref_ip, &ref_port))
4866         goto invalid_args;
4867     if (!getClusterHostFromCmdArgs(1, argv, &ip, &port))
4868         goto invalid_args;
4869     clusterManagerLogInfo(">>> Adding node %s:%d to cluster %s:%d\n", ip, port,
4870                           ref_ip, ref_port);
4871     // Check the existing cluster
4872     clusterManagerNode *refnode = clusterManagerNewNode(ref_ip, ref_port);
4873     if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;
4874     if (!clusterManagerCheckCluster(0)) return 0;
4875 
4876     /* If --cluster-master-id was specified, try to resolve it now so that we
4877      * abort before starting with the node configuration. */
4878     clusterManagerNode *master_node = NULL;
4879     if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_SLAVE) {
4880         char *master_id = config.cluster_manager_command.master_id;
4881         if (master_id != NULL) {
4882             master_node = clusterManagerNodeByName(master_id);
4883             if (master_node == NULL) {
4884                 clusterManagerLogErr("[ERR] No such master ID %s\n", master_id);
4885                 return 0;
4886             }
4887         } else {
4888             master_node = clusterManagerNodeWithLeastReplicas();
4889             assert(master_node != NULL);
4890             printf("Automatically selected master %s:%d\n", master_node->ip,
4891                    master_node->port);
4892         }
4893     }
4894 
4895     // Add the new node
4896     clusterManagerNode *new_node = clusterManagerNewNode(ip, port);
4897     int added = 0;
4898     if (!clusterManagerNodeConnect(new_node)) {
4899         clusterManagerLogErr("[ERR] Sorry, can't connect to node %s:%d\n",
4900                              ip, port);
4901         success = 0;
4902         goto cleanup;
4903     }
4904     char *err = NULL;
4905     if (!(success = clusterManagerNodeIsCluster(new_node, &err))) {
4906         clusterManagerPrintNotClusterNodeError(new_node, err);
4907         if (err) zfree(err);
4908         goto cleanup;
4909     }
4910     if (!clusterManagerNodeLoadInfo(new_node, 0, &err)) {
4911         if (err) {
4912             CLUSTER_MANAGER_PRINT_REPLY_ERROR(new_node, err);
4913             zfree(err);
4914         }
4915         success = 0;
4916         goto cleanup;
4917     }
4918     if (!(success = clusterManagerNodeIsEmpty(new_node, &err))) {
4919         clusterManagerPrintNotEmptyNodeError(new_node, err);
4920         if (err) zfree(err);
4921         goto cleanup;
4922     }
4923     clusterManagerNode *first = listFirst(cluster_manager.nodes)->value;
4924     listAddNodeTail(cluster_manager.nodes, new_node);
4925     added = 1;
4926 
4927     // Send CLUSTER MEET command to the new node
4928     clusterManagerLogInfo(">>> Send CLUSTER MEET to node %s:%d to make it "
4929                           "join the cluster.\n", ip, port);
4930     reply = CLUSTER_MANAGER_COMMAND(new_node, "CLUSTER MEET %s %d",
4931                                     first->ip, first->port);
4932     if (!(success = clusterManagerCheckRedisReply(new_node, reply, NULL)))
4933         goto cleanup;
4934 
4935     /* Additional configuration is needed if the node is added as a slave. */
4936     if (master_node) {
4937         sleep(1);
4938         clusterManagerWaitForClusterJoin();
4939         clusterManagerLogInfo(">>> Configure node as replica of %s:%d.\n",
4940                               master_node->ip, master_node->port);
4941         freeReplyObject(reply);
4942         reply = CLUSTER_MANAGER_COMMAND(new_node, "CLUSTER REPLICATE %s",
4943                                         master_node->name);
4944         if (!(success = clusterManagerCheckRedisReply(new_node, reply, NULL)))
4945             goto cleanup;
4946     }
4947     clusterManagerLogOk("[OK] New node added correctly.\n");
4948 cleanup:
4949     if (!added && new_node) freeClusterManagerNode(new_node);
4950     if (reply) freeReplyObject(reply);
4951     return success;
4952 invalid_args:
4953     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
4954     return 0;
4955 }
4956 
clusterManagerCommandDeleteNode(int argc,char ** argv)4957 static int clusterManagerCommandDeleteNode(int argc, char **argv) {
4958     UNUSED(argc);
4959     int success = 1;
4960     int port = 0;
4961     char *ip = NULL;
4962     if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;
4963     char *node_id = argv[1];
4964     clusterManagerLogInfo(">>> Removing node %s from cluster %s:%d\n",
4965                           node_id, ip, port);
4966     clusterManagerNode *ref_node = clusterManagerNewNode(ip, port);
4967     clusterManagerNode *node = NULL;
4968 
4969     // Load cluster information
4970     if (!clusterManagerLoadInfoFromNode(ref_node, 0)) return 0;
4971 
4972     // Check if the node exists and is not empty
4973     node = clusterManagerNodeByName(node_id);
4974     if (node == NULL) {
4975         clusterManagerLogErr("[ERR] No such node ID %s\n", node_id);
4976         return 0;
4977     }
4978     if (node->slots_count != 0) {
4979         clusterManagerLogErr("[ERR] Node %s:%d is not empty! Reshard data "
4980                              "away and try again.\n", node->ip, node->port);
4981         return 0;
4982     }
4983 
4984     // Send CLUSTER FORGET to all the nodes but the node to remove
4985     clusterManagerLogInfo(">>> Sending CLUSTER FORGET messages to the "
4986                           "cluster...\n");
4987     listIter li;
4988     listNode *ln;
4989     listRewind(cluster_manager.nodes, &li);
4990     while ((ln = listNext(&li)) != NULL) {
4991         clusterManagerNode *n = ln->value;
4992         if (n == node) continue;
4993         if (n->replicate && !strcasecmp(n->replicate, node_id)) {
4994             // Reconfigure the slave to replicate with some other node
4995             clusterManagerNode *master = clusterManagerNodeWithLeastReplicas();
4996             assert(master != NULL);
4997             clusterManagerLogInfo(">>> %s:%d as replica of %s:%d\n",
4998                                   n->ip, n->port, master->ip, master->port);
4999             redisReply *r = CLUSTER_MANAGER_COMMAND(n, "CLUSTER REPLICATE %s",
5000                                                     master->name);
5001             success = clusterManagerCheckRedisReply(n, r, NULL);
5002             if (r) freeReplyObject(r);
5003             if (!success) return 0;
5004         }
5005         redisReply *r = CLUSTER_MANAGER_COMMAND(n, "CLUSTER FORGET %s",
5006                                                 node_id);
5007         success = clusterManagerCheckRedisReply(n, r, NULL);
5008         if (r) freeReplyObject(r);
5009         if (!success) return 0;
5010     }
5011 
5012     // Finally shutdown the node
5013     clusterManagerLogInfo(">>> SHUTDOWN the node.\n");
5014     redisReply *r = redisCommand(node->context, "SHUTDOWN");
5015     success = clusterManagerCheckRedisReply(node, r, NULL);
5016     if (r) freeReplyObject(r);
5017     return success;
5018 invalid_args:
5019     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5020     return 0;
5021 }
5022 
clusterManagerCommandInfo(int argc,char ** argv)5023 static int clusterManagerCommandInfo(int argc, char **argv) {
5024     int port = 0;
5025     char *ip = NULL;
5026     if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;
5027     clusterManagerNode *node = clusterManagerNewNode(ip, port);
5028     if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;
5029     clusterManagerShowClusterInfo();
5030     return 1;
5031 invalid_args:
5032     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5033     return 0;
5034 }
5035 
clusterManagerCommandCheck(int argc,char ** argv)5036 static int clusterManagerCommandCheck(int argc, char **argv) {
5037     int port = 0;
5038     char *ip = NULL;
5039     if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;
5040     clusterManagerNode *node = clusterManagerNewNode(ip, port);
5041     if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;
5042     clusterManagerShowClusterInfo();
5043     return clusterManagerCheckCluster(0);
5044 invalid_args:
5045     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5046     return 0;
5047 }
5048 
clusterManagerCommandFix(int argc,char ** argv)5049 static int clusterManagerCommandFix(int argc, char **argv) {
5050     config.cluster_manager_command.flags |= CLUSTER_MANAGER_CMD_FLAG_FIX;
5051     return clusterManagerCommandCheck(argc, argv);
5052 }
5053 
clusterManagerCommandReshard(int argc,char ** argv)5054 static int clusterManagerCommandReshard(int argc, char **argv) {
5055     int port = 0;
5056     char *ip = NULL;
5057     if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;
5058     clusterManagerNode *node = clusterManagerNewNode(ip, port);
5059     if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;
5060     clusterManagerCheckCluster(0);
5061     if (cluster_manager.errors && listLength(cluster_manager.errors) > 0) {
5062         fflush(stdout);
5063         fprintf(stderr,
5064                 "*** Please fix your cluster problems before resharding\n");
5065         return 0;
5066     }
5067     int slots = config.cluster_manager_command.slots;
5068     if (!slots) {
5069         while (slots <= 0 || slots > CLUSTER_MANAGER_SLOTS) {
5070             printf("How many slots do you want to move (from 1 to %d)? ",
5071                    CLUSTER_MANAGER_SLOTS);
5072             fflush(stdout);
5073             char buf[6];
5074             int nread = read(fileno(stdin),buf,6);
5075             if (nread <= 0) continue;
5076             int last_idx = nread - 1;
5077             if (buf[last_idx] != '\n') {
5078                 int ch;
5079                 while ((ch = getchar()) != '\n' && ch != EOF) {}
5080             }
5081             buf[last_idx] = '\0';
5082             slots = atoi(buf);
5083         }
5084     }
5085     char buf[255];
5086     char *to = config.cluster_manager_command.to,
5087          *from = config.cluster_manager_command.from;
5088     while (to == NULL) {
5089         printf("What is the receiving node ID? ");
5090         fflush(stdout);
5091         int nread = read(fileno(stdin),buf,255);
5092         if (nread <= 0) continue;
5093         int last_idx = nread - 1;
5094         if (buf[last_idx] != '\n') {
5095             int ch;
5096             while ((ch = getchar()) != '\n' && ch != EOF) {}
5097         }
5098         buf[last_idx] = '\0';
5099         if (strlen(buf) > 0) to = buf;
5100     }
5101     int raise_err = 0;
5102     clusterManagerNode *target = clusterNodeForResharding(to, NULL, &raise_err);
5103     if (target == NULL) return 0;
5104     list *sources = listCreate();
5105     list *table = NULL;
5106     int all = 0, result = 1;
5107     if (from == NULL) {
5108         printf("Please enter all the source node IDs.\n");
5109         printf("  Type 'all' to use all the nodes as source nodes for "
5110                "the hash slots.\n");
5111         printf("  Type 'done' once you entered all the source nodes IDs.\n");
5112         while (1) {
5113             printf("Source node #%lu: ", listLength(sources) + 1);
5114             fflush(stdout);
5115             int nread = read(fileno(stdin),buf,255);
5116             if (nread <= 0) continue;
5117             int last_idx = nread - 1;
5118             if (buf[last_idx] != '\n') {
5119                 int ch;
5120                 while ((ch = getchar()) != '\n' && ch != EOF) {}
5121             }
5122             buf[last_idx] = '\0';
5123             if (!strcmp(buf, "done")) break;
5124             else if (!strcmp(buf, "all")) {
5125                 all = 1;
5126                 break;
5127             } else {
5128                 clusterManagerNode *src =
5129                     clusterNodeForResharding(buf, target, &raise_err);
5130                 if (src != NULL) listAddNodeTail(sources, src);
5131                 else if (raise_err) {
5132                     result = 0;
5133                     goto cleanup;
5134                 }
5135             }
5136         }
5137     } else {
5138         char *p;
5139         while((p = strchr(from, ',')) != NULL) {
5140             *p = '\0';
5141             if (!strcmp(from, "all")) {
5142                 all = 1;
5143                 break;
5144             } else {
5145                 clusterManagerNode *src =
5146                     clusterNodeForResharding(from, target, &raise_err);
5147                 if (src != NULL) listAddNodeTail(sources, src);
5148                 else if (raise_err) {
5149                     result = 0;
5150                     goto cleanup;
5151                 }
5152             }
5153             from = p + 1;
5154         }
5155         /* Check if there's still another source to process. */
5156         if (!all && strlen(from) > 0) {
5157             if (!strcmp(from, "all")) all = 1;
5158             if (!all) {
5159                 clusterManagerNode *src =
5160                     clusterNodeForResharding(from, target, &raise_err);
5161                 if (src != NULL) listAddNodeTail(sources, src);
5162                 else if (raise_err) {
5163                     result = 0;
5164                     goto cleanup;
5165                 }
5166             }
5167         }
5168     }
5169     listIter li;
5170     listNode *ln;
5171     if (all) {
5172         listEmpty(sources);
5173         listRewind(cluster_manager.nodes, &li);
5174         while ((ln = listNext(&li)) != NULL) {
5175             clusterManagerNode *n = ln->value;
5176             if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)
5177                 continue;
5178             if (!sdscmp(n->name, target->name)) continue;
5179             listAddNodeTail(sources, n);
5180         }
5181     }
5182     if (listLength(sources) == 0) {
5183         fprintf(stderr, "*** No source nodes given, operation aborted.\n");
5184         result = 0;
5185         goto cleanup;
5186     }
5187     printf("\nReady to move %d slots.\n", slots);
5188     printf("  Source nodes:\n");
5189     listRewind(sources, &li);
5190     while ((ln = listNext(&li)) != NULL) {
5191         clusterManagerNode *src = ln->value;
5192         sds info = clusterManagerNodeInfo(src, 4);
5193         printf("%s\n", info);
5194         sdsfree(info);
5195     }
5196     printf("  Destination node:\n");
5197     sds info = clusterManagerNodeInfo(target, 4);
5198     printf("%s\n", info);
5199     sdsfree(info);
5200     table = clusterManagerComputeReshardTable(sources, slots);
5201     printf("  Resharding plan:\n");
5202     clusterManagerShowReshardTable(table);
5203     if (!(config.cluster_manager_command.flags &
5204           CLUSTER_MANAGER_CMD_FLAG_YES))
5205     {
5206         printf("Do you want to proceed with the proposed "
5207                "reshard plan (yes/no)? ");
5208         fflush(stdout);
5209         char buf[4];
5210         int nread = read(fileno(stdin),buf,4);
5211         buf[3] = '\0';
5212         if (nread <= 0 || strcmp("yes", buf) != 0) {
5213             result = 0;
5214             goto cleanup;
5215         }
5216     }
5217     int opts = CLUSTER_MANAGER_OPT_VERBOSE;
5218     listRewind(table, &li);
5219     while ((ln = listNext(&li)) != NULL) {
5220         clusterManagerReshardTableItem *item = ln->value;
5221         char *err = NULL;
5222         result = clusterManagerMoveSlot(item->source, target, item->slot,
5223                                         opts, &err);
5224         if (!result) {
5225             if (err != NULL) {
5226                 //clusterManagerLogErr("\n%s\n", err);
5227                 zfree(err);
5228             }
5229             goto cleanup;
5230         }
5231     }
5232 cleanup:
5233     listRelease(sources);
5234     clusterManagerReleaseReshardTable(table);
5235     return result;
5236 invalid_args:
5237     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5238     return 0;
5239 }
5240 
clusterManagerCommandRebalance(int argc,char ** argv)5241 static int clusterManagerCommandRebalance(int argc, char **argv) {
5242     int port = 0;
5243     char *ip = NULL;
5244     clusterManagerNode **weightedNodes = NULL;
5245     list *involved = NULL;
5246     if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;
5247     clusterManagerNode *node = clusterManagerNewNode(ip, port);
5248     if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;
5249     int result = 1, i;
5250     if (config.cluster_manager_command.weight != NULL) {
5251         for (i = 0; i < config.cluster_manager_command.weight_argc; i++) {
5252             char *name = config.cluster_manager_command.weight[i];
5253             char *p = strchr(name, '=');
5254             if (p == NULL) {
5255                 result = 0;
5256                 goto cleanup;
5257             }
5258             *p = '\0';
5259             float w = atof(++p);
5260             clusterManagerNode *n = clusterManagerNodeByAbbreviatedName(name);
5261             if (n == NULL) {
5262                 clusterManagerLogErr("*** No such master node %s\n", name);
5263                 result = 0;
5264                 goto cleanup;
5265             }
5266             n->weight = w;
5267         }
5268     }
5269     float total_weight = 0;
5270     int nodes_involved = 0;
5271     int use_empty = config.cluster_manager_command.flags &
5272                     CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER;
5273     involved = listCreate();
5274     listIter li;
5275     listNode *ln;
5276     listRewind(cluster_manager.nodes, &li);
5277     /* Compute the total cluster weight. */
5278     while ((ln = listNext(&li)) != NULL) {
5279         clusterManagerNode *n = ln->value;
5280         if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)
5281             continue;
5282         if (!use_empty && n->slots_count == 0) {
5283             n->weight = 0;
5284             continue;
5285         }
5286         total_weight += n->weight;
5287         nodes_involved++;
5288         listAddNodeTail(involved, n);
5289     }
5290     weightedNodes = zmalloc(nodes_involved * sizeof(clusterManagerNode *));
5291     if (weightedNodes == NULL) goto cleanup;
5292     /* Check cluster, only proceed if it looks sane. */
5293     clusterManagerCheckCluster(1);
5294     if (cluster_manager.errors && listLength(cluster_manager.errors) > 0) {
5295         clusterManagerLogErr("*** Please fix your cluster problems "
5296                              "before rebalancing\n");
5297         result = 0;
5298         goto cleanup;
5299     }
5300     /* Calculate the slots balance for each node. It's the number of
5301      * slots the node should lose (if positive) or gain (if negative)
5302      * in order to be balanced. */
5303     int threshold_reached = 0, total_balance = 0;
5304     float threshold = config.cluster_manager_command.threshold;
5305     i = 0;
5306     listRewind(involved, &li);
5307     while ((ln = listNext(&li)) != NULL) {
5308         clusterManagerNode *n = ln->value;
5309         weightedNodes[i++] = n;
5310         int expected = (int) (((float)CLUSTER_MANAGER_SLOTS / total_weight) *
5311                         n->weight);
5312         n->balance = n->slots_count - expected;
5313         total_balance += n->balance;
5314         /* Compute the percentage of difference between the
5315          * expected number of slots and the real one, to see
5316          * if it's over the threshold specified by the user. */
5317         int over_threshold = 0;
5318         if (threshold > 0) {
5319             if (n->slots_count > 0) {
5320                 float err_perc = fabs((100-(100.0*expected/n->slots_count)));
5321                 if (err_perc > threshold) over_threshold = 1;
5322             } else if (expected > 1) {
5323                 over_threshold = 1;
5324             }
5325         }
5326         if (over_threshold) threshold_reached = 1;
5327     }
5328     if (!threshold_reached) {
5329         clusterManagerLogWarn("*** No rebalancing needed! "
5330                              "All nodes are within the %.2f%% threshold.\n",
5331                              config.cluster_manager_command.threshold);
5332         goto cleanup;
5333     }
5334     /* Because of rounding, it is possible that the balance of all nodes
5335      * summed does not give 0. Make sure that nodes that have to provide
5336      * slots are always matched by nodes receiving slots. */
5337     while (total_balance > 0) {
5338         listRewind(involved, &li);
5339         while ((ln = listNext(&li)) != NULL) {
5340             clusterManagerNode *n = ln->value;
5341             if (n->balance <= 0 && total_balance > 0) {
5342                 n->balance--;
5343                 total_balance--;
5344             }
5345         }
5346     }
5347     /* Sort nodes by their slots balance. */
5348     qsort(weightedNodes, nodes_involved, sizeof(clusterManagerNode *),
5349           clusterManagerCompareNodeBalance);
5350     clusterManagerLogInfo(">>> Rebalancing across %d nodes. "
5351                           "Total weight = %.2f\n",
5352                           nodes_involved, total_weight);
5353     if (config.verbose) {
5354         for (i = 0; i < nodes_involved; i++) {
5355             clusterManagerNode *n = weightedNodes[i];
5356             printf("%s:%d balance is %d slots\n", n->ip, n->port, n->balance);
5357         }
5358     }
5359     /* Now we have at the start of the 'sn' array nodes that should get
5360      * slots, at the end nodes that must give slots.
5361      * We take two indexes, one at the start, and one at the end,
5362      * incrementing or decrementing the indexes accordingly til we
5363      * find nodes that need to get/provide slots. */
5364     int dst_idx = 0;
5365     int src_idx = nodes_involved - 1;
5366     int simulate = config.cluster_manager_command.flags &
5367                    CLUSTER_MANAGER_CMD_FLAG_SIMULATE;
5368     while (dst_idx < src_idx) {
5369         clusterManagerNode *dst = weightedNodes[dst_idx];
5370         clusterManagerNode *src = weightedNodes[src_idx];
5371         int db = abs(dst->balance);
5372         int sb = abs(src->balance);
5373         int numslots = (db < sb ? db : sb);
5374         if (numslots > 0) {
5375             printf("Moving %d slots from %s:%d to %s:%d\n", numslots,
5376                                                             src->ip,
5377                                                             src->port,
5378                                                             dst->ip,
5379                                                             dst->port);
5380             /* Actually move the slots. */
5381             list *lsrc = listCreate(), *table = NULL;
5382             listAddNodeTail(lsrc, src);
5383             table = clusterManagerComputeReshardTable(lsrc, numslots);
5384             listRelease(lsrc);
5385             int table_len = (int) listLength(table);
5386             if (!table || table_len != numslots) {
5387                 clusterManagerLogErr("*** Assertion failed: Reshard table "
5388                                      "!= number of slots");
5389                 result = 0;
5390                 goto end_move;
5391             }
5392             if (simulate) {
5393                 for (i = 0; i < table_len; i++) printf("#");
5394             } else {
5395                 int opts = CLUSTER_MANAGER_OPT_QUIET |
5396                            CLUSTER_MANAGER_OPT_UPDATE;
5397                 listRewind(table, &li);
5398                 while ((ln = listNext(&li)) != NULL) {
5399                     clusterManagerReshardTableItem *item = ln->value;
5400                     result = clusterManagerMoveSlot(item->source,
5401                                                     dst,
5402                                                     item->slot,
5403                                                     opts, NULL);
5404                     if (!result) goto end_move;
5405                     printf("#");
5406                     fflush(stdout);
5407                 }
5408 
5409             }
5410             printf("\n");
5411 end_move:
5412             clusterManagerReleaseReshardTable(table);
5413             if (!result) goto cleanup;
5414         }
5415         /* Update nodes balance. */
5416         dst->balance += numslots;
5417         src->balance -= numslots;
5418         if (dst->balance == 0) dst_idx++;
5419         if (src->balance == 0) src_idx --;
5420     }
5421 cleanup:
5422     if (involved != NULL) listRelease(involved);
5423     if (weightedNodes != NULL) zfree(weightedNodes);
5424     return result;
5425 invalid_args:
5426     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5427     return 0;
5428 }
5429 
clusterManagerCommandSetTimeout(int argc,char ** argv)5430 static int clusterManagerCommandSetTimeout(int argc, char **argv) {
5431     UNUSED(argc);
5432     int port = 0;
5433     char *ip = NULL;
5434     if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;
5435     int timeout = atoi(argv[1]);
5436     if (timeout < 100) {
5437         fprintf(stderr, "Setting a node timeout of less than 100 "
5438                 "milliseconds is a bad idea.\n");
5439         return 0;
5440     }
5441     // Load cluster information
5442     clusterManagerNode *node = clusterManagerNewNode(ip, port);
5443     if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;
5444     int ok_count = 0, err_count = 0;
5445 
5446     clusterManagerLogInfo(">>> Reconfiguring node timeout in every "
5447                           "cluster node...\n");
5448     listIter li;
5449     listNode *ln;
5450     listRewind(cluster_manager.nodes, &li);
5451     while ((ln = listNext(&li)) != NULL) {
5452         clusterManagerNode *n = ln->value;
5453         char *err = NULL;
5454         redisReply *reply = CLUSTER_MANAGER_COMMAND(n, "CONFIG %s %s %d",
5455                                                     "SET",
5456                                                     "cluster-node-timeout",
5457                                                     timeout);
5458         if (reply == NULL) goto reply_err;
5459         int ok = clusterManagerCheckRedisReply(n, reply, &err);
5460         freeReplyObject(reply);
5461         if (!ok) goto reply_err;
5462         reply = CLUSTER_MANAGER_COMMAND(n, "CONFIG %s", "REWRITE");
5463         if (reply == NULL) goto reply_err;
5464         ok = clusterManagerCheckRedisReply(n, reply, &err);
5465         freeReplyObject(reply);
5466         if (!ok) goto reply_err;
5467         clusterManagerLogWarn("*** New timeout set for %s:%d\n", n->ip,
5468                               n->port);
5469         ok_count++;
5470         continue;
5471 reply_err:;
5472         int need_free = 0;
5473         if (err == NULL) err = "";
5474         else need_free = 1;
5475         clusterManagerLogErr("ERR setting node-timeot for %s:%d: %s\n", n->ip,
5476                              n->port, err);
5477         if (need_free) zfree(err);
5478         err_count++;
5479     }
5480     clusterManagerLogInfo(">>> New node timeout set. %d OK, %d ERR.\n",
5481                           ok_count, err_count);
5482     return 1;
5483 invalid_args:
5484     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5485     return 0;
5486 }
5487 
clusterManagerCommandImport(int argc,char ** argv)5488 static int clusterManagerCommandImport(int argc, char **argv) {
5489     int success = 1;
5490     int port = 0, src_port = 0;
5491     char *ip = NULL, *src_ip = NULL;
5492     char *invalid_args_msg = NULL;
5493     if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) {
5494         invalid_args_msg = CLUSTER_MANAGER_INVALID_HOST_ARG;
5495         goto invalid_args;
5496     }
5497     if (config.cluster_manager_command.from == NULL) {
5498         invalid_args_msg = "[ERR] Option '--cluster-from' is required for "
5499                            "subcommand 'import'.\n";
5500         goto invalid_args;
5501     }
5502     char *src_host[] = {config.cluster_manager_command.from};
5503     if (!getClusterHostFromCmdArgs(1, src_host, &src_ip, &src_port)) {
5504         invalid_args_msg = "[ERR] Invalid --cluster-from host. You need to "
5505                            "pass a valid address (ie. 120.0.0.1:7000).\n";
5506         goto invalid_args;
5507     }
5508     clusterManagerLogInfo(">>> Importing data from %s:%d to cluster %s:%d\n",
5509                           src_ip, src_port, ip, port);
5510 
5511     clusterManagerNode *refnode = clusterManagerNewNode(ip, port);
5512     if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;
5513     if (!clusterManagerCheckCluster(0)) return 0;
5514     char *reply_err = NULL;
5515     redisReply *src_reply = NULL;
5516     // Connect to the source node.
5517     redisContext *src_ctx = redisConnect(src_ip, src_port);
5518     if (src_ctx->err) {
5519         success = 0;
5520         fprintf(stderr,"Could not connect to Redis at %s:%d: %s.\n", src_ip,
5521                 src_port, src_ctx->errstr);
5522         goto cleanup;
5523     }
5524     src_reply = reconnectingRedisCommand(src_ctx, "INFO");
5525     if (!src_reply || src_reply->type == REDIS_REPLY_ERROR) {
5526         if (src_reply && src_reply->str) reply_err = src_reply->str;
5527         success = 0;
5528         goto cleanup;
5529     }
5530     if (getLongInfoField(src_reply->str, "cluster_enabled")) {
5531         clusterManagerLogErr("[ERR] The source node should not be a "
5532                              "cluster node.\n");
5533         success = 0;
5534         goto cleanup;
5535     }
5536     freeReplyObject(src_reply);
5537     src_reply = reconnectingRedisCommand(src_ctx, "DBSIZE");
5538     if (!src_reply || src_reply->type == REDIS_REPLY_ERROR) {
5539         if (src_reply && src_reply->str) reply_err = src_reply->str;
5540         success = 0;
5541         goto cleanup;
5542     }
5543     int size = src_reply->integer, i;
5544     clusterManagerLogWarn("*** Importing %d keys from DB 0\n", size);
5545 
5546     // Build a slot -> node map
5547     clusterManagerNode  *slots_map[CLUSTER_MANAGER_SLOTS];
5548     memset(slots_map, 0, sizeof(slots_map));
5549     listIter li;
5550     listNode *ln;
5551     for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {
5552         listRewind(cluster_manager.nodes, &li);
5553         while ((ln = listNext(&li)) != NULL) {
5554             clusterManagerNode *n = ln->value;
5555             if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;
5556             if (n->slots_count == 0) continue;
5557             if (n->slots[i]) {
5558                 slots_map[i] = n;
5559                 break;
5560             }
5561         }
5562     }
5563 
5564     char cmdfmt[50] = "MIGRATE %s %d %s %d %d";
5565     if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_COPY)
5566         strcat(cmdfmt, " %s");
5567     if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_REPLACE)
5568         strcat(cmdfmt, " %s");
5569 
5570     /* Use SCAN to iterate over the keys, migrating to the
5571      * right node as needed. */
5572     int cursor = -999, timeout = config.cluster_manager_command.timeout;
5573     while (cursor != 0) {
5574         if (cursor < 0) cursor = 0;
5575         freeReplyObject(src_reply);
5576         src_reply = reconnectingRedisCommand(src_ctx, "SCAN %d COUNT %d",
5577                                              cursor, 1000);
5578         if (!src_reply || src_reply->type == REDIS_REPLY_ERROR) {
5579             if (src_reply && src_reply->str) reply_err = src_reply->str;
5580             success = 0;
5581             goto cleanup;
5582         }
5583         assert(src_reply->type == REDIS_REPLY_ARRAY);
5584         assert(src_reply->elements >= 2);
5585         assert(src_reply->element[1]->type == REDIS_REPLY_ARRAY);
5586         if (src_reply->element[0]->type == REDIS_REPLY_STRING)
5587             cursor = atoi(src_reply->element[0]->str);
5588         else if (src_reply->element[0]->type == REDIS_REPLY_INTEGER)
5589             cursor = src_reply->element[0]->integer;
5590         int keycount = src_reply->element[1]->elements;
5591         for (i = 0; i < keycount; i++) {
5592             redisReply *kr = src_reply->element[1]->element[i];
5593             assert(kr->type == REDIS_REPLY_STRING);
5594             char *key = kr->str;
5595             uint16_t slot = clusterManagerKeyHashSlot(key, kr->len);
5596             clusterManagerNode *target = slots_map[slot];
5597             printf("Migrating %s to %s:%d: ", key, target->ip, target->port);
5598             redisReply *r = reconnectingRedisCommand(src_ctx, cmdfmt,
5599                                                      target->ip, target->port,
5600                                                      key, 0, timeout,
5601                                                      "COPY", "REPLACE");
5602             if (!r || r->type == REDIS_REPLY_ERROR) {
5603                 if (r && r->str) {
5604                     clusterManagerLogErr("Source %s:%d replied with "
5605                                          "error:\n%s\n", src_ip, src_port,
5606                                          r->str);
5607                 }
5608                 success = 0;
5609             }
5610             freeReplyObject(r);
5611             if (!success) goto cleanup;
5612             clusterManagerLogOk("OK\n");
5613         }
5614     }
5615 cleanup:
5616     if (reply_err)
5617         clusterManagerLogErr("Source %s:%d replied with error:\n%s\n",
5618                              src_ip, src_port, reply_err);
5619     if (src_ctx) redisFree(src_ctx);
5620     if (src_reply) freeReplyObject(src_reply);
5621     return success;
5622 invalid_args:
5623     fprintf(stderr, "%s", invalid_args_msg);
5624     return 0;
5625 }
5626 
clusterManagerCommandCall(int argc,char ** argv)5627 static int clusterManagerCommandCall(int argc, char **argv) {
5628     int port = 0, i;
5629     char *ip = NULL;
5630     if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;
5631     clusterManagerNode *refnode = clusterManagerNewNode(ip, port);
5632     if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;
5633     argc--;
5634     argv++;
5635     size_t *argvlen = zmalloc(argc*sizeof(size_t));
5636     clusterManagerLogInfo(">>> Calling");
5637     for (i = 0; i < argc; i++) {
5638         argvlen[i] = strlen(argv[i]);
5639         printf(" %s", argv[i]);
5640     }
5641     printf("\n");
5642     listIter li;
5643     listNode *ln;
5644     listRewind(cluster_manager.nodes, &li);
5645     while ((ln = listNext(&li)) != NULL) {
5646         clusterManagerNode *n = ln->value;
5647         if (!n->context && !clusterManagerNodeConnect(n)) continue;
5648         redisReply *reply = NULL;
5649         redisAppendCommandArgv(n->context, argc, (const char **) argv, argvlen);
5650         int status = redisGetReply(n->context, (void **)(&reply));
5651         if (status != REDIS_OK || reply == NULL )
5652             printf("%s:%d: Failed!\n", n->ip, n->port);
5653         else {
5654             sds formatted_reply = cliFormatReplyRaw(reply);
5655             printf("%s:%d: %s\n", n->ip, n->port, (char *) formatted_reply);
5656             sdsfree(formatted_reply);
5657         }
5658         if (reply != NULL) freeReplyObject(reply);
5659     }
5660     zfree(argvlen);
5661     return 1;
5662 invalid_args:
5663     fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);
5664     return 0;
5665 }
5666 
clusterManagerCommandHelp(int argc,char ** argv)5667 static int clusterManagerCommandHelp(int argc, char **argv) {
5668     UNUSED(argc);
5669     UNUSED(argv);
5670     int commands_count = sizeof(clusterManagerCommands) /
5671                          sizeof(clusterManagerCommandDef);
5672     int i = 0, j;
5673     fprintf(stderr, "Cluster Manager Commands:\n");
5674     int padding = 15;
5675     for (; i < commands_count; i++) {
5676         clusterManagerCommandDef *def = &(clusterManagerCommands[i]);
5677         int namelen = strlen(def->name), padlen = padding - namelen;
5678         fprintf(stderr, "  %s", def->name);
5679         for (j = 0; j < padlen; j++) fprintf(stderr, " ");
5680         fprintf(stderr, "%s\n", (def->args ? def->args : ""));
5681         if (def->options != NULL) {
5682             int optslen = strlen(def->options);
5683             char *p = def->options, *eos = p + optslen;
5684             char *comma = NULL;
5685             while ((comma = strchr(p, ',')) != NULL) {
5686                 int deflen = (int)(comma - p);
5687                 char buf[255];
5688                 memcpy(buf, p, deflen);
5689                 buf[deflen] = '\0';
5690                 for (j = 0; j < padding; j++) fprintf(stderr, " ");
5691                 fprintf(stderr, "  --cluster-%s\n", buf);
5692                 p = comma + 1;
5693                 if (p >= eos) break;
5694             }
5695             if (p < eos) {
5696                 for (j = 0; j < padding; j++) fprintf(stderr, " ");
5697                 fprintf(stderr, "  --cluster-%s\n", p);
5698             }
5699         }
5700     }
5701     fprintf(stderr, "\nFor check, fix, reshard, del-node, set-timeout you "
5702                     "can specify the host and port of any working node in "
5703                     "the cluster.\n\n");
5704     return 0;
5705 }
5706 
5707 /*------------------------------------------------------------------------------
5708  * Latency and latency history modes
5709  *--------------------------------------------------------------------------- */
5710 
latencyModePrint(long long min,long long max,double avg,long long count)5711 static void latencyModePrint(long long min, long long max, double avg, long long count) {
5712     if (config.output == OUTPUT_STANDARD) {
5713         printf("min: %lld, max: %lld, avg: %.2f (%lld samples)",
5714                 min, max, avg, count);
5715         fflush(stdout);
5716     } else if (config.output == OUTPUT_CSV) {
5717         printf("%lld,%lld,%.2f,%lld\n", min, max, avg, count);
5718     } else if (config.output == OUTPUT_RAW) {
5719         printf("%lld %lld %.2f %lld\n", min, max, avg, count);
5720     }
5721 }
5722 
5723 #define LATENCY_SAMPLE_RATE 10 /* milliseconds. */
5724 #define LATENCY_HISTORY_DEFAULT_INTERVAL 15000 /* milliseconds. */
latencyMode(void)5725 static void latencyMode(void) {
5726     redisReply *reply;
5727     long long start, latency, min = 0, max = 0, tot = 0, count = 0;
5728     long long history_interval =
5729         config.interval ? config.interval/1000 :
5730                           LATENCY_HISTORY_DEFAULT_INTERVAL;
5731     double avg;
5732     long long history_start = mstime();
5733 
5734     /* Set a default for the interval in case of --latency option
5735      * with --raw, --csv or when it is redirected to non tty. */
5736     if (config.interval == 0) {
5737         config.interval = 1000;
5738     } else {
5739         config.interval /= 1000; /* We need to convert to milliseconds. */
5740     }
5741 
5742     if (!context) exit(1);
5743     while(1) {
5744         start = mstime();
5745         reply = reconnectingRedisCommand(context,"PING");
5746         if (reply == NULL) {
5747             fprintf(stderr,"\nI/O error\n");
5748             exit(1);
5749         }
5750         latency = mstime()-start;
5751         freeReplyObject(reply);
5752         count++;
5753         if (count == 1) {
5754             min = max = tot = latency;
5755             avg = (double) latency;
5756         } else {
5757             if (latency < min) min = latency;
5758             if (latency > max) max = latency;
5759             tot += latency;
5760             avg = (double) tot/count;
5761         }
5762 
5763         if (config.output == OUTPUT_STANDARD) {
5764             printf("\x1b[0G\x1b[2K"); /* Clear the line. */
5765             latencyModePrint(min,max,avg,count);
5766         } else {
5767             if (config.latency_history) {
5768                 latencyModePrint(min,max,avg,count);
5769             } else if (mstime()-history_start > config.interval) {
5770                 latencyModePrint(min,max,avg,count);
5771                 exit(0);
5772             }
5773         }
5774 
5775         if (config.latency_history && mstime()-history_start > history_interval)
5776         {
5777             printf(" -- %.2f seconds range\n", (float)(mstime()-history_start)/1000);
5778             history_start = mstime();
5779             min = max = tot = count = 0;
5780         }
5781         usleep(LATENCY_SAMPLE_RATE * 1000);
5782     }
5783 }
5784 
5785 /*------------------------------------------------------------------------------
5786  * Latency distribution mode -- requires 256 colors xterm
5787  *--------------------------------------------------------------------------- */
5788 
5789 #define LATENCY_DIST_DEFAULT_INTERVAL 1000 /* milliseconds. */
5790 
5791 /* Structure to store samples distribution. */
5792 struct distsamples {
5793     long long max;   /* Max latency to fit into this interval (usec). */
5794     long long count; /* Number of samples in this interval. */
5795     int character;   /* Associated character in visualization. */
5796 };
5797 
5798 /* Helper function for latencyDistMode(). Performs the spectrum visualization
5799  * of the collected samples targeting an xterm 256 terminal.
5800  *
5801  * Takes an array of distsamples structures, ordered from smaller to bigger
5802  * 'max' value. Last sample max must be 0, to mean that it olds all the
5803  * samples greater than the previous one, and is also the stop sentinel.
5804  *
5805  * "tot' is the total number of samples in the different buckets, so it
5806  * is the SUM(samples[i].conut) for i to 0 up to the max sample.
5807  *
5808  * As a side effect the function sets all the buckets count to 0. */
showLatencyDistSamples(struct distsamples * samples,long long tot)5809 void showLatencyDistSamples(struct distsamples *samples, long long tot) {
5810     int j;
5811 
5812      /* We convert samples into a index inside the palette
5813      * proportional to the percentage a given bucket represents.
5814      * This way intensity of the different parts of the spectrum
5815      * don't change relative to the number of requests, which avoids to
5816      * pollute the visualization with non-latency related info. */
5817     printf("\033[38;5;0m"); /* Set foreground color to black. */
5818     for (j = 0; ; j++) {
5819         int coloridx =
5820             ceil((float) samples[j].count / tot * (spectrum_palette_size-1));
5821         int color = spectrum_palette[coloridx];
5822         printf("\033[48;5;%dm%c", (int)color, samples[j].character);
5823         samples[j].count = 0;
5824         if (samples[j].max == 0) break; /* Last sample. */
5825     }
5826     printf("\033[0m\n");
5827     fflush(stdout);
5828 }
5829 
5830 /* Show the legend: different buckets values and colors meaning, so
5831  * that the spectrum is more easily readable. */
showLatencyDistLegend(void)5832 void showLatencyDistLegend(void) {
5833     int j;
5834 
5835     printf("---------------------------------------------\n");
5836     printf(". - * #          .01 .125 .25 .5 milliseconds\n");
5837     printf("1,2,3,...,9      from 1 to 9     milliseconds\n");
5838     printf("A,B,C,D,E        10,20,30,40,50  milliseconds\n");
5839     printf("F,G,H,I,J        .1,.2,.3,.4,.5       seconds\n");
5840     printf("K,L,M,N,O,P,Q,?  1,2,4,8,16,30,60,>60 seconds\n");
5841     printf("From 0 to 100%%: ");
5842     for (j = 0; j < spectrum_palette_size; j++) {
5843         printf("\033[48;5;%dm ", spectrum_palette[j]);
5844     }
5845     printf("\033[0m\n");
5846     printf("---------------------------------------------\n");
5847 }
5848 
latencyDistMode(void)5849 static void latencyDistMode(void) {
5850     redisReply *reply;
5851     long long start, latency, count = 0;
5852     long long history_interval =
5853         config.interval ? config.interval/1000 :
5854                           LATENCY_DIST_DEFAULT_INTERVAL;
5855     long long history_start = ustime();
5856     int j, outputs = 0;
5857 
5858     struct distsamples samples[] = {
5859         /* We use a mostly logarithmic scale, with certain linear intervals
5860          * which are more interesting than others, like 1-10 milliseconds
5861          * range. */
5862         {10,0,'.'},         /* 0.01 ms */
5863         {125,0,'-'},        /* 0.125 ms */
5864         {250,0,'*'},        /* 0.25 ms */
5865         {500,0,'#'},        /* 0.5 ms */
5866         {1000,0,'1'},       /* 1 ms */
5867         {2000,0,'2'},       /* 2 ms */
5868         {3000,0,'3'},       /* 3 ms */
5869         {4000,0,'4'},       /* 4 ms */
5870         {5000,0,'5'},       /* 5 ms */
5871         {6000,0,'6'},       /* 6 ms */
5872         {7000,0,'7'},       /* 7 ms */
5873         {8000,0,'8'},       /* 8 ms */
5874         {9000,0,'9'},       /* 9 ms */
5875         {10000,0,'A'},      /* 10 ms */
5876         {20000,0,'B'},      /* 20 ms */
5877         {30000,0,'C'},      /* 30 ms */
5878         {40000,0,'D'},      /* 40 ms */
5879         {50000,0,'E'},      /* 50 ms */
5880         {100000,0,'F'},     /* 0.1 s */
5881         {200000,0,'G'},     /* 0.2 s */
5882         {300000,0,'H'},     /* 0.3 s */
5883         {400000,0,'I'},     /* 0.4 s */
5884         {500000,0,'J'},     /* 0.5 s */
5885         {1000000,0,'K'},    /* 1 s */
5886         {2000000,0,'L'},    /* 2 s */
5887         {4000000,0,'M'},    /* 4 s */
5888         {8000000,0,'N'},    /* 8 s */
5889         {16000000,0,'O'},   /* 16 s */
5890         {30000000,0,'P'},   /* 30 s */
5891         {60000000,0,'Q'},   /* 1 minute */
5892         {0,0,'?'},          /* > 1 minute */
5893     };
5894 
5895     if (!context) exit(1);
5896     while(1) {
5897         start = ustime();
5898         reply = reconnectingRedisCommand(context,"PING");
5899         if (reply == NULL) {
5900             fprintf(stderr,"\nI/O error\n");
5901             exit(1);
5902         }
5903         latency = ustime()-start;
5904         freeReplyObject(reply);
5905         count++;
5906 
5907         /* Populate the relevant bucket. */
5908         for (j = 0; ; j++) {
5909             if (samples[j].max == 0 || latency <= samples[j].max) {
5910                 samples[j].count++;
5911                 break;
5912             }
5913         }
5914 
5915         /* From time to time show the spectrum. */
5916         if (count && (ustime()-history_start)/1000 > history_interval) {
5917             if ((outputs++ % 20) == 0)
5918                 showLatencyDistLegend();
5919             showLatencyDistSamples(samples,count);
5920             history_start = ustime();
5921             count = 0;
5922         }
5923         usleep(LATENCY_SAMPLE_RATE * 1000);
5924     }
5925 }
5926 
5927 /*------------------------------------------------------------------------------
5928  * Slave mode
5929  *--------------------------------------------------------------------------- */
5930 
5931 /* Sends SYNC and reads the number of bytes in the payload. Used both by
5932  * slaveMode() and getRDB(). */
sendSync(int fd)5933 unsigned long long sendSync(int fd) {
5934     /* To start we need to send the SYNC command and return the payload.
5935      * The hiredis client lib does not understand this part of the protocol
5936      * and we don't want to mess with its buffers, so everything is performed
5937      * using direct low-level I/O. */
5938     char buf[4096], *p;
5939     ssize_t nread;
5940 
5941     /* Send the SYNC command. */
5942     if (write(fd,"SYNC\r\n",6) != 6) {
5943         fprintf(stderr,"Error writing to master\n");
5944         exit(1);
5945     }
5946 
5947     /* Read $<payload>\r\n, making sure to read just up to "\n" */
5948     p = buf;
5949     while(1) {
5950         nread = read(fd,p,1);
5951         if (nread <= 0) {
5952             fprintf(stderr,"Error reading bulk length while SYNCing\n");
5953             exit(1);
5954         }
5955         if (*p == '\n' && p != buf) break;
5956         if (*p != '\n') p++;
5957     }
5958     *p = '\0';
5959     if (buf[0] == '-') {
5960         printf("SYNC with master failed: %s\n", buf);
5961         exit(1);
5962     }
5963     return strtoull(buf+1,NULL,10);
5964 }
5965 
slaveMode(void)5966 static void slaveMode(void) {
5967     int fd = context->fd;
5968     unsigned long long payload = sendSync(fd);
5969     char buf[1024];
5970     int original_output = config.output;
5971 
5972     fprintf(stderr,"SYNC with master, discarding %llu "
5973                    "bytes of bulk transfer...\n", payload);
5974 
5975     /* Discard the payload. */
5976     while(payload) {
5977         ssize_t nread;
5978 
5979         nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
5980         if (nread <= 0) {
5981             fprintf(stderr,"Error reading RDB payload while SYNCing\n");
5982             exit(1);
5983         }
5984         payload -= nread;
5985     }
5986     fprintf(stderr,"SYNC done. Logging commands from master.\n");
5987 
5988     /* Now we can use hiredis to read the incoming protocol. */
5989     config.output = OUTPUT_CSV;
5990     while (cliReadReply(0) == REDIS_OK);
5991     config.output = original_output;
5992 }
5993 
5994 /*------------------------------------------------------------------------------
5995  * RDB transfer mode
5996  *--------------------------------------------------------------------------- */
5997 
5998 /* This function implements --rdb, so it uses the replication protocol in order
5999  * to fetch the RDB file from a remote server. */
getRDB(void)6000 static void getRDB(void) {
6001     int s = context->fd;
6002     int fd;
6003     unsigned long long payload = sendSync(s);
6004     char buf[4096];
6005 
6006     fprintf(stderr,"SYNC sent to master, writing %llu bytes to '%s'\n",
6007         payload, config.rdb_filename);
6008 
6009     /* Write to file. */
6010     if (!strcmp(config.rdb_filename,"-")) {
6011         fd = STDOUT_FILENO;
6012     } else {
6013         fd = open(config.rdb_filename, O_CREAT|O_WRONLY, 0644);
6014         if (fd == -1) {
6015             fprintf(stderr, "Error opening '%s': %s\n", config.rdb_filename,
6016                 strerror(errno));
6017             exit(1);
6018         }
6019     }
6020 
6021     while(payload) {
6022         ssize_t nread, nwritten;
6023 
6024         nread = read(s,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
6025         if (nread <= 0) {
6026             fprintf(stderr,"I/O Error reading RDB payload from socket\n");
6027             exit(1);
6028         }
6029         nwritten = write(fd, buf, nread);
6030         if (nwritten != nread) {
6031             fprintf(stderr,"Error writing data to file: %s\n",
6032                 (nwritten == -1) ? strerror(errno) : "short write");
6033             exit(1);
6034         }
6035         payload -= nread;
6036     }
6037     close(s); /* Close the file descriptor ASAP as fsync() may take time. */
6038     fsync(fd);
6039     close(fd);
6040     fprintf(stderr,"Transfer finished with success.\n");
6041     exit(0);
6042 }
6043 
6044 /*------------------------------------------------------------------------------
6045  * Bulk import (pipe) mode
6046  *--------------------------------------------------------------------------- */
6047 
6048 #define PIPEMODE_WRITE_LOOP_MAX_BYTES (128*1024)
pipeMode(void)6049 static void pipeMode(void) {
6050     int fd = context->fd;
6051     long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;
6052     char ibuf[1024*16], obuf[1024*16]; /* Input and output buffers */
6053     char aneterr[ANET_ERR_LEN];
6054     redisReader *reader = redisReaderCreate();
6055     redisReply *reply;
6056     int eof = 0; /* True once we consumed all the standard input. */
6057     int done = 0;
6058     char magic[20]; /* Special reply we recognize. */
6059     time_t last_read_time = time(NULL);
6060 
6061     srand(time(NULL));
6062 
6063     /* Use non blocking I/O. */
6064     if (anetNonBlock(aneterr,fd) == ANET_ERR) {
6065         fprintf(stderr, "Can't set the socket in non blocking mode: %s\n",
6066             aneterr);
6067         exit(1);
6068     }
6069 
6070     /* Transfer raw protocol and read replies from the server at the same
6071      * time. */
6072     while(!done) {
6073         int mask = AE_READABLE;
6074 
6075         if (!eof || obuf_len != 0) mask |= AE_WRITABLE;
6076         mask = aeWait(fd,mask,1000);
6077 
6078         /* Handle the readable state: we can read replies from the server. */
6079         if (mask & AE_READABLE) {
6080             ssize_t nread;
6081             int read_error = 0;
6082 
6083             /* Read from socket and feed the hiredis reader. */
6084             do {
6085                 nread = read(fd,ibuf,sizeof(ibuf));
6086                 if (nread == -1 && errno != EAGAIN && errno != EINTR) {
6087                     fprintf(stderr, "Error reading from the server: %s\n",
6088                         strerror(errno));
6089                     read_error = 1;
6090                     break;
6091                 }
6092                 if (nread > 0) {
6093                     redisReaderFeed(reader,ibuf,nread);
6094                     last_read_time = time(NULL);
6095                 }
6096             } while(nread > 0);
6097 
6098             /* Consume replies. */
6099             do {
6100                 if (redisReaderGetReply(reader,(void**)&reply) == REDIS_ERR) {
6101                     fprintf(stderr, "Error reading replies from server\n");
6102                     exit(1);
6103                 }
6104                 if (reply) {
6105                     if (reply->type == REDIS_REPLY_ERROR) {
6106                         fprintf(stderr,"%s\n", reply->str);
6107                         errors++;
6108                     } else if (eof && reply->type == REDIS_REPLY_STRING &&
6109                                       reply->len == 20) {
6110                         /* Check if this is the reply to our final ECHO
6111                          * command. If so everything was received
6112                          * from the server. */
6113                         if (memcmp(reply->str,magic,20) == 0) {
6114                             printf("Last reply received from server.\n");
6115                             done = 1;
6116                             replies--;
6117                         }
6118                     }
6119                     replies++;
6120                     freeReplyObject(reply);
6121                 }
6122             } while(reply);
6123 
6124             /* Abort on read errors. We abort here because it is important
6125              * to consume replies even after a read error: this way we can
6126              * show a potential problem to the user. */
6127             if (read_error) exit(1);
6128         }
6129 
6130         /* Handle the writable state: we can send protocol to the server. */
6131         if (mask & AE_WRITABLE) {
6132             ssize_t loop_nwritten = 0;
6133 
6134             while(1) {
6135                 /* Transfer current buffer to server. */
6136                 if (obuf_len != 0) {
6137                     ssize_t nwritten = write(fd,obuf+obuf_pos,obuf_len);
6138 
6139                     if (nwritten == -1) {
6140                         if (errno != EAGAIN && errno != EINTR) {
6141                             fprintf(stderr, "Error writing to the server: %s\n",
6142                                 strerror(errno));
6143                             exit(1);
6144                         } else {
6145                             nwritten = 0;
6146                         }
6147                     }
6148                     obuf_len -= nwritten;
6149                     obuf_pos += nwritten;
6150                     loop_nwritten += nwritten;
6151                     if (obuf_len != 0) break; /* Can't accept more data. */
6152                 }
6153                 /* If buffer is empty, load from stdin. */
6154                 if (obuf_len == 0 && !eof) {
6155                     ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf));
6156 
6157                     if (nread == 0) {
6158                         /* The ECHO sequence starts with a "\r\n" so that if there
6159                          * is garbage in the protocol we read from stdin, the ECHO
6160                          * will likely still be properly formatted.
6161                          * CRLF is ignored by Redis, so it has no effects. */
6162                         char echo[] =
6163                         "\r\n*2\r\n$4\r\nECHO\r\n$20\r\n01234567890123456789\r\n";
6164                         int j;
6165 
6166                         eof = 1;
6167                         /* Everything transferred, so we queue a special
6168                          * ECHO command that we can match in the replies
6169                          * to make sure everything was read from the server. */
6170                         for (j = 0; j < 20; j++)
6171                             magic[j] = rand() & 0xff;
6172                         memcpy(echo+21,magic,20);
6173                         memcpy(obuf,echo,sizeof(echo)-1);
6174                         obuf_len = sizeof(echo)-1;
6175                         obuf_pos = 0;
6176                         printf("All data transferred. Waiting for the last reply...\n");
6177                     } else if (nread == -1) {
6178                         fprintf(stderr, "Error reading from stdin: %s\n",
6179                             strerror(errno));
6180                         exit(1);
6181                     } else {
6182                         obuf_len = nread;
6183                         obuf_pos = 0;
6184                     }
6185                 }
6186                 if ((obuf_len == 0 && eof) ||
6187                     loop_nwritten > PIPEMODE_WRITE_LOOP_MAX_BYTES) break;
6188             }
6189         }
6190 
6191         /* Handle timeout, that is, we reached EOF, and we are not getting
6192          * replies from the server for a few seconds, nor the final ECHO is
6193          * received. */
6194         if (eof && config.pipe_timeout > 0 &&
6195             time(NULL)-last_read_time > config.pipe_timeout)
6196         {
6197             fprintf(stderr,"No replies for %d seconds: exiting.\n",
6198                 config.pipe_timeout);
6199             errors++;
6200             break;
6201         }
6202     }
6203     redisReaderFree(reader);
6204     printf("errors: %lld, replies: %lld\n", errors, replies);
6205     if (errors)
6206         exit(1);
6207     else
6208         exit(0);
6209 }
6210 
6211 /*------------------------------------------------------------------------------
6212  * Find big keys
6213  *--------------------------------------------------------------------------- */
6214 
sendScan(unsigned long long * it)6215 static redisReply *sendScan(unsigned long long *it) {
6216     redisReply *reply = redisCommand(context, "SCAN %llu", *it);
6217 
6218     /* Handle any error conditions */
6219     if(reply == NULL) {
6220         fprintf(stderr, "\nI/O error\n");
6221         exit(1);
6222     } else if(reply->type == REDIS_REPLY_ERROR) {
6223         fprintf(stderr, "SCAN error: %s\n", reply->str);
6224         exit(1);
6225     } else if(reply->type != REDIS_REPLY_ARRAY) {
6226         fprintf(stderr, "Non ARRAY response from SCAN!\n");
6227         exit(1);
6228     } else if(reply->elements != 2) {
6229         fprintf(stderr, "Invalid element count from SCAN!\n");
6230         exit(1);
6231     }
6232 
6233     /* Validate our types are correct */
6234     assert(reply->element[0]->type == REDIS_REPLY_STRING);
6235     assert(reply->element[1]->type == REDIS_REPLY_ARRAY);
6236 
6237     /* Update iterator */
6238     *it = strtoull(reply->element[0]->str, NULL, 10);
6239 
6240     return reply;
6241 }
6242 
getDbSize(void)6243 static int getDbSize(void) {
6244     redisReply *reply;
6245     int size;
6246 
6247     reply = redisCommand(context, "DBSIZE");
6248 
6249     if(reply == NULL || reply->type != REDIS_REPLY_INTEGER) {
6250         fprintf(stderr, "Couldn't determine DBSIZE!\n");
6251         exit(1);
6252     }
6253 
6254     /* Grab the number of keys and free our reply */
6255     size = reply->integer;
6256     freeReplyObject(reply);
6257 
6258     return size;
6259 }
6260 
6261 typedef struct {
6262     char *name;
6263     char *sizecmd;
6264     char *sizeunit;
6265     unsigned long long biggest;
6266     unsigned long long count;
6267     unsigned long long totalsize;
6268     sds biggest_key;
6269 } typeinfo;
6270 
6271 typeinfo type_string = { "string", "STRLEN", "bytes" };
6272 typeinfo type_list = { "list", "LLEN", "items" };
6273 typeinfo type_set = { "set", "SCARD", "members" };
6274 typeinfo type_hash = { "hash", "HLEN", "fields" };
6275 typeinfo type_zset = { "zset", "ZCARD", "members" };
6276 typeinfo type_stream = { "stream", "XLEN", "entries" };
6277 typeinfo type_other = { "other", NULL, "?" };
6278 
typeinfo_add(dict * types,char * name,typeinfo * type_template)6279 static typeinfo* typeinfo_add(dict *types, char* name, typeinfo* type_template) {
6280     typeinfo *info = zmalloc(sizeof(typeinfo));
6281     *info = *type_template;
6282     info->name = sdsnew(name);
6283     dictAdd(types, info->name, info);
6284     return info;
6285 }
6286 
type_free(void * priv_data,void * val)6287 void type_free(void* priv_data, void* val) {
6288     typeinfo *info = val;
6289     UNUSED(priv_data);
6290     if (info->biggest_key)
6291         sdsfree(info->biggest_key);
6292     sdsfree(info->name);
6293     zfree(info);
6294 }
6295 
6296 static dictType typeinfoDictType = {
6297     dictSdsHash,               /* hash function */
6298     NULL,                      /* key dup */
6299     NULL,                      /* val dup */
6300     dictSdsKeyCompare,         /* key compare */
6301     NULL,                      /* key destructor (owned by the value)*/
6302     type_free                  /* val destructor */
6303 };
6304 
getKeyTypes(dict * types_dict,redisReply * keys,typeinfo ** types)6305 static void getKeyTypes(dict *types_dict, redisReply *keys, typeinfo **types) {
6306     redisReply *reply;
6307     unsigned int i;
6308 
6309     /* Pipeline TYPE commands */
6310     for(i=0;i<keys->elements;i++) {
6311         redisAppendCommand(context, "TYPE %s", keys->element[i]->str);
6312     }
6313 
6314     /* Retrieve types */
6315     for(i=0;i<keys->elements;i++) {
6316         if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
6317             fprintf(stderr, "Error getting type for key '%s' (%d: %s)\n",
6318                 keys->element[i]->str, context->err, context->errstr);
6319             exit(1);
6320         } else if(reply->type != REDIS_REPLY_STATUS) {
6321             if(reply->type == REDIS_REPLY_ERROR) {
6322                 fprintf(stderr, "TYPE returned an error: %s\n", reply->str);
6323             } else {
6324                 fprintf(stderr,
6325                     "Invalid reply type (%d) for TYPE on key '%s'!\n",
6326                     reply->type, keys->element[i]->str);
6327             }
6328             exit(1);
6329         }
6330 
6331         sds typereply = sdsnew(reply->str);
6332         dictEntry *de = dictFind(types_dict, typereply);
6333         sdsfree(typereply);
6334         typeinfo *type = NULL;
6335         if (de)
6336             type = dictGetVal(de);
6337         else if (strcmp(reply->str, "none")) /* create new types for modules, (but not for deleted keys) */
6338             type = typeinfo_add(types_dict, reply->str, &type_other);
6339         types[i] = type;
6340         freeReplyObject(reply);
6341     }
6342 }
6343 
getKeySizes(redisReply * keys,typeinfo ** types,unsigned long long * sizes,int memkeys,unsigned memkeys_samples)6344 static void getKeySizes(redisReply *keys, typeinfo **types,
6345                         unsigned long long *sizes, int memkeys,
6346                         unsigned memkeys_samples)
6347 {
6348     redisReply *reply;
6349     unsigned int i;
6350 
6351     /* Pipeline size commands */
6352     for(i=0;i<keys->elements;i++) {
6353         /* Skip keys that disappeared between SCAN and TYPE (or unknown types when not in memkeys mode) */
6354         if(!types[i] || (!types[i]->sizecmd && !memkeys))
6355             continue;
6356 
6357         if (!memkeys)
6358             redisAppendCommand(context, "%s %s",
6359                 types[i]->sizecmd, keys->element[i]->str);
6360         else if (memkeys_samples==0)
6361             redisAppendCommand(context, "%s %s %s",
6362                 "MEMORY", "USAGE", keys->element[i]->str);
6363         else
6364             redisAppendCommand(context, "%s %s %s SAMPLES %u",
6365                 "MEMORY", "USAGE", keys->element[i]->str, memkeys_samples);
6366     }
6367 
6368     /* Retrieve sizes */
6369     for(i=0;i<keys->elements;i++) {
6370         /* Skip keys that disappeared between SCAN and TYPE (or unknown types when not in memkeys mode) */
6371         if(!types[i] || (!types[i]->sizecmd && !memkeys)) {
6372             sizes[i] = 0;
6373             continue;
6374         }
6375 
6376         /* Retrieve size */
6377         if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
6378             fprintf(stderr, "Error getting size for key '%s' (%d: %s)\n",
6379                 keys->element[i]->str, context->err, context->errstr);
6380             exit(1);
6381         } else if(reply->type != REDIS_REPLY_INTEGER) {
6382             /* Theoretically the key could have been removed and
6383              * added as a different type between TYPE and SIZE */
6384             fprintf(stderr,
6385                 "Warning:  %s on '%s' failed (may have changed type)\n",
6386                 !memkeys? types[i]->sizecmd: "MEMORY USAGE",
6387                 keys->element[i]->str);
6388             sizes[i] = 0;
6389         } else {
6390             sizes[i] = reply->integer;
6391         }
6392 
6393         freeReplyObject(reply);
6394     }
6395 }
6396 
findBigKeys(int memkeys,unsigned memkeys_samples)6397 static void findBigKeys(int memkeys, unsigned memkeys_samples) {
6398     unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0;
6399     redisReply *reply, *keys;
6400     unsigned int arrsize=0, i;
6401     dictIterator *di;
6402     dictEntry *de;
6403     typeinfo **types = NULL;
6404     double pct;
6405 
6406     dict *types_dict = dictCreate(&typeinfoDictType, NULL);
6407     typeinfo_add(types_dict, "string", &type_string);
6408     typeinfo_add(types_dict, "list", &type_list);
6409     typeinfo_add(types_dict, "set", &type_set);
6410     typeinfo_add(types_dict, "hash", &type_hash);
6411     typeinfo_add(types_dict, "zset", &type_zset);
6412     typeinfo_add(types_dict, "stream", &type_stream);
6413 
6414     /* Total keys pre scanning */
6415     total_keys = getDbSize();
6416 
6417     /* Status message */
6418     printf("\n# Scanning the entire keyspace to find biggest keys as well as\n");
6419     printf("# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec\n");
6420     printf("# per 100 SCAN commands (not usually needed).\n\n");
6421 
6422     /* SCAN loop */
6423     do {
6424         /* Calculate approximate percentage completion */
6425         pct = 100 * (double)sampled/total_keys;
6426 
6427         /* Grab some keys and point to the keys array */
6428         reply = sendScan(&it);
6429         keys  = reply->element[1];
6430 
6431         /* Reallocate our type and size array if we need to */
6432         if(keys->elements > arrsize) {
6433             types = zrealloc(types, sizeof(typeinfo*)*keys->elements);
6434             sizes = zrealloc(sizes, sizeof(unsigned long long)*keys->elements);
6435 
6436             if(!types || !sizes) {
6437                 fprintf(stderr, "Failed to allocate storage for keys!\n");
6438                 exit(1);
6439             }
6440 
6441             arrsize = keys->elements;
6442         }
6443 
6444         /* Retrieve types and then sizes */
6445         getKeyTypes(types_dict, keys, types);
6446         getKeySizes(keys, types, sizes, memkeys, memkeys_samples);
6447 
6448         /* Now update our stats */
6449         for(i=0;i<keys->elements;i++) {
6450             typeinfo *type = types[i];
6451             /* Skip keys that disappeared between SCAN and TYPE */
6452             if(!type)
6453                 continue;
6454 
6455             type->totalsize += sizes[i];
6456             type->count++;
6457             totlen += keys->element[i]->len;
6458             sampled++;
6459 
6460             if(type->biggest<sizes[i]) {
6461                 printf(
6462                    "[%05.2f%%] Biggest %-6s found so far '%s' with %llu %s\n",
6463                    pct, type->name, keys->element[i]->str, sizes[i],
6464                    !memkeys? type->sizeunit: "bytes");
6465 
6466                 /* Keep track of biggest key name for this type */
6467                 if (type->biggest_key)
6468                     sdsfree(type->biggest_key);
6469                 type->biggest_key = sdsnew(keys->element[i]->str);
6470                 if(!type->biggest_key) {
6471                     fprintf(stderr, "Failed to allocate memory for key!\n");
6472                     exit(1);
6473                 }
6474 
6475                 /* Keep track of the biggest size for this type */
6476                 type->biggest = sizes[i];
6477             }
6478 
6479             /* Update overall progress */
6480             if(sampled % 1000000 == 0) {
6481                 printf("[%05.2f%%] Sampled %llu keys so far\n", pct, sampled);
6482             }
6483         }
6484 
6485         /* Sleep if we've been directed to do so */
6486         if(sampled && (sampled %100) == 0 && config.interval) {
6487             usleep(config.interval);
6488         }
6489 
6490         freeReplyObject(reply);
6491     } while(it != 0);
6492 
6493     if(types) zfree(types);
6494     if(sizes) zfree(sizes);
6495 
6496     /* We're done */
6497     printf("\n-------- summary -------\n\n");
6498 
6499     printf("Sampled %llu keys in the keyspace!\n", sampled);
6500     printf("Total key length in bytes is %llu (avg len %.2f)\n\n",
6501        totlen, totlen ? (double)totlen/sampled : 0);
6502 
6503     /* Output the biggest keys we found, for types we did find */
6504     di = dictGetIterator(types_dict);
6505     while ((de = dictNext(di))) {
6506         typeinfo *type = dictGetVal(de);
6507         if(type->biggest_key) {
6508             printf("Biggest %6s found '%s' has %llu %s\n", type->name, type->biggest_key,
6509                type->biggest, !memkeys? type->sizeunit: "bytes");
6510         }
6511     }
6512     dictReleaseIterator(di);
6513 
6514     printf("\n");
6515 
6516     di = dictGetIterator(types_dict);
6517     while ((de = dictNext(di))) {
6518         typeinfo *type = dictGetVal(de);
6519         printf("%llu %ss with %llu %s (%05.2f%% of keys, avg size %.2f)\n",
6520            type->count, type->name, type->totalsize, !memkeys? type->sizeunit: "bytes",
6521            sampled ? 100 * (double)type->count/sampled : 0,
6522            type->count ? (double)type->totalsize/type->count : 0);
6523     }
6524     dictReleaseIterator(di);
6525 
6526     dictRelease(types_dict);
6527 
6528     /* Success! */
6529     exit(0);
6530 }
6531 
getKeyFreqs(redisReply * keys,unsigned long long * freqs)6532 static void getKeyFreqs(redisReply *keys, unsigned long long *freqs) {
6533     redisReply *reply;
6534     unsigned int i;
6535 
6536     /* Pipeline OBJECT freq commands */
6537     for(i=0;i<keys->elements;i++) {
6538         redisAppendCommand(context, "OBJECT freq %s", keys->element[i]->str);
6539     }
6540 
6541     /* Retrieve freqs */
6542     for(i=0;i<keys->elements;i++) {
6543         if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
6544             fprintf(stderr, "Error getting freq for key '%s' (%d: %s)\n",
6545                 keys->element[i]->str, context->err, context->errstr);
6546             exit(1);
6547         } else if(reply->type != REDIS_REPLY_INTEGER) {
6548             if(reply->type == REDIS_REPLY_ERROR) {
6549                 fprintf(stderr, "Error: %s\n", reply->str);
6550                 exit(1);
6551             } else {
6552                 fprintf(stderr, "Warning: OBJECT freq on '%s' failed (may have been deleted)\n", keys->element[i]->str);
6553                 freqs[i] = 0;
6554             }
6555         } else {
6556             freqs[i] = reply->integer;
6557         }
6558         freeReplyObject(reply);
6559     }
6560 }
6561 
6562 #define HOTKEYS_SAMPLE 16
findHotKeys(void)6563 static void findHotKeys(void) {
6564     redisReply *keys, *reply;
6565     unsigned long long counters[HOTKEYS_SAMPLE] = {0};
6566     sds hotkeys[HOTKEYS_SAMPLE] = {NULL};
6567     unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0;
6568     unsigned int arrsize = 0, i, k;
6569     double pct;
6570 
6571     /* Total keys pre scanning */
6572     total_keys = getDbSize();
6573 
6574     /* Status message */
6575     printf("\n# Scanning the entire keyspace to find hot keys as well as\n");
6576     printf("# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec\n");
6577     printf("# per 100 SCAN commands (not usually needed).\n\n");
6578 
6579     /* SCAN loop */
6580     do {
6581         /* Calculate approximate percentage completion */
6582         pct = 100 * (double)sampled/total_keys;
6583 
6584         /* Grab some keys and point to the keys array */
6585         reply = sendScan(&it);
6586         keys  = reply->element[1];
6587 
6588         /* Reallocate our freqs array if we need to */
6589         if(keys->elements > arrsize) {
6590             freqs = zrealloc(freqs, sizeof(unsigned long long)*keys->elements);
6591 
6592             if(!freqs) {
6593                 fprintf(stderr, "Failed to allocate storage for keys!\n");
6594                 exit(1);
6595             }
6596 
6597             arrsize = keys->elements;
6598         }
6599 
6600         getKeyFreqs(keys, freqs);
6601 
6602         /* Now update our stats */
6603         for(i=0;i<keys->elements;i++) {
6604             sampled++;
6605             /* Update overall progress */
6606             if(sampled % 1000000 == 0) {
6607                 printf("[%05.2f%%] Sampled %llu keys so far\n", pct, sampled);
6608             }
6609 
6610             /* Use eviction pool here */
6611             k = 0;
6612             while (k < HOTKEYS_SAMPLE && freqs[i] > counters[k]) k++;
6613             if (k == 0) continue;
6614             k--;
6615             if (k == 0 || counters[k] == 0) {
6616                 sdsfree(hotkeys[k]);
6617             } else {
6618                 sdsfree(hotkeys[0]);
6619                 memmove(counters,counters+1,sizeof(counters[0])*k);
6620                 memmove(hotkeys,hotkeys+1,sizeof(hotkeys[0])*k);
6621             }
6622             counters[k] = freqs[i];
6623             hotkeys[k] = sdsnew(keys->element[i]->str);
6624             printf(
6625                "[%05.2f%%] Hot key '%s' found so far with counter %llu\n",
6626                pct, keys->element[i]->str, freqs[i]);
6627         }
6628 
6629         /* Sleep if we've been directed to do so */
6630         if(sampled && (sampled %100) == 0 && config.interval) {
6631             usleep(config.interval);
6632         }
6633 
6634         freeReplyObject(reply);
6635     } while(it != 0);
6636 
6637     if (freqs) zfree(freqs);
6638 
6639     /* We're done */
6640     printf("\n-------- summary -------\n\n");
6641 
6642     printf("Sampled %llu keys in the keyspace!\n", sampled);
6643 
6644     for (i=1; i<= HOTKEYS_SAMPLE; i++) {
6645         k = HOTKEYS_SAMPLE - i;
6646         if(counters[k]>0) {
6647             printf("hot key found with counter: %llu\tkeyname: %s\n", counters[k], hotkeys[k]);
6648             sdsfree(hotkeys[k]);
6649         }
6650     }
6651 
6652     exit(0);
6653 }
6654 
6655 /*------------------------------------------------------------------------------
6656  * Stats mode
6657  *--------------------------------------------------------------------------- */
6658 
6659 /* Return the specified INFO field from the INFO command output "info".
6660  * A new buffer is allocated for the result, that needs to be free'd.
6661  * If the field is not found NULL is returned. */
getInfoField(char * info,char * field)6662 static char *getInfoField(char *info, char *field) {
6663     char *p = strstr(info,field);
6664     char *n1, *n2;
6665     char *result;
6666 
6667     if (!p) return NULL;
6668     p += strlen(field)+1;
6669     n1 = strchr(p,'\r');
6670     n2 = strchr(p,',');
6671     if (n2 && n2 < n1) n1 = n2;
6672     result = zmalloc(sizeof(char)*(n1-p)+1);
6673     memcpy(result,p,(n1-p));
6674     result[n1-p] = '\0';
6675     return result;
6676 }
6677 
6678 /* Like the above function but automatically convert the result into
6679  * a long. On error (missing field) LONG_MIN is returned. */
getLongInfoField(char * info,char * field)6680 static long getLongInfoField(char *info, char *field) {
6681     char *value = getInfoField(info,field);
6682     long l;
6683 
6684     if (!value) return LONG_MIN;
6685     l = strtol(value,NULL,10);
6686     zfree(value);
6687     return l;
6688 }
6689 
6690 /* Convert number of bytes into a human readable string of the form:
6691  * 100B, 2G, 100M, 4K, and so forth. */
bytesToHuman(char * s,long long n)6692 void bytesToHuman(char *s, long long n) {
6693     double d;
6694 
6695     if (n < 0) {
6696         *s = '-';
6697         s++;
6698         n = -n;
6699     }
6700     if (n < 1024) {
6701         /* Bytes */
6702         sprintf(s,"%lldB",n);
6703         return;
6704     } else if (n < (1024*1024)) {
6705         d = (double)n/(1024);
6706         sprintf(s,"%.2fK",d);
6707     } else if (n < (1024LL*1024*1024)) {
6708         d = (double)n/(1024*1024);
6709         sprintf(s,"%.2fM",d);
6710     } else if (n < (1024LL*1024*1024*1024)) {
6711         d = (double)n/(1024LL*1024*1024);
6712         sprintf(s,"%.2fG",d);
6713     }
6714 }
6715 
statMode(void)6716 static void statMode(void) {
6717     redisReply *reply;
6718     long aux, requests = 0;
6719     int i = 0;
6720 
6721     while(1) {
6722         char buf[64];
6723         int j;
6724 
6725         reply = reconnectingRedisCommand(context,"INFO");
6726         if (reply->type == REDIS_REPLY_ERROR) {
6727             printf("ERROR: %s\n", reply->str);
6728             exit(1);
6729         }
6730 
6731         if ((i++ % 20) == 0) {
6732             printf(
6733 "------- data ------ --------------------- load -------------------- - child -\n"
6734 "keys       mem      clients blocked requests            connections          \n");
6735         }
6736 
6737         /* Keys */
6738         aux = 0;
6739         for (j = 0; j < 20; j++) {
6740             long k;
6741 
6742             sprintf(buf,"db%d:keys",j);
6743             k = getLongInfoField(reply->str,buf);
6744             if (k == LONG_MIN) continue;
6745             aux += k;
6746         }
6747         sprintf(buf,"%ld",aux);
6748         printf("%-11s",buf);
6749 
6750         /* Used memory */
6751         aux = getLongInfoField(reply->str,"used_memory");
6752         bytesToHuman(buf,aux);
6753         printf("%-8s",buf);
6754 
6755         /* Clients */
6756         aux = getLongInfoField(reply->str,"connected_clients");
6757         sprintf(buf,"%ld",aux);
6758         printf(" %-8s",buf);
6759 
6760         /* Blocked (BLPOPPING) Clients */
6761         aux = getLongInfoField(reply->str,"blocked_clients");
6762         sprintf(buf,"%ld",aux);
6763         printf("%-8s",buf);
6764 
6765         /* Requests */
6766         aux = getLongInfoField(reply->str,"total_commands_processed");
6767         sprintf(buf,"%ld (+%ld)",aux,requests == 0 ? 0 : aux-requests);
6768         printf("%-19s",buf);
6769         requests = aux;
6770 
6771         /* Connections */
6772         aux = getLongInfoField(reply->str,"total_connections_received");
6773         sprintf(buf,"%ld",aux);
6774         printf(" %-12s",buf);
6775 
6776         /* Children */
6777         aux = getLongInfoField(reply->str,"bgsave_in_progress");
6778         aux |= getLongInfoField(reply->str,"aof_rewrite_in_progress") << 1;
6779         aux |= getLongInfoField(reply->str,"loading") << 2;
6780         switch(aux) {
6781         case 0: break;
6782         case 1:
6783             printf("SAVE");
6784             break;
6785         case 2:
6786             printf("AOF");
6787             break;
6788         case 3:
6789             printf("SAVE+AOF");
6790             break;
6791         case 4:
6792             printf("LOAD");
6793             break;
6794         }
6795 
6796         printf("\n");
6797         freeReplyObject(reply);
6798         usleep(config.interval);
6799     }
6800 }
6801 
6802 /*------------------------------------------------------------------------------
6803  * Scan mode
6804  *--------------------------------------------------------------------------- */
6805 
scanMode(void)6806 static void scanMode(void) {
6807     redisReply *reply;
6808     unsigned long long cur = 0;
6809 
6810     do {
6811         if (config.pattern)
6812             reply = redisCommand(context,"SCAN %llu MATCH %s",
6813                 cur,config.pattern);
6814         else
6815             reply = redisCommand(context,"SCAN %llu",cur);
6816         if (reply == NULL) {
6817             printf("I/O error\n");
6818             exit(1);
6819         } else if (reply->type == REDIS_REPLY_ERROR) {
6820             printf("ERROR: %s\n", reply->str);
6821             exit(1);
6822         } else {
6823             unsigned int j;
6824 
6825             cur = strtoull(reply->element[0]->str,NULL,10);
6826             for (j = 0; j < reply->element[1]->elements; j++)
6827                 printf("%s\n", reply->element[1]->element[j]->str);
6828         }
6829         freeReplyObject(reply);
6830     } while(cur != 0);
6831 
6832     exit(0);
6833 }
6834 
6835 /*------------------------------------------------------------------------------
6836  * LRU test mode
6837  *--------------------------------------------------------------------------- */
6838 
6839 /* Return an integer from min to max (both inclusive) using a power-law
6840  * distribution, depending on the value of alpha: the greater the alpha
6841  * the more bias towards lower values.
6842  *
6843  * With alpha = 6.2 the output follows the 80-20 rule where 20% of
6844  * the returned numbers will account for 80% of the frequency. */
powerLawRand(long long min,long long max,double alpha)6845 long long powerLawRand(long long min, long long max, double alpha) {
6846     double pl, r;
6847 
6848     max += 1;
6849     r = ((double)rand()) / RAND_MAX;
6850     pl = pow(
6851         ((pow(max,alpha+1) - pow(min,alpha+1))*r + pow(min,alpha+1)),
6852         (1.0/(alpha+1)));
6853     return (max-1-(long long)pl)+min;
6854 }
6855 
6856 /* Generates a key name among a set of lru_test_sample_size keys, using
6857  * an 80-20 distribution. */
LRUTestGenKey(char * buf,size_t buflen)6858 void LRUTestGenKey(char *buf, size_t buflen) {
6859     snprintf(buf, buflen, "lru:%lld",
6860         powerLawRand(1, config.lru_test_sample_size, 6.2));
6861 }
6862 
6863 #define LRU_CYCLE_PERIOD 1000 /* 1000 milliseconds. */
6864 #define LRU_CYCLE_PIPELINE_SIZE 250
LRUTestMode(void)6865 static void LRUTestMode(void) {
6866     redisReply *reply;
6867     char key[128];
6868     long long start_cycle;
6869     int j;
6870 
6871     srand(time(NULL)^getpid());
6872     while(1) {
6873         /* Perform cycles of 1 second with 50% writes and 50% reads.
6874          * We use pipelining batching writes / reads N times per cycle in order
6875          * to fill the target instance easily. */
6876         start_cycle = mstime();
6877         long long hits = 0, misses = 0;
6878         while(mstime() - start_cycle < 1000) {
6879             /* Write cycle. */
6880             for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {
6881                 char val[6];
6882                 val[5] = '\0';
6883                 for (int i = 0; i < 5; i++) val[i] = 'A'+rand()%('z'-'A');
6884                 LRUTestGenKey(key,sizeof(key));
6885                 redisAppendCommand(context, "SET %s %s",key,val);
6886             }
6887             for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++)
6888                 redisGetReply(context, (void**)&reply);
6889 
6890             /* Read cycle. */
6891             for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {
6892                 LRUTestGenKey(key,sizeof(key));
6893                 redisAppendCommand(context, "GET %s",key);
6894             }
6895             for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {
6896                 if (redisGetReply(context, (void**)&reply) == REDIS_OK) {
6897                     switch(reply->type) {
6898                         case REDIS_REPLY_ERROR:
6899                             printf("%s\n", reply->str);
6900                             break;
6901                         case REDIS_REPLY_NIL:
6902                             misses++;
6903                             break;
6904                         default:
6905                             hits++;
6906                             break;
6907                     }
6908                 }
6909             }
6910 
6911             if (context->err) {
6912                 fprintf(stderr,"I/O error during LRU test\n");
6913                 exit(1);
6914             }
6915         }
6916         /* Print stats. */
6917         printf(
6918             "%lld Gets/sec | Hits: %lld (%.2f%%) | Misses: %lld (%.2f%%)\n",
6919             hits+misses,
6920             hits, (double)hits/(hits+misses)*100,
6921             misses, (double)misses/(hits+misses)*100);
6922     }
6923     exit(0);
6924 }
6925 
6926 /*------------------------------------------------------------------------------
6927  * Intrisic latency mode.
6928  *
6929  * Measure max latency of a running process that does not result from
6930  * syscalls. Basically this software should provide an hint about how much
6931  * time the kernel leaves the process without a chance to run.
6932  *--------------------------------------------------------------------------- */
6933 
6934 /* This is just some computation the compiler can't optimize out.
6935  * Should run in less than 100-200 microseconds even using very
6936  * slow hardware. Runs in less than 10 microseconds in modern HW. */
compute_something_fast(void)6937 unsigned long compute_something_fast(void) {
6938     unsigned char s[256], i, j, t;
6939     int count = 1000, k;
6940     unsigned long output = 0;
6941 
6942     for (k = 0; k < 256; k++) s[k] = k;
6943 
6944     i = 0;
6945     j = 0;
6946     while(count--) {
6947         i++;
6948         j = j + s[i];
6949         t = s[i];
6950         s[i] = s[j];
6951         s[j] = t;
6952         output += s[(s[i]+s[j])&255];
6953     }
6954     return output;
6955 }
6956 
intrinsicLatencyModeStop(int s)6957 static void intrinsicLatencyModeStop(int s) {
6958     UNUSED(s);
6959     force_cancel_loop = 1;
6960 }
6961 
intrinsicLatencyMode(void)6962 static void intrinsicLatencyMode(void) {
6963     long long test_end, run_time, max_latency = 0, runs = 0;
6964 
6965     run_time = config.intrinsic_latency_duration*1000000;
6966     test_end = ustime() + run_time;
6967     signal(SIGINT, intrinsicLatencyModeStop);
6968 
6969     while(1) {
6970         long long start, end, latency;
6971 
6972         start = ustime();
6973         compute_something_fast();
6974         end = ustime();
6975         latency = end-start;
6976         runs++;
6977         if (latency <= 0) continue;
6978 
6979         /* Reporting */
6980         if (latency > max_latency) {
6981             max_latency = latency;
6982             printf("Max latency so far: %lld microseconds.\n", max_latency);
6983         }
6984 
6985         double avg_us = (double)run_time/runs;
6986         double avg_ns = avg_us * 1e3;
6987         if (force_cancel_loop || end > test_end) {
6988             printf("\n%lld total runs "
6989                 "(avg latency: "
6990                 "%.4f microseconds / %.2f nanoseconds per run).\n",
6991                 runs, avg_us, avg_ns);
6992             printf("Worst run took %.0fx longer than the average latency.\n",
6993                 max_latency / avg_us);
6994             exit(0);
6995         }
6996     }
6997 }
6998 
6999 /*------------------------------------------------------------------------------
7000  * Program main()
7001  *--------------------------------------------------------------------------- */
7002 
main(int argc,char ** argv)7003 int main(int argc, char **argv) {
7004     int firstarg;
7005 
7006     config.hostip = sdsnew("127.0.0.1");
7007     config.hostport = 6379;
7008     config.hostsocket = NULL;
7009     config.repeat = 1;
7010     config.interval = 0;
7011     config.dbnum = 0;
7012     config.interactive = 0;
7013     config.shutdown = 0;
7014     config.monitor_mode = 0;
7015     config.pubsub_mode = 0;
7016     config.latency_mode = 0;
7017     config.latency_dist_mode = 0;
7018     config.latency_history = 0;
7019     config.lru_test_mode = 0;
7020     config.lru_test_sample_size = 0;
7021     config.cluster_mode = 0;
7022     config.slave_mode = 0;
7023     config.getrdb_mode = 0;
7024     config.stat_mode = 0;
7025     config.scan_mode = 0;
7026     config.intrinsic_latency_mode = 0;
7027     config.pattern = NULL;
7028     config.rdb_filename = NULL;
7029     config.pipe_mode = 0;
7030     config.pipe_timeout = REDIS_CLI_DEFAULT_PIPE_TIMEOUT;
7031     config.bigkeys = 0;
7032     config.hotkeys = 0;
7033     config.stdinarg = 0;
7034     config.auth = NULL;
7035     config.eval = NULL;
7036     config.eval_ldb = 0;
7037     config.eval_ldb_end = 0;
7038     config.eval_ldb_sync = 0;
7039     config.enable_ldb_on_eval = 0;
7040     config.last_cmd_type = -1;
7041     config.verbose = 0;
7042     config.no_auth_warning = 0;
7043     config.cluster_manager_command.name = NULL;
7044     config.cluster_manager_command.argc = 0;
7045     config.cluster_manager_command.argv = NULL;
7046     config.cluster_manager_command.flags = 0;
7047     config.cluster_manager_command.replicas = 0;
7048     config.cluster_manager_command.from = NULL;
7049     config.cluster_manager_command.to = NULL;
7050     config.cluster_manager_command.weight = NULL;
7051     config.cluster_manager_command.weight_argc = 0;
7052     config.cluster_manager_command.slots = 0;
7053     config.cluster_manager_command.timeout = CLUSTER_MANAGER_MIGRATE_TIMEOUT;
7054     config.cluster_manager_command.pipeline = CLUSTER_MANAGER_MIGRATE_PIPELINE;
7055     config.cluster_manager_command.threshold =
7056         CLUSTER_MANAGER_REBALANCE_THRESHOLD;
7057     pref.hints = 1;
7058 
7059     spectrum_palette = spectrum_palette_color;
7060     spectrum_palette_size = spectrum_palette_color_size;
7061 
7062     if (!isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL))
7063         config.output = OUTPUT_RAW;
7064     else
7065         config.output = OUTPUT_STANDARD;
7066     config.mb_delim = sdsnew("\n");
7067 
7068     firstarg = parseOptions(argc,argv);
7069     argc -= firstarg;
7070     argv += firstarg;
7071 
7072     parseEnv();
7073 
7074     /* Cluster Manager mode */
7075     if (CLUSTER_MANAGER_MODE()) {
7076         clusterManagerCommandProc *proc = validateClusterManagerCommand();
7077         if (!proc) {
7078             sdsfree(config.hostip);
7079             sdsfree(config.mb_delim);
7080             exit(1);
7081         }
7082         clusterManagerMode(proc);
7083     }
7084 
7085     /* Latency mode */
7086     if (config.latency_mode) {
7087         if (cliConnect(0) == REDIS_ERR) exit(1);
7088         latencyMode();
7089     }
7090 
7091     /* Latency distribution mode */
7092     if (config.latency_dist_mode) {
7093         if (cliConnect(0) == REDIS_ERR) exit(1);
7094         latencyDistMode();
7095     }
7096 
7097     /* Slave mode */
7098     if (config.slave_mode) {
7099         if (cliConnect(0) == REDIS_ERR) exit(1);
7100         slaveMode();
7101     }
7102 
7103     /* Get RDB mode. */
7104     if (config.getrdb_mode) {
7105         if (cliConnect(0) == REDIS_ERR) exit(1);
7106         getRDB();
7107     }
7108 
7109     /* Pipe mode */
7110     if (config.pipe_mode) {
7111         if (cliConnect(0) == REDIS_ERR) exit(1);
7112         pipeMode();
7113     }
7114 
7115     /* Find big keys */
7116     if (config.bigkeys) {
7117         if (cliConnect(0) == REDIS_ERR) exit(1);
7118         findBigKeys(0, 0);
7119     }
7120 
7121     /* Find large keys */
7122     if (config.memkeys) {
7123         if (cliConnect(0) == REDIS_ERR) exit(1);
7124         findBigKeys(1, config.memkeys_samples);
7125     }
7126 
7127     /* Find hot keys */
7128     if (config.hotkeys) {
7129         if (cliConnect(0) == REDIS_ERR) exit(1);
7130         findHotKeys();
7131     }
7132 
7133     /* Stat mode */
7134     if (config.stat_mode) {
7135         if (cliConnect(0) == REDIS_ERR) exit(1);
7136         if (config.interval == 0) config.interval = 1000000;
7137         statMode();
7138     }
7139 
7140     /* Scan mode */
7141     if (config.scan_mode) {
7142         if (cliConnect(0) == REDIS_ERR) exit(1);
7143         scanMode();
7144     }
7145 
7146     /* LRU test mode */
7147     if (config.lru_test_mode) {
7148         if (cliConnect(0) == REDIS_ERR) exit(1);
7149         LRUTestMode();
7150     }
7151 
7152     /* Intrinsic latency mode */
7153     if (config.intrinsic_latency_mode) intrinsicLatencyMode();
7154 
7155     /* Start interactive mode when no command is provided */
7156     if (argc == 0 && !config.eval) {
7157         /* Ignore SIGPIPE in interactive mode to force a reconnect */
7158         signal(SIGPIPE, SIG_IGN);
7159 
7160         /* Note that in repl mode we don't abort on connection error.
7161          * A new attempt will be performed for every command send. */
7162         cliConnect(0);
7163         repl();
7164     }
7165 
7166     /* Otherwise, we have some arguments to execute */
7167     if (cliConnect(0) != REDIS_OK) exit(1);
7168     if (config.eval) {
7169         return evalMode(argc,argv);
7170     } else {
7171         return noninteractive(argc,convertToSds(argc,argv));
7172     }
7173 }
7174