summaryrefslogtreecommitdiff
path: root/src/functions.c
diff options
context:
space:
mode:
authorMeir Shpilraien (Spielrein) <meir@redis.com>2022-01-14 14:02:02 +0200
committerGitHub <noreply@github.com>2022-01-14 14:02:02 +0200
commit4db4b434175b519e2e5a78f2d33a7627c483c367 (patch)
treeb78a8c6c24e3d3dfba8f2c3ae85b6652a690f496 /src/functions.c
parent38a511672833037f83669f8f0dc681a7de30bfcb (diff)
downloadredis-4db4b434175b519e2e5a78f2d33a7627c483c367.tar.gz
Function Flags support (no-writes, no-cluster, allow-state, allow-oom) (#10066)
# Redis Functions Flags Following the discussion on #10025 Added Functions Flags support. The PR is divided to 2 sections: * Add named argument support to `redis.register_function` API. * Add support for function flags ## `redis.register_function` named argument support The first part of the PR adds support for named argument on `redis.register_function`, example: ``` redis.register_function{ function_name='f1', callback=function() return 'hello' end, description='some desc' } ``` The positional arguments is also kept, which means that it still possible to write: ``` redis.register_function('f1', function() return 'hello' end) ``` But notice that it is no longer possible to pass the optional description argument on the positional argument version. Positional argument was change to allow passing only the mandatory arguments (function name and callback). To pass more arguments the user must use the named argument version. As with positional arguments, the `function_name` and `callback` is mandatory and an error will be raise if those are missing. Also, an error will be raise if an unknown argument name is given or the arguments type is wrong. Tests was added to verify the new syntax. ## Functions Flags The second part of the PR is adding functions flags support. Flags are given to Redis when the engine calls `functionLibCreateFunction`, supported flags are: * `no-writes` - indicating the function perform no writes which means that it is OK to run it on: * read-only replica * Using FCALL_RO * If disk error detected It will not be possible to run a function in those situations unless the function turns on the `no-writes` flag * `allow-oom` - indicate that its OK to run the function even if Redis is in OOM state, if the function will not turn on this flag it will not be possible to run it if OOM reached (even if the function declares `no-writes` and even if `fcall_ro` is used). If this flag is set, any command will be allow on OOM (even those that is marked with CMD_DENYOOM). The assumption is that this flag is for advance users that knows its meaning and understand what they are doing, and Redis trust them to not increase the memory usage. (e.g. it could be an INCR or a modification on an existing key, or a DEL command) * `allow-state` - indicate that its OK to run the function on stale replica, in this case we will also make sure the function is only perform `stale` commands and raise an error if not. * `no-cluster` - indicate to disallow running the function if cluster is enabled. Default behaviure of functions (if no flags is given): 1. Allow functions to read and write 2. Do not run functions on OOM 3. Do not run functions on stale replica 4. Allow functions on cluster ### Lua API for functions flags On Lua engine, it is possible to give functions flags as `flags` named argument: ``` redis.register_function{function_name='f1', callback=function() return 1 end, flags={'no-writes', 'allow-oom'}, description='description'} ``` The function flags argument must be a Lua table that contains all the requested flags, The following will result in an error: * Unknown flag * Wrong flag type Default behaviour is the same as if no flags are used. Tests were added to verify all flags functionality ## Additional changes * mark FCALL and FCALL_RO with CMD_STALE flag (unlike EVAL), so that they can run if the function was registered with the `allow-stale` flag. * Verify `CMD_STALE` on `scriptCall` (`redis.call`), so it will not be possible to call commands from script while stale unless the command is marked with the `CMD_STALE` flags. so that even if the function is allowed while stale we do not allow it to bypass the `CMD_STALE` flag of commands. * Flags section was added to `FUNCTION LIST` command to provide the set of flags for each function: ``` > FUNCTION list withcode 1) 1) "library_name" 2) "test" 3) "engine" 4) "LUA" 5) "description" 6) (nil) 7) "functions" 8) 1) 1) "name" 2) "f1" 3) "description" 4) (nil) 5) "flags" 6) (empty array) 9) "library_code" 10) "redis.register_function{function_name='f1', callback=function() return 1 end}" ``` * Added API to get Redis version from within a script, The redis version can be provided using: 1. `redis.REDIS_VERSION` - string representation of the redis version in the format of MAJOR.MINOR.PATH 2. `redis.REDIS_VERSION_NUM` - number representation of the redis version in the format of `0x00MMmmpp` (`MM` - major, `mm` - minor, `pp` - patch). The number version can be used to check if version is greater or less another version. The string version can be used to return to the user or print as logs. This new API is provided to eval scripts and functions, it also possible to use this API during functions loading phase.
Diffstat (limited to 'src/functions.c')
-rw-r--r--src/functions.c84
1 files changed, 81 insertions, 3 deletions
diff --git a/src/functions.c b/src/functions.c
index 5a8cd27c1..76e8c21d7 100644
--- a/src/functions.c
+++ b/src/functions.c
@@ -198,7 +198,7 @@ functionsLibCtx* functionsLibCtxCreate() {
* the function will verify that the given name is following the naming format
* and return an error if its not.
*/
-int functionLibCreateFunction(sds name, void *function, functionLibInfo *li, sds desc, sds *err) {
+int functionLibCreateFunction(sds name, void *function, functionLibInfo *li, sds desc, uint64_t f_flags, sds *err) {
if (functionsVerifyName(name) != C_OK) {
*err = sdsnew("Function names can only contain letters and numbers and must be at least one character long");
return C_ERR;
@@ -215,6 +215,7 @@ int functionLibCreateFunction(sds name, void *function, functionLibInfo *li, sds
.function = function,
.li = li,
.desc = desc,
+ .f_flags = f_flags,
};
int res = dictAdd(li->functions, fi->name, fi);
@@ -410,6 +411,24 @@ void functionStatsCommand(client *c) {
dictReleaseIterator(iter);
}
+static void functionListReplyFlags(client *c, functionInfo *fi) {
+ /* First count the number of flags we have */
+ int flagcount = 0;
+ for (scriptFlag *flag = scripts_flags_def; flag->str ; ++flag) {
+ if (fi->f_flags & flag->flag) {
+ ++flagcount;
+ }
+ }
+
+ addReplySetLen(c, flagcount);
+
+ for (scriptFlag *flag = scripts_flags_def; flag->str ; ++flag) {
+ if (fi->f_flags & flag->flag) {
+ addReplyStatus(c, flag->str);
+ }
+ }
+}
+
/*
* FUNCTION LIST [LIBRARYNAME PATTERN] [WITHCODE]
*
@@ -480,7 +499,7 @@ void functionListCommand(client *c) {
dictEntry *function_entry = NULL;
while ((function_entry = dictNext(functions_iter))) {
functionInfo *fi = dictGetVal(function_entry);
- addReplyMapLen(c, 2);
+ addReplyMapLen(c, 3);
addReplyBulkCString(c, "name");
addReplyBulkCBuffer(c, fi->name, sdslen(fi->name));
addReplyBulkCString(c, "description");
@@ -489,6 +508,8 @@ void functionListCommand(client *c) {
} else {
addReplyNull(c);
}
+ addReplyBulkCString(c, "flags");
+ functionListReplyFlags(c, fi);
}
dictReleaseIterator(functions_iter);
@@ -549,12 +570,69 @@ static void fcallCommandGeneric(client *c, int ro) {
return;
}
+ if ((fi->f_flags & SCRIPT_FLAG_NO_CLUSTER) && server.cluster_enabled) {
+ addReplyError(c, "Can not run function on cluster, 'no-cluster' flag is set.");
+ return;
+ }
+
+ if (!(fi->f_flags & SCRIPT_FLAG_ALLOW_OOM) && server.script_oom && server.maxmemory) {
+ addReplyError(c, "-OOM allow-oom flag is not set on the function, "
+ "can not run it when used memory > 'maxmemory'");
+ return;
+ }
+
+ if (server.masterhost && server.repl_state != REPL_STATE_CONNECTED &&
+ server.repl_serve_stale_data == 0 && !(fi->f_flags & SCRIPT_FLAG_ALLOW_STALE))
+ {
+ addReplyError(c, "-MASTERDOWN Link with MASTER is down, "
+ "replica-serve-stale-data is set to 'no' "
+ "and 'allow-stale' flag is not set on the function.");
+ return;
+ }
+
+ if (!(fi->f_flags & SCRIPT_FLAG_NO_WRITES)) {
+ /* Function may perform writes we need to verify:
+ * 1. we are not a readonly replica
+ * 2. no disk error detected
+ * 3. command is not 'fcall_ro' */
+ if (server.masterhost && server.repl_slave_ro && c->id != CLIENT_ID_AOF
+ && !(c->flags & CLIENT_MASTER))
+ {
+ addReplyError(c, "Can not run a function with write flag on readonly replica");
+ return;
+ }
+
+ int deny_write_type = writeCommandsDeniedByDiskError();
+ if (deny_write_type != DISK_ERROR_TYPE_NONE && server.masterhost == NULL) {
+ if (deny_write_type == DISK_ERROR_TYPE_RDB)
+ addReplyError(c, "-MISCONF Redis is configured to save RDB snapshots, "
+ "but it is currently not able to persist on disk. "
+ "So its impossible to run functions that has 'write' flag on.");
+ else
+ addReplyErrorFormat(c, "-MISCONF Redis is configured to persist data to AOF, "
+ "but it is currently not able to persist on disk. "
+ "So its impossible to run functions that has 'write' flag on. "
+ "AOF error: %s", strerror(server.aof_last_write_errno));
+ return;
+ }
+
+ if (ro) {
+ addReplyError(c, "Can not execute a function with write flag using fcall_ro.");
+ return;
+ }
+ }
+
scriptRunCtx run_ctx;
scriptPrepareForRun(&run_ctx, fi->li->ei->c, c, fi->name);
- if (ro) {
+ if (ro || (fi->f_flags & SCRIPT_FLAG_NO_WRITES)) {
+ /* On fcall_ro or on functions that do not have the 'write'
+ * flag, we will not allow write commands. */
run_ctx.flags |= SCRIPT_READ_ONLY;
}
+ if (fi->f_flags & SCRIPT_FLAG_ALLOW_OOM) {
+ run_ctx.flags |= SCRIPT_ALLOW_OOM;
+ }
engine->call(&run_ctx, engine->engine_ctx, fi->function, c->argv + 3, numkeys,
c->argv + 3 + numkeys, c->argc - 3 - numkeys);
scriptResetRun(&run_ctx);