Skip to main content

metadata_convergence

How one datasource's file_metadata and scan_metadata runs are ordered.

Both runtimes hold metadata:<task_hash> (limit 1) around their whole body (metadata_lock), and take a fencing token on the same key right after acquiring it (take_metadata_fence), checked before every chunk of rows they bank. A holder whose lease lapsed and whose lock a newer run then took stops at its next write with FencedOutError.

A success full index pass submits a scan as the last thing it does under the lock (submit_scan_for_index), unless a scan that has not yet decided whether to run is already live. A scan, once it holds the lock, runs only over a current inventory (inventory_is_current and no undecided live index), and otherwise skips with INVENTORY_NOT_CURRENT: whatever made the inventory not current submits the next scan when it succeeds.

Neither runtime waits for the lock for longer than metadata_lock_wait_seconds, since the wait holds a deployment slot. A run that gives up raises MetadataBusyError and closes its bookkeeping row as a METADATA_BUSY skip (record_metadata_busy). An index also schedules one follow-up (schedule_busy_index_follow_up); a scan schedules none, since the index holding the lock submits one when it succeeds and metadata recovery is the backstop.

A run has decided once its bookkeeping row is closed; rows carry their flow run's id (FLOW_RUN_ID_TAG) so a live Prefect run can be matched to it. A run that has decided but whose Prefect state has not yet left RUNNING must not count as pending, or the index and the scan could each defer to the other.

Module​

Functions​

decided_flow_run_ids​

def decided_flow_run_ids(cache: CacheProtocol, task_hash: str, run_type: str) ‑> set[str]:

Return the flow runs of run_type whose bookkeeping row is closed.

Arguments

  • cache: An open cache for the pod's background cache DB.
  • task_hash: The (pod, datasource) task hash.
  • run_type: FILE_METADATA_RUN_TYPE or SCAN_METADATA_RUN_TYPE.

inventory_is_current​

def inventory_is_current(    cache: CacheProtocol, task_hash: str, *, as_of: datetime,) ‑> bool:

Whether the latest full index pass succeeded, and finished by as_of.

The bookkeeping half of a scan's input check; the other half is that no index run is pending (undecided_live_runs).

A pass that finished at as_of counts. The index closes its row and then submits the scan while it still holds the lock, so the scan's acquire comes after the pass by however long that handover takes — and datetime.now advances in ~15.6 ms ticks on Windows, which can land both on the same one. Reading that tie as "not current" has the scan the index just submitted skip as inventory_not_current, and nothing submits another until recovery's next sweep (metadata_recovery_interval, ten minutes by default).

This is deliberately the opposite tie rule to scan_behind_inventory and waves.scan_sweep_complete, which must not count a tie as a sweep: a sweep that stood down writes its row within the same tick as the index it stood down for, and reading that as "read the inventory" would wave a half-built cohort. Same rows, different question: this one asks whether the inventory existed when the scan started, those ask whether a sweep has read it.

Arguments

  • cache: An open cache for the pod's background cache DB.
  • task_hash: The (pod, datasource) task hash.
  • as_of: When the scan acquired its metadata lock.

Returns Whether the inventory is current as far as the bookkeeping rows say.

inventory_pending​

def inventory_pending(    cache: CacheProtocol,    task_hash: str,    *,    log: logging.Logger | logging.LoggerAdapter[Any],) ‑> bool:

Whether an index run for task_hash is live and has not yet decided.

Such a run will change the inventory, and submit a scan if it succeeds. Never raises: an unanswerable question reads as nothing pending, since the caller holds the metadata lock and so no index body is executing.

Arguments

  • cache: An open cache for the pod's background cache DB.
  • task_hash: The (pod, datasource) task hash.
  • log: The flow's run logger.

metadata_lock​

def metadata_lock(    task_hash: str,) ‑> Iterator[MetadataLock]:

Hold metadata:<task_hash> for the duration of the block.

Waits at most metadata_lock_wait_seconds: the wait holds the run's deployment slot, and the holder may be a walk that lasts hours. The slot's lease covers a holder that dies without releasing it.

Arguments

  • task_hash: The (pod, datasource) task hash.

Raises

  • MetadataBusyError: If the lock is not acquired within the wait.

record_metadata_busy​

def record_metadata_busy(    run: MetadataRun, busy: MetadataBusyError, flow_name: str,) ‑> None:

Close run's bookkeeping row as a METADATA_BUSY skip.

Arguments

  • run: The open bookkeeping handle.
  • busy: The timed-out wait.
  • flow_name: The flow's name, for the log line.

scan_behind_inventory​

def scan_behind_inventory(cache: CacheProtocol, task_hash: str) ‑> bool:

Whether the last index pass that walked is newer than the last real scan.

Compared the way bitfount.flows.dag.waves.scan_sweep_complete compares them, so False here is that gate passing: a no-change index pass and a scan skipped as inventory_not_current count on neither side.

