Skip to content

Commit

Permalink
Fix SETNX and MSETNX commands can't guarantee atomicity (apache#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShooterIT committed Sep 9, 2021
1 parent 0e31f8e commit 8a6ddab
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions src/redis_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ rocksdb::Status String::MSetNX(const std::vector<StringPair> &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;
Expand Down

0 comments on commit 8a6ddab

Please sign in to comment.