Hierarchy maintenance: stop project-wide rebuilds on every write (stage 0)
メンテナーはふだん 1 日以内に返信
@martastain がすでに取り組んでいます。
2026年11月2日 から。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- postgresql, python, redis
- 領域
- backend, databases, performance
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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)
- 主要言語
- Python
- スター
- 40
- フォーク
- 41
- 平均マージ
- 2日 16時間
- マージ済み PR(30日)
- 47
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ynput/ayon-backend のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ynput/ayon-backend#1256 ·
メンテナーはふだん 1 日以内に返信
-
Filters: like values can no longer anchor with a leading or trailing %再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンtype: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
ynput/ayon-backend#1254 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ynput/ayon-backend#928 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
GraphQL: events query stalls the server by loading payloads that aren't requested対応中かも @BigRoy が 1 日前に担当しました。 オープンtype: enhancement
ynput/ayon-backend#1318 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 28/100
ynput/ayon-backend#1317 ·
メンテナーはふだん 1 日以内に返信
ynput/ayon-backend の issue をすべて見る
似ている issue
-
[Bug]: streaming_end_of_stream_only guardrails rescan the whole stream on every chunk - blocks event loop対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンbug llm translation
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
arith ascend bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
tile-ai/tilelang#5173 · コメント 2 件 · リアクション 1 件 ·
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
lanl/text-fetch#92 ·
メンテナーはふだん 1 日以内に返信
-
kind: for-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
monumental-archive/.github#2284 ·
メンテナーはふだん 1 日以内に返信
-
good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 87/100
TanbirRamim/open-source-radar#761 ·
メンテナーはふだん 1 日以内に返信