2009-03-22 05:30:00 -04:00
|
|
|
/* Redis CLI (command line interface)
|
|
|
|
*
|
2012-11-08 12:25:23 -05:00
|
|
|
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
2009-03-22 05:30:00 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2009-03-27 17:00:27 -04:00
|
|
|
#include "fmacros.h"
|
2010-07-06 13:17:09 -04:00
|
|
|
#include "version.h"
|
2009-03-27 17:00:27 -04:00
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-18 00:59:12 -04:00
|
|
|
#include <signal.h>
|
2009-03-22 05:30:00 -04:00
|
|
|
#include <unistd.h>
|
2012-05-14 11:35:51 -04:00
|
|
|
#include <time.h>
|
2010-04-26 12:39:39 -04:00
|
|
|
#include <ctype.h>
|
2010-08-24 12:39:34 -04:00
|
|
|
#include <errno.h>
|
2010-08-25 08:48:50 -04:00
|
|
|
#include <sys/stat.h>
|
2010-11-02 13:08:30 -04:00
|
|
|
#include <sys/time.h>
|
2010-11-29 13:27:36 -05:00
|
|
|
#include <assert.h>
|
2013-01-16 13:42:40 -05:00
|
|
|
#include <fcntl.h>
|
2013-03-22 12:54:32 -04:00
|
|
|
#include <limits.h>
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2010-11-03 11:09:38 -04:00
|
|
|
#include "hiredis.h"
|
2009-03-22 05:30:00 -04:00
|
|
|
#include "sds.h"
|
|
|
|
#include "zmalloc.h"
|
2010-03-23 10:25:32 -04:00
|
|
|
#include "linenoise.h"
|
2010-11-16 08:50:26 -05:00
|
|
|
#include "help.h"
|
2012-05-06 04:05:31 -04:00
|
|
|
#include "anet.h"
|
|
|
|
#include "ae.h"
|
2009-03-22 05:30:00 -04:00
|
|
|
|
|
|
|
#define REDIS_NOTUSED(V) ((void) V)
|
|
|
|
|
2012-02-29 11:43:03 -05:00
|
|
|
#define OUTPUT_STANDARD 0
|
|
|
|
#define OUTPUT_RAW 1
|
|
|
|
#define OUTPUT_CSV 2
|
2013-03-04 05:14:32 -05:00
|
|
|
#define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */
|
2014-02-25 06:37:52 -05:00
|
|
|
#define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */
|
2014-11-11 01:40:25 -05:00
|
|
|
#define REDIS_CLI_HISTFILE_ENV "REDISCLI_HISTFILE"
|
|
|
|
#define REDIS_CLI_HISTFILE_DEFAULT ".rediscli_history"
|
2012-02-29 11:43:03 -05:00
|
|
|
|
2010-11-03 11:09:38 -04:00
|
|
|
static redisContext *context;
|
2009-03-22 05:30:00 -04:00
|
|
|
static struct config {
|
|
|
|
char *hostip;
|
|
|
|
int hostport;
|
2010-08-01 17:06:00 -04:00
|
|
|
char *hostsocket;
|
2009-11-02 19:35:39 -05:00
|
|
|
long repeat;
|
2011-05-28 09:41:08 -04:00
|
|
|
long interval;
|
2009-11-11 23:12:09 -05:00
|
|
|
int dbnum;
|
2010-08-25 04:05:50 -04:00
|
|
|
int interactive;
|
2010-05-14 10:38:09 -04:00
|
|
|
int shutdown;
|
2010-04-27 10:07:31 -04:00
|
|
|
int monitor_mode;
|
|
|
|
int pubsub_mode;
|
2011-09-15 13:28:00 -04:00
|
|
|
int latency_mode;
|
2013-04-11 07:11:41 -04:00
|
|
|
int latency_history;
|
2011-10-05 13:55:33 -04:00
|
|
|
int cluster_mode;
|
|
|
|
int cluster_reissue_command;
|
2012-02-29 11:10:21 -05:00
|
|
|
int slave_mode;
|
2012-05-06 04:05:31 -04:00
|
|
|
int pipe_mode;
|
2013-07-03 06:18:55 -04:00
|
|
|
int pipe_timeout;
|
2013-01-16 13:42:40 -05:00
|
|
|
int getrdb_mode;
|
2013-03-22 12:54:32 -04:00
|
|
|
int stat_mode;
|
2014-01-22 06:04:08 -05:00
|
|
|
int scan_mode;
|
2014-02-25 06:37:52 -05:00
|
|
|
int intrinsic_latency_mode;
|
|
|
|
int intrinsic_latency_duration;
|
2014-01-22 06:04:08 -05:00
|
|
|
char *pattern;
|
2013-01-16 13:42:40 -05:00
|
|
|
char *rdb_filename;
|
2012-04-18 14:33:02 -04:00
|
|
|
int bigkeys;
|
2010-09-09 10:38:10 -04:00
|
|
|
int stdinarg; /* get last arg from stdin. (-x option) */
|
2010-03-17 21:51:09 -04:00
|
|
|
char *auth;
|
2012-02-29 11:43:03 -05:00
|
|
|
int output; /* output mode, see OUTPUT_* defines */
|
2010-12-15 09:59:04 -05:00
|
|
|
sds mb_delim;
|
2012-02-22 10:07:06 -05:00
|
|
|
char prompt[128];
|
2011-12-13 10:22:28 -05:00
|
|
|
char *eval;
|
2014-08-01 11:44:28 -04:00
|
|
|
int last_cmd_type;
|
2009-03-22 05:30:00 -04:00
|
|
|
} config;
|
|
|
|
|
2014-06-17 10:12:57 -04:00
|
|
|
static volatile sig_atomic_t force_cancel_loop = 0;
|
2014-03-15 19:51:54 -04:00
|
|
|
static void usage(void);
|
2014-02-10 11:06:13 -05:00
|
|
|
static void slaveMode(void);
|
2010-11-08 10:26:02 -05:00
|
|
|
char *redisGitSHA1(void);
|
2010-12-15 08:34:01 -05:00
|
|
|
char *redisGitDirty(void);
|
2009-03-24 08:37:32 -04:00
|
|
|
|
2010-11-02 13:08:30 -04:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Utility functions
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
static long long ustime(void) {
|
2010-11-02 13:08:30 -04:00
|
|
|
struct timeval tv;
|
2014-02-25 06:24:45 -05:00
|
|
|
long long ust;
|
2010-11-02 13:08:30 -04:00
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
2014-02-25 06:24:45 -05:00
|
|
|
ust = ((long long)tv.tv_sec)*1000000;
|
|
|
|
ust += tv.tv_usec;
|
|
|
|
return ust;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long long mstime(void) {
|
|
|
|
return ustime()/1000;
|
2010-11-02 13:08:30 -04:00
|
|
|
}
|
|
|
|
|
2011-03-06 15:14:40 -05:00
|
|
|
static void cliRefreshPrompt(void) {
|
2012-02-22 10:07:06 -05:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (config.hostsocket != NULL)
|
|
|
|
len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
|
|
|
|
config.hostsocket);
|
2011-03-06 15:14:40 -05:00
|
|
|
else
|
2013-07-11 11:47:55 -04:00
|
|
|
len = snprintf(config.prompt,sizeof(config.prompt),
|
|
|
|
strchr(config.hostip,':') ? "[%s]:%d" : "%s:%d",
|
2012-02-22 10:07:06 -05:00
|
|
|
config.hostip, config.hostport);
|
|
|
|
/* Add [dbnum] if needed */
|
2014-08-01 11:44:28 -04:00
|
|
|
if (config.dbnum != 0 && config.last_cmd_type != REDIS_REPLY_ERROR)
|
2012-02-22 10:07:06 -05:00
|
|
|
len += snprintf(config.prompt+len,sizeof(config.prompt)-len,"[%d]",
|
|
|
|
config.dbnum);
|
|
|
|
snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
|
2011-03-06 15:14:40 -05:00
|
|
|
}
|
|
|
|
|
2014-11-11 01:40:25 -05:00
|
|
|
static sds getHistoryPath() {
|
|
|
|
char *path = NULL;
|
|
|
|
sds historyPath = NULL;
|
|
|
|
|
|
|
|
/* check the env for a histfile override */
|
|
|
|
path = getenv(REDIS_CLI_HISTFILE_ENV);
|
|
|
|
if (path != NULL && *path != '\0') {
|
|
|
|
if (!strcmp("/dev/null", path)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the env is set, return it */
|
|
|
|
historyPath = sdscatprintf(sdsempty(), "%s", path);
|
|
|
|
} else {
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
if (home != NULL && *home != '\0') {
|
|
|
|
/* otherwise, return the default */
|
|
|
|
historyPath = sdscatprintf(sdsempty(), "%s/%s", home, REDIS_CLI_HISTFILE_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return historyPath;
|
|
|
|
}
|
|
|
|
|
2010-11-28 15:37:19 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Help functions
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2010-11-29 14:26:03 -05:00
|
|
|
#define CLI_HELP_COMMAND 1
|
|
|
|
#define CLI_HELP_GROUP 2
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
int argc;
|
|
|
|
sds *argv;
|
|
|
|
sds full;
|
|
|
|
|
|
|
|
/* Only used for help on commands */
|
|
|
|
struct commandHelp *org;
|
|
|
|
} helpEntry;
|
|
|
|
|
|
|
|
static helpEntry *helpEntries;
|
|
|
|
static int helpEntriesLen;
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static sds cliVersion(void) {
|
2010-12-15 08:34:01 -05:00
|
|
|
sds version;
|
|
|
|
version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
|
|
|
|
|
|
|
|
/* Add git commit and working tree status when available */
|
|
|
|
if (strtoll(redisGitSHA1(),NULL,16)) {
|
|
|
|
version = sdscatprintf(version, " (git:%s", redisGitSHA1());
|
|
|
|
if (strtoll(redisGitDirty(),NULL,10))
|
|
|
|
version = sdscatprintf(version, "-dirty");
|
|
|
|
version = sdscat(version, ")");
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static void cliInitHelp(void) {
|
2010-11-29 14:26:03 -05:00
|
|
|
int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
|
|
|
|
int groupslen = sizeof(commandGroups)/sizeof(char*);
|
|
|
|
int i, len, pos = 0;
|
|
|
|
helpEntry tmp;
|
|
|
|
|
|
|
|
helpEntriesLen = len = commandslen+groupslen;
|
|
|
|
helpEntries = malloc(sizeof(helpEntry)*len);
|
|
|
|
|
|
|
|
for (i = 0; i < groupslen; i++) {
|
|
|
|
tmp.argc = 1;
|
|
|
|
tmp.argv = malloc(sizeof(sds));
|
|
|
|
tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
|
|
|
|
tmp.full = tmp.argv[0];
|
|
|
|
tmp.type = CLI_HELP_GROUP;
|
|
|
|
tmp.org = NULL;
|
|
|
|
helpEntries[pos++] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < commandslen; i++) {
|
|
|
|
tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
|
|
|
|
tmp.full = sdsnew(commandHelp[i].name);
|
|
|
|
tmp.type = CLI_HELP_COMMAND;
|
|
|
|
tmp.org = &commandHelp[i];
|
|
|
|
helpEntries[pos++] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 15:37:19 -05:00
|
|
|
/* Output command help to stdout. */
|
2010-11-29 13:27:36 -05:00
|
|
|
static void cliOutputCommandHelp(struct commandHelp *help, int group) {
|
|
|
|
printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
|
|
|
|
printf(" \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
|
|
|
|
printf(" \x1b[33msince:\x1b[0m %s\r\n", help->since);
|
|
|
|
if (group) {
|
|
|
|
printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
|
|
|
|
}
|
2010-11-28 15:37:19 -05:00
|
|
|
}
|
|
|
|
|
2010-11-29 13:27:36 -05:00
|
|
|
/* Print generic help. */
|
2014-03-15 19:51:54 -04:00
|
|
|
static void cliOutputGenericHelp(void) {
|
2010-12-15 08:34:01 -05:00
|
|
|
sds version = cliVersion();
|
2010-11-29 13:27:36 -05:00
|
|
|
printf(
|
|
|
|
"redis-cli %s\r\n"
|
|
|
|
"Type: \"help @<group>\" to get a list of commands in <group>\r\n"
|
|
|
|
" \"help <command>\" for help on <command>\r\n"
|
|
|
|
" \"help <tab>\" to get a list of possible help topics\r\n"
|
|
|
|
" \"quit\" to exit\r\n",
|
2010-12-15 08:34:01 -05:00
|
|
|
version
|
2010-11-29 13:27:36 -05:00
|
|
|
);
|
2010-12-15 08:34:01 -05:00
|
|
|
sdsfree(version);
|
2010-11-28 15:37:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Output all command help, filtering by group or command name. */
|
2010-11-29 13:27:36 -05:00
|
|
|
static void cliOutputHelp(int argc, char **argv) {
|
2010-11-29 14:26:03 -05:00
|
|
|
int i, j, len;
|
2010-11-29 13:27:36 -05:00
|
|
|
int group = -1;
|
2010-11-29 14:26:03 -05:00
|
|
|
helpEntry *entry;
|
|
|
|
struct commandHelp *help;
|
2010-11-28 15:37:19 -05:00
|
|
|
|
2010-11-29 13:27:36 -05:00
|
|
|
if (argc == 0) {
|
|
|
|
cliOutputGenericHelp();
|
2010-11-28 15:37:19 -05:00
|
|
|
return;
|
2010-11-29 13:27:36 -05:00
|
|
|
} else if (argc > 0 && argv[0][0] == '@') {
|
|
|
|
len = sizeof(commandGroups)/sizeof(char*);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
|
|
|
|
group = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-28 15:37:19 -05:00
|
|
|
}
|
|
|
|
|
2010-11-29 13:27:36 -05:00
|
|
|
assert(argc > 0);
|
2010-11-29 14:26:03 -05:00
|
|
|
for (i = 0; i < helpEntriesLen; i++) {
|
|
|
|
entry = &helpEntries[i];
|
|
|
|
if (entry->type != CLI_HELP_COMMAND) continue;
|
|
|
|
|
|
|
|
help = entry->org;
|
2010-11-28 15:37:19 -05:00
|
|
|
if (group == -1) {
|
2010-11-29 14:26:03 -05:00
|
|
|
/* Compare all arguments */
|
|
|
|
if (argc == entry->argc) {
|
|
|
|
for (j = 0; j < argc; j++) {
|
|
|
|
if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
|
|
|
|
}
|
|
|
|
if (j == argc) {
|
|
|
|
cliOutputCommandHelp(help,1);
|
|
|
|
}
|
2010-11-28 15:37:19 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (group == help->group) {
|
2010-11-29 13:27:36 -05:00
|
|
|
cliOutputCommandHelp(help,0);
|
2010-11-28 15:37:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-29 13:27:36 -05:00
|
|
|
printf("\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void completionCallback(const char *buf, linenoiseCompletions *lc) {
|
|
|
|
size_t startpos = 0;
|
|
|
|
int mask;
|
|
|
|
int i;
|
|
|
|
size_t matchlen;
|
2010-11-29 14:26:03 -05:00
|
|
|
sds tmp;
|
2010-11-29 13:27:36 -05:00
|
|
|
|
|
|
|
if (strncasecmp(buf,"help ",5) == 0) {
|
|
|
|
startpos = 5;
|
|
|
|
while (isspace(buf[startpos])) startpos++;
|
2010-11-29 14:26:03 -05:00
|
|
|
mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
|
2010-11-29 13:27:36 -05:00
|
|
|
} else {
|
2010-11-29 14:26:03 -05:00
|
|
|
mask = CLI_HELP_COMMAND;
|
2010-11-29 13:27:36 -05:00
|
|
|
}
|
|
|
|
|
2010-11-29 14:26:03 -05:00
|
|
|
for (i = 0; i < helpEntriesLen; i++) {
|
|
|
|
if (!(helpEntries[i].type & mask)) continue;
|
2010-11-29 13:27:36 -05:00
|
|
|
|
|
|
|
matchlen = strlen(buf+startpos);
|
2010-11-29 14:26:03 -05:00
|
|
|
if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
|
|
|
|
tmp = sdsnewlen(buf,startpos);
|
|
|
|
tmp = sdscat(tmp,helpEntries[i].full);
|
2010-11-29 13:27:36 -05:00
|
|
|
linenoiseAddCompletion(lc,tmp);
|
2010-11-29 14:26:03 -05:00
|
|
|
sdsfree(tmp);
|
2010-11-29 13:27:36 -05:00
|
|
|
}
|
|
|
|
}
|
2010-11-28 15:37:19 -05:00
|
|
|
}
|
|
|
|
|
2010-11-02 13:08:30 -04:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Networking / parsing
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2010-11-03 11:09:38 -04:00
|
|
|
/* Send AUTH command to the server */
|
2014-10-27 05:53:12 -04:00
|
|
|
static int cliAuth(void) {
|
2010-11-03 11:09:38 -04:00
|
|
|
redisReply *reply;
|
|
|
|
if (config.auth == NULL) return REDIS_OK;
|
|
|
|
|
|
|
|
reply = redisCommand(context,"AUTH %s",config.auth);
|
|
|
|
if (reply != NULL) {
|
|
|
|
freeReplyObject(reply);
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send SELECT dbnum to the server */
|
2014-10-27 05:53:12 -04:00
|
|
|
static int cliSelect(void) {
|
2010-11-03 11:09:38 -04:00
|
|
|
redisReply *reply;
|
|
|
|
if (config.dbnum == 0) return REDIS_OK;
|
|
|
|
|
2011-03-06 14:46:40 -05:00
|
|
|
reply = redisCommand(context,"SELECT %d",config.dbnum);
|
2010-11-03 11:09:38 -04:00
|
|
|
if (reply != NULL) {
|
2014-03-28 23:08:56 -04:00
|
|
|
int result = REDIS_OK;
|
|
|
|
if (reply->type == REDIS_REPLY_ERROR) result = REDIS_ERR;
|
2010-11-03 11:09:38 -04:00
|
|
|
freeReplyObject(reply);
|
2014-03-28 23:08:56 -04:00
|
|
|
return result;
|
2010-11-03 11:09:38 -04:00
|
|
|
}
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:00:20 -05:00
|
|
|
/* Connect to the server. If force is not zero the connection is performed
|
2010-08-24 12:39:34 -04:00
|
|
|
* even if there is already a connected socket. */
|
|
|
|
static int cliConnect(int force) {
|
2010-11-03 11:09:38 -04:00
|
|
|
if (context == NULL || force) {
|
|
|
|
if (context != NULL)
|
|
|
|
redisFree(context);
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2010-08-01 17:06:00 -04:00
|
|
|
if (config.hostsocket == NULL) {
|
2010-11-03 11:09:38 -04:00
|
|
|
context = redisConnect(config.hostip,config.hostport);
|
2010-08-01 17:06:00 -04:00
|
|
|
} else {
|
2010-11-03 11:09:38 -04:00
|
|
|
context = redisConnectUnix(config.hostsocket);
|
2010-08-01 17:06:00 -04:00
|
|
|
}
|
2010-11-03 11:09:38 -04:00
|
|
|
|
|
|
|
if (context->err) {
|
2010-08-01 17:06:00 -04:00
|
|
|
fprintf(stderr,"Could not connect to Redis at ");
|
|
|
|
if (config.hostsocket == NULL)
|
2010-11-03 11:09:38 -04:00
|
|
|
fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
|
2010-08-01 17:06:00 -04:00
|
|
|
else
|
2010-11-03 11:09:38 -04:00
|
|
|
fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
|
|
|
|
redisFree(context);
|
|
|
|
context = NULL;
|
|
|
|
return REDIS_ERR;
|
2010-03-02 14:24:21 -05:00
|
|
|
}
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2013-03-04 05:14:32 -05:00
|
|
|
/* Set aggressive KEEP_ALIVE socket option in the Redis context socket
|
|
|
|
* in order to prevent timeouts caused by the execution of long
|
|
|
|
* commands. At the same time this improves the detection of real
|
|
|
|
* errors. */
|
|
|
|
anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);
|
|
|
|
|
2010-11-03 11:09:38 -04:00
|
|
|
/* Do AUTH and select the right DB. */
|
|
|
|
if (cliAuth() != REDIS_OK)
|
|
|
|
return REDIS_ERR;
|
|
|
|
if (cliSelect() != REDIS_OK)
|
|
|
|
return REDIS_ERR;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_OK;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static void cliPrintContextError(void) {
|
2010-11-03 11:09:38 -04:00
|
|
|
if (context == NULL) return;
|
|
|
|
fprintf(stderr,"Error: %s\n",context->errstr);
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
|
|
|
|
2010-12-15 09:59:04 -05:00
|
|
|
static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
|
2010-11-03 11:09:38 -04:00
|
|
|
sds out = sdsempty();
|
|
|
|
switch (r->type) {
|
|
|
|
case REDIS_REPLY_ERROR:
|
2010-12-15 09:59:04 -05:00
|
|
|
out = sdscatprintf(out,"(error) %s\n", r->str);
|
2010-11-03 11:09:38 -04:00
|
|
|
break;
|
|
|
|
case REDIS_REPLY_STATUS:
|
|
|
|
out = sdscat(out,r->str);
|
|
|
|
out = sdscat(out,"\n");
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_INTEGER:
|
2010-12-15 09:59:04 -05:00
|
|
|
out = sdscatprintf(out,"(integer) %lld\n",r->integer);
|
2010-11-03 11:09:38 -04:00
|
|
|
break;
|
|
|
|
case REDIS_REPLY_STRING:
|
2010-12-15 09:59:04 -05:00
|
|
|
/* If you are producing output for the standard output we want
|
|
|
|
* a more interesting output with quoted characters and so forth */
|
|
|
|
out = sdscatrepr(out,r->str,r->len);
|
|
|
|
out = sdscat(out,"\n");
|
2010-11-03 11:09:38 -04:00
|
|
|
break;
|
|
|
|
case REDIS_REPLY_NIL:
|
|
|
|
out = sdscat(out,"(nil)\n");
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_ARRAY:
|
|
|
|
if (r->elements == 0) {
|
|
|
|
out = sdscat(out,"(empty list or set)\n");
|
2010-08-24 12:39:34 -04:00
|
|
|
} else {
|
2010-11-03 12:03:54 -04:00
|
|
|
unsigned int i, idxlen = 0;
|
|
|
|
char _prefixlen[16];
|
|
|
|
char _prefixfmt[16];
|
|
|
|
sds _prefix;
|
2010-11-03 11:09:38 -04:00
|
|
|
sds tmp;
|
|
|
|
|
2010-11-03 12:03:54 -04:00
|
|
|
/* Calculate chars needed to represent the largest index */
|
|
|
|
i = r->elements;
|
|
|
|
do {
|
|
|
|
idxlen++;
|
|
|
|
i /= 10;
|
|
|
|
} while(i);
|
|
|
|
|
|
|
|
/* Prefix for nested multi bulks should grow with idxlen+2 spaces */
|
|
|
|
memset(_prefixlen,' ',idxlen+2);
|
|
|
|
_prefixlen[idxlen+2] = '\0';
|
|
|
|
_prefix = sdscat(sdsnew(prefix),_prefixlen);
|
|
|
|
|
|
|
|
/* Setup prefix format for every entry */
|
|
|
|
snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
|
|
|
|
|
2010-11-03 11:09:38 -04:00
|
|
|
for (i = 0; i < r->elements; i++) {
|
2010-11-03 12:03:54 -04:00
|
|
|
/* Don't use the prefix for the first element, as the parent
|
|
|
|
* caller already prepended the index number. */
|
|
|
|
out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
|
|
|
|
|
|
|
|
/* Format the multi bulk entry */
|
2010-12-15 09:59:04 -05:00
|
|
|
tmp = cliFormatReplyTTY(r->element[i],_prefix);
|
2010-11-03 11:09:38 -04:00
|
|
|
out = sdscatlen(out,tmp,sdslen(tmp));
|
|
|
|
sdsfree(tmp);
|
|
|
|
}
|
2010-11-03 12:03:54 -04:00
|
|
|
sdsfree(_prefix);
|
2010-08-24 12:39:34 -04:00
|
|
|
}
|
2010-11-03 11:09:38 -04:00
|
|
|
break;
|
2009-03-24 08:37:32 -04:00
|
|
|
default:
|
2010-11-03 11:09:38 -04:00
|
|
|
fprintf(stderr,"Unknown reply type: %d\n", r->type);
|
|
|
|
exit(1);
|
2009-03-24 08:37:32 -04:00
|
|
|
}
|
2010-11-03 11:09:38 -04:00
|
|
|
return out;
|
2009-03-24 08:37:32 -04:00
|
|
|
}
|
|
|
|
|
2010-12-15 09:59:04 -05:00
|
|
|
static sds cliFormatReplyRaw(redisReply *r) {
|
|
|
|
sds out = sdsempty(), tmp;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
switch (r->type) {
|
|
|
|
case REDIS_REPLY_NIL:
|
|
|
|
/* Nothing... */
|
2011-03-29 11:51:15 -04:00
|
|
|
break;
|
2010-12-15 09:59:04 -05:00
|
|
|
case REDIS_REPLY_ERROR:
|
2011-03-29 11:51:15 -04:00
|
|
|
out = sdscatlen(out,r->str,r->len);
|
|
|
|
out = sdscatlen(out,"\n",1);
|
|
|
|
break;
|
2010-12-15 09:59:04 -05:00
|
|
|
case REDIS_REPLY_STATUS:
|
|
|
|
case REDIS_REPLY_STRING:
|
|
|
|
out = sdscatlen(out,r->str,r->len);
|
2011-03-29 11:51:15 -04:00
|
|
|
break;
|
2010-12-15 09:59:04 -05:00
|
|
|
case REDIS_REPLY_INTEGER:
|
|
|
|
out = sdscatprintf(out,"%lld",r->integer);
|
2011-03-29 11:51:15 -04:00
|
|
|
break;
|
2010-12-15 09:59:04 -05:00
|
|
|
case REDIS_REPLY_ARRAY:
|
|
|
|
for (i = 0; i < r->elements; i++) {
|
|
|
|
if (i > 0) out = sdscat(out,config.mb_delim);
|
|
|
|
tmp = cliFormatReplyRaw(r->element[i]);
|
|
|
|
out = sdscatlen(out,tmp,sdslen(tmp));
|
|
|
|
sdsfree(tmp);
|
|
|
|
}
|
2011-03-29 11:51:15 -04:00
|
|
|
break;
|
2010-12-15 09:59:04 -05:00
|
|
|
default:
|
|
|
|
fprintf(stderr,"Unknown reply type: %d\n", r->type);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-02-29 11:43:03 -05:00
|
|
|
static sds cliFormatReplyCSV(redisReply *r) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
sds out = sdsempty();
|
|
|
|
switch (r->type) {
|
|
|
|
case REDIS_REPLY_ERROR:
|
|
|
|
out = sdscat(out,"ERROR,");
|
|
|
|
out = sdscatrepr(out,r->str,strlen(r->str));
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_STATUS:
|
|
|
|
out = sdscatrepr(out,r->str,r->len);
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_INTEGER:
|
|
|
|
out = sdscatprintf(out,"%lld",r->integer);
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_STRING:
|
|
|
|
out = sdscatrepr(out,r->str,r->len);
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_NIL:
|
|
|
|
out = sdscat(out,"NIL\n");
|
|
|
|
break;
|
|
|
|
case REDIS_REPLY_ARRAY:
|
|
|
|
for (i = 0; i < r->elements; i++) {
|
|
|
|
sds tmp = cliFormatReplyCSV(r->element[i]);
|
|
|
|
out = sdscatlen(out,tmp,sdslen(tmp));
|
|
|
|
if (i != r->elements-1) out = sdscat(out,",");
|
|
|
|
sdsfree(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,"Unknown reply type: %d\n", r->type);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-12-15 09:59:04 -05:00
|
|
|
static int cliReadReply(int output_raw_strings) {
|
2010-12-23 06:25:56 -05:00
|
|
|
void *_reply;
|
2010-11-03 11:09:38 -04:00
|
|
|
redisReply *reply;
|
2012-03-19 14:29:06 -04:00
|
|
|
sds out = NULL;
|
2011-10-05 13:55:33 -04:00
|
|
|
int output = 1;
|
2010-11-03 11:09:38 -04:00
|
|
|
|
2010-12-23 06:25:56 -05:00
|
|
|
if (redisGetReply(context,&_reply) != REDIS_OK) {
|
2012-12-26 15:00:08 -05:00
|
|
|
if (config.shutdown) {
|
|
|
|
redisFree(context);
|
|
|
|
context = NULL;
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_OK;
|
2012-12-26 15:00:08 -05:00
|
|
|
}
|
2010-11-03 11:09:38 -04:00
|
|
|
if (config.interactive) {
|
|
|
|
/* Filter cases where we should reconnect */
|
2014-10-17 09:21:16 -04:00
|
|
|
if (context->err == REDIS_ERR_IO &&
|
|
|
|
(errno == ECONNRESET || errno == EPIPE))
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_ERR;
|
|
|
|
if (context->err == REDIS_ERR_EOF)
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
2011-05-28 09:04:12 -04:00
|
|
|
cliPrintContextError();
|
|
|
|
exit(1);
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_ERR; /* avoid compiler warning */
|
2009-11-11 23:12:09 -05:00
|
|
|
}
|
2010-11-03 11:09:38 -04:00
|
|
|
|
2010-12-23 06:25:56 -05:00
|
|
|
reply = (redisReply*)_reply;
|
2011-10-05 13:55:33 -04:00
|
|
|
|
2014-08-01 11:44:28 -04:00
|
|
|
config.last_cmd_type = reply->type;
|
|
|
|
|
2012-03-19 14:29:06 -04:00
|
|
|
/* Check if we need to connect to a different node and reissue the
|
|
|
|
* request. */
|
2011-10-05 13:55:33 -04:00
|
|
|
if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR &&
|
|
|
|
(!strncmp(reply->str,"MOVED",5) || !strcmp(reply->str,"ASK")))
|
|
|
|
{
|
|
|
|
char *p = reply->str, *s;
|
|
|
|
int slot;
|
|
|
|
|
|
|
|
output = 0;
|
|
|
|
/* Comments show the position of the pointer as:
|
|
|
|
*
|
|
|
|
* [S] for pointer 's'
|
|
|
|
* [P] for pointer 'p'
|
|
|
|
*/
|
|
|
|
s = strchr(p,' '); /* MOVED[S]3999 127.0.0.1:6381 */
|
|
|
|
p = strchr(s+1,' '); /* MOVED[S]3999[P]127.0.0.1:6381 */
|
|
|
|
*p = '\0';
|
|
|
|
slot = atoi(s+1);
|
|
|
|
s = strchr(p+1,':'); /* MOVED 3999[P]127.0.0.1[S]6381 */
|
|
|
|
*s = '\0';
|
|
|
|
sdsfree(config.hostip);
|
|
|
|
config.hostip = sdsnew(p+1);
|
|
|
|
config.hostport = atoi(s+1);
|
|
|
|
if (config.interactive)
|
|
|
|
printf("-> Redirected to slot [%d] located at %s:%d\n",
|
|
|
|
slot, config.hostip, config.hostport);
|
|
|
|
config.cluster_reissue_command = 1;
|
2013-02-14 12:49:08 -05:00
|
|
|
cliRefreshPrompt();
|
2011-10-05 13:55:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (output) {
|
|
|
|
if (output_raw_strings) {
|
2010-12-15 09:59:04 -05:00
|
|
|
out = cliFormatReplyRaw(reply);
|
|
|
|
} else {
|
2012-02-29 11:43:03 -05:00
|
|
|
if (config.output == OUTPUT_RAW) {
|
2011-10-05 13:55:33 -04:00
|
|
|
out = cliFormatReplyRaw(reply);
|
|
|
|
out = sdscat(out,"\n");
|
2012-02-29 11:43:03 -05:00
|
|
|
} else if (config.output == OUTPUT_STANDARD) {
|
2011-10-05 13:55:33 -04:00
|
|
|
out = cliFormatReplyTTY(reply,"");
|
2012-02-29 11:43:03 -05:00
|
|
|
} else if (config.output == OUTPUT_CSV) {
|
|
|
|
out = cliFormatReplyCSV(reply);
|
|
|
|
out = sdscat(out,"\n");
|
2011-10-05 13:55:33 -04:00
|
|
|
}
|
2010-12-15 09:59:04 -05:00
|
|
|
}
|
2011-10-05 13:55:33 -04:00
|
|
|
fwrite(out,sdslen(out),1,stdout);
|
|
|
|
sdsfree(out);
|
2010-12-15 09:59:04 -05:00
|
|
|
}
|
|
|
|
freeReplyObject(reply);
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_OK;
|
2009-11-11 23:12:09 -05:00
|
|
|
}
|
|
|
|
|
2010-03-23 09:54:49 -04:00
|
|
|
static int cliSendCommand(int argc, char **argv, int repeat) {
|
2010-05-26 12:18:37 -04:00
|
|
|
char *command = argv[0];
|
2010-11-03 11:09:38 -04:00
|
|
|
size_t *argvlen;
|
2010-12-15 09:59:04 -05:00
|
|
|
int j, output_raw;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2012-02-22 09:16:11 -05:00
|
|
|
if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
|
|
|
|
cliOutputHelp(--argc, ++argv);
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-28 09:04:12 -04:00
|
|
|
if (context == NULL) return REDIS_ERR;
|
2010-11-29 06:17:55 -05:00
|
|
|
|
2011-03-29 11:51:15 -04:00
|
|
|
output_raw = 0;
|
|
|
|
if (!strcasecmp(command,"info") ||
|
|
|
|
(argc == 2 && !strcasecmp(command,"cluster") &&
|
|
|
|
(!strcasecmp(argv[1],"nodes") ||
|
2011-04-21 09:38:02 -04:00
|
|
|
!strcasecmp(argv[1],"info"))) ||
|
|
|
|
(argc == 2 && !strcasecmp(command,"client") &&
|
2014-07-02 10:31:22 -04:00
|
|
|
!strcasecmp(argv[1],"list")) ||
|
|
|
|
(argc == 3 && !strcasecmp(command,"latency") &&
|
2014-07-08 05:31:46 -04:00
|
|
|
!strcasecmp(argv[1],"graph")) ||
|
|
|
|
(argc == 2 && !strcasecmp(command,"latency") &&
|
|
|
|
!strcasecmp(argv[1],"doctor")))
|
2011-03-29 11:51:15 -04:00
|
|
|
{
|
|
|
|
output_raw = 1;
|
|
|
|
}
|
|
|
|
|
2010-05-26 12:18:37 -04:00
|
|
|
if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
|
|
|
|
if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
|
|
|
|
if (!strcasecmp(command,"subscribe") ||
|
|
|
|
!strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
|
2014-02-10 11:06:13 -05:00
|
|
|
if (!strcasecmp(command,"sync") ||
|
|
|
|
!strcasecmp(command,"psync")) config.slave_mode = 1;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2010-11-03 11:09:38 -04:00
|
|
|
/* Setup argument length */
|
|
|
|
argvlen = malloc(argc*sizeof(size_t));
|
|
|
|
for (j = 0; j < argc; j++)
|
|
|
|
argvlen[j] = sdslen(argv[j]);
|
2010-05-26 12:22:05 -04:00
|
|
|
|
2010-03-23 09:54:49 -04:00
|
|
|
while(repeat--) {
|
2010-11-03 11:09:38 -04:00
|
|
|
redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
|
2010-04-27 10:07:31 -04:00
|
|
|
while (config.monitor_mode) {
|
2010-12-15 09:59:04 -05:00
|
|
|
if (cliReadReply(output_raw) != REDIS_OK) exit(1);
|
2010-11-08 10:14:15 -05:00
|
|
|
fflush(stdout);
|
2010-01-20 13:38:59 -05:00
|
|
|
}
|
|
|
|
|
2010-04-27 10:07:31 -04:00
|
|
|
if (config.pubsub_mode) {
|
2012-02-29 11:43:03 -05:00
|
|
|
if (config.output != OUTPUT_RAW)
|
2010-12-15 09:59:04 -05:00
|
|
|
printf("Reading messages... (press Ctrl-C to quit)\n");
|
2010-04-27 10:07:31 -04:00
|
|
|
while (1) {
|
2010-12-15 09:59:04 -05:00
|
|
|
if (cliReadReply(output_raw) != REDIS_OK) exit(1);
|
2010-04-27 10:07:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:06:13 -05:00
|
|
|
if (config.slave_mode) {
|
|
|
|
printf("Entering slave output mode... (press Ctrl-C to quit)\n");
|
|
|
|
slaveMode();
|
|
|
|
config.slave_mode = 0;
|
2014-04-04 04:25:40 -04:00
|
|
|
free(argvlen);
|
2014-02-10 11:06:13 -05:00
|
|
|
return REDIS_ERR; /* Error = slaveMode lost connection to master */
|
|
|
|
}
|
|
|
|
|
2011-03-06 14:13:01 -05:00
|
|
|
if (cliReadReply(output_raw) != REDIS_OK) {
|
|
|
|
free(argvlen);
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_ERR;
|
2011-03-06 14:46:40 -05:00
|
|
|
} else {
|
|
|
|
/* Store database number when SELECT was successfully executed. */
|
2011-03-06 15:14:40 -05:00
|
|
|
if (!strcasecmp(command,"select") && argc == 2) {
|
2011-03-06 14:46:40 -05:00
|
|
|
config.dbnum = atoi(argv[1]);
|
2011-03-06 15:14:40 -05:00
|
|
|
cliRefreshPrompt();
|
2014-03-28 23:08:56 -04:00
|
|
|
} else if (!strcasecmp(command,"auth") && argc == 2) {
|
|
|
|
cliSelect();
|
2011-03-06 15:14:40 -05:00
|
|
|
}
|
2011-03-06 14:13:01 -05:00
|
|
|
}
|
2011-05-28 09:41:08 -04:00
|
|
|
if (config.interval) usleep(config.interval);
|
|
|
|
fflush(stdout); /* Make it grep friendly */
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
2011-03-06 14:13:01 -05:00
|
|
|
|
|
|
|
free(argvlen);
|
2010-11-03 11:09:38 -04:00
|
|
|
return REDIS_OK;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
|
|
|
|
2013-03-22 12:54:32 -04:00
|
|
|
/* Send the INFO command, reconnecting the link if needed. */
|
|
|
|
static redisReply *reconnectingInfo(void) {
|
|
|
|
redisContext *c = context;
|
|
|
|
redisReply *reply = NULL;
|
|
|
|
int tries = 0;
|
|
|
|
|
|
|
|
assert(!c->err);
|
|
|
|
while(reply == NULL) {
|
|
|
|
while (c->err & (REDIS_ERR_IO | REDIS_ERR_EOF)) {
|
|
|
|
printf("Reconnecting (%d)...\r", ++tries);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
redisFree(c);
|
|
|
|
c = redisConnect(config.hostip,config.hostport);
|
|
|
|
usleep(1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = redisCommand(c,"INFO");
|
|
|
|
if (c->err && !(c->err & (REDIS_ERR_IO | REDIS_ERR_EOF))) {
|
|
|
|
fprintf(stderr, "Error: %s\n", c->errstr);
|
|
|
|
exit(1);
|
|
|
|
} else if (tries > 0) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
context = c;
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2010-11-02 13:08:30 -04:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* User interface
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
static int parseOptions(int argc, char **argv) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
int lastarg = i==argc-1;
|
2010-03-02 10:14:14 -05:00
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
if (!strcmp(argv[i],"-h") && !lastarg) {
|
2010-11-29 06:17:55 -05:00
|
|
|
sdsfree(config.hostip);
|
2011-12-13 10:22:28 -05:00
|
|
|
config.hostip = sdsnew(argv[++i]);
|
2010-01-15 16:42:29 -05:00
|
|
|
} else if (!strcmp(argv[i],"-h") && lastarg) {
|
|
|
|
usage();
|
2010-12-15 08:58:18 -05:00
|
|
|
} else if (!strcmp(argv[i],"--help")) {
|
|
|
|
usage();
|
2010-09-09 10:38:10 -04:00
|
|
|
} else if (!strcmp(argv[i],"-x")) {
|
|
|
|
config.stdinarg = 1;
|
2009-03-22 05:30:00 -04:00
|
|
|
} else if (!strcmp(argv[i],"-p") && !lastarg) {
|
2011-12-13 10:22:28 -05:00
|
|
|
config.hostport = atoi(argv[++i]);
|
2010-08-01 17:06:00 -04:00
|
|
|
} else if (!strcmp(argv[i],"-s") && !lastarg) {
|
2011-12-13 10:22:28 -05:00
|
|
|
config.hostsocket = argv[++i];
|
2009-11-02 19:35:39 -05:00
|
|
|
} else if (!strcmp(argv[i],"-r") && !lastarg) {
|
2011-12-13 10:22:28 -05:00
|
|
|
config.repeat = strtoll(argv[++i],NULL,10);
|
2011-05-28 09:41:08 -04:00
|
|
|
} else if (!strcmp(argv[i],"-i") && !lastarg) {
|
2011-12-13 10:22:28 -05:00
|
|
|
double seconds = atof(argv[++i]);
|
2011-05-28 09:41:08 -04:00
|
|
|
config.interval = seconds*1000000;
|
2009-11-11 23:12:09 -05:00
|
|
|
} else if (!strcmp(argv[i],"-n") && !lastarg) {
|
2011-12-13 10:22:28 -05:00
|
|
|
config.dbnum = atoi(argv[++i]);
|
2010-03-17 09:41:02 -04:00
|
|
|
} else if (!strcmp(argv[i],"-a") && !lastarg) {
|
2011-12-13 10:22:28 -05:00
|
|
|
config.auth = argv[++i];
|
2010-12-15 09:59:04 -05:00
|
|
|
} else if (!strcmp(argv[i],"--raw")) {
|
2012-02-29 11:43:03 -05:00
|
|
|
config.output = OUTPUT_RAW;
|
2014-08-01 12:05:04 -04:00
|
|
|
} else if (!strcmp(argv[i],"--no-raw")) {
|
|
|
|
config.output = OUTPUT_STANDARD;
|
2012-02-29 11:43:03 -05:00
|
|
|
} else if (!strcmp(argv[i],"--csv")) {
|
|
|
|
config.output = OUTPUT_CSV;
|
2011-09-15 13:28:00 -04:00
|
|
|
} else if (!strcmp(argv[i],"--latency")) {
|
|
|
|
config.latency_mode = 1;
|
2013-04-11 07:11:41 -04:00
|
|
|
} else if (!strcmp(argv[i],"--latency-history")) {
|
|
|
|
config.latency_mode = 1;
|
|
|
|
config.latency_history = 1;
|
2012-02-29 11:10:21 -05:00
|
|
|
} else if (!strcmp(argv[i],"--slave")) {
|
|
|
|
config.slave_mode = 1;
|
2013-03-22 12:54:32 -04:00
|
|
|
} else if (!strcmp(argv[i],"--stat")) {
|
|
|
|
config.stat_mode = 1;
|
2014-01-22 06:04:08 -05:00
|
|
|
} else if (!strcmp(argv[i],"--scan")) {
|
|
|
|
config.scan_mode = 1;
|
2014-02-25 06:38:24 -05:00
|
|
|
} else if (!strcmp(argv[i],"--pattern") && !lastarg) {
|
2014-01-22 06:04:08 -05:00
|
|
|
config.pattern = argv[++i];
|
2014-02-25 06:37:52 -05:00
|
|
|
} else if (!strcmp(argv[i],"--intrinsic-latency") && !lastarg) {
|
|
|
|
config.intrinsic_latency_mode = 1;
|
|
|
|
config.intrinsic_latency_duration = atoi(argv[++i]);
|
2013-01-16 13:42:40 -05:00
|
|
|
} else if (!strcmp(argv[i],"--rdb") && !lastarg) {
|
|
|
|
config.getrdb_mode = 1;
|
|
|
|
config.rdb_filename = argv[++i];
|
2012-05-06 04:05:31 -04:00
|
|
|
} else if (!strcmp(argv[i],"--pipe")) {
|
|
|
|
config.pipe_mode = 1;
|
2013-07-03 06:18:55 -04:00
|
|
|
} else if (!strcmp(argv[i],"--pipe-timeout") && !lastarg) {
|
|
|
|
config.pipe_timeout = atoi(argv[++i]);
|
2012-04-18 14:33:02 -04:00
|
|
|
} else if (!strcmp(argv[i],"--bigkeys")) {
|
|
|
|
config.bigkeys = 1;
|
2011-12-13 10:22:28 -05:00
|
|
|
} else if (!strcmp(argv[i],"--eval") && !lastarg) {
|
|
|
|
config.eval = argv[++i];
|
2011-10-05 13:55:33 -04:00
|
|
|
} else if (!strcmp(argv[i],"-c")) {
|
|
|
|
config.cluster_mode = 1;
|
2010-12-15 10:02:07 -05:00
|
|
|
} else if (!strcmp(argv[i],"-d") && !lastarg) {
|
|
|
|
sdsfree(config.mb_delim);
|
2011-12-13 10:22:28 -05:00
|
|
|
config.mb_delim = sdsnew(argv[++i]);
|
2010-12-15 08:58:18 -05:00
|
|
|
} else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
|
|
|
|
sds version = cliVersion();
|
|
|
|
printf("redis-cli %s\n", version);
|
|
|
|
sdsfree(version);
|
2010-07-06 13:17:09 -04:00
|
|
|
exit(0);
|
2009-03-22 05:30:00 -04:00
|
|
|
} else {
|
2013-04-11 07:15:20 -04:00
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Unrecognized option or bad number of args for: '%s'\n",
|
|
|
|
argv[i]);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
/* Likely the command name, stop here. */
|
|
|
|
break;
|
|
|
|
}
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sds readArgFromStdin(void) {
|
|
|
|
char buf[1024];
|
|
|
|
sds arg = sdsempty();
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
int nread = read(fileno(stdin),buf,1024);
|
|
|
|
|
|
|
|
if (nread == 0) break;
|
|
|
|
else if (nread == -1) {
|
|
|
|
perror("Reading from standard input");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
arg = sdscatlen(arg,buf,nread);
|
|
|
|
}
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static void usage(void) {
|
2010-12-15 08:58:18 -05:00
|
|
|
sds version = cliVersion();
|
|
|
|
fprintf(stderr,
|
|
|
|
"redis-cli %s\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" -h <hostname> Server hostname (default: 127.0.0.1).\n"
|
|
|
|
" -p <port> Server port (default: 6379).\n"
|
|
|
|
" -s <socket> Server socket (overrides hostname and port).\n"
|
|
|
|
" -a <password> Password to use when connecting to the server.\n"
|
|
|
|
" -r <repeat> Execute specified command N times.\n"
|
2013-07-03 06:18:55 -04:00
|
|
|
" -i <interval> When -r is used, waits <interval> seconds per command.\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" It is possible to specify sub-second times like -i 0.1.\n"
|
|
|
|
" -n <db> Database number.\n"
|
|
|
|
" -x Read last argument from STDIN.\n"
|
|
|
|
" -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n).\n"
|
|
|
|
" -c Enable cluster mode (follow -ASK and -MOVED redirections).\n"
|
2013-07-03 06:18:55 -04:00
|
|
|
" --raw Use raw formatting for replies (default when STDOUT is\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" not a tty).\n"
|
2014-08-01 12:05:04 -04:00
|
|
|
" --no-raw Force formatted output even when STDOUT is not a tty.\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" --csv Output in CSV format.\n"
|
2014-11-25 12:23:34 -05:00
|
|
|
" --stat Print rolling stats about server: mem, clients, ...\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" --latency Enter a special mode continuously sampling latency.\n"
|
2013-07-03 06:18:55 -04:00
|
|
|
" --latency-history Like --latency but tracking latency changes over time.\n"
|
|
|
|
" Default time interval is 15 sec. Change it using -i.\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" --slave Simulate a slave showing commands received from the master.\n"
|
2013-07-03 06:18:55 -04:00
|
|
|
" --rdb <filename> Transfer an RDB dump from remote server to local file.\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" --pipe Transfer raw Redis protocol from stdin to server.\n"
|
|
|
|
" --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.\n"
|
2013-07-03 06:18:55 -04:00
|
|
|
" no reply is received within <n> seconds.\n"
|
|
|
|
" Default timeout: %d. Use 0 to wait forever.\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" --bigkeys Sample Redis keys looking for big keys.\n"
|
|
|
|
" --scan List all keys using the SCAN command.\n"
|
|
|
|
" --pattern <pat> Useful with --scan to specify a SCAN pattern.\n"
|
2014-02-25 06:37:52 -05:00
|
|
|
" --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n"
|
|
|
|
" The test will run for the specified amount of seconds.\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" --eval <file> Send an EVAL command using the Lua script at <file>.\n"
|
|
|
|
" --help Output this help and exit.\n"
|
|
|
|
" --version Output version and exit.\n"
|
2010-12-15 08:58:18 -05:00
|
|
|
"\n"
|
|
|
|
"Examples:\n"
|
|
|
|
" cat /etc/passwd | redis-cli -x set mypasswd\n"
|
|
|
|
" redis-cli get mypasswd\n"
|
|
|
|
" redis-cli -r 100 lpush mylist x\n"
|
2011-05-28 09:41:08 -04:00
|
|
|
" redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
|
2011-12-13 10:22:28 -05:00
|
|
|
" redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n"
|
2014-01-22 06:07:42 -05:00
|
|
|
" redis-cli --scan --pattern '*:12345*'\n"
|
|
|
|
"\n"
|
2011-12-13 10:22:28 -05:00
|
|
|
" (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n"
|
2010-12-15 08:58:18 -05:00
|
|
|
"\n"
|
|
|
|
"When no command is given, redis-cli starts in interactive mode.\n"
|
|
|
|
"Type \"help\" in interactive mode for information on available commands.\n"
|
|
|
|
"\n",
|
2014-02-25 06:37:52 -05:00
|
|
|
version, REDIS_CLI_DEFAULT_PIPE_TIMEOUT);
|
2010-12-15 08:58:18 -05:00
|
|
|
sdsfree(version);
|
2010-01-15 16:42:29 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-03-02 10:14:14 -05:00
|
|
|
/* Turn the plain C strings into Sds strings */
|
|
|
|
static char **convertToSds(int count, char** args) {
|
|
|
|
int j;
|
2010-05-26 12:18:37 -04:00
|
|
|
char **sds = zmalloc(sizeof(char*)*count);
|
2010-03-02 10:14:14 -05:00
|
|
|
|
|
|
|
for(j = 0; j < count; j++)
|
|
|
|
sds[j] = sdsnew(args[j]);
|
|
|
|
|
|
|
|
return sds;
|
|
|
|
}
|
|
|
|
|
2014-10-30 15:05:50 -04:00
|
|
|
static int issueCommandRepeat(int argc, char **argv, long repeat) {
|
|
|
|
while (1) {
|
|
|
|
config.cluster_reissue_command = 0;
|
|
|
|
if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
|
|
|
|
cliConnect(1);
|
|
|
|
|
|
|
|
/* If we still cannot send the command print error.
|
|
|
|
* We'll try to reconnect the next time. */
|
|
|
|
if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
|
|
|
|
cliPrintContextError();
|
|
|
|
return REDIS_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Issue the command again if we got redirected in cluster mode */
|
|
|
|
if (config.cluster_mode && config.cluster_reissue_command) {
|
|
|
|
cliConnect(1);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return REDIS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int issueCommand(int argc, char **argv) {
|
|
|
|
return issueCommandRepeat(argc, argv, config.repeat);
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static void repl(void) {
|
2011-03-06 14:00:08 -05:00
|
|
|
sds historyfile = NULL;
|
|
|
|
int history = 0;
|
2010-08-05 05:36:39 -04:00
|
|
|
char *line;
|
2011-03-06 14:00:08 -05:00
|
|
|
int argc;
|
2010-08-05 05:36:39 -04:00
|
|
|
sds *argv;
|
2010-03-02 10:14:14 -05:00
|
|
|
|
2010-08-25 04:05:50 -04:00
|
|
|
config.interactive = 1;
|
2014-03-13 10:11:08 -04:00
|
|
|
linenoiseSetMultiLine(1);
|
2010-11-29 13:27:36 -05:00
|
|
|
linenoiseSetCompletionCallback(completionCallback);
|
2010-11-30 05:39:55 -05:00
|
|
|
|
2011-03-06 14:00:08 -05:00
|
|
|
/* Only use history when stdin is a tty. */
|
|
|
|
if (isatty(fileno(stdin))) {
|
2014-11-11 01:40:25 -05:00
|
|
|
historyfile = getHistoryPath();
|
|
|
|
if (historyfile != NULL) {
|
|
|
|
history = 1;
|
2011-03-06 14:00:08 -05:00
|
|
|
linenoiseHistoryLoad(historyfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-06 15:14:40 -05:00
|
|
|
cliRefreshPrompt();
|
|
|
|
while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
|
2010-03-23 10:25:32 -04:00
|
|
|
if (line[0] != '\0') {
|
2010-08-05 05:36:39 -04:00
|
|
|
argv = sdssplitargs(line,&argc);
|
2011-03-06 14:00:08 -05:00
|
|
|
if (history) linenoiseHistoryAdd(line);
|
|
|
|
if (historyfile) linenoiseHistorySave(historyfile);
|
|
|
|
|
2010-08-04 09:29:28 -04:00
|
|
|
if (argv == NULL) {
|
|
|
|
printf("Invalid argument(s)\n");
|
2011-10-28 11:43:04 -04:00
|
|
|
free(line);
|
2010-08-04 09:29:28 -04:00
|
|
|
continue;
|
|
|
|
} else if (argc > 0) {
|
2010-04-26 12:39:39 -04:00
|
|
|
if (strcasecmp(argv[0],"quit") == 0 ||
|
|
|
|
strcasecmp(argv[0],"exit") == 0)
|
2010-08-24 12:39:34 -04:00
|
|
|
{
|
|
|
|
exit(0);
|
2010-11-29 06:17:55 -05:00
|
|
|
} else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
|
|
|
|
sdsfree(config.hostip);
|
|
|
|
config.hostip = sdsnew(argv[1]);
|
|
|
|
config.hostport = atoi(argv[2]);
|
2013-01-16 20:20:54 -05:00
|
|
|
cliRefreshPrompt();
|
2010-11-29 06:17:55 -05:00
|
|
|
cliConnect(1);
|
2010-12-01 05:18:59 -05:00
|
|
|
} else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
|
|
|
|
linenoiseClearScreen();
|
2010-08-24 12:39:34 -04:00
|
|
|
} else {
|
2010-11-02 13:08:30 -04:00
|
|
|
long long start_time = mstime(), elapsed;
|
2011-05-28 09:13:55 -04:00
|
|
|
int repeat, skipargs = 0;
|
2010-08-24 12:39:34 -04:00
|
|
|
|
2011-05-28 09:13:55 -04:00
|
|
|
repeat = atoi(argv[0]);
|
2011-09-21 16:22:14 -04:00
|
|
|
if (argc > 1 && repeat) {
|
2011-05-28 09:13:55 -04:00
|
|
|
skipargs = 1;
|
|
|
|
} else {
|
|
|
|
repeat = 1;
|
|
|
|
}
|
|
|
|
|
2014-10-30 15:05:50 -04:00
|
|
|
issueCommandRepeat(argc-skipargs, argv+skipargs, repeat);
|
|
|
|
|
2010-11-02 13:08:30 -04:00
|
|
|
elapsed = mstime()-start_time;
|
2010-11-03 12:07:10 -04:00
|
|
|
if (elapsed >= 500) {
|
|
|
|
printf("(%.2fs)\n",(double)elapsed/1000);
|
|
|
|
}
|
2010-08-24 12:39:34 -04:00
|
|
|
}
|
2010-04-26 12:39:39 -04:00
|
|
|
}
|
|
|
|
/* Free the argument vector */
|
2013-03-06 06:38:32 -05:00
|
|
|
sdsfreesplitres(argv,argc);
|
2010-03-02 10:14:14 -05:00
|
|
|
}
|
2010-04-26 12:39:39 -04:00
|
|
|
/* linenoise() returns malloc-ed lines like readline() */
|
2010-03-23 10:25:32 -04:00
|
|
|
free(line);
|
2010-03-02 10:14:14 -05:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2010-08-25 08:48:50 -04:00
|
|
|
static int noninteractive(int argc, char **argv) {
|
|
|
|
int retval = 0;
|
2010-09-09 10:38:10 -04:00
|
|
|
if (config.stdinarg) {
|
2010-08-25 08:48:50 -04:00
|
|
|
argv = zrealloc(argv, (argc+1)*sizeof(char*));
|
|
|
|
argv[argc] = readArgFromStdin();
|
2014-10-30 15:05:50 -04:00
|
|
|
retval = issueCommand(argc+1, argv);
|
2010-08-25 08:48:50 -04:00
|
|
|
} else {
|
2014-10-30 15:05:50 -04:00
|
|
|
retval = issueCommand(argc, argv);
|
2010-08-25 08:48:50 -04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Eval mode
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2011-12-13 10:22:28 -05:00
|
|
|
static int evalMode(int argc, char **argv) {
|
|
|
|
sds script = sdsempty();
|
|
|
|
FILE *fp;
|
|
|
|
char buf[1024];
|
|
|
|
size_t nread;
|
|
|
|
char **argv2;
|
|
|
|
int j, got_comma = 0, keys = 0;
|
|
|
|
|
|
|
|
/* Load the script from the file, as an sds string. */
|
|
|
|
fp = fopen(config.eval,"r");
|
|
|
|
if (!fp) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Can't open file '%s': %s\n", config.eval, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while((nread = fread(buf,1,sizeof(buf),fp)) != 0) {
|
|
|
|
script = sdscatlen(script,buf,nread);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
/* Create our argument vector */
|
|
|
|
argv2 = zmalloc(sizeof(sds)*(argc+3));
|
|
|
|
argv2[0] = sdsnew("EVAL");
|
|
|
|
argv2[1] = script;
|
|
|
|
for (j = 0; j < argc; j++) {
|
|
|
|
if (!got_comma && argv[j][0] == ',' && argv[j][1] == 0) {
|
|
|
|
got_comma = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
argv2[j+3-got_comma] = sdsnew(argv[j]);
|
|
|
|
if (!got_comma) keys++;
|
|
|
|
}
|
|
|
|
argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
|
|
|
|
|
|
|
|
/* Call it */
|
2014-10-30 15:05:50 -04:00
|
|
|
return issueCommand(argc+3-got_comma, argv2);
|
2011-12-13 10:22:28 -05:00
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Latency and latency history modes
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2013-04-11 07:11:41 -04:00
|
|
|
#define LATENCY_SAMPLE_RATE 10 /* milliseconds. */
|
|
|
|
#define LATENCY_HISTORY_DEFAULT_INTERVAL 15000 /* milliseconds. */
|
2011-09-15 13:28:00 -04:00
|
|
|
static void latencyMode(void) {
|
|
|
|
redisReply *reply;
|
2012-04-24 05:11:55 -04:00
|
|
|
long long start, latency, min = 0, max = 0, tot = 0, count = 0;
|
2013-04-11 07:11:41 -04:00
|
|
|
long long history_interval =
|
|
|
|
config.interval ? config.interval/1000 :
|
|
|
|
LATENCY_HISTORY_DEFAULT_INTERVAL;
|
2011-09-15 13:28:00 -04:00
|
|
|
double avg;
|
2013-04-11 07:11:41 -04:00
|
|
|
long long history_start = mstime();
|
2011-09-15 13:28:00 -04:00
|
|
|
|
|
|
|
if (!context) exit(1);
|
|
|
|
while(1) {
|
|
|
|
start = mstime();
|
|
|
|
reply = redisCommand(context,"PING");
|
|
|
|
if (reply == NULL) {
|
|
|
|
fprintf(stderr,"\nI/O error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
latency = mstime()-start;
|
|
|
|
freeReplyObject(reply);
|
|
|
|
count++;
|
|
|
|
if (count == 1) {
|
|
|
|
min = max = tot = latency;
|
|
|
|
avg = (double) latency;
|
|
|
|
} else {
|
|
|
|
if (latency < min) min = latency;
|
|
|
|
if (latency > max) max = latency;
|
2011-09-15 13:32:25 -04:00
|
|
|
tot += latency;
|
2011-09-15 13:28:00 -04:00
|
|
|
avg = (double) tot/count;
|
|
|
|
}
|
|
|
|
printf("\x1b[0G\x1b[2Kmin: %lld, max: %lld, avg: %.2f (%lld samples)",
|
|
|
|
min, max, avg, count);
|
|
|
|
fflush(stdout);
|
2013-04-11 07:11:41 -04:00
|
|
|
if (config.latency_history && mstime()-history_start > history_interval)
|
|
|
|
{
|
|
|
|
printf(" -- %.2f seconds range\n", (float)(mstime()-history_start)/1000);
|
|
|
|
history_start = mstime();
|
|
|
|
min = max = tot = count = 0;
|
|
|
|
}
|
|
|
|
usleep(LATENCY_SAMPLE_RATE * 1000);
|
2011-09-15 13:28:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Slave mode
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2013-01-16 13:42:40 -05:00
|
|
|
/* Sends SYNC and reads the number of bytes in the payload. Used both by
|
|
|
|
* slaveMode() and getRDB(). */
|
|
|
|
unsigned long long sendSync(int fd) {
|
2012-02-29 11:10:21 -05:00
|
|
|
/* To start we need to send the SYNC command and return the payload.
|
|
|
|
* The hiredis client lib does not understand this part of the protocol
|
|
|
|
* and we don't want to mess with its buffers, so everything is performed
|
|
|
|
* using direct low-level I/O. */
|
2013-01-16 13:42:40 -05:00
|
|
|
char buf[4096], *p;
|
2012-02-29 11:10:21 -05:00
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* Send the SYNC command. */
|
2012-03-19 14:29:06 -04:00
|
|
|
if (write(fd,"SYNC\r\n",6) != 6) {
|
|
|
|
fprintf(stderr,"Error writing to master\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-02-29 11:10:21 -05:00
|
|
|
|
|
|
|
/* Read $<payload>\r\n, making sure to read just up to "\n" */
|
|
|
|
p = buf;
|
|
|
|
while(1) {
|
|
|
|
nread = read(fd,p,1);
|
|
|
|
if (nread <= 0) {
|
|
|
|
fprintf(stderr,"Error reading bulk length while SYNCing\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-01-17 23:55:43 -05:00
|
|
|
if (*p == '\n' && p != buf) break;
|
|
|
|
if (*p != '\n') p++;
|
2012-02-29 11:10:21 -05:00
|
|
|
}
|
|
|
|
*p = '\0';
|
2013-01-16 13:42:40 -05:00
|
|
|
if (buf[0] == '-') {
|
|
|
|
printf("SYNC with master failed: %s\n", buf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return strtoull(buf+1,NULL,10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void slaveMode(void) {
|
|
|
|
int fd = context->fd;
|
|
|
|
unsigned long long payload = sendSync(fd);
|
|
|
|
char buf[1024];
|
2014-02-10 11:06:13 -05:00
|
|
|
int original_output = config.output;
|
2013-01-16 13:42:40 -05:00
|
|
|
|
|
|
|
fprintf(stderr,"SYNC with master, discarding %llu "
|
2013-01-16 12:00:20 -05:00
|
|
|
"bytes of bulk transfer...\n", payload);
|
2012-02-29 11:10:21 -05:00
|
|
|
|
|
|
|
/* Discard the payload. */
|
|
|
|
while(payload) {
|
2013-01-16 13:42:40 -05:00
|
|
|
ssize_t nread;
|
|
|
|
|
2012-02-29 11:10:21 -05:00
|
|
|
nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
|
|
|
|
if (nread <= 0) {
|
|
|
|
fprintf(stderr,"Error reading RDB payload while SYNCing\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
payload -= nread;
|
|
|
|
}
|
2012-02-29 11:43:03 -05:00
|
|
|
fprintf(stderr,"SYNC done. Logging commands from master.\n");
|
2012-02-29 11:10:21 -05:00
|
|
|
|
2013-01-16 13:42:40 -05:00
|
|
|
/* Now we can use hiredis to read the incoming protocol. */
|
2012-02-29 11:43:03 -05:00
|
|
|
config.output = OUTPUT_CSV;
|
|
|
|
while (cliReadReply(0) == REDIS_OK);
|
2014-02-10 11:06:13 -05:00
|
|
|
config.output = original_output;
|
2012-02-29 11:10:21 -05:00
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* RDB transfer mode
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2013-01-16 13:42:40 -05:00
|
|
|
/* This function implements --rdb, so it uses the replication protocol in order
|
|
|
|
* to fetch the RDB file from a remote server. */
|
|
|
|
static void getRDB(void) {
|
|
|
|
int s = context->fd;
|
|
|
|
int fd;
|
|
|
|
unsigned long long payload = sendSync(s);
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
fprintf(stderr,"SYNC sent to master, writing %llu bytes to '%s'\n",
|
|
|
|
payload, config.rdb_filename);
|
|
|
|
|
|
|
|
/* Write to file. */
|
|
|
|
if (!strcmp(config.rdb_filename,"-")) {
|
|
|
|
fd = STDOUT_FILENO;
|
|
|
|
} else {
|
|
|
|
fd = open(config.rdb_filename, O_CREAT|O_WRONLY, 0644);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening '%s': %s\n", config.rdb_filename,
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(payload) {
|
|
|
|
ssize_t nread, nwritten;
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2013-01-16 13:42:40 -05:00
|
|
|
nread = read(s,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
|
|
|
|
if (nread <= 0) {
|
|
|
|
fprintf(stderr,"I/O Error reading RDB payload from socket\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
nwritten = write(fd, buf, nread);
|
|
|
|
if (nwritten != nread) {
|
|
|
|
fprintf(stderr,"Error writing data to file: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
payload -= nread;
|
|
|
|
}
|
|
|
|
close(s); /* Close the file descriptor ASAP as fsync() may take time. */
|
|
|
|
fsync(fd);
|
|
|
|
fprintf(stderr,"Transfer finished with success.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Bulk import (pipe) mode
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2012-05-06 04:05:31 -04:00
|
|
|
static void pipeMode(void) {
|
|
|
|
int fd = context->fd;
|
|
|
|
long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;
|
|
|
|
char ibuf[1024*16], obuf[1024*16]; /* Input and output buffers */
|
|
|
|
char aneterr[ANET_ERR_LEN];
|
|
|
|
redisReader *reader = redisReaderCreate();
|
|
|
|
redisReply *reply;
|
|
|
|
int eof = 0; /* True once we consumed all the standard input. */
|
|
|
|
int done = 0;
|
|
|
|
char magic[20]; /* Special reply we recognize. */
|
2013-07-03 06:18:55 -04:00
|
|
|
time_t last_read_time = time(NULL);
|
2012-05-06 04:05:31 -04:00
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
/* Use non blocking I/O. */
|
|
|
|
if (anetNonBlock(aneterr,fd) == ANET_ERR) {
|
|
|
|
fprintf(stderr, "Can't set the socket in non blocking mode: %s\n",
|
|
|
|
aneterr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer raw protocol and read replies from the server at the same
|
|
|
|
* time. */
|
|
|
|
while(!done) {
|
|
|
|
int mask = AE_READABLE;
|
|
|
|
|
|
|
|
if (!eof || obuf_len != 0) mask |= AE_WRITABLE;
|
|
|
|
mask = aeWait(fd,mask,1000);
|
|
|
|
|
|
|
|
/* Handle the readable state: we can read replies from the server. */
|
|
|
|
if (mask & AE_READABLE) {
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* Read from socket and feed the hiredis reader. */
|
|
|
|
do {
|
|
|
|
nread = read(fd,ibuf,sizeof(ibuf));
|
2012-05-11 10:08:57 -04:00
|
|
|
if (nread == -1 && errno != EAGAIN && errno != EINTR) {
|
2012-05-06 04:05:31 -04:00
|
|
|
fprintf(stderr, "Error reading from the server: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-03 06:18:55 -04:00
|
|
|
if (nread > 0) {
|
|
|
|
redisReaderFeed(reader,ibuf,nread);
|
|
|
|
last_read_time = time(NULL);
|
|
|
|
}
|
2012-05-06 04:05:31 -04:00
|
|
|
} while(nread > 0);
|
|
|
|
|
|
|
|
/* Consume replies. */
|
|
|
|
do {
|
|
|
|
if (redisReaderGetReply(reader,(void**)&reply) == REDIS_ERR) {
|
|
|
|
fprintf(stderr, "Error reading replies from server\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (reply) {
|
|
|
|
if (reply->type == REDIS_REPLY_ERROR) {
|
|
|
|
fprintf(stderr,"%s\n", reply->str);
|
|
|
|
errors++;
|
|
|
|
} else if (eof && reply->type == REDIS_REPLY_STRING &&
|
|
|
|
reply->len == 20) {
|
2014-06-26 12:48:40 -04:00
|
|
|
/* Check if this is the reply to our final ECHO
|
2012-05-06 04:05:31 -04:00
|
|
|
* command. If so everything was received
|
|
|
|
* from the server. */
|
|
|
|
if (memcmp(reply->str,magic,20) == 0) {
|
|
|
|
printf("Last reply received from server.\n");
|
|
|
|
done = 1;
|
|
|
|
replies--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
replies++;
|
|
|
|
freeReplyObject(reply);
|
|
|
|
}
|
|
|
|
} while(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the writable state: we can send protocol to the server. */
|
|
|
|
if (mask & AE_WRITABLE) {
|
|
|
|
while(1) {
|
|
|
|
/* Transfer current buffer to server. */
|
|
|
|
if (obuf_len != 0) {
|
|
|
|
ssize_t nwritten = write(fd,obuf+obuf_pos,obuf_len);
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2012-05-06 04:05:31 -04:00
|
|
|
if (nwritten == -1) {
|
2012-05-11 10:08:57 -04:00
|
|
|
if (errno != EAGAIN && errno != EINTR) {
|
2012-05-11 04:45:12 -04:00
|
|
|
fprintf(stderr, "Error writing to the server: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
nwritten = 0;
|
|
|
|
}
|
2012-05-06 04:05:31 -04:00
|
|
|
}
|
|
|
|
obuf_len -= nwritten;
|
|
|
|
obuf_pos += nwritten;
|
|
|
|
if (obuf_len != 0) break; /* Can't accept more data. */
|
|
|
|
}
|
|
|
|
/* If buffer is empty, load from stdin. */
|
|
|
|
if (obuf_len == 0 && !eof) {
|
|
|
|
ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf));
|
|
|
|
|
|
|
|
if (nread == 0) {
|
2013-07-03 05:59:44 -04:00
|
|
|
/* The ECHO sequence starts with a "\r\n" so that if there
|
|
|
|
* is garbage in the protocol we read from stdin, the ECHO
|
|
|
|
* will likely still be properly formatted.
|
|
|
|
* CRLF is ignored by Redis, so it has no effects. */
|
2012-05-06 04:05:31 -04:00
|
|
|
char echo[] =
|
2013-07-03 05:59:44 -04:00
|
|
|
"\r\n*2\r\n$4\r\nECHO\r\n$20\r\n01234567890123456789\r\n";
|
2012-05-06 04:05:31 -04:00
|
|
|
int j;
|
|
|
|
|
|
|
|
eof = 1;
|
2013-01-16 12:00:20 -05:00
|
|
|
/* Everything transferred, so we queue a special
|
2012-05-06 04:05:31 -04:00
|
|
|
* ECHO command that we can match in the replies
|
|
|
|
* to make sure everything was read from the server. */
|
|
|
|
for (j = 0; j < 20; j++)
|
|
|
|
magic[j] = rand() & 0xff;
|
2013-07-03 05:59:44 -04:00
|
|
|
memcpy(echo+21,magic,20);
|
2012-05-06 04:05:31 -04:00
|
|
|
memcpy(obuf,echo,sizeof(echo)-1);
|
|
|
|
obuf_len = sizeof(echo)-1;
|
|
|
|
obuf_pos = 0;
|
|
|
|
printf("All data transferred. Waiting for the last reply...\n");
|
|
|
|
} else if (nread == -1) {
|
|
|
|
fprintf(stderr, "Error reading from stdin: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
obuf_len = nread;
|
|
|
|
obuf_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (obuf_len == 0 && eof) break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 06:18:55 -04:00
|
|
|
|
|
|
|
/* Handle timeout, that is, we reached EOF, and we are not getting
|
|
|
|
* replies from the server for a few seconds, nor the final ECHO is
|
|
|
|
* received. */
|
|
|
|
if (eof && config.pipe_timeout > 0 &&
|
|
|
|
time(NULL)-last_read_time > config.pipe_timeout)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"No replies for %d seconds: exiting.\n",
|
|
|
|
config.pipe_timeout);
|
|
|
|
errors++;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-06 04:05:31 -04:00
|
|
|
}
|
|
|
|
redisReaderFree(reader);
|
|
|
|
printf("errors: %lld, replies: %lld\n", errors, replies);
|
|
|
|
if (errors)
|
|
|
|
exit(1);
|
|
|
|
else
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Find big keys
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2012-04-18 14:33:02 -04:00
|
|
|
#define TYPE_STRING 0
|
|
|
|
#define TYPE_LIST 1
|
|
|
|
#define TYPE_SET 2
|
|
|
|
#define TYPE_HASH 3
|
|
|
|
#define TYPE_ZSET 4
|
2014-02-27 15:01:57 -05:00
|
|
|
#define TYPE_NONE 5
|
2012-04-18 14:33:02 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
static redisReply *sendScan(unsigned long long *it) {
|
|
|
|
redisReply *reply = redisCommand(context, "SCAN %llu", *it);
|
2012-04-18 14:33:02 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Handle any error conditions */
|
|
|
|
if(reply == NULL) {
|
|
|
|
fprintf(stderr, "\nI/O error\n");
|
|
|
|
exit(1);
|
|
|
|
} else if(reply->type == REDIS_REPLY_ERROR) {
|
|
|
|
fprintf(stderr, "SCAN error: %s\n", reply->str);
|
|
|
|
exit(1);
|
|
|
|
} else if(reply->type != REDIS_REPLY_ARRAY) {
|
|
|
|
fprintf(stderr, "Non ARRAY response from SCAN!\n");
|
|
|
|
exit(1);
|
|
|
|
} else if(reply->elements != 2) {
|
|
|
|
fprintf(stderr, "Invalid element count from SCAN!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-02-25 08:41:30 -05:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Validate our types are correct */
|
|
|
|
assert(reply->element[0]->type == REDIS_REPLY_STRING);
|
|
|
|
assert(reply->element[1]->type == REDIS_REPLY_ARRAY);
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Update iterator */
|
|
|
|
*it = atoi(reply->element[0]->str);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getDbSize(void) {
|
|
|
|
redisReply *reply;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
reply = redisCommand(context, "DBSIZE");
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
if(reply == NULL || reply->type != REDIS_REPLY_INTEGER) {
|
|
|
|
fprintf(stderr, "Couldn't determine DBSIZE!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab the number of keys and free our reply */
|
|
|
|
size = reply->integer;
|
|
|
|
freeReplyObject(reply);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int toIntType(char *key, char *type) {
|
|
|
|
if(!strcmp(type, "string")) {
|
|
|
|
return TYPE_STRING;
|
|
|
|
} else if(!strcmp(type, "list")) {
|
|
|
|
return TYPE_LIST;
|
|
|
|
} else if(!strcmp(type, "set")) {
|
|
|
|
return TYPE_SET;
|
|
|
|
} else if(!strcmp(type, "hash")) {
|
|
|
|
return TYPE_HASH;
|
|
|
|
} else if(!strcmp(type, "zset")) {
|
|
|
|
return TYPE_ZSET;
|
|
|
|
} else if(!strcmp(type, "none")) {
|
|
|
|
return TYPE_NONE;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown type '%s' for key '%s'\n", type, key);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getKeyTypes(redisReply *keys, int *types) {
|
|
|
|
redisReply *reply;
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned int i;
|
2014-02-27 15:01:57 -05:00
|
|
|
|
|
|
|
/* Pipeline TYPE commands */
|
|
|
|
for(i=0;i<keys->elements;i++) {
|
|
|
|
redisAppendCommand(context, "TYPE %s", keys->element[i]->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve types */
|
|
|
|
for(i=0;i<keys->elements;i++) {
|
|
|
|
if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
|
|
|
|
fprintf(stderr, "Error getting type for key '%s' (%d: %s)\n",
|
|
|
|
keys->element[i]->str, context->err, context->errstr);
|
2012-04-18 14:33:02 -04:00
|
|
|
exit(1);
|
2014-02-27 15:01:57 -05:00
|
|
|
} else if(reply->type != REDIS_REPLY_STATUS) {
|
|
|
|
fprintf(stderr, "Invalid reply type (%d) for TYPE on key '%s'!\n",
|
|
|
|
reply->type, keys->element[i]->str);
|
2012-04-18 14:33:02 -04:00
|
|
|
exit(1);
|
2014-02-27 15:01:57 -05:00
|
|
|
}
|
|
|
|
|
2014-06-26 12:48:40 -04:00
|
|
|
types[i] = toIntType(keys->element[i]->str, reply->str);
|
2014-02-27 15:01:57 -05:00
|
|
|
freeReplyObject(reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 12:48:40 -04:00
|
|
|
static void getKeySizes(redisReply *keys, int *types,
|
|
|
|
unsigned long long *sizes)
|
2014-02-27 15:01:57 -05:00
|
|
|
{
|
|
|
|
redisReply *reply;
|
|
|
|
char *sizecmds[] = {"STRLEN","LLEN","SCARD","HLEN","ZCARD"};
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned int i;
|
2014-02-27 15:01:57 -05:00
|
|
|
|
|
|
|
/* Pipeline size commands */
|
|
|
|
for(i=0;i<keys->elements;i++) {
|
|
|
|
/* Skip keys that were deleted */
|
2014-06-26 12:48:40 -04:00
|
|
|
if(types[i]==TYPE_NONE)
|
2014-02-27 15:01:57 -05:00
|
|
|
continue;
|
|
|
|
|
2014-06-26 12:48:40 -04:00
|
|
|
redisAppendCommand(context, "%s %s", sizecmds[types[i]],
|
2014-02-27 15:01:57 -05:00
|
|
|
keys->element[i]->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retreive sizes */
|
|
|
|
for(i=0;i<keys->elements;i++) {
|
|
|
|
/* Skip keys that dissapeared between SCAN and TYPE */
|
|
|
|
if(types[i] == TYPE_NONE) {
|
|
|
|
sizes[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retreive size */
|
|
|
|
if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
|
|
|
|
fprintf(stderr, "Error getting size for key '%s' (%d: %s)\n",
|
|
|
|
keys->element[i]->str, context->err, context->errstr);
|
2013-03-12 04:57:49 -04:00
|
|
|
exit(1);
|
2014-02-27 15:01:57 -05:00
|
|
|
} else if(reply->type != REDIS_REPLY_INTEGER) {
|
|
|
|
/* Theoretically the key could have been removed and
|
|
|
|
* added as a different type between TYPE and SIZE */
|
2014-06-26 12:48:40 -04:00
|
|
|
fprintf(stderr,
|
2014-02-27 15:01:57 -05:00
|
|
|
"Warning: %s on '%s' failed (may have changed type)\n",
|
|
|
|
sizecmds[types[i]], keys->element[i]->str);
|
|
|
|
sizes[i] = 0;
|
|
|
|
} else {
|
|
|
|
sizes[i] = reply->integer;
|
|
|
|
}
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
freeReplyObject(reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void findBigKeys(void) {
|
|
|
|
unsigned long long biggest[5] = {0}, counts[5] = {0}, totalsize[5] = {0};
|
|
|
|
unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0;
|
|
|
|
sds maxkeys[5] = {0};
|
|
|
|
char *typename[] = {"string","list","set","hash","zset"};
|
|
|
|
char *typeunit[] = {"bytes","items","members","fields","members"};
|
|
|
|
redisReply *reply, *keys;
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned int arrsize=0, i;
|
|
|
|
int type, *types=NULL;
|
2014-02-27 15:01:57 -05:00
|
|
|
double pct;
|
|
|
|
|
|
|
|
/* Total keys pre scanning */
|
|
|
|
total_keys = getDbSize();
|
|
|
|
|
|
|
|
/* Status message */
|
|
|
|
printf("\n# Scanning the entire keyspace to find biggest keys as well as\n");
|
|
|
|
printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n");
|
|
|
|
printf("# per 100 SCAN commands (not usually needed).\n\n");
|
|
|
|
|
|
|
|
/* New up sds strings to keep track of overall biggest per type */
|
|
|
|
for(i=0;i<TYPE_NONE; i++) {
|
|
|
|
maxkeys[i] = sdsempty();
|
|
|
|
if(!maxkeys[i]) {
|
2014-10-15 03:21:02 -04:00
|
|
|
fprintf(stderr, "Failed to allocate memory for largest key names!\n");
|
2012-04-18 14:33:02 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-02-27 15:01:57 -05:00
|
|
|
}
|
2014-02-25 08:41:30 -05:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* SCAN loop */
|
|
|
|
do {
|
|
|
|
/* Calculate approximate percentage completion */
|
|
|
|
pct = 100 * (double)sampled/total_keys;
|
2014-02-25 08:41:30 -05:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Grab some keys and point to the keys array */
|
|
|
|
reply = sendScan(&it);
|
|
|
|
keys = reply->element[1];
|
|
|
|
|
|
|
|
/* Reallocate our type and size array if we need to */
|
|
|
|
if(keys->elements > arrsize) {
|
|
|
|
types = zrealloc(types, sizeof(int)*keys->elements);
|
|
|
|
sizes = zrealloc(sizes, sizeof(unsigned long long)*keys->elements);
|
|
|
|
|
|
|
|
if(!types || !sizes) {
|
|
|
|
fprintf(stderr, "Failed to allocate storage for keys!\n");
|
2014-02-25 08:41:30 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-02-27 15:01:57 -05:00
|
|
|
|
|
|
|
arrsize = keys->elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retreive types and then sizes */
|
|
|
|
getKeyTypes(keys, types);
|
|
|
|
getKeySizes(keys, types, sizes);
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Now update our stats */
|
|
|
|
for(i=0;i<keys->elements;i++) {
|
|
|
|
if((type = types[i]) == TYPE_NONE)
|
|
|
|
continue;
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
totalsize[type] += sizes[i];
|
|
|
|
counts[type]++;
|
|
|
|
totlen += keys->element[i]->len;
|
|
|
|
sampled++;
|
|
|
|
|
|
|
|
if(biggest[type]<sizes[i]) {
|
|
|
|
printf(
|
|
|
|
"[%05.2f%%] Biggest %-6s found so far '%s' with %llu %s\n",
|
|
|
|
pct, typename[type], keys->element[i]->str, sizes[i],
|
|
|
|
typeunit[type]);
|
|
|
|
|
|
|
|
/* Keep track of biggest key name for this type */
|
|
|
|
maxkeys[type] = sdscpy(maxkeys[type], keys->element[i]->str);
|
|
|
|
if(!maxkeys[type]) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory for key!\n");
|
|
|
|
exit(1);
|
2014-02-25 08:41:30 -05:00
|
|
|
}
|
2014-02-27 15:01:57 -05:00
|
|
|
|
|
|
|
/* Keep track of the biggest size for this type */
|
2014-06-26 12:48:40 -04:00
|
|
|
biggest[type] = sizes[i];
|
2012-04-18 14:33:02 -04:00
|
|
|
}
|
2014-02-25 08:41:30 -05:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Update overall progress */
|
|
|
|
if(sampled % 1000000 == 0) {
|
|
|
|
printf("[%05.2f%%] Sampled %llu keys so far\n", pct, sampled);
|
|
|
|
}
|
2012-04-18 14:33:02 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
/* Sleep if we've been directed to do so */
|
|
|
|
if(sampled && (sampled %100) == 0 && config.interval) {
|
2012-04-18 14:33:02 -04:00
|
|
|
usleep(config.interval);
|
2014-02-27 15:01:57 -05:00
|
|
|
}
|
2014-06-26 12:48:40 -04:00
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
freeReplyObject(reply);
|
2014-02-25 08:41:30 -05:00
|
|
|
} while(it != 0);
|
|
|
|
|
2014-02-27 15:01:57 -05:00
|
|
|
if(types) zfree(types);
|
|
|
|
if(sizes) zfree(sizes);
|
|
|
|
|
|
|
|
/* We're done */
|
|
|
|
printf("\n-------- summary -------\n\n");
|
|
|
|
|
|
|
|
printf("Sampled %llu keys in the keyspace!\n", sampled);
|
|
|
|
printf("Total key length in bytes is %llu (avg len %.2f)\n\n",
|
|
|
|
totlen, totlen ? (double)totlen/sampled : 0);
|
|
|
|
|
|
|
|
/* Output the biggest keys we found, for types we did find */
|
|
|
|
for(i=0;i<TYPE_NONE;i++) {
|
|
|
|
if(sdslen(maxkeys[i])>0) {
|
|
|
|
printf("Biggest %6s found '%s' has %llu %s\n", typename[i], maxkeys[i],
|
|
|
|
biggest[i], typeunit[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
for(i=0;i<TYPE_NONE;i++) {
|
|
|
|
printf("%llu %ss with %llu %s (%05.2f%% of keys, avg size %.2f)\n",
|
|
|
|
counts[i], typename[i], totalsize[i], typeunit[i],
|
|
|
|
sampled ? 100 * (double)counts[i]/sampled : 0,
|
|
|
|
counts[i] ? (double)totalsize[i]/counts[i] : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free sds strings containing max keys */
|
|
|
|
for(i=0;i<TYPE_NONE;i++) {
|
|
|
|
sdsfree(maxkeys[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success! */
|
2014-02-25 08:41:30 -05:00
|
|
|
exit(0);
|
2012-04-18 14:33:02 -04:00
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Stats mode
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2013-03-22 12:54:32 -04:00
|
|
|
/* Return the specified INFO field from the INFO command output "info".
|
|
|
|
* A new buffer is allocated for the result, that needs to be free'd.
|
|
|
|
* If the field is not found NULL is returned. */
|
|
|
|
static char *getInfoField(char *info, char *field) {
|
|
|
|
char *p = strstr(info,field);
|
|
|
|
char *n1, *n2;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (!p) return NULL;
|
|
|
|
p += strlen(field)+1;
|
|
|
|
n1 = strchr(p,'\r');
|
|
|
|
n2 = strchr(p,',');
|
|
|
|
if (n2 && n2 < n1) n1 = n2;
|
|
|
|
result = malloc(sizeof(char)*(n1-p)+1);
|
|
|
|
memcpy(result,p,(n1-p));
|
|
|
|
result[n1-p] = '\0';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like the above function but automatically convert the result into
|
|
|
|
* a long. On error (missing field) LONG_MIN is returned. */
|
|
|
|
static long getLongInfoField(char *info, char *field) {
|
|
|
|
char *value = getInfoField(info,field);
|
|
|
|
long l;
|
|
|
|
|
|
|
|
if (!value) return LONG_MIN;
|
|
|
|
l = strtol(value,NULL,10);
|
|
|
|
free(value);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert number of bytes into a human readable string of the form:
|
|
|
|
* 100B, 2G, 100M, 4K, and so forth. */
|
|
|
|
void bytesToHuman(char *s, long long n) {
|
|
|
|
double d;
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
*s = '-';
|
|
|
|
s++;
|
|
|
|
n = -n;
|
|
|
|
}
|
|
|
|
if (n < 1024) {
|
|
|
|
/* Bytes */
|
|
|
|
sprintf(s,"%lluB",n);
|
|
|
|
return;
|
|
|
|
} else if (n < (1024*1024)) {
|
|
|
|
d = (double)n/(1024);
|
|
|
|
sprintf(s,"%.2fK",d);
|
|
|
|
} else if (n < (1024LL*1024*1024)) {
|
|
|
|
d = (double)n/(1024*1024);
|
|
|
|
sprintf(s,"%.2fM",d);
|
|
|
|
} else if (n < (1024LL*1024*1024*1024)) {
|
|
|
|
d = (double)n/(1024LL*1024*1024);
|
|
|
|
sprintf(s,"%.2fG",d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static void statMode(void) {
|
2013-03-22 12:54:32 -04:00
|
|
|
redisReply *reply;
|
|
|
|
long aux, requests = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
char buf[64];
|
|
|
|
int j;
|
|
|
|
|
|
|
|
reply = reconnectingInfo();
|
|
|
|
if (reply->type == REDIS_REPLY_ERROR) {
|
|
|
|
printf("ERROR: %s\n", reply->str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((i++ % 20) == 0) {
|
|
|
|
printf(
|
|
|
|
"------- data ------ --------------------- load -------------------- - child -\n"
|
|
|
|
"keys mem clients blocked requests connections \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keys */
|
|
|
|
aux = 0;
|
|
|
|
for (j = 0; j < 20; j++) {
|
|
|
|
long k;
|
|
|
|
|
|
|
|
sprintf(buf,"db%d:keys",j);
|
|
|
|
k = getLongInfoField(reply->str,buf);
|
|
|
|
if (k == LONG_MIN) continue;
|
|
|
|
aux += k;
|
|
|
|
}
|
|
|
|
sprintf(buf,"%ld",aux);
|
|
|
|
printf("%-11s",buf);
|
|
|
|
|
|
|
|
/* Used memory */
|
|
|
|
aux = getLongInfoField(reply->str,"used_memory");
|
|
|
|
bytesToHuman(buf,aux);
|
|
|
|
printf("%-8s",buf);
|
|
|
|
|
|
|
|
/* Clients */
|
|
|
|
aux = getLongInfoField(reply->str,"connected_clients");
|
|
|
|
sprintf(buf,"%ld",aux);
|
|
|
|
printf(" %-8s",buf);
|
|
|
|
|
|
|
|
/* Blocked (BLPOPPING) Clients */
|
|
|
|
aux = getLongInfoField(reply->str,"blocked_clients");
|
|
|
|
sprintf(buf,"%ld",aux);
|
|
|
|
printf("%-8s",buf);
|
|
|
|
|
|
|
|
/* Requets */
|
|
|
|
aux = getLongInfoField(reply->str,"total_commands_processed");
|
|
|
|
sprintf(buf,"%ld (+%ld)",aux,requests == 0 ? 0 : aux-requests);
|
|
|
|
printf("%-19s",buf);
|
|
|
|
requests = aux;
|
|
|
|
|
|
|
|
/* Connections */
|
|
|
|
aux = getLongInfoField(reply->str,"total_connections_received");
|
|
|
|
sprintf(buf,"%ld",aux);
|
|
|
|
printf(" %-12s",buf);
|
|
|
|
|
|
|
|
/* Children */
|
|
|
|
aux = getLongInfoField(reply->str,"bgsave_in_progress");
|
|
|
|
aux |= getLongInfoField(reply->str,"aof_rewrite_in_progress") << 1;
|
|
|
|
switch(aux) {
|
|
|
|
case 0: break;
|
|
|
|
case 1:
|
|
|
|
printf("SAVE");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
printf("AOF");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
printf("SAVE+AOF");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
freeReplyObject(reply);
|
|
|
|
usleep(config.interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:24:45 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Scan mode
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2014-03-15 19:51:54 -04:00
|
|
|
static void scanMode(void) {
|
2014-01-22 06:04:08 -05:00
|
|
|
redisReply *reply;
|
|
|
|
unsigned long long cur = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (config.pattern)
|
|
|
|
reply = redisCommand(context,"SCAN %llu MATCH %s",
|
|
|
|
cur,config.pattern);
|
|
|
|
else
|
|
|
|
reply = redisCommand(context,"SCAN %llu",cur);
|
|
|
|
if (reply == NULL) {
|
|
|
|
printf("I/O error\n");
|
|
|
|
exit(1);
|
|
|
|
} else if (reply->type == REDIS_REPLY_ERROR) {
|
|
|
|
printf("ERROR: %s\n", reply->str);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2014-08-13 05:44:38 -04:00
|
|
|
unsigned int j;
|
2014-01-22 06:04:08 -05:00
|
|
|
|
|
|
|
cur = strtoull(reply->element[0]->str,NULL,10);
|
|
|
|
for (j = 0; j < reply->element[1]->elements; j++)
|
|
|
|
printf("%s\n", reply->element[1]->element[j]->str);
|
|
|
|
}
|
|
|
|
freeReplyObject(reply);
|
|
|
|
} while(cur != 0);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:37:52 -05:00
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Intrisic latency mode.
|
|
|
|
*
|
|
|
|
* Measure max latency of a running process that does not result from
|
|
|
|
* syscalls. Basically this software should provide an hint about how much
|
|
|
|
* time the kernel leaves the process without a chance to run.
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* This is just some computation the compiler can't optimize out.
|
|
|
|
* Should run in less than 100-200 microseconds even using very
|
|
|
|
* slow hardware. Runs in less than 10 microseconds in modern HW. */
|
2014-02-25 07:43:47 -05:00
|
|
|
unsigned long compute_something_fast(void) {
|
2014-02-25 07:47:37 -05:00
|
|
|
unsigned char s[256], i, j, t;
|
2014-02-25 06:37:52 -05:00
|
|
|
int count = 1000, k;
|
2014-02-25 07:43:47 -05:00
|
|
|
unsigned long output = 0;
|
2014-02-25 06:37:52 -05:00
|
|
|
|
|
|
|
for (k = 0; k < 256; k++) s[k] = k;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
j = 0;
|
|
|
|
while(count--) {
|
|
|
|
i++;
|
|
|
|
j = j + s[i];
|
|
|
|
t = s[i];
|
|
|
|
s[i] = s[j];
|
|
|
|
s[j] = t;
|
|
|
|
output += s[(s[i]+s[j])&255];
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-06-17 10:12:57 -04:00
|
|
|
static void intrinsicLatencyModeStop(int s) {
|
|
|
|
REDIS_NOTUSED(s);
|
|
|
|
force_cancel_loop = 1;
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:37:52 -05:00
|
|
|
static void intrinsicLatencyMode(void) {
|
|
|
|
long long test_end, run_time, max_latency = 0, runs = 0;
|
|
|
|
|
|
|
|
run_time = config.intrinsic_latency_duration*1000000;
|
|
|
|
test_end = ustime() + run_time;
|
2014-06-17 10:12:57 -04:00
|
|
|
signal(SIGINT, intrinsicLatencyModeStop);
|
2014-02-25 06:37:52 -05:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
long long start, end, latency;
|
|
|
|
|
|
|
|
start = ustime();
|
|
|
|
compute_something_fast();
|
|
|
|
end = ustime();
|
|
|
|
latency = end-start;
|
|
|
|
runs++;
|
|
|
|
if (latency <= 0) continue;
|
|
|
|
|
|
|
|
/* Reporting */
|
|
|
|
if (latency > max_latency) {
|
|
|
|
max_latency = latency;
|
|
|
|
printf("Max latency so far: %lld microseconds.\n", max_latency);
|
|
|
|
}
|
|
|
|
|
2014-05-22 18:26:40 -04:00
|
|
|
double avg_us = (double)run_time/runs;
|
|
|
|
double avg_ns = avg_us * 10e3;
|
2014-06-17 10:12:57 -04:00
|
|
|
if (force_cancel_loop || end > test_end) {
|
2014-05-22 18:26:40 -04:00
|
|
|
printf("\n%lld total runs "
|
|
|
|
"(avg latency: "
|
|
|
|
"%.4f microseconds / %.2f nanoseconds per run).\n",
|
|
|
|
runs, avg_us, avg_ns);
|
|
|
|
printf("Worst run took %.0fx longer than the average latency.\n",
|
|
|
|
max_latency / avg_us);
|
2014-02-25 06:37:52 -05:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Program main()
|
|
|
|
*--------------------------------------------------------------------------- */
|
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
int main(int argc, char **argv) {
|
2010-03-02 10:14:14 -05:00
|
|
|
int firstarg;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2010-11-29 06:17:55 -05:00
|
|
|
config.hostip = sdsnew("127.0.0.1");
|
2009-03-22 05:30:00 -04:00
|
|
|
config.hostport = 6379;
|
2010-08-01 17:06:00 -04:00
|
|
|
config.hostsocket = NULL;
|
2009-11-02 19:35:39 -05:00
|
|
|
config.repeat = 1;
|
2011-05-28 09:41:08 -04:00
|
|
|
config.interval = 0;
|
2009-11-11 23:12:09 -05:00
|
|
|
config.dbnum = 0;
|
2010-08-25 04:05:50 -04:00
|
|
|
config.interactive = 0;
|
2010-05-14 10:38:09 -04:00
|
|
|
config.shutdown = 0;
|
2010-04-27 10:07:31 -04:00
|
|
|
config.monitor_mode = 0;
|
|
|
|
config.pubsub_mode = 0;
|
2011-09-15 13:28:00 -04:00
|
|
|
config.latency_mode = 0;
|
2013-04-11 07:11:41 -04:00
|
|
|
config.latency_history = 0;
|
2011-10-05 13:55:33 -04:00
|
|
|
config.cluster_mode = 0;
|
2012-04-18 14:33:02 -04:00
|
|
|
config.slave_mode = 0;
|
2013-01-16 13:42:40 -05:00
|
|
|
config.getrdb_mode = 0;
|
2014-01-22 06:04:08 -05:00
|
|
|
config.stat_mode = 0;
|
|
|
|
config.scan_mode = 0;
|
2014-02-25 06:37:52 -05:00
|
|
|
config.intrinsic_latency_mode = 0;
|
2014-01-22 06:04:08 -05:00
|
|
|
config.pattern = NULL;
|
2013-01-16 13:42:40 -05:00
|
|
|
config.rdb_filename = NULL;
|
2012-05-06 04:05:31 -04:00
|
|
|
config.pipe_mode = 0;
|
2014-02-25 06:37:52 -05:00
|
|
|
config.pipe_timeout = REDIS_CLI_DEFAULT_PIPE_TIMEOUT;
|
2012-04-18 14:33:02 -04:00
|
|
|
config.bigkeys = 0;
|
2010-09-09 10:38:10 -04:00
|
|
|
config.stdinarg = 0;
|
2010-03-17 21:51:09 -04:00
|
|
|
config.auth = NULL;
|
2011-12-13 10:22:28 -05:00
|
|
|
config.eval = NULL;
|
2014-08-01 11:44:28 -04:00
|
|
|
config.last_cmd_type = -1;
|
|
|
|
|
2012-02-29 11:43:03 -05:00
|
|
|
if (!isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL))
|
|
|
|
config.output = OUTPUT_RAW;
|
|
|
|
else
|
|
|
|
config.output = OUTPUT_STANDARD;
|
2010-12-15 09:59:04 -05:00
|
|
|
config.mb_delim = sdsnew("\n");
|
2010-11-29 13:27:36 -05:00
|
|
|
cliInitHelp();
|
2010-07-07 12:44:53 -04:00
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
firstarg = parseOptions(argc,argv);
|
|
|
|
argc -= firstarg;
|
|
|
|
argv += firstarg;
|
|
|
|
|
2012-05-06 04:05:31 -04:00
|
|
|
/* Latency mode */
|
2011-09-15 13:28:00 -04:00
|
|
|
if (config.latency_mode) {
|
2013-01-18 04:13:10 -05:00
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
2011-09-15 13:28:00 -04:00
|
|
|
latencyMode();
|
|
|
|
}
|
|
|
|
|
2012-05-06 04:05:31 -04:00
|
|
|
/* Slave mode */
|
2012-02-29 11:10:21 -05:00
|
|
|
if (config.slave_mode) {
|
2013-01-18 04:13:10 -05:00
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
2012-02-29 11:10:21 -05:00
|
|
|
slaveMode();
|
|
|
|
}
|
|
|
|
|
2013-01-16 13:42:40 -05:00
|
|
|
/* Get RDB mode. */
|
|
|
|
if (config.getrdb_mode) {
|
2013-01-18 04:13:10 -05:00
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
2013-01-16 13:42:40 -05:00
|
|
|
getRDB();
|
|
|
|
}
|
|
|
|
|
2012-05-06 04:05:31 -04:00
|
|
|
/* Pipe mode */
|
|
|
|
if (config.pipe_mode) {
|
2012-07-15 08:35:02 -04:00
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
2012-05-06 04:05:31 -04:00
|
|
|
pipeMode();
|
|
|
|
}
|
|
|
|
|
2012-04-18 14:33:02 -04:00
|
|
|
/* Find big keys */
|
|
|
|
if (config.bigkeys) {
|
2013-01-18 04:13:10 -05:00
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
2012-04-18 14:33:02 -04:00
|
|
|
findBigKeys();
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:54:32 -04:00
|
|
|
/* Stat mode */
|
|
|
|
if (config.stat_mode) {
|
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
|
|
|
if (config.interval == 0) config.interval = 1000000;
|
|
|
|
statMode();
|
|
|
|
}
|
|
|
|
|
2014-01-22 06:04:08 -05:00
|
|
|
/* Scan mode */
|
|
|
|
if (config.scan_mode) {
|
|
|
|
if (cliConnect(0) == REDIS_ERR) exit(1);
|
|
|
|
scanMode();
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:37:52 -05:00
|
|
|
/* Intrinsic latency mode */
|
|
|
|
if (config.intrinsic_latency_mode) intrinsicLatencyMode();
|
|
|
|
|
2010-08-04 12:36:03 -04:00
|
|
|
/* Start interactive mode when no command is provided */
|
2011-12-13 10:22:28 -05:00
|
|
|
if (argc == 0 && !config.eval) {
|
2014-12-08 18:57:46 -05:00
|
|
|
/* Ignore SIGPIPE in interactive mode to force a reconnect */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2011-05-28 09:04:12 -04:00
|
|
|
/* Note that in repl mode we don't abort on connection error.
|
|
|
|
* A new attempt will be performed for every command send. */
|
|
|
|
cliConnect(0);
|
|
|
|
repl();
|
|
|
|
}
|
|
|
|
|
2010-08-25 08:48:50 -04:00
|
|
|
/* Otherwise, we have some arguments to execute */
|
2011-05-28 09:04:12 -04:00
|
|
|
if (cliConnect(0) != REDIS_OK) exit(1);
|
2011-12-13 10:22:28 -05:00
|
|
|
if (config.eval) {
|
|
|
|
return evalMode(argc,argv);
|
|
|
|
} else {
|
|
|
|
return noninteractive(argc,convertToSds(argc,argv));
|
|
|
|
}
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|