summaryrefslogtreecommitdiff
path: root/src/multi.c
diff options
context:
space:
mode:
authorMeir Shpilraien (Spielrein) <meir@redislabs.com>2020-11-17 18:58:55 +0200
committerGitHub <noreply@github.com>2020-11-17 18:58:55 +0200
commitd87a0d02868df3f8739b6e2e1cd8c2a1a9cc14c3 (patch)
tree383a79a3c6eac25af15577baaf55f95d37ad0093 /src/multi.c
parent39f716a121dd66d9a13b14797ef4bdcff531e27b (diff)
downloadredis-d87a0d02868df3f8739b6e2e1cd8c2a1a9cc14c3.tar.gz
Unified MULTI, LUA, and RM_Call with respect to blocking commands (#8025)
Blocking command should not be used with MULTI, LUA, and RM_Call. This is because, the caller, who executes the command in this context, expects a reply. Today, LUA and MULTI have a special (and different) treatment to blocking commands: LUA - Most commands are marked with no-script flag which are checked when executing and command from LUA, commands that are not marked (like XREAD) verify that their blocking mode is not used inside LUA (by checking the CLIENT_LUA client flag). MULTI - Command that is going to block, first verify that the client is not inside multi (by checking the CLIENT_MULTI client flag). If the client is inside multi, they return a result which is a match to the empty key with no timeout (for example blpop inside MULTI will act as lpop) For modules that perform RM_Call with blocking command, the returned results type is REDISMODULE_REPLY_UNKNOWN and the caller can not really know what happened. Disadvantages of the current state are: No unified approach, LUA, MULTI, and RM_Call, each has a different treatment Module can not safely execute blocking command (and get reply or error). Though It is true that modules are not like LUA or MULTI and should be smarter not to execute blocking commands on RM_Call, sometimes you want to execute a command base on client input (for example if you create a module that provides a new scripting language like javascript or python). While modules (on modules command) can check for REDISMODULE_CTX_FLAGS_LUA or REDISMODULE_CTX_FLAGS_MULTI to know not to block the client, there is no way to check if the command came from another module using RM_Call. So there is no way for a module to know not to block another module RM_Call execution. This commit adds a way to unify the treatment for blocking clients by introducing a new CLIENT_DENY_BLOCKING client flag. On LUA, MULTI, and RM_Call the new flag turned on to signify that the client should not be blocked. A blocking command verifies that the flag is turned off before blocking. If a blocking command sees that the CLIENT_DENY_BLOCKING flag is on, it's not blocking and return results which are matches to empty key with no timeout (as MULTI does today). The new flag is checked on the following commands: List blocking commands: BLPOP, BRPOP, BRPOPLPUSH, BLMOVE, Zset blocking commands: BZPOPMIN, BZPOPMAX Stream blocking commands: XREAD, XREADGROUP SUBSCRIBE, PSUBSCRIBE, MONITOR In addition, the new flag is turned on inside the AOF client, we do not want to block the AOF client to prevent deadlocks and commands ordering issues (and there is also an existing assert in the code that verifies it). To keep backward compatibility on LUA, all the no-script flags on existing commands were kept untouched. In addition, a LUA special treatment on XREAD and XREADGROUP was kept. To keep backward compatibility on MULTI (which today allows SUBSCRIBE, and PSUBSCRIBE). We added a special treatment on those commands to allow executing them on MULTI. The only backward compatibility issue that this PR introduces is that now MONITOR is not allowed inside MULTI. Tests were added to verify blocking commands are not blocking the client on LUA, MULTI, or RM_Call. Tests were added to verify the module can check for CLIENT_DENY_BLOCKING flag. Co-authored-by: Oran Agra <oran@redislabs.com> Co-authored-by: Itamar Haber <itamar@redislabs.com>
Diffstat (limited to 'src/multi.c')
-rw-r--r--src/multi.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/multi.c b/src/multi.c
index 3ce6d60ec..7790b0223 100644
--- a/src/multi.c
+++ b/src/multi.c
@@ -100,6 +100,7 @@ void multiCommand(client *c) {
return;
}
c->flags |= CLIENT_MULTI;
+
addReply(c,shared.ok);
}
@@ -168,6 +169,11 @@ void execCommand(client *c) {
goto handle_monitor;
}
+ uint64_t old_flags = c->flags;
+
+ /* we do not want to allow blocking commands inside multi */
+ c->flags |= CLIENT_DENY_BLOCKING;
+
/* Exec all the queued commands */
unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
orig_argv = c->argv;
@@ -206,6 +212,7 @@ void execCommand(client *c) {
"no permission to touch the specified keys");
} else {
call(c,server.loading ? CMD_CALL_NONE : CMD_CALL_FULL);
+ serverAssert((c->flags & CLIENT_BLOCKED) == 0);
}
/* Commands may alter argc/argv, restore mstate. */
@@ -213,6 +220,11 @@ void execCommand(client *c) {
c->mstate.commands[j].argv = c->argv;
c->mstate.commands[j].cmd = c->cmd;
}
+
+ // restore old DENY_BLOCKING value
+ if (!(old_flags & CLIENT_DENY_BLOCKING))
+ c->flags &= ~CLIENT_DENY_BLOCKING;
+
c->argv = orig_argv;
c->argc = orig_argc;
c->cmd = orig_cmd;