Commit f95146b
committed
perf(api): memoize ProviderManager.get_configurations within request/task scope (#27299)
Incremental step for #27299 — assembling `ProviderConfigurations` runs six
independent DB queries plus a provider-factory load, costing ~1s per call in
production. A single workflow step (e.g. a Retrieval node inside an Iteration)
invokes this path many times through independently-created `ProviderManager`
instances, so per-instance caching is not enough.
- `core/provider_manager.py`: add a module-level
`RecyclableContextVar[dict[str, ProviderConfigurations]]` and memoize the
result in `get_configurations` for the current request/task scope, keyed by
`tenant_id`. The cache is shared across `ProviderManager` instances because
callers routinely spawn fresh ones via `create_plugin_provider_manager`.
- `extensions/ext_celery.py`: call `RecyclableContextVar.increment_thread_recycles()`
inside `FlaskTask.__call__`, matching the Flask `before_request` hook in
`app_factory.py` so per-task caches reset cleanly between Celery tasks on
recycled worker threads.
- `tests/unit_tests/core/test_provider_manager.py`: cover the memoization (hit
within a scope, per-tenant isolation, sharing across `ProviderManager`
instances, reset on new scope) behind an autouse fixture that bumps the
recycle counter for each test.
No cross-request invalidation is introduced — the cache lives only for the
current request/task, so mutations performed through subsequent requests are
always observed. This deliberately scopes the change: it targets the reported
hot path (iterative retrieval/LLM nodes) with no staleness risk.
## Verification
- uv run --group dev basedpyright -> 0 errors, 0 warnings (project-wide).
- pytest tests/unit_tests/core/test_provider_manager.py and related -> 40 passed.
- ruff check / ruff format --check -> clean.1 parent 7862b61 commit f95146b
3 files changed
Lines changed: 112 additions & 0 deletions
File tree
- api
- core
- extensions
- tests/unit_tests/core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
63 | 87 | | |
64 | 88 | | |
65 | 89 | | |
| |||
114 | 138 | | |
115 | 139 | | |
116 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
117 | 151 | | |
118 | 152 | | |
119 | 153 | | |
| |||
273 | 307 | | |
274 | 308 | | |
275 | 309 | | |
| 310 | + | |
| 311 | + | |
276 | 312 | | |
277 | 313 | | |
278 | 314 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
78 | 79 | | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
15 | 23 | | |
16 | 24 | | |
17 | 25 | | |
| |||
563 | 571 | | |
564 | 572 | | |
565 | 573 | | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
566 | 638 | | |
567 | 639 | | |
568 | 640 | | |
| |||
0 commit comments