Commit 781bd06
authored
ENG-8212: Redis Oplock implementation (#5932)
* Token manager tracks instance_id in token_to_socket
* RedisTokenManager: keep local dicts globally updated via pub/sub
* Implement lost+found for StateUpdate without websocket
When an update is emitted for a token, but the websocket for that token is on
another instance of the app, post it to the lost+found channel where other
instances are listening for updates to send to their clients.
* Implement `enumerate_tokens` for TokenManager
Set the groundwork for being able to broadcast updates to all connected states.
* Consolidate on `_get_token_owner`
* fix test_connection_banner.py: expect SocketRecord JSON
* Use a single lock waiter
For more efficient and fair lock queueing, each StateManagerRedis uses a single
task to monitor the keyspace for lock release/expire and then wakes up the next
caller that was waiting in the queue (no fairness between separate processes
though).
Now lockers will wait for an `asyncio.Event` which is set by the redis pubsub
waiter. If any locker waits longer than the lock_expiration, it will just try
to get the lock in case there was some mixup with the pub/sub, the locker won't
be blocked forever.
* Redis Oplock implementation
* When taking a lock from redis, hold it for 80% of the lock expiration timeout
* While the lock is held, other events processed against the instance will use
the cached in-memory copy of the state.
* When the timeout expires or another process signals intention to access a
locked state, flush the modifed states to redis and release the lock.
Set REFLEX_OPLOCK_ENABLED=1 to use this feature
* add test_background_task.py::test_fast_yielding
* Implement real redis-backed test cases for lost+found
* add some polling for the emit mocks since L+F doesn't happen immediately
* Fix up unit tests for OPLOCK_ENABLED mode
* support py3.10
* Do not track contended leases in-process
Always check redis for contended leases before granting a lease. It's a bit
slower, but much more reliable and avoids racy lock_expiration timeouts when
contention occurs before the lease is created or when the pubsub hasn't caught
up to reality.
Always start _lock_update_task in __post_init__ to avoid race where the lease
is granted, then contended, but the pubsub task hasn't started to catch the
contention.
* Add real+mock test cases for StateManagerRedis
* update test_state to use mock_redis when real redis is not available
* safe await cancelled task
* explicitly disable oplock for basic test_redis cases
* py3.10 support: asyncio.TimeoutError != TimeoutError
* break out of forever tasks when event loop goes away
No point in continually spamming "no running event loop" to the console.
* generalize "forever" tasks to centralize exception handling/retry
* remove unused arg
* less racy way test_ensure_task_limit_window_passed
* rename REFLEX_STATE_MANAGER_REDIS_DEBUG to match the class name1 parent 62264f1 commit 781bd06
File tree
13 files changed
+1943
-115
lines changed- .github/workflows
- reflex
- istate/manager
- utils
- tests
- integration
- units
- istate
- manager
- utils
13 files changed
+1943
-115
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
65 | 72 | | |
66 | 73 | | |
67 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
727 | 727 | | |
728 | 728 | | |
729 | 729 | | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
730 | 736 | | |
731 | 737 | | |
732 | 738 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
239 | 240 | | |
240 | 241 | | |
241 | 242 | | |
242 | | - | |
| 243 | + | |
243 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
244 | 250 | | |
245 | 251 | | |
246 | 252 | | |
| |||
260 | 266 | | |
261 | 267 | | |
262 | 268 | | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | 269 | | |
278 | 270 | | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
283 | 277 | | |
284 | 278 | | |
285 | 279 | | |
| |||
386 | 380 | | |
387 | 381 | | |
388 | 382 | | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | 383 | | |
407 | 384 | | |
408 | 385 | | |
| |||
412 | 389 | | |
413 | 390 | | |
414 | 391 | | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
419 | 399 | | |
420 | 400 | | |
421 | 401 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
58 | 63 | | |
59 | 64 | | |
60 | 65 | | |
| |||
202 | 207 | | |
203 | 208 | | |
204 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
205 | 215 | | |
206 | 216 | | |
207 | 217 | | |
| |||
451 | 461 | | |
452 | 462 | | |
453 | 463 | | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
Whitespace-only changes.
Whitespace-only changes.
0 commit comments