Recorded provenance: code identity by default, and a source record on Ingest tables

Aperta
#1,771 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
git, python

Direzione di ricerca

Start with src/datajoint/jobs.py, src/datajoint/autopopulate.py, and src/datajoint/provenance.py, then review src/datajoint/declare.py, deploy.py, and table.py for schema and insert behavior. Run tests/integration/test_hidden_job_metadata.py and tests/integration/test_entry_provenance.py to reproduce the described cases before changing behavior. Done means the agreed defaults, transactional metadata, code identity, and Ingest source records are covered by tests and the affected documentation matches the implemented behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A row written by make() records the identity of the code that produced it, in the transaction that inserts the row. A row that enters from outside the pipeline records where it came from: an Entry row (dj.Manual), and an Ingest row (dj.Imported) whose make() reads a file, instrument, or API the pipeline does not track. Both records are readable through the query API. Foreign keys already establish which rows a result was computed from. They do not say which commit computed it or which file an imported row was read from, and neither fact can be recovered after the insert, so a pipeline author who needs either one a year later has it only if the library recorded it by default. Today the code version is recorded only when two settings are turned on, names the working directory's commit, and is written after the population transaction commits; Ingest rows carry no origin record; and several docs pages state these records as unconditional.

Every finding below was read, not run: each comes from reading master at b2994e5e and the docs at b68b6cc6. Items 1, 2, 4, and 6 describe runtime behaviour and carry a reproduction sketch, which is the test to write first and should fail before its fix. Items 3 and 5 are configuration defaults pinned by existing tests.

What is recorded today, where, and the smallest change

The sketches share two tables:

@schema
class Src(dj.Lookup):
    definition = "src_id : int16"
    contents = [(1,)]

