removal
Take a removed dataset's patients and scans out of the trials it fed.
Every dataset of one type on a pod writes the same scan_eligibility and
patient_eligibility partitions of a trial, under one published pointer. A
dataset removed from the pod's config, or unlinked from the trial on the Hub,
no longer contributes to the trial (load_dataset_liveness), and
reconcile_dataset_removals takes out what it left behind without waiting for
a run:
- its stored scans, and the trial's wave-ledger rows for their files, so a relink, or a live dataset sharing one of the files, processes it again;
- its saved scope for the trial;
- the patients it was the only source of. A patient with a live dataset's
scans too is recomputed from the stored rows (
read_trial_scans_for_recompute,build_patient_records), or left as published while a run would hold them back, and every other patient is left as published.
A trial none of whose lineages on the pod is live is unpublished instead.
Each call works out what to remove from the cache as it stands rather than reacting to an event, so a second call finds nothing to do, and a call after a run in flight wrote a removed dataset's rows again removes them again. What a call finds its work by is removed last, so a call that fails part-way leaves the rest for the next one.
A call runs often (after every configured-datasources snapshot, once per health-check tick), so a trial is checked in SQL first, and nothing of it is loaded while it holds no removed dataset's scan or saved scope. The file inventory's datasets are read at most once per call, and not at all while no dataset can be judged gone.
Current limitation: a removed dataset's scan is found only when its stored
scan_id is the file inventory's key exactly. One stored under another
spelling of its file (a mapped drive against its UNC path, a symlinked root)
would have to be resolved on disk to be found, so its scan_eligibility row
and its wave-ledger rows stay, and the patient API's scan endpoints keep
serving that scan, for instance among the scans of a patient who also has a
live dataset's. Runs leave it out of every verdict (_drop_non_live resolves
the scans the inventory misses), so a patient it was the only source of stays
in the trial's list until a live dataset's next run cuts over and removes
them.
Module
Functions
reconcile_dataset_removals
def reconcile_dataset_removals( cache: CacheProtocol, now: datetime | None = None,) ‑> list[RemovalOutcome]:Take every removed dataset's rows out of each published trial.
Every trial with a published patient_eligibility partition is checked.
A dataset is live for a trial as load_dataset_liveness says, and a
trial's lineage (its flow_specs row for a dataset name) is live unless
the Hub reports the dataset unlinked or, once the pod has a
configured-datasources snapshot, the snapshot does not hold the name as
configured. Unknown counts as live: a trial the pod holds no lineage for
is never unpublished, and with neither a snapshot nor an unlink record no
dataset is judged gone.
A stored scan is a removed dataset's when the file inventory holds it under a dataset no longer live, exactly as stored, and no live dataset's saved scope names it: a file under two datasets' roots has one inventory row, under whichever indexed it last, and the live dataset that selected it keeps it.
For a trial none of whose lineages is live:
- the trial's ledger rows for the removed scans' files, the removed datasets' saved scopes and those scans are deleted;
- then the whole published
patient_eligibilitypartition; - then the published pointer, so the trial serves nothing.
Nothing is deleted if the pointer's publish stamp shows a run has published, or withheld its cutover, since the reconcile began, and the pointer is cleared only while that stamp is unchanged.
That covers an EHR-only trial too. Otherwise, for a trial with a removed dataset's stored scans or saved scope:
- a patient whose every stored scan is a removed one has their row
deleted. A patient with other scans too is recomputed from the scans
read_trial_scans_for_recomputereads for them, and their row deleted if it reads none, unless a run would hold them back for a file a live dataset has yet to judge. Every other patient's row is left as published. If any row changed, the pointer is published again with the same partitions and tags, unless its publish stamp shows a run has published, or withheld its cutover, since the reconcile began; - then the trial's ledger rows for the removed scans' files, in every lineage, so a relink or a live dataset sharing a file processes it again, and the removed datasets' saved scopes;
- then the removed scans themselves.
If the pointer's publish stamp shows a run has published, or withheld its cutover, since the reconcile began, no patient row is written and the trial is left whole for the next reconcile: that run's rows are newer than the ones recomputed here.
An EHR-only trial with a live lineage left is not changed: its patients come from no scan. Each table is written through its store's own write session; no lock fence is held, so a run in flight can write a removed dataset's rows again, and the next call removes them again.
Arguments
cache: The pod's background cache.now: The time recomputed rows and a republished pointer are stamped with; the current time whenNone.
Returns
One outcome per trial where anything was removed, in project order;
[] when nothing was. A trial whose reconcile fails is logged and
left out, and the next call tries it again.
Classes
RemovalOutcome
class RemovalOutcome( project_id: str, scans_deleted: int, ledger_rows_deleted: int, patients_removed: int, unpublished: bool,):What a reconcile removed from one trial.
Attributes
project_id: The trial (project).scans_deleted: Stored scan rows deleted from the trial's publishedscan_eligibilitypartition.ledger_rows_deleted: The trial's wave-ledger rows deleted for those scans' files, across every lineage.patients_removed: Patient rows deleted from the trial's publishedpatient_eligibilitypartition.unpublished: Whether the trial's published pointer was cleared.
Variables
-
ledger_rows_deleted : int
-
patients_removed : int
-
project_id : str
-
scans_deleted : int
-
unpublished : bool