-
Notifications
You must be signed in to change notification settings - Fork 551
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
Implement Timeouts For Lua Scripts #983
base: main
Are you sure you want to change the base?
Conversation
…ot surprisingly there's a bunch of overhead
… and expensive, so sketch out an on-demand option
…kless; there's some finese in the concurrency around Lua, but the Debug interfaces are threadsafe; all tests passing; still needs benchmarking
…ists and whatnot, since sessions script is already synchronized
…6 (:/), are unnecessary
… attempting to upper case the command; speed that up to claw back some of the timeout overhead
|
||
return true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check warning on line 676 in libs/server/Lua/LuaStateWrapper.cs: XML comment has cref attribute 'SetAllocFunction' that could not be resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are also 3-4 other warnings in the PR, please check those.
@@ -1045,5 +1053,142 @@ public void NoScriptCommandsForbidden() | |||
ClassicAssert.True(exc.Message.StartsWith("ERR This Redis command is not allowed from script"), $"Allowed NoScript command: {cmd}"); | |||
} | |||
} | |||
|
|||
[Test] | |||
public void IntentialTimeout() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit: rename to IntentionalTimeout
For perf reasons this became a little more complicated than I would like, but I think it's justifiable.
Introduces
LuaTimeoutManager
which spins up a dedicatedThread
. WhenSessionScriptCache
s are populated (either for the first time, or after a previousClear
), they register themselves withLuaTimeoutManager
. That registration is used to track when a Lua script is actually running, and to trigger best-effort timeouts.The timeouts themselves use Lua's debug interface which provides a thread-safe way to clear/set hooks. This hook is set dynamically, because always having a hook registered causes a significant performance hit.
Performance issues are also why
LuaTimeoutManager
spawns aThread
. Using built-in timers orTask
s can hurt perf by putting more pressure on the shared thread pool - and most .NET timers also have some internal locking that hurts in practice.To claw back some of the unavoidable overhead, I made three unrelated performance improvements:
ScriptHashKey.Equals
will typically return trueMakeUpperCase
to avoid walking byte-by-byte in the common case, as this is called unconditionally for scripting commandsCorrectness
There are a couple concerns to handle in this design.
The first is to make sure we're not timing out spuriously - that's done by conceptually splitting the timeout into 10ths and waiting ~11 / 10ths before triggering a timeout. This deals with the case where registration happens in the middle of one of the 10ths.
The second is timeouts trigger after a Lua script has finished but before
LuaTimeoutManager
has been informed of that completion. If handled incorrectly, this could cause a subsequent script to be spuriously cancelled. This is handled by generating a "cookie" value when a script starts, and having it passed back on cancellation - if the cookies do not match expected values, the cancellation signal is ignored.I added a stress test that runs 16 sessions in parallel with timeouts for an hour, and ran it for ~12 hours, to check the implementation. So it doesn't seem obviously broken, at least. This test is
[Ignored]
by default.Performance
All runs are as of
d148f51740d79548ad78dc19008fca0e58c0de43
formain
and28e1129eaba16cf60fdc101ed91d455e4a123df3
forluaTimeLimits
.ScriptOperations
Here timeouts are disabled for both branches, reporting just Native workloads.
There's a little bit of motion in either direction, but I'd say it's all within the margin of error.
main
luaTimeLimits
LuaTimeouts
A new benchmark focused on just timeouts, meant for checking improvements there. I'm benchmarking a 1 sec timeout (so checking for timeouts ever 100ms) and a 1 minute (so every 6 seconds there's a check), about the bounds of "reasonable" for timeouts.
These runs have 500K
EVALSHA
s (to tamp down variance). Naively there was about a 10% loss - but with some profiling and the aforementioned performance improvements I clawed that back. With timeouts enabled we're basically the same, with them disabled this is a ~2% improvement.main
This was run by copying the benchmark on top of
d148f51740d79548ad78dc19008fca0e58c0de43
. Obviously the two with timeout cases are not included.luaTimeLimits