Hierarchy maintenance: stop project-wide rebuilds on every write (stage 0)
I maintainer di solito rispondono entro 1 giorno
@martastain ci sta già lavorando.
Dal 2/11/2026.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- postgresql, python, redis
- Ambito
- backend, databases, performance
Direzione di ricerca
Start by tracing FolderEntity.save and refresh_views, then compare the cache rebuild calls in TaskEntity, helpers/thumbnails/store_thumbnail.py, and activities/create_activity.py. Read the entity load queries in folder.py, task.py, product.py, version.py, and representation.py before changing mid-batch hierarchy reads. Done means the proposed invalidation and exported-attributes changes preserve mid-batch folder resolution and avoid unnecessary full rebuilds; the issue names no tests.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
Folder hierarchy maintenance assumes finite projects (~5–10k folders). Sync addons now push much larger, continuously growing trees, and nearly every write triggers project-wide work:
| Trigger | Work done | Cost |
|---|---|---|
FolderEntity.save (create and update, also with auto_commit=False) |
REFRESH MATERIALIZED VIEW hierarchy |
full recursive CTE over all folders, ACCESS EXCLUSIVE lock on a view joined by almost every resolver / ACL check |
FolderEntity.refresh_views |
rebuild_inherited_attributes + rebuild_hierarchy_cache |
full scan of all folders + Python crawl; recursive folder closure joined with products/versions, serialized to one Redis value |
TaskEntity.refresh_views |
rebuild_hierarchy_cache |
same as above, on every task write |
thumbnail store (helpers/thumbnails/store_thumbnail.py) |
rebuild_hierarchy_cache |
same |
reviewable activity (activities/create_activity.py) |
rebuild_hierarchy_cache |
same |
The operations endpoint already defers refresh_views to once per entity type per batch, but FolderEntity.save still refreshes the materialized view per folder, so a batch of N folders does N full refreshes. Outside of operations (REST create/update), each write pays the full price. Sync workloads end up roughly O(N²), with readers blocked by the matview lock.
Stage 0 = cheap, no-schema-change mitigations. The structural fix (incrementally maintained hierarchy in exported_attributes, subtree-only inheritance, flags column) is a separate stage-1 issue.
Proposed changes
1. Invalidate the hierarchy cache instead of rebuilding it
Task, thumbnail and reviewable-activity writes only need to invalidate the project-folders Redis key. FolderListLoader already rebuilds a missing key on read through RequestCoalescer, so a burst of 10k task writes collapses into a single rebuild on the next /folders request.
- Add
invalidate_hierarchy_cache(project_name)— done in #1169 (helpers/hierarchy_cache.py). Besides deleting the key it bumps aproject-folders-gencounter, so a rebuild that was already running when the cache was invalidated discards its result instead of caching stale data.ProjectEntityalready uses it (only when project attrib, folder type or status names change). - Use it in
TaskEntity.refresh_views,store_thumbnail,create_activity— done in #1244. - Same for
FolderEntity.refresh_views(keepsrebuild_inherited_attributes, drops the eager cache rebuild) — done in #1244. - Check other consumers of the cache key that assume it is warm — the only reader is
FolderListLoader(api/folders/list_folders.py), which rebuilds a missing key.dbimportandfix_attribute_valueskeep their explicit rebuilds. Project delete now also usesinvalidate_hierarchy_cache(after its transaction), so a project recreated under the same name can't pick up the old list.
Invalidate after commit, not inside the transaction. ProjectLevelEntity.save() calls commit() → refresh_views() inside its transaction, and store_thumbnail rebuilds inside one too. Today's eager rebuild works there because it runs on the writer's connection and sees its own write. A plain invalidation in the same place does not: a concurrent /folders request can rebuild from a pre-commit snapshot after the invalidation and cache stale data for an hour (the generation counter doesn't help, it was bumped before that reader started). Invalidate after the transaction ends (operations: next to the per-batch refresh_views; REST: after save() returns), as ProjectEntity.commit() does.
→ Solved in #1244 with Postgres.on_commit(hook): hooks added anywhere inside a transaction (including nested transaction() blocks, which join the outer one) run in order after the outermost transaction commits, and are dropped on rollback. Outside a transaction the hook runs immediately, so the operations path (which refreshes views after its transaction) is unchanged. A failing hook is logged and doesn't affect the others or the response. All the call sites above use Postgres.on_commit(lambda: invalidate_hierarchy_cache(project_name)); repeated invalidations within one transaction are cheap (two Redis commands each).
Side effect to accept: during a long sync burst every write invalidates, so in-flight rebuilds keep being discarded and the cache stays cold until the burst ends — each /folders request rebuilds (coalesced per process). Correct, and still cheaper than rebuilding on every write.
2. Insert exported_attributes on create instead of refreshing hierarchy per save
The per-save REFRESH MATERIALIZED VIEW hierarchy in FolderEntity.save exists so that entities created later in the same batch (operations) can resolve the new folder's path. Instead, FolderEntity.save inserts the folder's exported_attributes row in the same transaction — it's O(1) and fully determined by the parent's row:
INSERT INTO exported_attributes (folder_id, path, attrib, active)
SELECT $id,
parent.path || '/' || $name,
(parent.attrib - $non_inheritable_keys) || $own_attrib,
parent.active AND $active
FROM exported_attributes parent
WHERE parent.folder_id = $parent_id
-- root folders: path = $name, attrib = project inheritable attrib || own
Readers that run mid-batch switch from hierarchy to exported_attributes (all currently INNER JOIN hierarchy, so they break for folders created earlier in the batch without the refresh):
- entity load queries:
folder.py(own path),task.py,product.py,version.py,representation.py(task already joinsexported_attributesonfolder_id) FolderEntity.ensure_create_access(parent path lookup)ensure_entity_accessinaccess/utils.py
Everything that runs after commit (GraphQL resolvers, ACL path lists, /folders cache) keeps reading the matview, which is refreshed once per batch / commit in refresh_views. Stage 1 replaces the matview with a plain view over exported_attributes.
Consequences:
- Folder creates no longer need
rebuild_inherited_attributes— the inserted row is already correct. Create-only batches (typical sync workload) skip the full rebuild entirely. - Rename / move still refresh the matview and rebuild inherited attributes (rare; stage 1 makes it subtree-only).
3. Skip inherited-attribute rebuild when nothing inheritable changed
rebuild_inherited_attributes only needs to run when one of these changes:
- folder renamed / moved (creates are handled by the insert in #2; deletes cascade),
activechanged,- an inheritable attribute changed in own attrib.
Status / label / tags / data / thumbnail / non-inheritable attribute edits — the vast majority of sync writes — can skip it. Implementation idea: the entity computes a "hierarchy-affecting" flag on save (it already knows the original vs. new payload) and passes it to commit() / refresh_views(**kwargs). For operations batches, aggregate the flag across the batch and pass it to the single refresh_views call.
4. Split the hierarchy cache by visibility
Inactive/invisible folders (exported_attributes.active = false) are only needed when explicitly requested (visibility=all|hidden) and are mostly dead weight for the default view. Cache them under a separate key (e.g. project-folders / project-folders-hidden) so default /folders requests load and deserialize only visible folders; merge both only when requested.
Notes / risks
- Mid-batch inherited values are only correct if inheritance-affecting changes come before dependent creates are resolved: a batch that moves a folder / changes an inheritable attrib and then creates a child under it would insert the child from the parent's stale exported row. Either fall back to refresh + rebuild before the create in that case, or rely on the end-of-batch rebuild fixing it (it runs because the batch is hierarchy-affecting) and document it.
- Any other code relying on
hierarchybeing fresh inside a transaction needs auditing (grep\.hierarchy). - Full rebuilds stay where they're legitimately needed: project attrib change,
fix_attribute_values, dbimport, setup, attribute type migration.
Out of scope
- Replacing the
hierarchymaterialized view with a plain view overexported_attributes, subtree-only inheritance recompute, JSONBflagscolumn (hasVersions,hasReviewables, …) — stage 1. - Lazy / paginated folder loading, retiring the Redis cache — stage 2.
Related: #1018 (Entity Index)
- Lingua principale
- Python
- Stelle
- 40
- Fork
- 41
- Merge medio
- 2g 16h
- PR unite (30g)
- 47
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ynput/ayon-backend
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
ynput/ayon-backend#1256 ·
I maintainer di solito rispondono entro 1 giorno
-
Filters: like values can no longer anchor with a leading or trailing %Forse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertatype: bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
ynput/ayon-backend#1254 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
ynput/ayon-backend#928 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
GraphQL: events query stalls the server by loading payloads that aren't requestedForse già presa @BigRoy l’ha presa 1 giorno fa. Apertatype: enhancement
ynput/ayon-backend#1318 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 28/100
ynput/ayon-backend#1317 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di ynput/ayon-backend
Issue simili
-
bug llm translation
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
arith ascend bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
tile-ai/tilelang#5173 · 2 commenti · 1 reazione ·
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
lanl/text-fetch#92 ·
I maintainer di solito rispondono entro 1 giorno
-
kind: for-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
monumental-archive/.github#2284 ·
I maintainer di solito rispondono entro 1 giorno
-
good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
TanbirRamim/open-source-radar#761 ·
I maintainer di solito rispondono entro 1 giorno