Skip to content

Commit

Permalink
script add dump subcommand
Browse files Browse the repository at this point in the history
Change-Id: I60aacd3198652174883f335b36efc18d69a1fb43
Signed-off-by: wei.kukey <wei.kukey@gmail.com>
  • Loading branch information
kukey authored and huangwei.1123 committed Jun 10, 2024
1 parent 71dd85d commit f22bd20
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -5210,6 +5210,31 @@ struct COMMAND_ARG SCRIPT_DEBUG_Args[] = {
{MAKE_ARG("mode",ARG_TYPE_ONEOF,-1,NULL,NULL,NULL,CMD_ARG_NONE,3,NULL),.subargs=SCRIPT_DEBUG_mode_Subargs},
};

/********** SCRIPT DUMP ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* SCRIPT DUMP history */
#define SCRIPT_DUMP_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* SCRIPT DUMP tips */
const char *SCRIPT_DUMP_Tips[] = {
"request_policy:all_shards",
"response_policy:agg_logical_and",
};
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* SCRIPT DUMP key specs */
#define SCRIPT_DUMP_Keyspecs NULL
#endif

/* SCRIPT DUMP argument table */
struct COMMAND_ARG SCRIPT_DUMP_Args[] = {
{MAKE_ARG("sha1",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_MULTIPLE,0,NULL)},
};

/********** SCRIPT EXISTS ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
Expand Down Expand Up @@ -5333,6 +5358,7 @@ struct COMMAND_ARG SCRIPT_LOAD_Args[] = {
/* SCRIPT command table */
struct COMMAND_STRUCT SCRIPT_Subcommands[] = {
{MAKE_CMD("debug","Sets the debug mode of server-side Lua scripts.","O(1)","3.2.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_DEBUG_History,0,SCRIPT_DEBUG_Tips,0,scriptCommand,3,CMD_NOSCRIPT,ACL_CATEGORY_SCRIPTING,SCRIPT_DEBUG_Keyspecs,0,NULL,1),.args=SCRIPT_DEBUG_Args},
{MAKE_CMD("dump","Dump server-side Lua scripts in the script cache.","O(N) with N being the number of scripts to check (so checking a single script is an O(1) operation).","7.2.4",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_DUMP_History,0,SCRIPT_DUMP_Tips,2,scriptCommand,-3,CMD_NOSCRIPT,ACL_CATEGORY_SCRIPTING,SCRIPT_DUMP_Keyspecs,0,NULL,1),.args=SCRIPT_DUMP_Args},
{MAKE_CMD("exists","Determines whether server-side Lua scripts exist in the script cache.","O(N) with N being the number of scripts to check (so checking a single script is an O(1) operation).","2.6.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_EXISTS_History,0,SCRIPT_EXISTS_Tips,2,scriptCommand,-3,CMD_NOSCRIPT,ACL_CATEGORY_SCRIPTING,SCRIPT_EXISTS_Keyspecs,0,NULL,1),.args=SCRIPT_EXISTS_Args},
{MAKE_CMD("flush","Removes all server-side Lua scripts from the script cache.","O(N) with N being the number of scripts in cache","2.6.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_FLUSH_History,1,SCRIPT_FLUSH_Tips,2,scriptCommand,-2,CMD_NOSCRIPT,ACL_CATEGORY_SCRIPTING,SCRIPT_FLUSH_Keyspecs,0,NULL,1),.args=SCRIPT_FLUSH_Args},
{MAKE_CMD("help","Returns helpful text about the different subcommands.","O(1)","5.0.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_HELP_History,0,SCRIPT_HELP_Tips,0,scriptCommand,2,CMD_LOADING|CMD_STALE,ACL_CATEGORY_SCRIPTING,SCRIPT_HELP_Keyspecs,0,NULL,0)},
Expand Down
44 changes: 44 additions & 0 deletions src/commands/script-dump.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"DUMP": {
"summary": "Dump server-side Lua scripts in the script cache.",
"complexity": "O(N) with N being the number of scripts to check (so checking a single script is an O(1) operation).",
"group": "scripting",
"since": "7.2.4",
"arity": -3,
"container": "SCRIPT",
"function": "scriptCommand",
"command_flags": [
"NOSCRIPT"
],
"acl_categories": [
"SCRIPTING"
],
"command_tips": [
"REQUEST_POLICY:ALL_SHARDS",
"RESPONSE_POLICY:AGG_LOGICAL_AND"
],
"arguments": [
{
"name": "sha1",
"type": "string",
"multiple": true
}
],
"reply_schema": {
"description": "An array of lua scripts that correspond to the specified SHA1 digest arguments.",
"type": "array",
"items": {
"oneOf": [
{
"description": "lua script if sha1 hash exists in script cache.",
"type": "string"
},
{
"description": "null if sha1 hash does not exist in script cache.",
"type": "null"
}
]
}
}
}
}
22 changes: 22 additions & 0 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* 2. scriptingInit() - initServer() function from server.c invokes this to initialize LUA at startup.
* It is also invoked between 2 eval invocations to reset Lua.
*/
#include "dict.h"
#include "server.h"
#include "sha1.h"
#include "rand.h"
Expand Down Expand Up @@ -675,6 +676,8 @@ void scriptCommand(client *c) {
" Kill the currently executing Lua script.",
"LOAD <script>",
" Load a script into the scripts cache without executing it.",
"DUMP <sha1> [<sha1> ...]",
" Dump a script from the scipts cache.",
NULL
};
/* clang-format on */
Expand Down Expand Up @@ -728,6 +731,25 @@ NULL
addReplyError(c, "Use SCRIPT DEBUG YES/SYNC/NO");
return;
}
} else if (c->argc > 2 && !strcasecmp(c->argv[1]->ptr, "dump")) {
int j;
dictEntry *de;
luaScript *ls;

addReplyArrayLen(c, c->argc - 2);
for (j = 2; j < c->argc; j++) {
if (sdslen(c->argv[j]->ptr) != 40) {
addReply(c, shared.null[c->resp]);
continue;
}

if ((de = dictFind(lctx.lua_scripts, c->argv[j]->ptr))) {
ls = dictGetVal(de);
addReplyBulk(c, ls->body);
} else {
addReply(c, shared.null[c->resp]);
}
}
} else {
addReplySubcommandSyntaxError(c);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/scripting.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ start_server {tags {"scripting"}} {
[r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0]
} {b534286061d4b9e4026607613b95c06c06015ae8 loaded}

test {SCRIPT DUMP - is able to dump scripts from the scripting cache} {
list \
[r script dump b534286061d4b9e4026607613b95c06c06015ae8 b534286061d4b9 b534286061d4b9e4026607613b95c06c06015ae8 b534286061d4b9e4026607613b95c06c06015a11] \
} {{{return 'loaded'} {} {return 'loaded'} {}}}

test "SORT is normally not alpha re-ordered for the scripting engine" {
r del myset
r sadd myset 1 2 3 4 10
Expand Down

0 comments on commit f22bd20

Please sign in to comment.