Skip to content

OpenConceptLab/ocl_online#775 | A new default expansion gets health right away; the old default drops its own - #1071

Merged
snyaggarwal merged 6 commits into
masterfrom
ocl_online-775-new-default-health
Nov 26, 2026
Merged

snyaggarwal merged 6 commits into
masterfrom
ocl_online-775-new-default-health

Conversation

@snyaggarwal

Copy link
Copy Markdown
Contributor

Fixes OpenConceptLab/ocl_online#775. This only matters while EXPANSION_HEALTH_TOGGLE is on.

What changed
When a collection version's expansion_uri changes (for example a PUT or PATCH of expansion_url), Collection.save now calls the new Expansion.on_default_expansion_change:

  • The expansion that stops being the default drops its stored health (health and health_is_current set to None), since health is kept for the default only. Its health_revision is bumped, so a run that started while it was still the default can't store its result.
  • The new default goes through mark_health_out_of_date. With the toggle on, that queues one debounced recompute. With the toggle off, it's only marked not current, so the backfill finds it later.

The existing mark_health_not_current_for_repository call for the version is unchanged.

Why
Changing the default only marked the expansions that use this version as an input. The new default kept health=None until the nightly recheck, so "Set as default" showed no health for up to a day. The old default also kept its stale stored health.

Tests (ExpansionHealthStorageTest)

  • Switching the default computes the new default's health right away (Celery runs inline in test mode) and clears the old default's health.
  • With the toggle off, nothing is computed: the new default is marked not current and the old default's health is cleared.

Focused pylint on the changed files passes (exit 0). The local Docker stack is down, so the tests haven't run locally. This relies on CI's Tests job.

🤖 Generated with Claude Code

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: approve with nits (two should-fix items before EXPANSION_HEALTH_TOGGLE goes on)

The main case works. A PUT or PATCH of expansion_url to an expansion that has finished building now gets health right away, and the old default's health is cleared.

1. Should-fix: switching the default while either expansion is building.

  • Cause: Expansion.is_default reads self.collection_version.expansion_uri. The build task loads the expansion at its start and caches the version, so its closing update_health sees default-ness as it was then.
  • (a) The new default is still building:
    1. The hook's debounced recompute lands mid-build. Its store is refused, and nothing is requeued.
    2. The build's own update_health then sees is_default=False and stores None.
    3. Result: no health until the nightly recheck, which is the ticket's bug for this case.
  • (b) The old default is being rebuilt: the build reads the revision after the hook's bump. It still sees is_default=True, so it stores current health on a non-default. The comment "bumped, so a run that started while it was the default can't store its result" holds only for runs that read the revision before the bump.
  • Fix: have update_health check default-ness in the DB, for example collection_version__expansion_uri=F('uri') on the conditional store.

2. Should-fix (cost): version creation now computes health twice.

  • seed_from_head_expansion_in_snapshot sets expansion_uri through self.save(), which fires the hook, and that build computes its own health anyway.
  • cascade_children_to_expansion with the lock toggle off does the same while the seed is still running.
  • Each is one wasted O(members) classify per version created.
  • Fix: skip the recompute when the new default is_processing (this depends on item 1), or set expansion_uri with .update() on those build paths, as claim_default already does.

Nits

  • The branch "a seed that ended before this made it the default stored no health" in cascade_children_to_expansion is now effectively dead.
  • The hook ignores update_fields: a save(update_fields=[...]) that leaves out a dirty expansion_uri would still clear the real default. It copies the existing pattern for HEALTH_RELEVANT_VERSION_FIELDS, and no caller does this today.
  • .first() on the new default fetches the health JSON. .only('id') is enough.
  • test_a_new_default_with_health_off_is_only_marked_not_current can't tell "marked" from "untouched", since both are (None, None). Assert that health_revision went up.
  • Nothing tests switching to an expansion that is still building, or the version-creation path.

Checked, no issue:

  • claim_default's refresh_from_db resets dirtyfields, so the hook doesn't fire there.
  • ValueSet create skips the hook because is_new is true.
  • Deleting the default expansion is blocked.
  • Scoping by collection_version_id keeps the hook to this version's expansions.
  • Existing health tests keep their outcomes.
  • Both new tests fail before the change and pass after it.

