Repository navigation
Conversation
…, behind the per-evaluation switch Under FHIR's rules each concept member records the source version that selected it (a new table beside the membership), one member per concept per source version; exclusions by what they name; versionsMatch; value sets that hold a concept in a version; health, the rebuild snapshot, comparison and the stored $expand read them. Without the rules nothing reads or writes the new table. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
… a version, an import's namespace read once Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…s, each case built without the rules, left to the toggle, and with them Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…time, exclusions at other versions, import parameters, comparison by version, hidden releases Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ated with their parameters, imports' own rules and imports of imports, comparison apart, switch-off rebuilds Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ilds with the locks kept, every build under the rules re-evaluated, imports read as built, deletes gated on the switch Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…rite in one place Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ules on late is rebuilt from the references given, exclusions of value sets in each version, stale memberships pruned Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…ed by the behaviour they pin Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm
…s a code of another copy of a code system version by code Under FHIR's rules a value set that a reference with a system names holds a candidate concept by concept rows: its members, or what its includes select there. A code system a request carries beside a stored one is loaded as another source with the same canonical URL and versions, its concepts new rows, so an include naming the carried code system intersected with a stored value set lost every code the two have in common. A value set now also holds, by code, a candidate whose code it has as a member in another copy of that code system version: a source version with the same canonical URL and version (never HEAD), as FHIR names a code by (system, version, code). - Only a copy the public can view is read (the version active, with an active HEAD, both public): who may view a private one depends on a caller the evaluation doesn't know. A member row deleted since doesn't count. - Codes compare as FHIR's operations spell them: a member's and a candidate's code are both decoded, as core.fhir.ops.concepts.fhir_code decodes them, since OCL may store a code percent-encoded. - Two source versions with different canonical URLs or versions never match, and nothing changes without FHIR's rules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…52-b-versions) into versions kept apart Brings in #1087's follow-up: a version parameter naming a canonical URL applies whichever repository with it holds the version (version_rules keys parameters by repository and canonical URL). One import line conflicted; both kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…sion found by canonical URL too, as the version rules key it The version rules now key a parameter by the repository its entry names and, for one naming a canonical URL, by that URL (#1087). So force-system-version can send a reference to another repository with the URL. - The two places here that ask whether force-system-version fixes a reference's version (MemberVersions.forced, ImportedValueSet.forced) ask the rules the same way, so they agree with the version the build gives the reference. - An imported value set's reference is read as of the repository its own force-system-version sends it to, not the one its system resolves to, when the import is asked what it holds there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…odes read from every row in its release, forced exclusions kept at their own source Two fixes from the review of the last two commits: - A candidate whose code OCL stores percent-encoded is matched by code (as FHIR spells it) when it's a row the release holds (one made in the release included) or the HEAD row of one, as an extensional member is. They're read in one indexed query on the release's concept links (on production, about the cost of reading LOINC 2.81's links once). - An imported value set's exclusion that its own force-system-version sends to another source is read as of that source and of its own: from its own it takes away what it selects in the forced version, as the import's build did. An include is read as of the source its force sends it to. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWnJcYXZ5geiBgML73
…import evaluated again carries what reading it by version needs Rebased onto the versions-kept-apart branch (#1090). An import evaluated again outside a request (EvaluatedImport) now carries what reading it by version reads: its parameters, how it was built, whether it keeps versions apart, the versions it recorded and its memberships, read before its rollback. ImportedValueSet reads memberships through one helper that answers for both a stored expansion and such an import. Tests: an intersection and an exclusion ask an import by version, as versions kept apart read it; a carried import's marker covers its memberships too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qTSj4R6AHvrLWEVgWn
…52-b-versions) into versions kept apart Brings in #1087's new head, which carries #1086's review answers: a request-time evaluation evaluates only expansions built under it, for its caller, and a version the caller can't view is answered as one not found, by the version rules' parameters too. One conflict, in core/collections/models.py: this branch moved the system version's queryset into get_system_queryset, and #1086 made the check there ask the evaluation's can_view (its viewer: the creator, or at request time the caller). get_system_queryset now asks can_view. Adapted to the new interface (each creator check goes through the evaluation): - CodeReferenceBatch.found_by_version reads viewability by the reference's viewer, as the batch now records it (it was keyed by creator, so at request time it found nothing). - member_versions.held_at_version groups plain codes by their reference's viewer and asks can_view. - In add_references the versions a caller can't view are left out before kept.meet, so a kept member version is never one the caller can't view (as for the version rules' checks). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWddiMQHvvG5BYWRfh
…r-452-b-apart) into imports Brings in #1090's new head, which carries #1087's and #1086's: a request-time evaluation evaluates only expansions built under it, for its caller (what references find is what the caller can view) unless opened for none, a version the caller can't view is answered as one not found, and a statement runs as it is while the evaluation has no deadline. Conflicts, in core/collections: - evaluation.py: ExpansionEvaluation keeps both sides' state (the caller, its own expansions and what it may view; what it built and the imports it evaluated). at_request_time's description keeps both halves, the no-seconds sentence included. within_deadline is #1086's, which lets a statement through with no deadline, as this branch's did. - models.py, build_at_request_time: both sides kept: refused for a version the evaluation may not build; locks_of's locks and the parameters it was built under recorded; the expansion registered as its own. Adapted to the new interface: this branch opens an evaluation at request time outside a request too, so that nothing a stored expansion's import evaluates again is kept (ValueSetImports.evaluate). An opening answers its caller unless opened with for_caller False, which #1086 now offers, so that path opens with for_caller False: the import is evaluated as its references' creators view it, as the stored build is, rather than as an anonymous caller (which refused a private import). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWddiMQHvvG5BYWRfh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7 # Conflicts: # core/collections/models.py
… in the versions that hold it An expansion built without FHIR's rules records its members as concept versions, and the versions they're read in are the ones it recorded that hold them. A member none of them holds was placed in its source's HEAD, even when HEAD doesn't hold it and only a release does. It is now in its source's HEAD when HEAD holds it or no active version does (an older row OCL keeps for what took it), else in each active version that holds it. One rule, in unrecorded_placements, for legacy_memberships (the version comparisons) and an imported value set's versions and members. So an evaluation that leaves out versions its viewer can't view leaves such a member out with its release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
… the caller can't view are left out A reference with value sets and no system is evaluated in each version its first value set has members in. Those versions weren't asked of the evaluation, so at request time a release the caller can't view (one the import recorded, or one that alone holds a member's row) still gave its members, and with versionsMatch could outrank a release the caller is shown. They're now left out as a reference to a code system finds nothing in one (ExpansionEvaluation.hides). A build outside a request is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
… releases that hold it, else HEAD A member no recorded version holds went in each active version that holds it, drafts and retired ones too. Those aren't what OCL resolves a source to, nor what FHIR lists members in, so such a member could rank above a live release under versionsMatch and then not be listed, or make an expansion look as if it mixed versions of its source. It now goes in each live release (released, active, not retired) that holds it, else in its source's HEAD as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…etired releases hold goes in them A member no recorded version holds, that HEAD and every live release don't hold, fell back to its source's HEAD. At request time a caller who can't view the draft or retired release that alone holds it was then given it, at HEAD, where versionsMatch could rank it above a live release's row of the same concept. It now goes in each active version that holds it, so an evaluation for the caller leaves it out with them; only a member no active version holds goes in HEAD. A member a live release holds still goes there first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…ease and a newer draft hold is the release's A row HEAD has moved on from, held by a live release and a newer draft that the expansion recorded neither of, is the live release's member only, and versionsMatch keeps it there. It fails under the rule before 75d0f84, which put it in both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
…given to the caller at HEAD either The request-time test now also rules out the member only a private draft holds coming back at its source's HEAD, as it did under 75d0f84's rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVWYYmWDsL1uzvh9q7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVC3osEQxNxh6dcRnV
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVC3osEQxNxh6dcRnV
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qV2gzsF5GhF5BwTJ6x
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qV2gzsF5GhF5BwTJ6x
…es), which the FHIR series builds on fhir-452-b-codes ends the queue of OCL API PRs, so the FHIR series sits on all of them: #1085, #1086, #1089, #1088, #1087, #1090, #1091, #1092 and #1093, and the core changes that need them together. No conflicts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qV2gzsF5GhF5BwTJ6x
…ge into fhir-452-b-apart Takes #1087's new head, which brings in master (#1085, #1086 and #1082). Merged cleanly; no change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qWUTom6BGgjQy6oehZ
|
A performance fix is coming to this PR before it merges. |
…version are read by the primary key ImportedValueSet.members_at read a value set's members in a version (built without the rules) as those recorded there OR those placed there unrecorded: two IN subqueries joined by OR, which PostgreSQL can answer only by reading every concept. Wrapped in versioned_object_id IN (...) for each code a reference with a system and value sets checks, it read all of concepts per statement (on 1.5 M concepts, about 0.6 s each, nine a request). It's now one IN over their UNION ALL (ids_in), answered by the primary key, with the same members; code_matches reads its copies the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qX3xher9sfoCLLKrTC
|
The fix is pushed in 9c35a2e (+27/−8, two files). |
…into fhir-452-b-apart Takes #1087's new head, which brings in master (#1089, #1109 and #1106): REFERENCE_SEARCH_OPTIONS_TOGGLE is on in every environment, as on master. Merged cleanly; no change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…o fhir-452-c-storage Takes #1090's new head, which brings in master (#1089, #1109 and #1106): REFERENCE_SEARCH_OPTIONS_TOGGLE is on in every environment, as on master. Merged cleanly; no change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
snyaggarwal
left a comment
There was a problem hiding this comment.
Verdict: request changes. One small fix, in migration 0078: its lock waits need a bound (inline). The rest is ready.
Merge order: first #1087, then retarget this PR to master. Merge master into it and let CI run on that, including #1093 if it has landed by then. Then merge this PR, with #1103 right after it. #1088 conflicts with this stack in core/collections/evaluation.py and core/collections/models.py, so whichever of them merges second needs a master merge first.
I reviewed only this PR's own diff against fhir-452-b-versions.
With the switch off, nothing changes. I traced every new path:
MemberVersionsreads the toggle only in these cases: an expansion already markedversions_apart, a switch value the caller passed,versionsMatch, a reference with value sets, or a second version of one source. When the answer is off, it never reads or writescollection_expansions_concept_memberships.capture()iterates the queryset thatrel.add()has already evaluated, so it runs no extra statement.- Health, comparison, the dry-run snapshot, reference deletes and stored
$expandall checkversions_apartfirst. - The API refuses
versionsMatchwhile the toggle is off.
That matches the SQL-capture tests.
Non-blocking:
$expandof an expansion that keeps versions apart runsExpander.entries()three times: once for the count, once for the page and once inused(). Each run is a NOT IN plus a correlatedExistsovermembers(), and once #1093 merges,members()also carries its check for rows the caller is shown. This only runs behind the switch, and PERF-18 says the draft series is reworking the same kind of statement (selected_shown, #1097). Before the toggle is turned on anywhere, please time it on a LOINC-sized expansion that keeps versions apart.- Interaction with #1093:
git merge-treeshows no conflict, and the two rules agree: each shows only rows that a version the caller is shown holds. This PR's FHIR$expandtests were written against master'smembers(), though, so CI needs to run on the merge once #1093 lands. - The
members_atUNION fix you raised (9c35a2e) is in and has a test on the query's shape. The ranking of retired releases underversionsMatch, which the description notes, is fine as a next step.
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( |
There was a problem hiding this comment.
Blocking. Django runs this migration in one transaction, so the ACCESS EXCLUSIVE lock that this AddField takes on collection_expansions is held until the migration ends. That includes the time the foreign keys below spend waiting for SHARE ROW EXCLUSIVE on concepts and sources.
That wait lasts as long as any open write transaction on concepts, a bulk import for example. All that time, every read of collection_expansions queues behind this migration: collection pages, $expand, builds. The description's "deploy when no import is running" is the only guard.
#1103 found the same problem in its own migrations and fixed it by making SET LOCAL lock_timeout = '5s' the first operation, applying and reversing (a53b7ff, a744936). Please do the same here. Moving this AddField after the RunSQL that adds the foreign keys would also shorten how long collection_expansions is locked against reads.
There was a problem hiding this comment.
Thank you, you're right. Fixed in 6b3a81a: every lock wait is now bounded, applying and reversing, and the column is added after the foreign keys. Two things differ from what you suggested:
- The bound is 0.3 s for each lock, not 5 s. The migration takes locks on three tables, so it can end up in a deadlock with another transaction. With 5 s, PostgreSQL's deadlock check (after 1 s) can cancel the other one: I got it to cancel a transaction that writes
sourcesand thenconcepts. Three waits of 0.3 s end before that check, so it's always the migration that gives up. collection_expansionscomes last for its foreign key too, and its ACCESS EXCLUSIVE lock is the first one taken there. With only the column moved, the foreign key took a weaker lock first and the column a stronger one later. A build that had read the table and then wrote to it was cancelled at once.
Tried on PostgreSQL 14.7:
- A write held on
conceptsfor 30 s: before, reads ofcollection_expansionswaited 25 s. Now the migration gives up and they wait 0.1 s. - A read held on
collection_expansions: other reads waited 25 s before, and now 0.3 s. - Six deadlock cases: the other transaction went on in all of them.
A try that gives up changes nothing. Run it again once the other transaction is over; it then applies in 0.3 s. The description's Migration section says what that means for a deploy.
Merging master, and $expand's three reads of the entries, come separately: we're timing those now on sets kept apart of up to 110,000 members.
|
Merge call: merge after the 0078 fix and after #1087, as the description says: merge #1087 first, then retarget this PR to master without a rebase. That fits the Dec 8–12 slot in the road (ocl_online#797). My non-blocking note about |
… and locks the expansions last and once Applied or reversed, it waits at most 0.3 s for each lock, then fails and changes nothing. Its three waits together are below PostgreSQL's deadlock_timeout (1 s), so in a deadlock this migration gives up and the other transaction goes on. collection_expansions, whose lock reads wait for, comes after the foreign keys to concepts and sources, so it isn't held while they wait for a write there. Its strongest lock is the first taken: a weaker one made stronger later is a deadlock with a build that has read the table and then writes to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
snyaggarwal
left a comment
There was a problem hiding this comment.
Verdict: approve. 6b3a81a fixes the blocking point on 0078.
- Every lock wait is bounded.
SET LOCAL lock_timeout = '300ms'runs first when the migration is applied, and the closing no-op/reverse pair makes it run first when it's reversed. It lasts until the migration's own transaction ends. Your reason for 0.3 s rather than my 5 s is right: the three waits together stay underdeadlock_timeout, so in a deadlock the migration is the one that gives up. I hadn't thought of the deadlock check. collection_expansionsis locked last. It takes ACCESS EXCLUSIVE before its foreign key, so reads of it no longer queue while the migration waits for writers onconceptsandsources. It also never asks for a stronger lock on a table it already locked.- Reversed, the migration drops the column before the foreign keys, so it locks
collection_expansionsfirst. Each later wait is still capped at 0.3 s, so reads queue for under a second. That's fine for a rollback. - Tests: the new
MigrationTestapplies, reverses and applies again, and pins the order of the operations. CI is green at 6b3a81a.
Merge order is unchanged: first #1087, then retarget this PR to master without a rebase. Merge master into it and rerun CI, then merge it, with #1103 right after. EXPANSION_FHIR_RULES_TOGGLE stays off for API users until the $expand fix you're timing is on master.
|
On your timing point, from the review's non-blocking list: you were right. I timed stored
The fix is in #1094, so this PR doesn't change. |
…o fhir-452-c-storage Takes #1090's new head. Merged cleanly; no change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…or _apply_filters takes any further arguments #1088 gives CollectionReference._apply_filters a further argument, so the stand-in that #1090's exclusion test patches in takes it too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qV2gzsF5GhF5BwTJ6x
…o fhir-452-c-storage Takes #1090's new head. Merged cleanly; no change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…nto fhir-452-b-codes Takes #1091's new head: #1090 now has the test stand-in's fix this branch had (f990fd0), and an imported value set whose expansion recorded no versions is read by those its references resolve to, or evaluated again. No change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
…o fhir-452-c-storage Takes #1090's new head. Merged cleanly; no change to this branch's own code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AaZ4qVQviVc1p6MQwqUUed
|
@snyaggarwal, please don't merge this one before our next push to it, although GitHub shows it as mergeable and you've approved it. #1121, now on master, names each member of a comparison by its source and code. This PR has its own version of that step for expansions that keep versions apart, and it still goes by code alone. The lines don't clash, so git merges them, but then comparing such an expansion fails with an error, and three of this PR's tests fail on master. The fix is two lines, with tests, and it comes in the push we make after #1087 is merged. It'll be a commit of its own, so you can look at just that. |
Not part of the Dec 1 run.
Stacked on #1087 (#1086 is merged): merge #1087 first. This PR is then retargeted to master, without a rebase.
Part of OpenConceptLab/ocl_online#452 (phase B, PR 5: two versions of one source kept apart).
What it does
Today an expansion's members are concept versions, and a build keeps only the newest version of each concept, so a value set whose references name two versions of a source shows each concept once, as the newer version has it. An exclusion removes every version of each concept it selects. FHIR's value sets keep two versions of one code system apart (the HL7 Terminology Ecosystem IG's overload cases). This PR adds that behaviour behind the per-evaluation switch PR 2 added (
ExpansionEvaluation.fhir_rules). FHIR's request-time evaluations will turn the switch on. For API requests theEXPANSION_FHIR_RULES_TOGGLEtoggle decides, and it isn't set anywhere, so nothing changes for API users until it is.With the rules on (
core/collections/member_versions.py):Each concept member records the source version that selected it, in a new table beside the membership (
ConceptMembership). A concept version that two releases share is a member of each release it was included from. The expansion's concepts (collection_expansions_concepts) are still filled, with each concept version once, so everything that reads them keeps working. A concept that a cascade reached in another source is recorded at that source's HEAD. Mappings are kept as they are today.One member per concept per source version: the newest concept version that version selected.
Exclusions follow what they name. An exclusion that names codes or filters in a version it states removes them from that version only. Any other exclusion (a whole version, or one that states no version) removes what it holds from every version of its source. It is evaluated at a member's version when it can be (it states no version, or a wildcard that names that version, and
force-system-versiondoesn't fix it); otherwise it uses what it holds in its own version. A reference to a concept version still removes only that concept version (REF-12).versionsMatch, a new expansion parameter (the value set's own).falsekeeps every exclusion to its own version.trueremoves in every version and keeps each concept once, in the newest version that has it (by the source's declared version algorithm from OpenConceptLab/ocl_online#452 | Version rules in OCL's core: wildcards, a declared version algorithm and the version parameters, behind the per-evaluation switch #1087, else the most recently created). The API accepts this parameter only while the toggle is on, as withdefault-valueset-version.Value sets hold a concept in a version. A value set that a reference with a system names (an intersection) holds a candidate in version V when:
That covers the design's two cases: an import whose
1.x.xinclude expands alone at 1.2.0 still holds a 1.0.0 candidate; and an import that lists X from version 1 still holds an outer version-2 X. An import's ownforce-system-versionand expansion parameters apply to what its includes select at another version. An import of an import is read the same way, and a value set that leads back to one already being evaluated holds there only its own members, so evaluation always ends (refusing such loops is PR 6). A reference with value sets and no system takes the first value set's members, each in its version, that the others hold. A value set built without the rules holds each member in the versions its expansion recorded that contain it. Under the rules, every build evaluates its references again with the expansion's parameters, including an auto-generated expansion and the re-add after a reference is removed: saved results don't say which version selected what.What reads these memberships:
dry_run_rebuild's snapshot adds each concept's memberships (concept_versions).concept_versions, the (concept, version) memberships each expansion has that the other hasn't. A version is named there only when it's a public release.$expandlists:$expandof the value set itself serves its stored expansion as it is (each concept once) until it's rebuilt.When a build asks the switch: only when it meets something the rules could change: a reference or exclusion evaluated in a second version of one source, a reference with value sets,
versionsMatch, or an expansion that already records versions. Until then an exclusion removes the same either way. Any other build never reads the toggle, and with the switch off nothing reads or writes the new table (tested by capturing every SQL statement of builds, a reference delete, a dry-run rebuild and a concept delete).When the switch changes for an expansion:
keep_locks), because an add alone would keep what the other rules made.versions_apart, and the next build under the rules clears the leftover rows.So with the switch off, an expansion holds exactly today's members after its next build, whatever it was built under before.
Migration
collections.0078_concept_memberships:The 6.1M-row membership table (
collection_expansions_concepts) isn't touched. A column there couldn't hold a concept version that two releases share without replacing its unique index, so the memberships go in a new table instead.The new table starts empty, so creating it and its indexes reads no existing rows. Its ids are 64-bit.
versions_apartis a nullable column with no default oncollection_expansions(about 26,000 rows). PostgreSQL adds it by updating the catalog only. It takes a brief ACCESS EXCLUSIVE lock on that table, which reads wait for, so that table comes last: the lock isn't held while the migration waits forconceptsorsources.Each foreign key takes a brief SHARE ROW EXCLUSIVE lock on the table it references (
collection_expansions,concepts,sources), and checks only the new, empty table. Writes there wait for it; reads don't.Each lock wait is bounded at 0.3 s (
SET LOCAL lock_timeout, the first operation applied and reversed). The three waits together stay below PostgreSQL'sdeadlock_timeout(1 s). So if this migration and another transaction wait for each other, the migration gives up and the other goes on.If it fails during a deploy ("canceling statement due to lock timeout"), another transaction held one of these tables. Nothing has changed: the migration is one transaction, and all of it is rolled back. The container that runs the migrations stops there. Run it again (or let it start again) once that transaction is over, so it's still best to deploy when no import is running.
Measured on PostgreSQL 14.7, with another transaction holding a lock for 30 s:
conceptscollection_expansionswith it, for 25 scollection_expansionsRun again afterwards, it applies in 0.3 s.
The foreign keys cascade in the database (
ON DELETE CASCADE), and Django's side isDO_NOTHING. So deleting a concept, source version or expansion through Django never queries the new table.Code without this PR ignores the column and the table, so a database shared with an older deployment (staging's, when fhir-next deploys) is unaffected.
Tests
core/collections/tests/tests_member_versions.py, following the IG's overload cases on stored content (1.0.0: code1, code2, code3; 2.0.0: code1 unchanged, a new version of code2, code4):versionsMatchtrue and false, the declared version algorithm;$expand;core/collections/tests/tests_version_rules.py: OpenConceptLab/ocl_online#452 | Version rules in OCL's core: wildcards, a declared version algorithm and the version parameters, behind the per-evaluation switch #1087's toggle-read test now names the version its other reference resolves to, since two versions of one source also ask the toggle.--parallel=4withTEST_ES=off, then the search-index tests): pylint 10.00; 4,264 + 803 tests OK; coverage 97% (member_versions.py96%).Still to come: a dry-run rebuild of real collections (switch off against #1087, then switch on). It belongs to B's final checks, before the whole goes to staging.
Review
Three Codex defect passes (10, 10 and 10 findings; every P0 and P1 fixed, with its class), a simplification review (1 of 6 cuts applied) and a Codex check of the later commits (5 findings, all fixed; those last fixes had no further pass, the loop's limit).
Expansions built before this change: with the switch on, a stored expansion recorded before per-version membership, read where its value set is imported, makes a concept row that two recorded releases share a member at both. So the importing expansion lists it once per version, while the stored expansion itself still lists it once until it's rebuilt. They converge on rebuild.
Follow-up: #1087's new head merged in (a version parameter naming a canonical URL applies whichever repository with it holds the version), with
force-system-versionfound here the same way. A value set that an include with a system names now also holds, by code, a candidate whose code it has as a member in another public copy of that code system version (the same canonical URL and version), as when a code system a request carries is loaded beside a stored one. Codes compare as FHIR spells them (6b072be, ea88b82, 6c1d689, a1d6351; Codex passes and checks, every P0/P1 fixed, P2s fixed or accepted on OpenConceptLab/ocl_online#797).Follow-up: #1087's new head merged in, with #1086's review answers (8a79df0). Adapted here:
get_system_queryset,CodeReferenceBatch.found_by_versionandImportedValueSet.held_at_versionread what the reference's viewer can view (can_view,viewer_id: its creator, or at request time the caller), and the versions the caller can't view are left out before member versions are kept. Codex check: three P1s answered, not changed: the core keeps the API's rule that a stored expansion's members are what anyone who can view its collection sees; FHIR leaves out members of code systems its caller isn't shown when it answers.Follow-up: #1087's new head merged in (5a06ee5, no conflicts): a failed version check names the version it used by its id.
Follow-up: one rule for where a member of an expansion built without FHIR's rules goes when no version it recorded holds it (c8205a4, refined in 75d0f84 and 73fd5b4). It goes in its source's HEAD when HEAD holds it; else in each live release that holds it; else in each draft or retired release that holds it; else, when no active version holds it (an older row OCL keeps for what took it), in HEAD. Before, it always went to HEAD, even a row only a release holds. The rule is the same for the version comparisons (
legacy_memberships) and for an imported value set's versions and members. At request time, an import's versions the caller can't view are left out, as a reference to a code system finds nothing in one (49da2c8). Builds outside a request are unchanged. Each step has a test that fails without it (f7a529e adds a row a live release and a newer draft share). Full suite on 73fd5b4: 4,322 + 803 tests OK, coverage 97%, pylint 10.00. Codex checks: three P1s fixed, and a missing test added. One P1 answered: a row held only by a public retired release goes in it and can win versionsMatch, then not be listed by FHIR, which lists live releases only. A recorded retired release already behaves that way, so it's a next step for versionsMatch's ranking.🤖 Generated with Claude Code
https://claude.ai/code/session_01AaZ4qXEukuZvEHX3KQwVHm