Skip to content

Commit

Permalink
Improvements to PR #2425
Browse files Browse the repository at this point in the history
1. Remove useless "cs" initialization.
2. Add a "select" var to capture a condition checked multiple times.
3. Avoid duplication of the same if (!copy) conditional.
4. Don't increment dirty if copy is given (no deletion is performed),
   otherwise we propagate MIGRATE when not needed.
  • Loading branch information
antirez committed Feb 26, 2015
1 parent 7fda935 commit 832b0c7
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -4481,7 +4481,6 @@ void migrateCommand(redisClient *c) {

try_again:
/* Initialization */
cs = NULL;
copy = 0;
replace = 0;
ttl = 0;
Expand Down Expand Up @@ -4519,13 +4518,15 @@ void migrateCommand(redisClient *c) {

rioInitWithBuffer(&cmd,sdsempty());

/* Create RESTORE payload and generate the protocol to call the command. */
if (cs->last_dbid != dbid) {
/* Send the SELECT command if the current DB is not already selected. */
int select = cs->last_dbid != dbid; /* Should we emit SELECT? */
if (select) {
redisAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',2));
redisAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,"SELECT",6));
redisAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,dbid));
}

/* Create RESTORE payload and generate the protocol to call the command. */
expireat = getExpire(c->db,c->argv[3]);
if (expireat != -1) {
ttl = expireat-mstime();
Expand Down Expand Up @@ -4575,12 +4576,12 @@ void migrateCommand(redisClient *c) {
char buf2[1024];

/* Read the two replies */
if (cs->last_dbid != dbid && syncReadLine(cs->fd, buf1, sizeof(buf1), timeout) <= 0)
if (select && syncReadLine(cs->fd, buf1, sizeof(buf1), timeout) <= 0)
goto socket_rd_err;
if (syncReadLine(cs->fd, buf2, sizeof(buf2), timeout) <= 0)
goto socket_rd_err;
if ((cs->last_dbid != dbid && buf1[0] == '-') || buf2[0] == '-') {
/* If we got an error at all, assume that the last_dbid is no longer valid */
if ((select && buf1[0] == '-') || buf2[0] == '-') {
/* On error assume that last_dbid is no longer valid. */
cs->last_dbid = -1;
addReplyErrorFormat(c,"Target instance replied with error: %s",
(cs->last_dbid != dbid && buf1[0] == '-') ? buf1+1 : buf2+1);
Expand All @@ -4589,15 +4590,14 @@ void migrateCommand(redisClient *c) {
cs->last_dbid = dbid;
robj *aux;

addReply(c,shared.ok);

if (!copy) {
/* No COPY option: remove the local key, signal the change. */
dbDelete(c->db,c->argv[3]);
signalModifiedKey(c->db,c->argv[3]);
}
addReply(c,shared.ok);
server.dirty++;
server.dirty++;

if (!copy) {
/* Translate MIGRATE as DEL for replication/AOF. */
aux = createStringObject("DEL",3);
rewriteClientCommandVector(c,2,aux,c->argv[3]);
Expand Down

0 comments on commit 832b0c7

Please sign in to comment.