Arguments

  • cache: An open cache for the pod's background cache DB.
  • task_hash: The (pod, datasource) task hash.

Returns Whether a scan has yet to follow the last walking index. False when no index has walked.

scan_parameters_for_index​

def scan_parameters_for_index(index_parameters: dict[str, Any]) ‑> dict[str, typing.Any]:

Return the parameters of the scan that follows an index run.

Arguments

  • index_parameters: The index run's parameters.

Returns The scan's parameters: the datasource identity, the index's scope and preprocessing pass, and no constructor kwargs (instantiate_ophthalmology_datasource ignores them for scan).

schedule_busy_index_follow_up​

def schedule_busy_index_follow_up(    deployment: str,    *,    parameters: dict[str, Any],    task_hash: str,    run_type: str,    log: logging.Logger | logging.LoggerAdapter[Any],) ‑> bool:

Schedule one index run METADATA_BUSY_FOLLOW_UP_DELAY from now, deduped.

Declines when an index run for task_hash is already scheduled or pending. The follow-up carries this run's transient_retry: tag unchanged, so a busy lock neither consumes nor restarts that ladder. Never raises.

Arguments

  • deployment: The index's "<flow>/<deployment>" name.
  • parameters: This run's parameters, forwarded verbatim.
  • task_hash: The (pod, datasource) task hash.
  • run_type: The run's run_type: tag value.
  • log: The flow's run logger.

Returns Whether a follow-up was scheduled.

submit_scan_for_index​

def submit_scan_for_index(    *,    index_parameters: dict[str, Any],    task_hash: str,    index_run_id: str | None,    log: logging.Logger | logging.LoggerAdapter[Any],) ‑> bool:

Submit the scan that follows a successful index pass, deduped.

Declines when a live scan for task_hash has not yet decided whether to run: it acquires the lock after this index and so reads its inventory. Never raises; a failed submission is logged and reported, and periodic metadata recovery submits the scan instead.

Arguments

  • index_parameters: The index run's parameters.
  • task_hash: The (pod, datasource) task hash.
  • index_run_id: The index pass's bookkeeping run id.
  • log: The flow's run logger.

Returns Whether a scan was submitted.

take_metadata_fence​

def take_metadata_fence(    cache: CacheProtocol, task_hash: str, holder_run_id: str | None,) ‑> MetadataFence:

Take the next fencing token on metadata:<task_hash>.

Call once the lock is held; every earlier holder is fenced out from here.

Arguments

  • cache: The run's open cache.
  • task_hash: The (pod, datasource) task hash.
  • holder_run_id: The flow run taking the token.

Returns The fence to check before each write.

undecided_live_runs​

async def undecided_live_runs(    client: PrefectClient, deployment: str, task_hash: str, decided: set[str],) ‑> list[FlowRun]:

Return the live runs of deployment that have not yet decided anything.

Arguments

  • client: An open PrefectClient.
  • deployment: The "<flow>/<deployment>" handle.
  • task_hash: The (pod, datasource) task hash.
  • decided: Flow run ids whose bookkeeping row is closed (decided_flow_run_ids).

Returns The live runs not in decided; empty when the deployment is not registered.

Global variables​

  • FENCED_OUT - stop_reason of a run stopped by a newer holder of its metadata lock.
  • FLOW_RUN_ID_TAG - Bookkeeping-row tag holding the Prefect flow run that wrote the row.
  • METADATA_BUSY_FOLLOW_UP_DELAY - How long after giving up on a busy lock an index's follow-up is scheduled.

Classes​

MetadataBusyError​

class MetadataBusyError(task_hash: str, waited_seconds: float):

The metadata lock was not acquired within metadata_lock_wait_seconds.

Arguments

  • task_hash: The (pod, datasource) task hash.
  • waited_seconds: How long the run waited.

Attributes

  • task_hash: The (pod, datasource) task hash.
  • waited_seconds: How long the run waited.

Build the error.

MetadataFence​

class MetadataFence(cache: CacheProtocol, fence_key: str, token: int):

A fencing token on metadata:<task_hash>, checked before each write.

Attributes

  • cache: The run's open cache.
  • fence_key: The fenced lock's name.
  • token: The token this holder took.

Variables​

  • cache : CacheProtocol
  • fence_key : str
  • token : int

Methods​


check​

def check(self) ‑> None:

Raise unless this holder's token is still the latest.

Raises

  • FencedOutError: If a later holder has taken the fence, or the fence row is gone.

MetadataLock​

class MetadataLock(held: HeldLock, acquired_at: datetime):

A held metadata:<task_hash> lock.

Attributes

  • held: The slot, as keyed_concurrency_sync yields it.
  • acquired_at: When the slot was acquired, by the wall clock.

Variables​