perf: pipeline redis set_state writes, cache required-state classes
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
StateManagerRedis.set_state recursed over the substate tree spawning a task per substate; every recursive call re-GETed the same lock key and re-PTTLed it, and each touched state got its own unpipelined SET — roughly 2N+1 redis round trips per event flush for N states. get_state already pipelines its reads; set_state now does the same for writes (ENG-10097):
- Verify the lock once, write once: the tree is walked iteratively, the lock GET + PTTL happen a single time, and all touched states' payloads go out in one pipeline. 3 round trips per flush regardless of tree size.
- Off-loop pickling: states whose previous payload exceeded 64 KiB are serialized via
asyncio.to_threadinstead of stalling the event loop (safe: the token's lock is held, so nothing mutates the tree mid-pickle — the old code awaited between pickles too). - Cached
_get_required_state_classes: the recursive class-level computation ran on everyget_state. It's now anlru_cached module function keyed on(target class, registration context, state hierarchy generation);BaseState.__init_subclass__andreload_state_modulebump the generation, so dynamically created classes and hot reload invalidate cleanly (including same-name class redefinition, where registry sizes don't change). _was_touchedreset after write (mirrors the disk manager's existingget_and_reset_touched_state): previously the flag was never cleared, so a once-touched state kept alive by the oplock cache was re-pickled and re-SET on every subsequent flush. Reset only happens after a successful pipeline execute, so failed writes retry.
Round trips per event flush (modeled on the exact before/after command flows; "3" = lock GET + PTTL + one pipelined write):
| state tree | before | after | worst-case serialized latency @ 0.25 ms RTT |
|---|---|---|---|
| root + 3 substates | 12 | 3 | 3.00 ms → 0.75 ms (4x) |
| root + 9 substates | 30 | 3 | 7.50 ms → 0.75 ms (10x) |
| 3 levels × 3 wide (13 states) | 39 | 3 | 9.75 ms → 0.75 ms (13x) |
New unit tests (run against both mock and real redis in CI): test_set_state_verifies_lock_once_and_pipelines_writes counts actual GET/PTTL commands during a 3-substate modify (asserts exactly one of each); test_set_state_resets_touched_flag asserts a second flush of an unchanged instance creates no write pipeline at all; test_set_state_offloads_large_pickles asserts the to_thread path engages above the threshold; test_required_state_classes_cache_invalidation asserts a subclass defined after cache warm-up appears in the cached result via the generation bump. Existing modify_state/oplock/expiration tests cover end-to-end behavior.
Linear: ENG-10097
🤖 Generated with Claude Code
https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx
Generated by Claude Code