Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new SCRIPT STATS subcommand #1418

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -5458,6 +5458,23 @@ struct COMMAND_ARG SCRIPT_SHOW_Args[] = {
{MAKE_ARG("sha1",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_NONE,0,NULL)},
};

/********** SCRIPT STATS ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* SCRIPT STATS history */
#define SCRIPT_STATS_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* SCRIPT STATS tips */
#define SCRIPT_STATS_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* SCRIPT STATS key specs */
#define SCRIPT_STATS_Keyspecs NULL
#endif

/* 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},
Expand All @@ -5467,6 +5484,7 @@ struct COMMAND_STRUCT SCRIPT_Subcommands[] = {
{MAKE_CMD("kill","Terminates a server-side Lua script during execution.","O(1)","2.6.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_KILL_History,0,SCRIPT_KILL_Tips,2,scriptCommand,2,CMD_NOSCRIPT|CMD_ALLOW_BUSY,ACL_CATEGORY_SCRIPTING,SCRIPT_KILL_Keyspecs,0,NULL,0)},
{MAKE_CMD("load","Loads a server-side Lua script to the script cache.","O(N) with N being the length in bytes of the script body.","2.6.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_LOAD_History,0,SCRIPT_LOAD_Tips,2,scriptCommand,3,CMD_NOSCRIPT|CMD_STALE,ACL_CATEGORY_SCRIPTING,SCRIPT_LOAD_Keyspecs,0,NULL,1),.args=SCRIPT_LOAD_Args},
{MAKE_CMD("show","Show server-side Lua script in the script cache.","O(1).","8.0.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_SHOW_History,0,SCRIPT_SHOW_Tips,0,scriptCommand,3,CMD_NOSCRIPT,ACL_CATEGORY_SCRIPTING,SCRIPT_SHOW_Keyspecs,0,NULL,1),.args=SCRIPT_SHOW_Args},
{MAKE_CMD("stats","Returns information about a script during execution.","O(1).","8.0.0",CMD_DOC_NONE,NULL,NULL,"scripting",COMMAND_GROUP_SCRIPTING,SCRIPT_STATS_History,0,SCRIPT_STATS_Tips,0,scriptCommand,2,CMD_NOSCRIPT|CMD_ALLOW_BUSY,ACL_CATEGORY_SCRIPTING,SCRIPT_STATS_Keyspecs,0,NULL,0)},
{0}
};

Expand Down
55 changes: 55 additions & 0 deletions src/commands/script-stats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"STATS": {
"summary": "Returns information about a script during execution.",
"complexity": "O(1).",
"group": "scripting",
"since": "8.0.0",
"arity": 2,
"container": "SCRIPT",
"function": "scriptCommand",
"command_flags": [
"NOSCRIPT",
"ALLOW_BUSY"
],
"acl_categories": [
"SCRIPTING"
],
"reply_schema": {
"type": "object",
"additionalProperties": false,
"properties": {
"running_script": {
"description": "Information about the running script.",
"oneOf": [
{
"description": "If there's no in-flight script.",
"type": "null"
},
{
"description": "A map with the information about the running script.",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"description": "The name of the script.",
"type": "string"
},
"command": {
"description": "The command and arguments used for invoking the script.",
"type": "array",
"items": {
"type": "string"
}
},
"duration_ms": {
"description": "The script's runtime duration in milliseconds.",
"type": "integer"
}
}
}
]
}
}
}
}
}
21 changes: 21 additions & 0 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ void scriptCommand(client *c) {
" Load a script into the scripts cache without executing it.",
"SHOW <sha1>",
" Show a script from the scripts cache.",
"STATS",
" Returns information about a script during execution.",
NULL,
};
addReplyHelp(c, help);
Expand Down Expand Up @@ -750,6 +752,25 @@ void scriptCommand(client *c) {
} else {
addReplyErrorObject(c, shared.noscripterr);
}
} else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr, "stats")) {
addReplyMapLen(c, 1);

addReplyBulkCString(c, "running_script");
Comment on lines +756 to +758
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the thought we will have more stats in the future? I'm wondering if we should just have a command just for the running script, instead of something generic like stats.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, this part is a reference to FUNCTION STATS.
Currently, there is really only one return value., but as a stat method, it is best to reserve the ability for subsequent extensions.
Alternatively, we can consider adding some Lua-related context, such as Lua execution time/ Lua use Memory, to help users locate problems.

Copy link
Member

@madolson madolson Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the f$% did the Redis folks do this. I swear they barely thought through functions and then dumped it on the community.

if (!scriptIsRunning()) {
addReplyNull(c);
} else {
addReplyMapLen(c, 3);
addReplyBulkCString(c, "name");
addReplyBulkCString(c, scriptCurrFunction());
addReplyBulkCString(c, "command");
client *script_client = scriptGetCaller();
addReplyArrayLen(c, script_client->argc);
for (int i = 0; i < script_client->argc; ++i) {
addReplyBulkCBuffer(c, script_client->argv[i]->ptr, sdslen(script_client->argv[i]->ptr));
}
addReplyBulkCString(c, "duration_ms");
addReplyLongLong(c, scriptRunDuration());
}
} else {
addReplySubcommandSyntaxError(c);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/scripting.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -1161,10 +1161,13 @@ start_server {tags {"scripting"}} {
test {Timedout read-only scripts can be killed by SCRIPT KILL} {
set rd [valkey_deferring_client]
r config set lua-time-limit 10
assert_match {running_script {}} [r SCRIPT STATS]

run_script_on_connection $rd {while true do end} 0
after 200
catch {r ping} e
assert_match {BUSY*} $e
assert_match {running_script {name * command * duration_ms *}} [r SCRIPT STATS]
kill_script
after 200 ; # Give some time to Lua to call the hook again...
assert_equal [r ping] "PONG"
Expand Down
Loading