From 8a6ddab56894302e72feff70900455357f6b5e50 Mon Sep 17 00:00:00 2001 From: Wang Yuan Date: Wed, 21 Jul 2021 17:32:08 +0800 Subject: [PATCH] Fix SETNX and MSETNX commands can't guarantee atomicity (#337) --- src/redis_cmd.cc | 2 +- src/redis_string.cc | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc index ff4b52f5c14..8c164a39c7a 100644 --- a/src/redis_cmd.cc +++ b/src/redis_cmd.cc @@ -4321,7 +4321,7 @@ CommandAttributes redisCommandTable[] = { ADD_CMD("setex", 4, "write", 1, 1, 1, CommandSetEX), ADD_CMD("psetex", 4, "write", 1, 1, 1, CommandPSetEX), ADD_CMD("setnx", 3, "write", 1, 1, 1, CommandSetNX), - ADD_CMD("msetnx", -3, "write", 1, -1, 2, CommandMSetNX), + ADD_CMD("msetnx", -3, "write exclusive", 1, -1, 2, CommandMSetNX), ADD_CMD("mset", -3, "write", 1, -1, 2, CommandMSet), ADD_CMD("incrby", 3, "write", 1, 1, 1, CommandIncrBy), ADD_CMD("incrbyfloat", 3, "write", 1, 1, 1, CommandIncrByFloat), diff --git a/src/redis_string.cc b/src/redis_string.cc index d56a8c4b211..773aef7d6db 100644 --- a/src/redis_string.cc +++ b/src/redis_string.cc @@ -354,6 +354,9 @@ rocksdb::Status String::MSetNX(const std::vector &pairs, int ttl, in for (StringPair pair : pairs) { AppendNamespacePrefix(pair.key, &ns_key); LockGuard guard(storage_->GetLockManager(), ns_key); + if (Exists({pair.key}, &exists).ok() && exists == 1) { + return rocksdb::Status::OK(); + } std::string bytes; Metadata metadata(kRedisString, false); metadata.expire = expire;