From 5c2fda5f730d92a9e1acf47db9a15fc33e9d4e7a Mon Sep 17 00:00:00 2001 From: Binbin Date: Wed, 15 Jan 2025 16:31:49 +0800 Subject: [PATCH] handle extended_redis_compat Signed-off-by: Binbin --- src/config.c | 2 +- src/db.c | 6 +++--- src/debug.c | 2 +- src/functions.c | 2 +- src/script.c | 4 ++-- src/server.c | 42 +++++++++++++++++++++++++++++++++++------- src/server.h | 1 + 7 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/config.c b/src/config.c index 5b90ebbd60..9629cd0072 100644 --- a/src/config.c +++ b/src/config.c @@ -919,7 +919,7 @@ void configSetCommand(client *c) { err: if (deny_loading_error) { /* We give the loading error precedence because it may be handled by clients differently, unlike a plain -ERR. */ - addReplyErrorObject(c, shared.loadingerr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.loadingerr_compat : shared.loadingerr); } else if (invalid_arg_name) { addReplyErrorFormat(c, "Unknown option or number of arguments for CONFIG SET - '%s'", invalid_arg_name); } else if (errstr) { diff --git a/src/db.c b/src/db.c index 94074bf668..d09bc14de4 100644 --- a/src/db.c +++ b/src/db.c @@ -1361,11 +1361,11 @@ void shutdownCommand(client *c) { if (server.busy_module_yield_flags && server.busy_module_yield_reply) { addReplyErrorFormat(c, "-BUSY %s", server.busy_module_yield_reply); } else if (server.busy_module_yield_flags) { - addReplyErrorObject(c, shared.slowmoduleerr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.slowmoduleerr_compat : shared.slowmoduleerr); } else if (scriptIsEval()) { - addReplyErrorObject(c, shared.slowevalerr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.slowevalerr_compat : shared.slowevalerr); } else { - addReplyErrorObject(c, shared.slowscripterr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.slowscripterr_compat : shared.slowscripterr); } return; } diff --git a/src/debug.c b/src/debug.c index 915e0c264d..b96386b193 100644 --- a/src/debug.c +++ b/src/debug.c @@ -723,7 +723,7 @@ void debugCommand(client *c) { if (getPositiveLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != C_OK) return; if (server.loading || server.async_loading) { - addReplyErrorObject(c, shared.loadingerr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.loadingerr_compat : shared.loadingerr); return; } diff --git a/src/functions.c b/src/functions.c index 0d003f7fac..5b7992dbb0 100644 --- a/src/functions.c +++ b/src/functions.c @@ -554,7 +554,7 @@ int functionsUnregisterEngine(const char *engine_name) { */ void functionStatsCommand(client *c) { if (scriptIsRunning() && scriptIsEval()) { - addReplyErrorObject(c, shared.slowevalerr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.slowevalerr_compat : shared.slowevalerr); return; } diff --git a/src/script.c b/src/script.c index a8e5b18eb9..0d420b64fd 100644 --- a/src/script.c +++ b/src/script.c @@ -315,12 +315,12 @@ void scriptKill(client *c, int is_eval) { } if (is_eval && !(curr_run_ctx->flags & SCRIPT_EVAL_MODE)) { /* Kill a function with 'SCRIPT KILL' is not allow */ - addReplyErrorObject(c, shared.slowscripterr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.slowscripterr_compat : shared.slowscripterr); return; } if (!is_eval && (curr_run_ctx->flags & SCRIPT_EVAL_MODE)) { /* Kill an eval with 'FUNCTION KILL' is not allow */ - addReplyErrorObject(c, shared.slowevalerr); + addReplyErrorObject(c, server.extended_redis_compat ? shared.slowevalerr_compat : shared.slowevalerr); return; } curr_run_ctx->flags |= SCRIPT_KILLED; diff --git a/src/server.c b/src/server.c index e8effe462c..bb112b4d9e 100644 --- a/src/server.c +++ b/src/server.c @@ -1963,19 +1963,39 @@ void afterSleep(struct aeEventLoop *eventLoop, int numevents) { * called when the config changes. When the config is phased out, these * initializations can be moved back inside createSharedObjects() below. */ void createSharedObjectsWithCompat(void) { - const char *name = server.extended_redis_compat ? "Redis" : SERVER_TITLE; + const char *name = SERVER_TITLE; + const char *name_compat = "Redis"; + shared.loadingerr = makeObjectShared(createObject( OBJ_STRING, sdscatfmt(sdsempty(), "-LOADING %s is loading the dataset in memory\r\n", name))); + shared.loadingerr_compat = makeObjectShared(createObject( + OBJ_STRING, sdscatfmt(sdsempty(), "-LOADING %s is loading the dataset in memory\r\n", name_compat))); + shared.slowevalerr = makeObjectShared(createObject( OBJ_STRING, sdscatfmt(sdsempty(), "-BUSY %s is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n", name))); + shared.slowevalerr_compat = makeObjectShared(createObject( + OBJ_STRING, + sdscatfmt(sdsempty(), + "-BUSY %s is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n", + name_compat))); + shared.slowscripterr = makeObjectShared(createObject( OBJ_STRING, sdscatfmt(sdsempty(), "-BUSY %s is busy running a script. You can only call FUNCTION KILL or SHUTDOWN NOSAVE.\r\n", name))); + shared.slowscripterr_compat = makeObjectShared(createObject( + OBJ_STRING, + sdscatfmt(sdsempty(), + "-BUSY %s is busy running a script. You can only call FUNCTION KILL or SHUTDOWN NOSAVE.\r\n", + name_compat))); + shared.slowmoduleerr = makeObjectShared( createObject(OBJ_STRING, sdscatfmt(sdsempty(), "-BUSY %s is busy running a module command.\r\n", name))); + shared.slowmoduleerr_compat = makeObjectShared( + createObject(OBJ_STRING, sdscatfmt(sdsempty(), "-BUSY %s is busy running a module command.\r\n", name_compat))); + shared.bgsaveerr = makeObjectShared( createObject(OBJ_STRING, sdscatfmt(sdsempty(), "-MISCONF %s is configured to save RDB snapshots, but it's currently" @@ -1984,6 +2004,14 @@ void createSharedObjectsWithCompat(void) { " writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)." " Please check the %s logs for details about the RDB error.\r\n", name, name))); + shared.bgsaveerr_compat = makeObjectShared( + createObject(OBJ_STRING, sdscatfmt(sdsempty(), + "-MISCONF %s is configured to save RDB snapshots, but it's currently" + " unable to persist to disk. Commands that may modify the data set are" + " disabled, because this instance is configured to report errors during" + " writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)." + " Please check the %s logs for details about the RDB error.\r\n", + name, name_compat))); } void createSharedObjects(void) { @@ -4257,13 +4285,13 @@ int processCommand(client *c) { /* Loading DB? Return an error if the command has not the * CMD_LOADING flag. */ if (server.loading && !server.async_loading && is_denyloading_command) { - rejectCommand(c, shared.loadingerr); + rejectCommand(c, server.extended_redis_compat ? shared.loadingerr_compat : shared.loadingerr); return C_OK; } /* During async-loading, block certain commands. */ if (server.async_loading && is_deny_async_loading_command) { - rejectCommand(c, shared.loadingerr); + rejectCommand(c, server.extended_redis_compat ? shared.loadingerr_compat : shared.loadingerr); return C_OK; } @@ -4278,11 +4306,11 @@ int processCommand(client *c) { if (server.busy_module_yield_flags && server.busy_module_yield_reply) { rejectCommandFormat(c, "-BUSY %s", server.busy_module_yield_reply); } else if (server.busy_module_yield_flags) { - rejectCommand(c, shared.slowmoduleerr); + rejectCommand(c, server.extended_redis_compat ? shared.slowmoduleerr_compat : shared.slowmoduleerr); } else if (scriptIsEval()) { - rejectCommand(c, shared.slowevalerr); + rejectCommand(c, server.extended_redis_compat ? shared.slowevalerr_compat : shared.slowevalerr); } else { - rejectCommand(c, shared.slowscripterr); + rejectCommand(c, server.extended_redis_compat ? shared.slowscripterr_compat : shared.slowscripterr); } return C_OK; } @@ -4634,7 +4662,7 @@ int writeCommandsDeniedByDiskError(void) { sds writeCommandsGetDiskErrorMessage(int error_code) { sds ret = NULL; if (error_code == DISK_ERROR_TYPE_RDB) { - ret = sdsdup(shared.bgsaveerr->ptr); + ret = sdsdup(server.extended_redis_compat ? shared.bgsaveerr_compat->ptr : shared.bgsaveerr->ptr); } else { ret = sdscatfmt(sdsempty(), "-MISCONF Errors writing to the AOF file: %s\r\n", strerror(server.aof_last_write_errno)); diff --git a/src/server.h b/src/server.h index d186d16c73..49b1cd8c1a 100644 --- a/src/server.h +++ b/src/server.h @@ -1286,6 +1286,7 @@ struct sharedObjectsStruct { robj *ok, *err, *emptybulk, *czero, *cone, *pong, *space, *queued, *null[4], *nullarray[4], *emptymap[4], *emptyset[4], *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr, *outofrangeerr, *noscripterr, *loadingerr, *slowevalerr, *slowscripterr, *slowmoduleerr, *bgsaveerr, *primarydownerr, *roreplicaerr, + *loadingerr_compat, *slowevalerr_compat, *slowscripterr_compat, *slowmoduleerr_compat, *bgsaveerr_compat, *execaborterr, *noautherr, *noreplicaserr, *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk, *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink, *rpop, *lpop, *lpush, *rpoplpush, *lmove, *blmove, *zpopmin, *zpopmax, *emptyscan, *multi, *exec, *left, *right, *hset, *srem,