summaryrefslogtreecommitdiff
path: root/src/redis-cli.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/redis-cli.c')
-rw-r--r--src/redis-cli.c110
1 files changed, 93 insertions, 17 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c
index 3969fbab5..c182ac17d 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -41,6 +41,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <assert.h>
+#include <fcntl.h>
#include "hiredis.h"
#include "sds.h"
@@ -73,6 +74,8 @@ static struct config {
int cluster_reissue_command;
int slave_mode;
int pipe_mode;
+ int getrdb_mode;
+ char *rdb_filename;
int bigkeys;
int stdinarg; /* get last arg from stdin. (-x option) */
char *auth;
@@ -305,7 +308,7 @@ static int cliSelect() {
return REDIS_ERR;
}
-/* Connect to the client. If force is not zero the connection is performed
+/* Connect to the server. If force is not zero the connection is performed
* even if there is already a connected socket. */
static int cliConnect(int force) {
if (context == NULL || force) {
@@ -660,6 +663,9 @@ static int parseOptions(int argc, char **argv) {
config.latency_mode = 1;
} else if (!strcmp(argv[i],"--slave")) {
config.slave_mode = 1;
+ } else if (!strcmp(argv[i],"--rdb") && !lastarg) {
+ config.getrdb_mode = 1;
+ config.rdb_filename = argv[++i];
} else if (!strcmp(argv[i],"--pipe")) {
config.pipe_mode = 1;
} else if (!strcmp(argv[i],"--bigkeys")) {
@@ -720,6 +726,7 @@ static void usage() {
" --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
" --latency Enter a special mode continuously sampling latency\n"
" --slave Simulate a slave showing commands received from the master\n"
+" --rdb <filename> Transfer an RDB dump from remote server to local file.\n"
" --pipe Transfer raw Redis protocol from stdin to server\n"
" --bigkeys Sample Redis keys looking for big keys\n"
" --eval <file> Send an EVAL command using the Lua script at <file>\n"
@@ -794,6 +801,7 @@ static void repl() {
sdsfree(config.hostip);
config.hostip = sdsnew(argv[1]);
config.hostport = atoi(argv[2]);
+ cliRefreshPrompt();
cliConnect(1);
} else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
linenoiseClearScreen();
@@ -927,15 +935,15 @@ static void latencyMode(void) {
}
}
-static void slaveMode(void) {
+/* Sends SYNC and reads the number of bytes in the payload. Used both by
+ * slaveMode() and getRDB(). */
+unsigned long long sendSync(int fd) {
/* 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. */
- int fd = context->fd;
- char buf[1024], *p;
+ char buf[4096], *p;
ssize_t nread;
- unsigned long long payload;
/* Send the SYNC command. */
if (write(fd,"SYNC\r\n",6) != 6) {
@@ -951,16 +959,29 @@ static void slaveMode(void) {
fprintf(stderr,"Error reading bulk length while SYNCing\n");
exit(1);
}
- if (*p == '\n') break;
- p++;
+ if (*p == '\n' && p != buf) break;
+ if (*p != '\n') p++;
}
*p = '\0';
- payload = strtoull(buf+1,NULL,10);
- fprintf(stderr,"SYNC with master, discarding %lld bytes of bulk tranfer...\n",
- payload);
+ 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];
+
+ fprintf(stderr,"SYNC with master, discarding %llu "
+ "bytes of bulk transfer...\n", payload);
/* Discard the payload. */
while(payload) {
+ ssize_t nread;
+
nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
if (nread <= 0) {
fprintf(stderr,"Error reading RDB payload while SYNCing\n");
@@ -970,11 +991,56 @@ static void slaveMode(void) {
}
fprintf(stderr,"SYNC done. Logging commands from master.\n");
- /* Now we can use the hiredis to read the incoming protocol. */
+ /* Now we can use hiredis to read the incoming protocol. */
config.output = OUTPUT_CSV;
while (cliReadReply(0) == REDIS_OK);
}
+/* 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;
+
+ 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);
+}
+
static void pipeMode(void) {
int fd = context->fd;
long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;
@@ -1075,7 +1141,7 @@ static void pipeMode(void) {
int j;
eof = 1;
- /* Everything transfered, so we queue a special
+ /* Everything transferred, so we queue a special
* ECHO command that we can match in the replies
* to make sure everything was read from the server. */
for (j = 0; j < 20; j++)
@@ -1117,6 +1183,7 @@ static void findBigKeys(void) {
unsigned long long samples = 0;
redisReply *reply1, *reply2, *reply3 = NULL;
char *sizecmd, *typename[] = {"string","list","set","hash","zset"};
+ char *typeunit[] = {"bytes","items","members","fields","members"};
int type;
printf("\n# Press ctrl+c when you have had enough of it... :)\n");
@@ -1168,9 +1235,10 @@ static void findBigKeys(void) {
reply3 = redisCommand(context,"%s %s", sizecmd, reply1->str);
if (reply3 && reply3->type == REDIS_REPLY_INTEGER) {
if (biggest[type] < reply3->integer) {
- printf("[%6s] %s | biggest so far with size %llu\n",
+ printf("Biggest %-6s found so far '%s' with %llu %s.\n",
typename[type], reply1->str,
- (unsigned long long) reply3->integer);
+ (unsigned long long) reply3->integer,
+ typeunit[type]);
biggest[type] = reply3->integer;
}
}
@@ -1203,6 +1271,8 @@ int main(int argc, char **argv) {
config.latency_mode = 0;
config.cluster_mode = 0;
config.slave_mode = 0;
+ config.getrdb_mode = 0;
+ config.rdb_filename = NULL;
config.pipe_mode = 0;
config.bigkeys = 0;
config.stdinarg = 0;
@@ -1221,16 +1291,22 @@ int main(int argc, char **argv) {
/* Latency mode */
if (config.latency_mode) {
- cliConnect(0);
+ if (cliConnect(0) == REDIS_ERR) exit(1);
latencyMode();
}
/* Slave mode */
if (config.slave_mode) {
- cliConnect(0);
+ if (cliConnect(0) == REDIS_ERR) exit(1);
slaveMode();
}
+ /* Get RDB mode. */
+ if (config.getrdb_mode) {
+ if (cliConnect(0) == REDIS_ERR) exit(1);
+ getRDB();
+ }
+
/* Pipe mode */
if (config.pipe_mode) {
if (cliConnect(0) == REDIS_ERR) exit(1);
@@ -1239,7 +1315,7 @@ int main(int argc, char **argv) {
/* Find big keys */
if (config.bigkeys) {
- cliConnect(0);
+ if (cliConnect(0) == REDIS_ERR) exit(1);
findBigKeys();
}