Skip to main content

root_change

Detect and report a datasource root that changed to another name.

A file's row in file_metadata is keyed by its resolved path, so the host part of a UNC root is part of every key. canonical_path_key makes case-only variants of a root (\\IMG-SERVER-01\Scans and \\Img-Server-01\Scans) one key, but it cannot tell that \\IMG-ALIAS\Scans (a DNS alias) or \\10.0.0.5\Scans (an IP address) name the same share. When the configured root is changed to such a name, the next walk indexes the whole share again as new files, and caches keyed by path stop matching.

This module does not re-key anything. It detects the change at the start of a walk and reports it, with a verdict from a small probe, so the change is visible in telemetry and in the pod log:

  1. The previous root is the one the last complete full pass walked, read from that run's tags (last_full_pass_root); a cache with no such pass falls back to the root of the most recently written rows. When it is on the same share or drive as the new root (in any case), nothing was renamed and the check ends there: a root narrowed or widened within one share keeps the same host in every key.
  2. Otherwise up to SAMPLE_SIZE stored files under the previous root are mapped to the new root by their path relative to the root, and each is checked there: same size and a modification time within MTIME_TOLERANCE_SECONDS (the test _is_unchanged uses). On Windows, when the file can be opened under both roots, the volume serial number and file ID are compared instead, which identifies the file itself.
  3. The verdict is RootChangeVerdict.SAME_SERVER when at least SAME_SERVER_MATCH_RATIO of the comparable samples match, RootChangeVerdict.DIFFERENT when fewer do, and RootChangeVerdict.UNKNOWN when no sample could be compared.

Module​

Functions​

check_datasource_root​

def check_datasource_root(    cache: CacheProtocol,    task_hash: str,    new_root: str,    *,    dataset_identifier: str | None = None,) ‑> RootChangeReport | None:

Detect a changed root and, when there is one, log and report it.

Logs a WARNING and sends a DatasourceRootChangedEvent, both naming only the share or drive of each root. Nothing is re-keyed.

Arguments

  • cache: The open background cache.
  • task_hash: The datasource's task hash.
  • new_root: The root this walk uses, as configured.
  • dataset_identifier: "{pod_name}/{datasource_name}".

Returns The report, or None when the root did not change.

detect_root_change​

def detect_root_change(    cache: CacheProtocol,    task_hash: str,    new_root: str,    *,    dataset_identifier: str | None = None,) ‑> RootChangeReport | None:

Compare new_root with the root the datasource was last indexed under.

Only the root of the last complete full pass is compared, so a change is reported by walks under the new root until one of them completes, and not after. A root on the same share or drive (in any case) is not a change.

Arguments

  • cache: The open background cache.
  • task_hash: The datasource's task hash.
  • new_root: The root this walk uses, as configured.
  • dataset_identifier: "{pod_name}/{datasource_name}", for the report.

Returns The probe's report, or None when the root did not change or nothing has been indexed yet.

share_root​

def share_root(path: str) ‑> str:

Return the share or drive of path, without any folder below it.

\\IMG-SERVER-01\Scans\2024\a.dcm gives \\IMG-SERVER-01\Scans and D:\Scans gives D:. A POSIX path gives its first component, e.g. /mnt for /mnt/scans, and a relative path gives ..

Arguments

  • path: The path to reduce.

Returns The share root, safe to send in telemetry.

Global variables​

  • MTIME_TOLERANCE_SECONDS : Final[float] - Modification times closer than this count as equal, as in _is_unchanged.
  • PROBE_TIME_BUDGET_SECONDS : Final[float] - The probe stops sampling after this long. A previous root on a dead server can stall each open for the SMB timeout, and the walk must not wait on it.
  • SAME_SERVER_MATCH_RATIO : Final[float] - The share of comparable samples that must match for SAME_SERVER. Below 1.0 so a few files deleted or rewritten since the last walk do not turn a rename of the same share into DIFFERENT.
  • SAMPLE_SIZE : Final[int] - Stored files sampled under the previous root. The sample query is bounded by this, so the probe stays cheap on an inventory of millions of rows.

Classes​

RootChangeReport​

class RootChangeReport(    task_hash: str,    dataset_identifier: str | None,    previous_root: str,    new_root: str,    other_roots: int,    sample_size: int,    comparable: int,    matches: int,    verdict: RootChangeVerdict,    file_id_compared: bool,):

The outcome of probing a changed datasource root.

Attributes

  • task_hash: The (pod, datasource) task hash.
  • dataset_identifier: "{pod_name}/{datasource_name}", when known.
  • previous_root: The root the inventory was last indexed under, as stored.
  • new_root: The root this walk uses, as configured.
  • other_roots: Further distinct roots still stored for the datasource.
  • sample_size: Stored files examined under the previous root.
  • comparable: Examined files that could be checked under the new root.
  • matches: Comparable files that are the same file under the new root.
  • verdict: The probe's verdict.
  • file_id_compared: Whether any sample was compared by file ID.

Variables​

  • comparable : int
  • dataset_identifier : str | None
  • file_id_compared : bool
  • matches : int
  • new_root : str
  • new_share : str - The share or drive of new_root.
  • other_roots : int
  • previous_root : str
  • previous_share : str - The share or drive of previous_root.
  • sample_size : int
  • task_hash : str

RootChangeVerdict​

class RootChangeVerdict(*args, **kwds):

Whether the new root reaches the same files as the previous one.

Variables​

  • static DIFFERENT
  • static SAME_SERVER
  • static UNKNOWN