-
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
Open
kevin-montrose
wants to merge
21
commits into
main
Choose a base branch
from
luaTimeLimits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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