@schema
class Comp(dj.Compute):
    definition = """
    -> Src
    ---
    val : int32
    """
    def make(self, key):
        self.insert1({**key, "val": 1})
  • 1. The recorded version names the working directory's commit, abbreviated, with no mark for uncommitted changes. _get_job_version runs git rev-parse --short HEAD with no cwd (src/datajoint/jobs.py:49-55), so a worker started outside the pipeline's checkout records another repository's commit, or '', which is also the value for "not configured" (jobs.py:45-46, :55-57). The value is resolved separately for the reservation (jobs.py:492), the ingesting context (autopopulate.py:695), the metadata update (:745), and each insert that records _prov (src/datajoint/provenance.py:113), and the results can differ within one job. No test pins it: tests/integration/test_hidden_job_metadata.py:181-182 accepts any string.

    • Change: resolve once per process from the repository of the module that defines the table class, as the full hash with a -dirty suffix; accept a deployment-supplied version string for installs without a checkout; log a warning when git fails. _job_version keeps '' as its empty default: the empty default of a string attribute is the empty string, never NULL, so a reader tests one value (Table Definition Syntax, "Defaults"; datajoint/datajoint-docs#271).
    • Compatibility: none for code. Recorded values change form, within varchar(64).
    • Read, not run. Reproduction:
    # jobs.add_job_metadata = True, jobs.version_method = "git"
    monkeypatch.chdir(other_repo)                      # a second checkout with its own HEAD
    Comp.populate()
    (version,) = Comp.connection.query(f"SELECT _job_version FROM {Comp.full_table_name}").fetchone()
    assert version.startswith(head_of_repo_defining_Comp)   # today: the short HEAD of other_repo
    
  • 2. Job metadata is written by an UPDATE after the population transaction commits. _populate1 commits at src/datajoint/autopopulate.py:734, then calls _update_job_metadata (:742), which runs UPDATE ... SET _job_start_time, _job_duration, _job_version in autocommit (:881-885). Other sessions read the row before its version is set, and a worker killed between the two statements leaves the row with null times and '' permanently. test_metadata_populated_after_make (tests/integration/test_hidden_job_metadata.py:166) covers only the completed case.

    • Change: move the UPDATE into the try body after make() returns (:697-714), before commit_transaction(), so one placement covers the plain and the tripartite paths and a failure rolls the row back.
    • Compatibility: none. _job_duration then excludes commit time.
    • Read, not run. Reproduction:
    # jobs.add_job_metadata = True
    monkeypatch.setattr(Comp, "_update_job_metadata", raise_error)   # stands in for a worker killed after make()
    with pytest.raises(RuntimeError):
        Comp.populate()
    assert len(Comp & "_job_start_time IS NULL") == 0   # today 1: the row is committed without its record
    
  • 3. No code version is recorded under default settings. jobs.add_job_metadata defaults to False (src/datajoint/settings.py:499-500) and jobs.version_method to None (:491-492). A Compute or Ingest table declared with defaults has no _job_version column, and one declared with only the first setting stores '' in every row. Pinned by TestConfigDisabled.test_no_metadata_when_disabled (tests/integration/test_hidden_job_metadata.py:231).

    • Change: after items 1 and 2, default both settings on in the next minor release, for newly declared tables. migrate.add_job_metadata_columns (src/datajoint/migrate.py:639) stays the route for existing tables.
    • Compatibility: no user code breaks. An unchanged schema declared after the upgrade gains three hidden columns, the effect #1568 kept out of a patch release; a minor release with a release note is the path, and both settings can still be turned off.
    • Sketch: declare Comp under default settings; "_job_version" in Comp().heading._attributes is False today.
  • 4. Ingest rows record neither the agent nor the external source. _tier_attributes grants _prov to the Entry tier only (src/datajoint/declare.py:582). With jobs.add_job_metadata on, an Ingest master gets start time, duration, and version (declare.py:564-568), with no user or host. The job table records host and user at reservation (src/datajoint/jobs.py:479-492), and complete() deletes that entry unless jobs.keep_completed is on (jobs.py:515-528). Pinned by test_only_entry_tables_get_the_column (tests/integration/test_entry_provenance.py:112, assertion at :124) and the Imported case of test_platform_attributes_added_per_tier (tests/unit/test_declare_hidden_attribute.py:117). #1547 proposed the slot for Entry and Ingest tables. Its settled design kept Ingest out because job metadata already records agent, time, and version, and #1555 shipped the slot for Entry only. This item adds that job metadata records no agent, and records time and version only when a non-default setting is on.

    • Change, which needs a decision because it reverses that exclusion: grant _prov to Ingest masters when capture is on, in declare.py:582 and in the Entry-only tier check of deploy.add_prov_column (src/datajoint/deploy.py:283). The insert path already fills any table that has the column (src/datajoint/table.py:979-1010), so agent, time, configured source, and version commit with the row. Which file was read is known only inside make(), so it stays a declared attribute that make() writes, and the docs say so (below).
    • Compatibility: an additive hidden column on newly declared Ingest tables with capture on. The two tests change.
    • Read, not run. Reproduction:
    # provenance.capture = True
    @schema
    class Frame(dj.Ingest):
        definition = """
        -> Src
        ---
        mean : float32
        """
        def make(self, key):
            self.insert1({**key, "mean": 0.0})
    
    Frame.populate()
    assert len(Frame & "_prov IS NULL") == 0            # today: unknown column `_prov`
    
  • 5. The Entry origin record is off by default, and an insert that cannot record it proceeds without one. provenance.capture defaults to False (src/datajoint/settings.py:517-518, pinned by test_settings_default_to_not_capturing, tests/unit/test_provenance.py:123). A table declared without the column is skipped with no message (src/datajoint/table.py:989-992), and rows present before deploy.add_prov_column keep null.

    • Change: default capture on in the minor release that carries item 3, for newly declared tables. Until then the docs qualify every "every Manual table" sentence (below).
    • Compatibility: as item 3. Newly declared Entry tables gain one hidden nullable column (#1568).
  • 6. Turning capture off stops recording on tables that carry the column. _attach_provenance returns when provenance.capture is false, before it looks for the column (src/datajoint/table.py:987), so a worker whose environment lacks the setting inserts unrecorded rows beside recorded ones. The how-to says such tables keep recording. No test pins either behaviour: tests/integration/test_entry_provenance.py:175 covers only the absent column.

    • Change: correct the how-to sentence and the spec's "At insert". Alternative: record whenever the table has _prov and keep capture as the declaration-time switch, which makes the schema, not each worker's environment, decide.
    • Compatibility: none for the docs change. The alternative resumes recording for a deployment that turned capture off after declaring tables with it.
    • Read, not run. Reproduction:
    # Subject (dj.Entry, subject_id : int16) declared with provenance.capture = True, then:
    dj.config.provenance.capture = False
    Subject.insert1({"subject_id": 2})
    assert len(Subject & "_prov IS NULL") == 0          # today 1; the how-to says 0
    
  • 7. The recorded attributes cannot be fetched or projected, and a mapping restriction on one returns every row. Tracked in #1562 (no reader) and #1561 (dropped predicate); #1553 is covered by #1562 and can close. The library's tests read the values with raw SQL (tests/integration/test_hidden_job_metadata.py:173; _raw_prov, tests/integration/test_entry_provenance.py:104).

    • Change: none beyond those two issues. They are listed because items 1 to 5 record values that no query can return until both close.
    • Compatibility: as stated in #1561, raising on a hidden name changes code that today receives every row.

Docs pages that misstate what is recorded

Paths are under src/ in datajoint-docs, and line numbers are at b68b6cc6.

  • Extrinsic Provenance at Entry Tables (reference/specs/boundary-provenance.md)
    • Overview (:5): rows carry _prov when capture is on and the table has the column.
    • Motivation (:13): foreign keys guarantee that the upstream rows exist, not that they are "correct". Name this half "intrinsic provenance", the counterpart of the page's title; same in the provenance.py:3 docstring.
    • Which Tables Carry It (:37-38, :45-47): job metadata records start time, duration, and an optional version, not the agent. Delete the sentence that calls an unregistered source "the modeling problem described in Table Declaration"; that page has no such passage. State that an Ingest make() writes its source's identity to declared attributes. The dj.Computed row says provenance is entailed; the code version is recorded.
    • Content (:56): "the code version" appears only when jobs.version_method is set; say what the value is.
    • Behavior, At insert (:129): "while capture is on", per item 6.
    • Querying (:195-207): rewrite when #1562 closes.
  • Record Data Origin (how-to/record-data-origin.md): Turn capture on (:15) states the reverse of item 6. Configure the source (:35) promises the code version unconditionally. New section after "What you cannot do, and what to do instead": an Ingest make() stores the source's path and checksum as required attributes with no default, with an example.
  • AutoPopulate Specification, §4.3 The make() reproducibility contract (reference/specs/autopopulate.md): the opening (:309) counts "the recorded make() code" among a row's inputs, which holds only with job metadata on. Rule 3 (:315) gains the Ingest clause that Computation Model, "Why the contract matters" (explanation/computation-model.md:83-86) already states: an Ingest make() also reads its external source and records that source's identity in the row.
  • Hidden Job Metadata in Computed Tables (reference/specs/job-metadata.md): Hidden Job Metadata Attributes (:23), Setting Interactions (:92-96), Population-time (:143-149), and Implementation Details §2 and §4 follow items 1 to 3.
  • Table Declaration, §3.4 Hidden Attributes (reference/specs/table-declaration.md:196-197, :202): _job_version is described as "Library version that produced the row"; it is the pipeline code's version. With item 2, _job_* is written inside the population transaction. The _prov row gains Ingest if item 4 is adopted.
  • Configuration (reference/configuration.md:182-183, :190): the three defaults, when items 3 and 5 ship.
  • Schema as a Workflow Specification (explanation/schema-as-workflow-specification.md): Semantics (:115-117) says "Each inserted row records the git hash of the make() source that produced it". Today that needs two settings, and the value is a repository commit. The AverageFrame example (:55-60) declares no source attribute.
  • The Relational Workflow Model (explanation/relational-workflow-model.md): the imaging example (:36-38) reads a TIFF and records no path or checksum. Lineage and reproducibility (:102-105) calls reproducibility structural and says "The model enforces what other systems merely log"; the foreign keys are enforced, but the code version a reproduction needs is recorded only with two settings on, and origin at entry only with capture on.
  • Scientific Data Pipelines (explanation/data-pipelines.md:137-145): the ScanInfo example declares no source attribute.
  • The Fan-Out Ingestion Pattern (explanation/fan-out-ingestion.md): "Every row written into a Manual table carries a hidden _prov attribute" (:70-71) holds only with capture on; with it off the modeled source_file column is the only origin record. The context entry is a record, unchecked and without cascade, not "exactly the link the missing foreign key would have carried" (:72-73). The Ingest example (:33-44) stores no checksum of the file it parsed.
  • Comparison to Provenance Systems (explanation/comparison-to-provenance-systems.md:56-64): "on every Manual table", and the same foreign-key sentence.
  • Data Entry tutorial (tutorials/basics/03-data-entry.ipynb, line 610 of the notebook JSON): "and the code version" holds only when jobs.version_method is set.

Order: correct the record before turning it on by default

  1. Write the failing tests from the sketches for items 1, 2, 4, and 6.
  2. Correct the docs sentences that misstate today's behaviour. They depend on no code change.
  3. Items 2 and 1, in that order. Neither changes a schema.
  4. Decide item 4 (the slot on Ingest masters) and item 6 (docs or library).
  5. Close #1562 and #1561, so the recorded values can be queried.
  6. Items 3 and 5 together in the next minor release, with a release note. Turning the default on before step 3 would spread a value that can name the wrong repository.
Lingua principale
Python
Stelle
198
Fork
107
Merge medio
4g 7h
PR unite (30g)
12

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di datajoint/datajoint-python

Tutte le issue di datajoint/datajoint-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.