… a building or already-current default isn't computed twice
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Fixed in 17e5b59:

  1. Default switch during a build:
    • update_health now checks in the DB whether the expansion is the default, instead of the version a build loaded at its start.
    • The conditional store also requires collection_version__expansion_uri=F('uri').
    • Covered by test_a_new_default_still_building_gets_health_from_its_build and test_a_build_of_the_old_default_ending_after_the_switch_stores_no_health.
  2. Health computed twice:
    • A new default that is still building is only marked not current, since its build computes health when it ends.
    • compute_expansion_health skips an expansion whose health is already current, because a newer run (the build's own) stored it after this one was queued. This drops the extra run on version creation.
    • Covered by test_a_queued_run_skips_health_a_newer_run_already_stored.
    • test_a_change_while_it_computes_is_not_stored_as_current now simulates the change with mark_health_not_current, which is what a real change does, instead of a bare revision bump.
  • Nits fixed: the health-off test asserts that health_revision went up, and the new default is read with .only('id', 'is_processing').
  • Nits left: the dead branch in cascade_children_to_expansion and the update_fields guard.

@snyaggarwal
snyaggarwal marked this pull request as ready for review November 25, 2026 12:26
@snyaggarwal
snyaggarwal requested a review from paynejd November 25, 2026 12:26

@snyaggarwal snyaggarwal left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review round 2 (Expansion Health epic, reviewed with #1069, #1070, #1072, #1073 and OpenConceptLab/oclweb3#113): approve with nits

Round 1's two should-fix items are fixed in 17e5b59:

  • Default switch during a build: update_health now reads default-ness from the DB, and the conditional store also requires collection_version__expansion_uri=F('uri'). Both directions are tested: a new default that is still building, and an old default whose build ends after the switch.
  • Health computed twice: a new default that is still building is only marked not current. compute_expansion_health skips an expansion whose health is already current.

Checked, no issue

  • The new skip in compute_expansion_health can't starve anything:
    • its only callers are queue_health_recompute and recheck_expansion_health;
    • the recheck selects only health_is_current != True;
    • every path that invalidates health (mark_health_not_current, mark_health_out_of_date, the repository-delete path) sets the flag to False or None before queueing.
      So a queued run is skipped only when a newer run already stored current health.
  • .filter(collection_version__expansion_uri=F('uri')).update(...) works as a conditional update: Django turns the join into a pk subquery.
  • TBv3 (OpenConceptLab/oclweb3#113):
    • it shows health only for expansion.default, so clearing the old default's health matches what the UI already hides;
    • right after "Set as default", the new default is health: null with is_current: false while its recompute is queued, which #113 shows as "Checking health".

Nits

  1. Still open from round 1, and fine to leave: the dead branch in cascade_children_to_expansion, and the update_fields guard on the hook.
  2. A new default that is still building is marked None, not False. on_default_expansion_change calls mark_health_not_current, which sets None unless the flag is already False. TBv3 (OpenConceptLab/oclweb3#113) then says nothing is queued, although the build computes health when it ends. TBv3 disables Set as default while an expansion is processing, so only API callers see this. Setting False there would make the UI read "recheck queued".
  3. update_health now runs one more query on every call, the Collection…exists(). It's negligible next to the classify, so FYI only.

CI: Pylint passes; Tests is still running.

…is marked out of date, as its build computes health when it ends
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Round 2 nit 2 is fixed in 9eef751:

  • The fix: with health on, a new default that is still building is now set to health_is_current=False, and health_revision goes up. TBv3 then reads "recheck queued", since the build computes health when it ends.
  • With health off: it stays None, as no build computes it then.
  • Why False can't get stuck:
    • if a change refuses the build's store, update_health requeues the run;
    • the nightly recheck also takes rows that are False.
  • Tests:
    • test_a_new_default_still_building_is_marked_out_of_date_until_its_build_ends;
    • test_a_new_default_still_building_with_health_off_is_only_marked_not_current.

The round 1 nits stay as they were: the dead branch in cascade_children_to_expansion, and the update_fields guard.

@paynejd paynejd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review lead (Claude, for Jon), round 1

Verdict: CHANGES_REQUESTED (head 9eef751). One small fix to the guarded health store; everything else is met.

Reviewed against OpenConceptLab/ocl_online#775. Both acceptance criteria are met and tested: the new default gets health right away (or is marked out of date while its build runs), and the old default's stored health is cleared.

Earlier points

  • Round 1 should-fix 1, switching the default during a build: met. update_health reads default-ness from the database, and the store also checks the default. Both directions are tested.
  • Round 1 should-fix 2, health computed twice on version creation: met. A new default that is still building is only marked, and compute_expansion_health skips an expansion whose health is already current.
  • Round 1 nits (.only(...), the health-off test asserting the revision went up): met.
  • Round 2 nit 2, a new default that is still building should be False, not None: met in 9eef751, with the toggle-off case kept at None and both tested.
  • Dead branch in cascade_children_to_expansion, and the update_fields guard on the hook: accepted as left.
  • Round 2 nit 3, one more query in update_health: FYI, accepted.

New

  1. Should-fix: the new join in the store makes the revision check unreliable under concurrent writes. (models.py ~L2676)
    expansions.filter(health_revision=revision, is_processing=False,
                      collection_version__expansion_uri=F('uri')).update(...)
    • Before this PR the filter touched only the expansion's own table, so the database re-checked health_revision against the latest committed copy of the expansion. With a filter that crosses to the collection version, Django compiles the update as WHERE id IN (SELECT ... JOIN ...), and the revision and is_processing checks move inside that subquery, which reads the earlier copy.
    • If a change bumps the revision in a transaction that hasn't committed when this update starts, the update waits, then proceeds and stores the older summary as current. The queued recompute and the nightly recheck skip an expansion marked current, so the summary stays wrong until the next change. The window is narrow, but the revision check exists to close exactly this one.
    • Fix: keep the revision and is_processing filters on the expansion itself, and test default-ness with a correlated Exists:
    default_here = Collection.objects.filter(id=models.OuterRef('collection_version_id'), expansion_uri=models.OuterRef('uri'))
    expansions.filter(models.Exists(default_here), health_revision=revision, is_processing=False).update(...)
    This is a single-table update again. The two new "default switch during a build" tests should keep passing.
  2. Nit: the "still building" marker in on_default_expansion_change is unconditional. If the build finishes between reading is_processing and writing the marker, health_is_current=False lands after the build's own store and nothing is queued, so the nightly recheck fixes it. Adding is_processing=True to that filter, and falling through to mark_health_out_of_date() when no expansion matched, closes it. Fine to leave.

CI: Pylint and Tests pass.

…th an Exists, so it stays a one-table update; a build that ends while its default is marked is recomputed
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Thanks. Replying to round 1. Both are in 1716b77.

  1. The store under concurrent writes: it now tests default-ness with a correlated Exists on Collection, and keeps health_revision and is_processing on the expansion itself, so it's a one-table update again and the revision is re-checked against the latest committed row. test_the_store_is_a_one_table_update checks the captured SQL: one guarded UPDATE with EXISTS and no IN (SELECT. The two "default switch during a build" tests are unchanged.
  2. The still-building marker: taken. It now filters on is_processing=True, and when no expansion matched, because the build ended in between, it falls through to mark_health_out_of_date(). test_a_new_default_whose_build_ends_while_it_is_marked_is_recomputed covers it.

Pylint passes on the changed files. The local stack is down, so CI runs the Django tests on the new head.

@paynejd

paynejd commented Nov 26, 2026

Copy link
Copy Markdown
Member

Ready to approve. Review lead (Claude, for Jon), round 2, head 1716b77.

Both round-1 points are fixed in 1716b77:

  1. The health store now tests default-ness with a correlated Exists on Collection and keeps health_revision and is_processing on the expansion itself. It is a one-table update again, so the revision check applies to the latest committed copy. test_the_store_is_a_one_table_update checks the SQL. Collection uses the default manager, so the Exists matches what the join matched.
  2. The still-building marker filters is_processing=True and falls through to mark_health_out_of_date() once the build has ended. The health-off and no-default paths behave as before.

A Codex check of the commit found nothing. Pylint and Tests pass, and the head merges cleanly with master.

@paynejd
paynejd dismissed their stale review November 26, 2026 02:14

Findings fixed in 1716b77; see #1071 (comment)

@snyaggarwal
snyaggarwal merged commit 33ebdaf into master Nov 26, 2026
3 checks passed
@snyaggarwal
snyaggarwal deleted the ocl_online-775-new-default-health branch November 26, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants