From 82298b812298f11c32efe3b1102dbe892b3b36d8 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Fri, 2 Aug 2024 23:49:51 +0300 Subject: [PATCH] fix(server): Implement SCRIPT GC command (#3431) * fix(server): Implement SCRIPT GC command --- src/core/interpreter.cc | 14 ++++++++++++++ src/core/interpreter.h | 5 +++++ src/server/script_mgr.cc | 20 +++++++++++++++++--- src/server/script_mgr.h | 1 + src/server/server_state.cc | 4 ++++ src/server/server_state.h | 4 ++++ tests/dragonfly/eval_test.py | 23 +++++++++++++++++++++++ 7 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/core/interpreter.cc b/src/core/interpreter.cc index 72d6216638aa..5c2b04644d6e 100644 --- a/src/core/interpreter.cc +++ b/src/core/interpreter.cc @@ -884,6 +884,10 @@ void Interpreter::ResetStack() { lua_settop(lua_, 0); } +void Interpreter::RunGC() { + lua_gc(lua_, LUA_GCCOLLECT); +} + // Returns number of results, which is always 1 in this case. // Please note that lua resets the stack once the function returns so no need // to unwind the stack manually in the function (though lua allows doing this). @@ -1104,4 +1108,14 @@ void InterpreterManager::Reset() { VLOG(1) << "InterpreterManager::Reset ended"; } +void InterpreterManager::Alter(std::function modf) { + vector taken; + swap(taken, available_); // swap data because modf can preempt + + for (Interpreter* ir : taken) { + modf(ir); + Return(ir); + } +} + } // namespace dfly diff --git a/src/core/interpreter.h b/src/core/interpreter.h index b0bb1b924c47..8f25d63d8d49 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -110,6 +110,8 @@ class Interpreter { void ResetStack(); + void RunGC(); + // fp must point to buffer with at least 41 chars. // fp[40] will be set to '\0'. static void FuncSha1(std::string_view body, char* fp); @@ -166,6 +168,9 @@ class InterpreterManager { // Clear all interpreters, keeps capacity. Waits until all are returned. void Reset(); + // Run on all unused interpreters. Those are marked as used at once, so the callback can preempt + void Alter(std::function modf); + static Stats& tl_stats(); private: diff --git a/src/server/script_mgr.cc b/src/server/script_mgr.cc index bf257f334cea..1c950c149c34 100644 --- a/src/server/script_mgr.cc +++ b/src/server/script_mgr.cc @@ -72,7 +72,7 @@ void ScriptMgr::Run(CmdArgList args, ConnectionContext* cntx) { "LOAD