From 530fcf868786950fbd07f294dd73f9f9bd631cb6 Mon Sep 17 00:00:00 2001 From: xuzhou Date: Fri, 16 Jun 2017 17:51:38 +0800 Subject: [PATCH 1/2] Fix set with ex/px option when propagated to aof --- src/aof.c | 17 +++++++++++++++++ src/server.c | 2 ++ src/server.h | 3 ++- tests/unit/expire.tcl | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/aof.c b/src/aof.c index 9b15ad1d..071657dd 100644 --- a/src/aof.c +++ b/src/aof.c @@ -536,6 +536,23 @@ void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int a buf = catAppendOnlyGenericCommand(buf,3,tmpargv); decrRefCount(tmpargv[0]); buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]); + } else if (cmd->proc == setCommand) { + int i; + robj *exarg = NULL, *pxarg = NULL; + /* Translate SET [EX seconds][PX milliseconds] to SET and PEXPIREAT */ + buf = catAppendOnlyGenericCommand(buf,3,argv); + for (i = 3; i < argc; i ++) { + if (sdsEncodedObject(argv[i]) && !strcasecmp(argv[i]->ptr, "ex")) + exarg = argv[i+1]; + + if (sdsEncodedObject(argv[i]) && !strcasecmp(argv[i]->ptr, "px")) + pxarg = argv[i+1]; + } + serverAssert(!(exarg && pxarg)); + if (exarg) + buf = catAppendOnlyExpireAtCommand(buf,server.expireCommand,argv[1],exarg); + if (pxarg) + buf = catAppendOnlyExpireAtCommand(buf,server.pexpireCommand,argv[1],pxarg); } else { /* All the other commands don't need translation or need the * same translation already operated in the command vector diff --git a/src/server.c b/src/server.c index ba93eb78..a3c13284 100644 --- a/src/server.c +++ b/src/server.c @@ -1500,6 +1500,8 @@ void initServerConfig(void) { server.rpopCommand = lookupCommandByCString("rpop"); server.sremCommand = lookupCommandByCString("srem"); server.execCommand = lookupCommandByCString("exec"); + server.expireCommand = lookupCommandByCString("expire"); + server.pexpireCommand = lookupCommandByCString("pexpire"); /* Slow log */ server.slowlog_log_slower_than = CONFIG_DEFAULT_SLOWLOG_LOG_SLOWER_THAN; diff --git a/src/server.h b/src/server.h index 18924090..aaad64bd 100644 --- a/src/server.h +++ b/src/server.h @@ -909,7 +909,8 @@ struct redisServer { off_t loading_process_events_interval_bytes; /* Fast pointers to often looked up command */ struct redisCommand *delCommand, *multiCommand, *lpushCommand, *lpopCommand, - *rpopCommand, *sremCommand, *execCommand; + *rpopCommand, *sremCommand, *execCommand, *expireCommand, + *pexpireCommand; /* Fields used only for stats */ time_t stat_starttime; /* Server start time */ long long stat_numcommands; /* Number of processed commands */ diff --git a/tests/unit/expire.tcl b/tests/unit/expire.tcl index 0a50dd31..eddc7c30 100644 --- a/tests/unit/expire.tcl +++ b/tests/unit/expire.tcl @@ -204,4 +204,19 @@ start_server {tags {"expire"}} { catch {r expire foo ""} e set e } {*not an integer*} + + test {SET - use EX/PX option, TTL should not be reseted after loadaof} { + r config set appendonly yes + r set foo bar EX 100 + after 2000 + r debug loadaof + set ttl [r ttl foo] + assert {$ttl <= 98 && $ttl > 90} + + r set foo bar PX 100000 + after 2000 + r debug loadaof + set ttl [r ttl foo] + assert {$ttl <= 98 && $ttl > 90} + } } From 86e9f48a0cdfb26fcf0743c91bf593414d336a01 Mon Sep 17 00:00:00 2001 From: xuzhou Date: Thu, 22 Jun 2017 11:06:40 +0800 Subject: [PATCH 2/2] Optimize set command with ex/px when updating aof. --- src/aof.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/aof.c b/src/aof.c index 071657dd..79e2b9b7 100644 --- a/src/aof.c +++ b/src/aof.c @@ -536,16 +536,16 @@ void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int a buf = catAppendOnlyGenericCommand(buf,3,tmpargv); decrRefCount(tmpargv[0]); buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]); - } else if (cmd->proc == setCommand) { + } else if (cmd->proc == setCommand && argc > 3) { int i; robj *exarg = NULL, *pxarg = NULL; /* Translate SET [EX seconds][PX milliseconds] to SET and PEXPIREAT */ buf = catAppendOnlyGenericCommand(buf,3,argv); for (i = 3; i < argc; i ++) { - if (sdsEncodedObject(argv[i]) && !strcasecmp(argv[i]->ptr, "ex")) + if (!strcasecmp(argv[i]->ptr, "ex")) exarg = argv[i+1]; - if (sdsEncodedObject(argv[i]) && !strcasecmp(argv[i]->ptr, "px")) + if (!strcasecmp(argv[i]->ptr, "px")) pxarg = argv[i+1]; } serverAssert(!(exarg && pxarg));