Repository navigation
refactor(redis): every SingleFlightLock renew and release is one Lua script (#4028) - #4251
Merged
Merged
Conversation
…script (#4028) refresh_if_owned / release_if_owned were GET-then-EXPIRE and GET-then-DELETE: a holder whose lease lapsed between the two commands extended or deleted the successor's lease, and two holders ran at once. The atomic bodies added for the apply lock now carry the plain names; the non-atomic ones are gone. - refresh_if_owned(retake_lapsed=True) renews, or re-takes an absent key with SET NX, in one script. Only the fleet restart passes it, keeping its #1919 re-take; by default a lapsed lease is LOST, as system apply relies on. - One home for the lease scripts (LEASE_*_LUA, KEYS[1] form); the canary leader lease uses them and is pinned to never re-take. Registered on the client per call, no process-wide cache. - A reply that is not one of the script's ASCII integers is DEGRADED, never OWNED, and never raised. - The KEYS/SCAN guard refuses the scan command as an attribute call, as a string literal in any case, and as the bare words, and admits a script's KEYS[n]; 14 spellings are pinned. - tests/unit/_ent695_lua.py is the one executing emulator (race hooks on client commands only, SET NX EX, byte compare, Lua truth, non-positive TTLs, with_lua for fakeredis); every lock client in the caller tests runs the real script text, and the memory retain lease gets a release test.
Contributor
|
merge-train (2026-11-04): validated READY at Notes that ride with it and do not block:
|
This file contains hidden or 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
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.
Closes #4028
What changes
SingleFlightLock.refresh_if_ownedandrelease_if_ownedwere GET-then-EXPIRE and GET-then-DELETE. If a holder's lease lapsed between the two commands and a successor wonSET NXin that gap, the second command extended or deleted the successor's lease, and two holders ran at once. Now both methods are one Lua script each: the token compare and the mutation happen inside Redis._atomicsiblings in trinity-enterprise#695) take the plain names. The non-atomic bodies and the_atomicnames are gone. Every caller is now atomic without a call-site change, except the three that used_atomic(system_service,memory_service), which are renamed.refresh_if_owned(retake_lapsed=True)runs one script that renews our token, or re-takes an absent key withSET NX(REACQUIRED), or reports another token (LOST). Onlyfleet_ops_servicepasses it, so its bug: fleet-restart lock — lease refresh isn't ownership-checked, and acquisition sits outside the try/finally (#1912 review follow-up) #1919 behaviour (finish a half-done run after an unclaimed lapse,lease_reacquiredaudited) is unchanged. By default a lapsed lease is LOST and never re-taken, which is what system apply relies on. With the re-take, LOST means another token holds the key, solast_currentand the foreign/absent log word are removed andstopped_early="lease_lost_foreign"stays accurate.LEASE_REFRESH_LUA,LEASE_REFRESH_OR_RETAKE_LUAandLEASE_RELEASE_LUAlive inredis_breaker_util, inKEYS[1]form. The canary leader lease drops its private copies and uses them. They are registered on the injected client at each call, with no process-wide cache: building aScriptis a local sha1, and redis-py reloads it on NOSCRIPT.'²'andb'\xff'included). A client that never executed the script (for example aMagicMock, whereint(MagicMock())is 1) used to read as a held lease.test_helper_has_no_KEYS_or_scanrefused the substringKEYSanywhere in the file, and that is why the earlier atomic scripts passed the key through ARGV. It now refuses the scan command as an attribute call (.keys(,.scan (,.scan_iter(), as a string literal in any case (execute_command('keys'),redis.pcall('SCAN'),getattr(client, 'keys')), and as the bare wordsSCAN/KEYS, while allowing a script'sKEYS[n]. A parametrised test proves 14 spellings are refused, including 7 that the old substring guard also missed. Dispatch built from string pieces is beyond any text guard.Tests
fakeredis here has no Lua, so with these methods on scripts, 11 test files would have stopped checking anything without any error. fakeredis turns a script call into a swallowed
unknown command 'evalsha', a MagicMock reads as owned, and hand-written fakes released nothing.tests/unit/_ent695_lua.pyis now the one executing emulator:LeaseRedisruns client commands and scripts over one store, and its race hook fires only inside client-side commands, never inside a script.SET NX EXis supported, values are compared as bytes the way Redis compares them, aniftests Lua truth (0 is true), a chained comparison is refused, and a non-positive TTL deletes the key (EXPIRE) or errors (SET EX), as Redis does.with_lua(client)gives a fakeredis client an executingregister_script.Every lock client in the caller tests now runs the real script text.
NXdropped from the re-take, a coercing reply parse, the default refresh re-taking, release not gated onheld, and the fleet restart not passingretake_lapsed. 10/10 red.docker-compose.yml, and 6.2): own / absent / foreign key × str and bytes clients give the expected OWNED / LOST / REACQUIRED, a release leaves a foreign key alone, and a release afterSCRIPT FLUSHstill deletes our key (NOSCRIPT reload). The emulator matches Redis on all three production scripts.Validation: the 33 test files that touch the lock, its callers, the canary or the apply engine: 1238 passed under
pytest-randomlywith seeds 2026, 4028 and 91731. No local full suite; CI runs it.Mutation: 10/10 production mutations red (listed under Tests). Two further tests are red on the code without their fix: odd replies (
'²',b'\xff', fullwidth digit) and the canary heartbeat switched to the re-take script.Unverified: not run against a live Trinity stack. Real-Redis behaviour of the three scripts was checked on throwaway Redis 7.4 and 6.2 servers outside the stack.
tests/unit/test_ent1022_retrieval_memory.py(the other memory-lease test) can't import locally because numpy is missing from the local venv; CI has it.Not in this PR
ResumeLock(session_turn_service.py) keeps its own Lua. It runs on an async client and polls to acquire.SingleFlightLock, in the credential-requirements probe lock and the backup lease (credential_requirements_service.py:895,db_backup_service.py:356), and in the monitoring and operator-queue leader leases. They are untouched here: refactor(redis): the credential-requirements probe lock and the backup lease release with GET-then-DELETE #4252 (probe lock, backup lease) and refactor(redis): the monitoring and operator-queue leader leases release with GET-then-DELETE #4253 (leader leases).sync_health_service.py) keeps its own copy of the release script, and its refresh is GET-then-EXPIRE, the same race this PR fixes. It belongs with the leader leases in refactor(redis): the monitoring and operator-queue leader leases release with GET-then-DELETE #4253, so "one home" above coversSingleFlightLockand canary, not that lease.Upgrade note
Every
SingleFlightLockrenew and release now needs Redis scripting. The bundled compose files already grant+@scripting. An operator running an external Redis whose ACL lacks it was served by the old GET and DELETE; now a release silently leaves the key until its TTL, andops:fleet_restartanswers 409 for up to 2100s after a run. Grant+@scriptingbefore upgrading.Journey Impact: none: internal lock primitive; no user journey changes