Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Deny SYNC and PSYNC in redis-cli #1545

Merged
merged 1 commit into from

2 participants

@mattsta

Running SYNC or PSYNC in the cli returns the
Redis replication stream as output, then upon encountering
the next command, redis-cli aborts because it's receiving
a replication stream instead of command output.

Trying to read the replication stream from redis-cli isn't useful (as it breaks the CLI and doesn't feed you ongoing output anyway).

If you want to view/debug a live replication stream, you should do cat <(echo sync) - | nc localhost 6379 instead.

@antirez
Owner

Matt what about instead just entering the --slave mode when those commands are detected? Maybe adding some output like Entering redis-cli slave mode... just to make clear that what they are going to see is processed output and not raw one.

@mattsta mattsta Auto-enter slaveMode when SYNC from redis-cli
If someone asks for SYNC or PSYNC from redis-cli,
automatically enter slaveMode (as if they ran
redis-cli --slave) and continue printing the replication
stream until either they Ctrl-C or the master gets disconnected.
2164847
@mattsta

Great idea! Updated.

@antirez antirez merged commit e163332 into from
@antirez
Owner

Thank you! merged.

@mattsta mattsta deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 10, 2014
  1. @mattsta

    Auto-enter slaveMode when SYNC from redis-cli

    mattsta authored
    If someone asks for SYNC or PSYNC from redis-cli,
    automatically enter slaveMode (as if they ran
    redis-cli --slave) and continue printing the replication
    stream until either they Ctrl-C or the master gets disconnected.
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +12 −0 src/redis-cli.c
View
12 src/redis-cli.c
@@ -94,6 +94,7 @@ static struct config {
} config;
static void usage();
+static void slaveMode(void);
char *redisGitSHA1(void);
char *redisGitDirty(void);
@@ -600,6 +601,8 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
if (!strcasecmp(command,"subscribe") ||
!strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
+ if (!strcasecmp(command,"sync") ||
+ !strcasecmp(command,"psync")) config.slave_mode = 1;
/* Setup argument length */
argvlen = malloc(argc*sizeof(size_t));
@@ -621,6 +624,13 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
}
}
+ if (config.slave_mode) {
+ printf("Entering slave output mode... (press Ctrl-C to quit)\n");
+ slaveMode();
+ config.slave_mode = 0;
+ return REDIS_ERR; /* Error = slaveMode lost connection to master */
+ }
+
if (cliReadReply(output_raw) != REDIS_OK) {
free(argvlen);
return REDIS_ERR;
@@ -1061,6 +1071,7 @@ static void slaveMode(void) {
int fd = context->fd;
unsigned long long payload = sendSync(fd);
char buf[1024];
+ int original_output = config.output;
fprintf(stderr,"SYNC with master, discarding %llu "
"bytes of bulk transfer...\n", payload);
@@ -1081,6 +1092,7 @@ static void slaveMode(void) {
/* Now we can use hiredis to read the incoming protocol. */
config.output = OUTPUT_CSV;
while (cliReadReply(0) == REDIS_OK);
+ config.output = original_output;
}
/* This function implements --rdb, so it uses the replication protocol in order
Something went wrong with that request. Please try again.