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

Abierto Apto para principiantes
#934 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
79/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
django, python
Área
backend, search

Línea de trabajo

Empieza con tag_pre_delete en src/openedx_tagging/signal_handlers.py y compara su consulta con las consultas de linaje limitadas por taxonomía mencionadas en api.py y models/base.py. Añade una prueba de regresión en tests/openedx_tagging/test_models.py que use dos taxonomías con el mismo valor de etiqueta raíz. Se considera terminado cuando eliminar la taxonomía no utilizada no emite ningún evento para el objeto etiquetado únicamente en la otra taxonomía, mientras que el comportamiento de eliminación existente sigue estando cubierto.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
10
Forks
35
Merge medio
5 d 2 h
PR fusionados (30 d)
23

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de openedx/openedx-core

Todos los issues de openedx/openedx-core

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.