Skip to content

Commit

Permalink
Lua scripts promoted from eval to script load to avoid evict
Browse files Browse the repository at this point in the history
In ad28d22, we added a Lua eval
scripts eviction. If the script was previously added via EVAL, we
promote it to SCRIPT LOAD, prevent it from being evicted later.

Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin committed Jun 12, 2024
1 parent e65b2d2 commit a7d1846
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ sds luaCreateFunction(client *c, robj *body, int evalsha) {
sha1hex(funcname + 2, body->ptr, sdslen(body->ptr));

if ((de = dictFind(lctx.lua_scripts, funcname + 2)) != NULL) {
/* If the script was previously added via EVAL, we promote it to
* SCRIPT LOAD, prevent it from being evicted later. */
luaScript *l = dictGetVal(de);
if (evalsha && l->node) {
listDelNode(lctx.lua_scripts_lru_list, l->node);
l->node = NULL;
}
return dictGetKey(de);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/scripting.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,19 @@ start_server {tags {"scripting external:skip"}} {
# cached = num load scripts + 500 eval scripts
assert_equal $cached [expr $num+500]
}

test {Lua scripts promoted from eval to script load} {
r script flush
r config resetstat

r eval "return 'hello world'" 0
set sha [r script load "return 'hello world'"]
for {set j 1} {$j <= 500} {incr j} {
r script load "return $j"
r eval "return 'str_$j'" 0
}
assert_equal {hello world} [r evalsha $sha 0]
}
}

} ;# is_eval
Expand Down

0 comments on commit a7d1846

Please sign in to comment.