Deleting a tag or taxonomy emits search-index events for objects tagged in other taxonomies

オープン 初心者向け
#934 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
79/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
django, python
領域
backend, search

調査の方向性

src/openedx_tagging/signal_handlers.py の tag_pre_delete から始め、そのクエリを api.py と models/base.py で言及されている taxonomy スコープの lineage クエリと比較してください。同じルートタグ値を持つ2つの taxonomy を使用して、tests/openedx_tagging/test_models.py に回帰テストを追加してください。未使用の taxonomy を削除したとき、もう一方の taxonomy でのみタグ付けされたオブジェクトに対してイベントが発生せず、既存の削除動作も引き続きカバーされていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Summary

When a tag is deleted, tag_pre_delete in src/openedx_tagging/signal_handlers.py collects the objects whose search documents need refreshing with ObjectTag.objects.filter(tag__lineage__startswith=instance.lineage). A tag's lineage is the tab-separated chain of tag values from the root to the tag, and it carries no taxonomy. The filter therefore matches every object tagged with a tag of the same value path in any taxonomy, not just the taxonomy being changed. Deleting a whole taxonomy runs the same filter for each of its root tags.

The handler was added in https://github.com/openedx/openedx-core/pull/571 for https://github.com/openedx/modular-learning/issues/260, and https://github.com/openedx/openedx-core/pull/575 extended it to taxonomy deletes. The unscoped filter is on main and in the 1.0.x releases.

The cross-taxonomy match does not look intended. The description of https://github.com/openedx/openedx-core/pull/571 speaks of "a tag and its descendants", and the code comments say the root tag's handler "covers the whole subtree". The event spec in https://github.com/openedx/openedx-core/issues/557 asks for events for "every object tagged using that particular tag" on a tag delete and "every object that had been tagged using that taxonomy" on a taxonomy delete. The tests in tests/openedx_tagging/test_models.py use a single taxonomy, so none of them covers the case. Neither PR's review discussed matching across taxonomies.

Steps to reproduce

  1. In Studio, create taxonomy A with a root tag Math, make it available to an organization, and apply Math to a library component or a course unit of that organization.
  2. Create taxonomy B with a root tag Math. Don't apply it to anything.
  3. Delete taxonomy B.
  4. Check the CMS log.

Expected result

Taxonomy B has no object tags, so deleting it emits no CONTENT_OBJECT_ASSOCIATIONS_CHANGED events and logs nothing about associated objects.

Actual result

Following the handler code, CMS logs Deleted tag(s) affected 1 associated objects. Emitted CONTENT_OBJECT_ASSOCIATIONS_CHANGED events. The object tagged in taxonomy A gets an event and its search document is rebuilt, although none of its tags changed. The same happens when only the tag Math is deleted from taxonomy B rather than the whole taxonomy.

The search data stays correct, because the platform handler rebuilds the document from the object's current tags. The cost is extra work: one event and one index update per matching object, for every delete.

Measured on a local Tutor dev stack (openedx-core 7e150e7), where Celery runs eagerly so the events are emitted inside the request: deleting a taxonomy with no object tags of its own, whose root tag value matched 60 objects tagged in other taxonomies, returned 204 after 18.8 s. After those 60 object tags were removed, the same delete took 0.15 s. On an install with Celery workers the request itself stays fast, and the extra events go to the workers instead.

Impact

Tag values like Math, Beginner or Python are common across taxonomies, and taxonomies are often created by copying or re-importing an existing one. On such an install, deleting one tag or one unused taxonomy queues index updates for every object tagged with the same value in any other taxonomy, potentially thousands. Installs without Celery workers, such as development stacks, can hit request timeouts.

Suggested fix

Scope the query to the tag's own taxonomy:

ObjectTag.objects.using(using)
    .filter(tag__taxonomy_id=instance.taxonomy_id, tag__lineage__startswith=instance.lineage)

The other lineage__startswith queries in openedx_tagging already scope to the taxonomy (api.py, and Tag.descendant_count and Tag.save in models/base.py), so this one is the only exception. A regression test can create two taxonomies with the same root tag value, tag an object in the first, delete the second, and assert that no event is emitted.

主要言語
Python
スター
10
フォーク
35
平均マージ
5日 2時間
マージ済み PR(30日)
23

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openedx/openedx-core のほかの issue

openedx/openedx-core の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。