Repository navigation
OpenConceptLab/ocl_online#740 | Deleting a collection, or one of its versions, reindexes its former members - #1046
Conversation
…versions, reindexes its former members A concept's or mapping's search document names every collection version holding it, read from the expansions. Deleting a collection removed the rows but left its members' documents as they were, so search kept the deleted collection's name and URLs on them until something else reindexed them: in the collection filters and facets, and for a collection later created under the same name and URL. Collection.delete now reads the ids of the concepts and mappings in the expansions of the version being deleted (of every version, when HEAD is), and once the delete commits it queues their reindex, as Source.delete does since OpenConceptLab/ocl_online#744. The chunked queueing moves from Source.queue_reindex to ConceptContainerModel.queue_children_reindex, unchanged: 5,000 ids a Task on the indexing queue, with its arguments saved so it can be rerun, and a chunk that can't be queued left as a failed Task. Source.queue_reindex keeps its latest-release step. delete_collection no longer deletes a version's expansions before the version: that would hide their members from Collection.delete. The delete's cascade removes them, as it already did for the other versions when HEAD is deleted. This covers the HEAD and version DELETE routes and an organization's delete. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
snyaggarwal
left a comment
There was a problem hiding this comment.
Reviewed the diff and the paths it touches: delete_collection, Organization.delete, ConceptContainerModel.delete cascading to versions and expansions, and ConceptDocument reading collection_url from expansion_set. The approach looks right. Reading the ids before super().delete() and queueing after commit matches what Source.delete does. Dropping the explicit expansions.all().delete() is needed so the ids are still there to read, and the cascade removes the expansions anyway.
No blocking issues. Five nits are inline. The most useful is robust=True on the on_commit callback.
The gaps listed in the description, a user's hard delete and deleting a non-default expansion, are confirmed. I have nothing to add to them.
| field, flat=True)) for relation, field in ((Expansion.concepts, 'concept_id'), | ||
| (Expansion.mappings, 'mapping_id'))) | ||
| super().delete(using=using, keep_parents=keep_parents, force=force, sync=sync) | ||
| transaction.on_commit(lambda: self.queue_children_reindex(concept_ids, mapping_ids)) |
There was a problem hiding this comment.
Nit (optional): this on_commit callback isn't robust. delete_collection runs in autocommit, so the callback fires right away inside collection.delete. If queue_children_reindex raises (for example Task.new hits a DB error), the exception reaches delete_collection's except, which logs "Collection delete failed" and returns False for a delete that already committed. Inside Organization.delete's atomic block, it propagates after the commit. queue_count_task already uses robust=True for this reason. Source.delete has the same pattern, so it could be fixed there too.
| transaction.on_commit(lambda: self.queue_children_reindex(concept_ids, mapping_ids)) | |
| transaction.on_commit(lambda: self.queue_children_reindex(concept_ids, mapping_ids), robust=True) |
There was a problem hiding this comment.
Done in e068a8c: the callback is robust=True, and Source.delete's too. It's also registered in a finally (Codex F1.1), so a failure after the rows are deleted still queues the reindex.
| concept_ids, mapping_ids = (set( | ||
| relation.through.objects.filter(expansion__collection_version_id__in=version_ids).values_list( | ||
| field, flat=True)) for relation, field in ((Expansion.concepts, 'concept_id'), | ||
| (Expansion.mappings, 'mapping_id'))) |
There was a problem hiding this comment.
Nit (optional): the tuple-unpacked generator is hard to read for just two queries. Two explicit lines say the same thing:
| concept_ids, mapping_ids = (set( | |
| relation.through.objects.filter(expansion__collection_version_id__in=version_ids).values_list( | |
| field, flat=True)) for relation, field in ((Expansion.concepts, 'concept_id'), | |
| (Expansion.mappings, 'mapping_id'))) | |
| expansions = dict(expansion__collection_version_id__in=version_ids) | |
| concept_ids = set(Expansion.concepts.through.objects.filter(**expansions).values_list('concept_id', flat=True)) | |
| mapping_ids = set(Expansion.mappings.through.objects.filter(**expansions).values_list('mapping_id', flat=True)) |
There was a problem hiding this comment.
Done in e068a8c, with your two explicit lines.
| """ | ||
| from core.sources.constants import DROPPED_CHILDREN_CHUNK_SIZE | ||
| from core.tasks.models import Task | ||
| user = user or self.updated_by |
There was a problem hiding this comment.
Nit (optional): when a collection version is deleted, self is the version, so the reindex Tasks belong to that version's last editor. That may be neither the person deleting it nor HEAD's editor. Source.delete calls this on head (head.queue_reindex), so the two repo types now differ. To match Source, Collection.delete could pass user=self.head.updated_by, or call it on the head, when self isn't HEAD.
There was a problem hiding this comment.
Done in e068a8c: a version's reindex Tasks belong to HEAD's last editor (get(self, 'head.updated_by'), read before the delete), as Source.delete's do.
| indexing queue, with its arguments saved so it can be rerun; a chunk that can't be queued is left as a failed | ||
| Task. user: who the Tasks belong to (and can cancel them), by default this repository's last editor. | ||
| """ | ||
| from core.sources.constants import DROPPED_CHILDREN_CHUNK_SIZE |
There was a problem hiding this comment.
Nit (optional): this helper is now shared by collections, but its chunk size still comes from core.sources.constants through a lazy import. Moving DROPPED_CHILDREN_CHUNK_SIZE, or a neutral alias for it, to core.common.constants would let this module import it at the top and drop the dependency on sources.
There was a problem hiding this comment.
Done in e068a8c: REINDEX_CHUNK_SIZE in core.common.constants, imported at the top of core.common.models. DROPPED_CHILDREN_CHUNK_SIZE is now that value, so its existing uses and test patches are unchanged.
|
|
||
| @patch('core.common.models.delete_s3_objects', Mock()) | ||
| @patch('core.common.models.batch_index_resources') | ||
| def test_deleting_a_collection_indexes_its_former_members_again(self, batch_index_resources_mock): |
There was a problem hiding this comment.
Nit (optional): the description says an organization's delete is covered, but only Collection.delete and delete_collection are tested. Organization.delete runs everything in one transaction.atomic() and iterates collection_set (HEAD and versions, in any order), so its reindexes queue only after the outer commit. A short test that deletes an org with a collection HEAD and a version, then checks the reindex calls after commit, would keep that path from regressing silently.
There was a problem hiding this comment.
Added in e068a8c: test_deleting_an_organization_indexes_its_collections_former_members_again deletes an org holding a collection HEAD and a version with another owner's concept. Nothing is queued until the outer commit, and then both rows' ids are.
… failure and doesn't fail the delete; it belongs to HEAD's last editor Review on oclapi2#1046 (Codex pass 1 F1.1, and Sunny's nits): - Collection.delete registers the reindex in a `finally`, so a failure after the rows are deleted (queueing the export cleanup) still reindexes the former members. A rollback drops it. - The callback is `robust`: in autocommit it runs inside the delete, and a failure there (Task.new on a DB error) no longer makes delete_collection report a committed delete as failed. Source.delete's callback is robust too. - A version's reindex Tasks belong to HEAD's last editor, as Source.delete's do. - The chunk size is REINDEX_CHUNK_SIZE in core.common.constants; DROPPED_CHILDREN_CHUNK_SIZE is that value. - Two explicit queries read the ids. A test covers an organization's delete: one transaction, reindexed after it commits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
Codex review, pass 1 (codex-cli 0.169.0, commit a1aa1c6)Verdict: findings: 4 P2, no P0 or P1. One fixed in e068a8c; three accepted.
Detailed scenarios are on the tracking ticket. |
…port made before it had a default expansion The delete now runs with the version's expansions still in place, so its export prefix names its default expansion. An export made while the version had none sits under the prefix without it, which the delete cleared before. Both are cleared now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTz8ve2veELx7gFNaJ
Codex review, pass 2 (codex-cli 0.169.0, commit e068a8c)Verdict: findings: 2 new P2s, no P0 or P1. Pass 1's F1.1 is confirmed fixed; F1.2-F1.4 stay accepted as before. One new finding fixed in 424bbe4, one accepted.
Detailed scenarios are on the tracking ticket. |
snyaggarwal
left a comment
There was a problem hiding this comment.
Re-review of e068a8c and 424bbe4.
All five earlier nits are fixed as described in the thread replies:
robust=Trueon both on_commit calls- the two explicit id queries
- HEAD's last editor owns the Tasks
REINDEX_CHUNK_SIZEincore.common.constants- the organization delete test
The Codex passes check out too. The F1.1 finally works: inside a transaction a rollback drops the callback, and in autocommit it runs at once with the rows already gone. The F2.1 fix and its test cover the current key format. I agree with accepting F1.2-F1.4 and F2.2. They're the same races and gaps Source.delete has, and a full reindex corrects them.
New in this pass:
- Important (inline): the legacy export prefix needs the same bare-prefix treatment as F2.1, with its own ownership check.
- Pre-existing (inline):
Source.deletelacks the F1.1finally. - Nit, no change needed: the
finallyalso registers the reindex whensuper().delete()raises before deleting anything, for example a ValidationError when deleting the only version withoutforce. The extra reindex rewrites unchanged documents and does no harm.
| bare = self.get_version_export_path(suffix=None, with_expansion=False) | ||
| if bare != prefixes[0][0]: | ||
| prefixes.append((bare, get_export_key_pattern(bare))) |
There was a problem hiding this comment.
Important: this restores the bare prefix for the current key format, but the legacy prefix loses its bare form in the same way, as in F2.1. Before this PR, delete_collection deleted the expansions first, so get_export_key_prefixes built both the current and the legacy prefix without the expansion. Now the legacy entry carries the expansion (orgs/O/O_C_v1_exp.). A legacy export taken before the version had a default expansion (orgs/O/O_C_v1.) is no longer cleared.
It can't be a one-line append like the current-format bare prefix. owns_legacy_export_path checks the legacy path with the expansion, and a sibling with the same legacy name (a source C with version v1, or version 1 against v1) builds the same bare legacy prefix. So the bare legacy prefix needs its own ownership check before it's added. If you'd rather accept the gap, note it with F2.1 on the ticket.
There was a problem hiding this comment.
Fixed in a546ff6 (Codex F3.1/F3.2), and I checked the fix: the bare legacy prefix is cleared only when this version owns it.
There was a problem hiding this comment.
Thanks for checking. 463dcc8 adds your example as a test: collection C v1's old-format prefix without its expansion stays when source C v1 builds it. The same commit fixes what the Codex check of a546ff6 found: on a HEAD delete, versions deleted in the same call no longer count as other owners, so a prefix that only they build (version a with expansion b, and version a_b) is cleared.
| @@ -1090,30 +1089,17 @@ def delete(self, using=None, keep_parents=False, force=False, sync=False): | |||
| concept_ids = list(self.concepts.values_list('id', flat=True)) | |||
| mapping_ids = list(self.mappings.values_list('id', flat=True)) | |||
| super().delete(using=using, keep_parents=keep_parents, force=force, sync=sync) | |||
There was a problem hiding this comment.
Pre-existing (not from this change, not blocking): Source.delete now has robust=True, but not the try/finally from F1.1. If the export cleanup raises after the rows are deleted (for example delete_s3_objects.apply_async can't reach the broker), the reindex is never registered. In autocommit the rows stay deleted and the members keep the version's name. This came in with #1038. The same finally would fix it here, or it can wait for a follow-up.
There was a problem hiding this comment.
Done in 8c8c3d5: Source.delete registers the reindex in the same finally. test_deleting_a_version_indexes_its_former_members_again now also covers a failing export-cleanup publish.
…fore its default expansion is cleared on delete, and isn't taken for another's Deleting a version also clears its old-format export prefix without the default expansion, when no other version builds it. A version's old-format prefix is now someone else's if another version builds it with or without that version's own default expansion, so a version that gained an expansion after an export keeps that export. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTz8ve2veELx7gFNaJ
Codex review, pass 3 (codex-cli 0.169.0, commit 424bbe4)Verdict: findings: 3 P2s, no P0 or P1. Two fixed in a546ff6, one accepted. This was the last defect pass; earlier accepted findings are unchanged.
Full suites on 424bbe4: ES 698 OK, non-ES 3,650 OK. Detailed scenarios are on the tracking ticket. |
Simplification review (Claude subagent, commit a546ff6)Verdict: 3 suggested cuts, all low confidence. None applied, so the head stays a546ff6.
|
snyaggarwal
left a comment
There was a problem hiding this comment.
Re-review of a546ff6.
My legacy bare-prefix finding is fixed. Codex pass 3 found the same problem as F3.1, and a546ff6 fixes it. When the default-expansion prefix differs from the bare one, get_export_paths_to_delete now adds the bare current-format prefix and the bare legacy one. The legacy one is added only when this version owns it. The F3.2 change to owns_legacy_export_path is right: a legacy prefix that matches another version's bare or current-expansion prefix isn't owned, so it's kept. test_legacy_export_of_a_lookalike_made_before_its_expansion_is_not_owned covers the collision.
Two side effects of the stricter ownership check, both deliberate and cautious:
- Serving. A version whose legacy prefix matches a lookalike's bare prefix no longer has its legacy export served. Jon's comment says so.
migrate_export_keys. It calls the same check, so those prefixes are now listed as "Shared, skipped" instead of being moved, and--assigncan still move them. If the migration has already run in production, this makes no difference.
Still open: my pre-existing note on Source.delete lacking the F1.1 finally hasn't had a reply. It's fine as a follow-up, but please reply on that thread either way.
New, inline: a pre-existing ownership gap for a candidate's former default expansion. It only risks losing an export file nothing can reach.
I agree with accepting F3.3 (batch_index_full paging by offset). It sits in the shared indexer, not in this change. Nothing here blocks merging.
| base = candidate.get_legacy_version_export_path(suffix=None, with_expansion=False).rstrip('.') | ||
| same_path = candidate.get_legacy_version_export_path(suffix=None) == legacy_path | ||
| if legacy_path.startswith(base) and same_path: | ||
| if legacy_path in (candidate.get_legacy_version_export_path(suffix=None, with_expansion=False), |
There was a problem hiding this comment.
Pre-existing (not from this change, not blocking): this check covers a candidate's prefix without an expansion and with its current default expansion. It doesn't cover a default expansion the candidate used to have. Take candidate version a, exported when its default expansion was b and since moved to c. Its old export sits under O_coll_va_b.. Version a_b with no expansion builds that same bare prefix and passes this check, so deleting a_b clears a's old export. Version a can't reach that export any more, so the only harm is a lost orphan file. Comparing against all of the candidate's expansion mnemonics would close it, but that can be a follow-up.
There was a problem hiding this comment.
Agreed: it predates this PR, so I've left it for a follow-up. Comparing all of a candidate's expansions would narrow it but not close it, because a deleted expansion leaves no record of having been the default. As you say, the only harm is an orphan export that version a can't reach any more.
…port prefix that only its own versions build A version's old-format prefix counts as another version's only if that version outlives the delete. When HEAD goes, its versions go too, so a prefix two of them build (version a with expansion b, and version a_b) is cleared. Tests also pin that a version's old-format prefix without its expansion stays when a source of the same name and version builds it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTz8ve2veELx7gFNaJ
…reindex even if a later step fails As Collection.delete does: registered in a finally, so a failure after the rows are deleted (export cleanup) still reindexes the version's former members, and a rollback drops it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTz8ve2veELx7gFNaJ
Codex check of commits 424bbe4..a546ff6 (codex-cli 0.169.0)Verdict: one P2, fixed in 463dcc8.
8c8c3d5 also folds in the review's pre-existing point: |
Codex check of commits a546ff6..8c8c3d5 (codex-cli 0.169.0)Verdict: the previous check's FC.1 is confirmed fixed. Two new P2s, both accepted; no change.
No commits after 8c8c3d5. Detailed scenarios are on the tracking ticket. |
snyaggarwal
left a comment
There was a problem hiding this comment.
Re-review of 463dcc8 and 8c8c3d5. Everything from my earlier reviews is resolved, and I have no new findings.
Source.deletenow has the F1.1finally(8c8c3d5), with a test that fails the export-cleanup publish. Thanks for picking up the pre-existing note.- Lookalike prefix on a HEAD delete (FC.1, 463dcc8):
deletingis skipped only whenisinstance(self, model), so a source and a collection whose ids happen to match can't hide each other. On a HEAD delete, the prefix that only HEAD's own versions build is cleared, and a prefix that another repo also builds is kept. Both tests cover those cases. I also traced an organization purge. Sources go first, so a source's old-format prefix that its same-named collection also builds is kept, and then cleared when the collection is deleted, because by then the source rows are gone inside the same transaction. Nothing is left orphaned. - Former default expansions in the ownership check: I agree with leaving it for a follow-up, for the reason you give.
- The two accepted Codex points (FC.1 and FC.2): I agree with both. The second is the same harmless extra reindex I noted on
Collection.delete.
From my side this is ready to merge.
Closes OpenConceptLab/ocl_online#740.
What changes
Deleting a collection, or one of its versions, reindexes the concepts and mappings it held, so search stops naming it on them. Until now their documents kept the deleted collection's name and URLs until something else reindexed them. That affected the
collection,collection_urlandcollection_owner_urlfilters and facets, and a collection later created under the same name and URL matched the old members.Collection.delete(new override, likeSource.deletesince OpenConceptLab/ocl_online#744 | A seeding import reindexes the rows it supersedes, and deleting a source version reindexes its rows #1038) reads the ids of the concepts and mappings in the expansions of the version being deleted, or of every version when HEAD is. Once the delete commits (transaction.on_commit,robust=True), it queues their reindex. The callback is registered in afinally, so a failure after the rows are deleted (queueing the export cleanup) still reindexes them; a rollback drops it. The Tasks belong to HEAD's last editor, asSource.delete's do.Source.deletegets the samerobust=Trueandfinally.Source.queue_reindextoConceptContainerModel.queue_children_reindex, unchanged: 5,000 ids a Task on theindexingqueue, arguments saved so it can be rerun, and a chunk that can't be queued left as a failed Task.Source.queue_reindexkeeps its latest-release step and its signature. Five test patch targets move fromcore.sources.models.batch_index_resourcestocore.common.models.batch_index_resources.delete_collectionno longer deletes a version's expansions before callingcollection.delete, which would hide their members from it. The delete's cascade removes them, as it already did for the other versions when HEAD is deleted. References still go first.REINDEX_CHUNK_SIZEincore.common.constants;DROPPED_CHILDREN_CHUNK_SIZEis that value.This covers the HEAD and version DELETE routes (both use
delete_collection) and an organization's delete. Not covered: a user's hard delete cascades in the database without callingCollection.delete, and deleting a non-default expansion leaves its name on its members.Cost
One query on each expansion link table for each deleted collection row. The reindex runs on the indexing workers, never in the request.
Tests
core.collections.teststest_deleting_a_collection_indexes_its_former_members_again: deleting a version reindexes only its members (both expansions'), after commit.delete_collectionon HEAD reindexes HEAD's and the remaining versions' members. A failing export-cleanup publish still reindexes them.test_deleting_an_organization_indexes_its_collections_former_members_again: an organization's delete queues nothing until its transaction commits, then reindexes both collection rows' members.test_delete_version_clears_its_exports_with_and_without_its_default_expansion,test_delete_head_clears_an_old_format_export_prefix_only_its_versions_build,test_delete_version_leaves_an_old_format_export_prefix_another_version_builds, andtests_sourcestest_legacy_export_of_a_lookalike_made_before_its_expansion_is_not_owned.core.sources.teststest_deleting_a_version_indexes_its_former_members_againalso covers a failing export-cleanup publish.QueryCollectionAccessTest.test_deleting_a_collection_indexes_its_members_again(ES): staff'scollection_urlsearch no longer finds a deleted version's members. After a collection is deleted and a public one is created under its name and URL, nobody'scollectionorcollection_urlsearch finds the old members.Local runs on 8c8c3d5 (oclapi2-local image with pylint 4.0.9, fresh test databases):
pylint -j0 core/clean; ES-tagged suite 698 tests OK;TEST_ES=off --exclude-tag es --parallel=43,653 tests OK.Review
Codex: 3 defect passes and 2 checks of later commits, 12 P2s, no P0 or P1. Fixed: F1.1, F2.1, F3.1, F3.2, and FC.1 from the first check. Accepted, with reasons in each pass comment: F1.2-F1.4, F2.2, F3.3 (filed as OpenConceptLab/ocl_online#754), and the second check's two. The simplification review's three cuts were declined.
🤖 Generated with Claude Code
https://claude.ai/code/session_01AaZ4qTpN4J5JFjNSmhYF49
https://claude.ai/code/session_01AaZ4qTz8ve2veELx7gFNaJ