Skip to content

Latest commit

 

History

History
379 lines (305 loc) · 17.9 KB

alert-processing.md

File metadata and controls

379 lines (305 loc) · 17.9 KB

Alert processing

BOOM consumes Kafka streams of alerts from astronomical surveys and outputs Kafka streams for consumers like SkyPortal.

Each alert is processed with the following pipeline:

  1. Alerts are normalized to unify their schemas as much as possible. Their data is split and inserted into an alert dataset, an object dataset, and an image dataset, named according to the survey with which it is associated.
  2. Cross-matches with object IDs from other data catalogs (from both live and archival surveys) are added. This is done based on the location (right ascension and declination) of the object in the alert.
  3. Machine learning model classification scores and other properties are added to "enrich" the alerts.
  4. A set of user-defined filters are applied. Any alert that passes through at least one filter is sent to a dedicated Kafka output stream for that alert's input stream.

Data flows through the system as follows:

graph TB

    Input[Input alert stream]

    subgraph Kafka
        Output[Output alert stream]
        BabamulOutput[Babamul streams]
    end

    subgraph Valkey
        AlertQueue[Alert queue]
        EnrichmentQueue[Enrichment queue]
        FilterQueue[Filter queue]
    end

    subgraph MongoDB
        direction TB
        AlertCollection[Alerts]
        ObjectCollection[Objects]
        ImageCollection[Images]
    end

    subgraph BOOM services
        KafkaConsumer[Kafka consumer]
        subgraph Scheduler
            AlertWorker[Alert worker]
            EnrichmentWorker[Enrichment worker]
            FilterWorker[Filter worker]
        end
    end

    Input --> KafkaConsumer
    KafkaConsumer -- Alert Avro --> AlertQueue
    AlertQueue -- Alert Avro --> AlertWorker
    AlertWorker -- Candidate ID --> EnrichmentQueue
    AlertWorker -- Alert, object, images --> MongoDB
    EnrichmentQueue -- Candidate ID --> EnrichmentWorker
    EnrichmentWorker -- Candidate ID --> FilterQueue
    EnrichmentWorker -- Alert Enrichment scores --> AlertCollection
    EnrichmentWorker -. Public ZTF/LSST alerts .-> BabamulOutput
    MongoDB -- Alert, object, images --> EnrichmentWorker
    FilterQueue -- Candidate ID --> FilterWorker
    MongoDB -- Enriched alert --> FilterWorker
    FilterWorker -- Enriched alert that passed at least one filter --> Output
Loading

MongoDB serves as the storage, cross-matching, and filtering engine.

When scaling the system to include additional live survey input streams, each one will have its own:

  • Kafka consumer
  • Alert queue
  • Alert workers
  • Enrichment queue
  • Enrichment workers
  • Filter queue
  • Filter workers
  • Alert collection
  • Object collection
  • Image collection
  • Output stream

Re-enriching alerts after a change

Enrichment happens once, as an alert is ingested. Nothing revisits an alert afterwards, so a change to enrichment — a new classifier, a retrained model, a corrected formula — leaves every alert already in the database holding values the current code would not produce.

The problem is that a stale value looks exactly like a current one. classifications.btsbot is present before and after a model swap; only the number differs, and nothing on the document says which model produced it. The same applies to everything under properties, which is computed by BOOM's own code rather than a model.

You can see where this has been worked around by hand. ZtfAlertProperties.sso is an Option whose doc comment warns that None means "enriched before this field existed", not "evaluated and found not to be a solar system object". That is this problem, patched one field at a time.

The enrichment set

Each alert therefore carries an integer, enrichment_set, naming a row in the enrichment_sets collection that records exactly what produced its enrichment:

{ "_id": 7, "survey": "ztf",
  "models": {
    "acai_h": { "name": "d1_dnn_20201130", "sha256": "ab12…" },
    "btsbot": { "name": "v2.0.0",          "sha256": "cd34…" }
  },
  "derivations": { "photstats": 1, "sso": 1, "crossmatch_flags": 1,
                   "detection_history": 1 },
  "fingerprint": "…", "first_seen": 1765000000 }

One integer per alert rather than a version per field: at BOOM's scale the difference is tens of gigabytes of bookkeeping. Finding stale alerts is then { enrichment_set: { $nin: <acceptable sets> } }, indexable and exact.

Sets record components, not one opaque number, so a diff says which part moved. If two sets differ only in btsbot, the alerts at the older set need that one model re-run, not all six.

The stamp says which enrichment ran over an alert, not which fields came out of it. A component can decline an alert — AppleCiDER off the light curves it does not cover, a classifier whose cutouts would not read — and it leaves its own key absent when it does, which is what the missing_field selection below is for. Stamping only the alerts that came out complete would leave every declined one unstamped, so a stale run would select it, fail the same way, and never clear it.

On a GPU deployment the set records villar_fit as well, but only when the GPU is enabled: the same image runs with it switched off in config, and then computes no fit.

Models are hashed, derivation logic is declared

A model's identity is the SHA-256 of its file. A filename is a claim — the weights behind acai_h.d1_dnn_20201130.onnx could be replaced without anyone renaming it — and a hash cannot lie. Models are declared once in ZTF_MODELS (src/enrichment/version.rs), which is both what the worker loads and what gets hashed, so the two cannot disagree.

Derivation logic cannot be hashed the same way. "Did this change alter the output?" is a semantic question: a refactor does not, a corrected formula does, and hashing the source would flag every comment edit until nobody trusted it.

So when you change enrichment logic in a way that alters what it writes, bump the matching version in DERIVATIONS. The components are deliberately coarse:

Component Covers
photstats properties.photstats, multisurvey_photstats
sso properties.sso, and the activity metrics derived from it
crossmatch_flags rock, star, near_brightstar, stationary
detection_history properties.detection_history, multisurvey_detection_history
variability properties.variability
host_association properties.hosted and properties.best_host, from the host_galaxy the DLR scoring writes
applecider_postprocess applecider_fusion, applecider_outputs and the fusion embedding, which BOOM derives from the model's alpha
villar_fit villar_fit, on a GPU build. Fitted by BOOM's own PSO, so a change to the fit or its PsoConfig belongs here rather than in a model hash
lightcurve_fits arnett_fit and thermal_fit, including a change to the pinned lightcurve-fitting revision that alters them
gp_fit gp_fit, including a change to the pinned rust-gp revision that alters it

These are the ZTF worker's. The LSST, DECam and WINTER workers write properties only, so a set of theirs names no models.

villar_fit is declared in GPU_DERIVATIONS rather than DERIVATIONS, because a CPU build writes no fit at all and so produces genuinely different enrichment — the same reasoning that keeps a feature-gated model out of ZTF_MODELS when the feature is off. The two builds therefore get different set ids instead of a CPU-enriched alert claiming a fit it does not carry.

A bump records which code wrote an alert; on its own it does not make older alerts stale. When the values already written are wrong and must be recomputed, also raise the derivation's entry in STALE_BELOW to the new version: alerts below it are stale. A bump that only adds a field, or that improves values without making the old ones wrong, leaves STALE_BELOW alone, and older alerts simply lack the new field. An unstamped alert counts as version 0 of every derivation, so any entry in STALE_BELOW makes the alerts predating stamping stale too.

Adding or swapping a model

Pointing an existing model at retrained weights is one edit: its entry in ZTF_MODELS. The worker loads the file through model_path, which reads this list, and the same list is hashed into the fingerprint, so a changed entry moves the fingerprint and interns a new set. Alerts enriched before keep their scores and their set, which names the model that produced them: a model change never makes an alert stale, and rescoring the archive is an explicit all or candid_range reprocess.

Adding a new classifier is not, and the entry is the last step rather than the whole job. SharedModels holds one named field per model and classify makes a fixed set of inference calls, so a new entry on its own declares a file and hashes it without ever loading or running it — which stamps alerts with a set naming a model that never scored them. The wiring it also needs: a field on SharedModels, its load in SharedModels::load, an inference call in classify, and a field on ZtfAlertClassifications for the score to land in.

Skipping it is silent. The model runs, scores get written, and the fingerprint does not move, so alerts carry a set that does not name the model that scored them, which is worse than having no stamp at all.

Nothing catches this for you if the model lives in its own module and calls load_model directly. Models routed through src/enrichment/models/ get a panic at startup (<field> is not declared in ZTF_MODELS) because they resolve their path through the list; a module that hardcodes its own path does not.

Two constraints on what may go in the list:

  • Every declared file must exist wherever the worker runs. current_models hashes all of them at startup and a missing file is a hard error, not a warning — the worker will not start.
  • A model behind a cargo feature must be #[cfg]'d out of the list when the feature is off, which follows from the above. That is also the right answer: a build without the model genuinely produces different enrichment, so it should get a different set id rather than claim the same one.

Do not bump DERIVATIONS for a model change. The file hash already covers it; DERIVATIONS is for output computed by BOOM's own code.

Seeing what is stale

The admin page shows an Enrichment section per survey: the current set, and any set alerts are still sitting at, with what each differs by (differs by: btsbot, differs by: sso). Alerts enriched before stamping existed are listed separately when STALE_BELOW makes them stale.

Like the catalogs table, it reports and never acts: re-enriching an archive is days of work and stays an explicit decision. The count also feeds the badge on the admin link, so drift is visible without going looking for it.

GET /enrichment/status is the same data.

Both that query and the stale selection need an index on enrichment_set. initialize_survey_indexes creates it for ZTF alone, since no other survey stamps the field and the index would hold nothing but nulls there. It is started and not waited on: on an archive that predates the field the build takes hours, and the scheduler has no reason to hold up ingestion for it. Nothing to run by hand, on this or any other deployment.

While it builds, the status query is a collection scan. It is capped at five seconds rather than left to run, and the survey then reads not known — an empty list of stale sets is not the same answer as "nothing is stale". So the first hours after a deploy read not known, then turn into a real answer on their own once the index lands.

"Current" is what the workers say it is, not what the reader computes. An enrichment worker publishes the set it resolved into enrichment_current, one document per survey, and the admin page reads that. It does not resolve a set of its own, for three reasons: the API has no reason to carry the ONNX model files; if it carried them they could differ from the ones the worker actually loaded, so the page would report a "current" set no alert was ever stamped with; and resolving interns, so polling a status endpoint would create set rows as a side effect.

Until a worker has started, the survey reads no set published rather than "up to date" — nothing can be shown stale against a set that does not exist, and saying otherwise would be the guess this whole mechanism is meant to avoid.

Accepting a set instead of reprocessing

Not every change is worth a reprocessing run. A derivation version might be bumped for something that cannot affect the alerts already scored, or the stale values might simply be good enough. Accept the set and it stops being reported as drift, and a stale reprocess skips it.

Accepting records the decision against the set, in one document. It does not touch a single alert, and that is deliberate: stamping alerts with a set that did not produce them would make each of them claim provenance it does not have — the one thing the stamp exists to prevent — and would cost a write per alert across the archive to do it. Every alert keeps saying exactly what enriched it; the acceptance sits beside that, with who decided and why.

A reason is required, because "this is fine" is only useful to the next person if it says on what grounds. The decision is written to the data_mutations ledger, and it can be withdrawn — nothing is destroyed.

An acceptance is scoped to the set it was made against. Accepting set 6 while 7 is current says "6 is as good as 7". When the current set becomes 8, that is a new question and set 6 is reported as stale again.

Kicking off a re-enrichment

From the admin page, or the API:

curl -X POST http://localhost:4000/tasks \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"task_type": "enrich_reprocess",
       "params": {"survey": "ztf", "selection": {"kind": "stale"}}}'

selection decides what gets reprocessed:

Selection Use it when
{"kind": "stale"} After raising STALE_BELOW. Selects the alerts whose derivations are below it and not accepted, including alerts predating stamping.
{"kind": "missing_field", "field": "classifications.acai_h"} Adding a classifier, where the field genuinely does not exist yet.
{"kind": "candid_range", "from": …, "to": …} Reprocessing a known import.
{"kind": "all"} Everything. Hundreds of millions of alerts.

Prefer stale after raising STALE_BELOW. To rescore after a model change, use all or candid_range: missing_field only finds alerts that never had the field, so it selects none of them: every alert still has the field, just with an old value.

missing_field also selects alerts a component skipped deliberately: an output that can be absent is skipped rather than written as null, so classifications is omitted when the cutouts cannot be read and the AppleCiDER keys are omitted for light curves it does not cover. cross_matches is the exception — a missing catalog key there does mean the alert predates that catalog being configured. So does gp_fit, which is written with every key null for a light curve it cannot fit (see Gaussian-process fits).

The task selects the alerts, queues them, runs the enrichment workers over them, and finishes when the queue drains. It disables Babamul and does not forward to the filter queue, so reprocessing never re-alerts anyone.

Writing only some outputs

To backfill a field that enrichment gained after older alerts were processed, list what to write instead of re-running everything:

-d '{"task_type": "enrich_reprocess",
     "params": {"survey": "ztf", "selection": {"kind": "missing_field", "field": "gp_fit"},
                "gp_fit": true}}'

properties lists keys of properties to rewrite (DECam and ZTF), and gp_fit: true refits gp_fit (ZTF). Nothing else on the alert changes: no model is loaded, no free GPU memory is required, no cutout is read, and enrichment_set keeps its value, since the classifications it vouches for were not recomputed. That is also why such a run cannot use the stale selection.

Watching and controlling a run

The admin page shows active runs with a progress bar and a live log tail. Or:

curl -s .../tasks | jq '.data[] | {task_type, status, progress}'   # recent runs
curl -s .../tasks/<task_id>/logs?after_seq=0                        # tail the log
curl -X POST .../tasks/<task_id>/cancel                             # stop it

Cancelling stops the workers at a batch boundary rather than mid-alert. Because re-enrichment is idempotent — scores are recomputed from the stored alert — resubmitting after a cancellation or a failure picks up the remaining work: the alerts already done now carry the current set and no longer match stale.

A run competes with live ingest for the same GPU and model mutexes. n_workers throttles it, and a backfill that takes a week without delaying tonight's alerts is better than one that takes two days and does.

GET /data/mutations?collection=ZTF_alerts shows what past runs did, including the enrichment set each one wrote. See the task system.

What this does not do yet

  • Selective re-running. The set records components and version::diff says which moved, but enrich_reprocess re-runs the whole enrichment for a selected alert unless told which outputs to write (see Writing only some outputs). Narrowing it from the recorded versions is not built.
  • Catching a forgotten version bump. Nothing verifies that a change to derivation logic came with a bump — it is a code-review responsibility. A golden-output test (fixed input alerts, checked-in expected output, failing when output changes without a version change) is the natural guard and does not exist yet.
  • Alerts enriched before stamping. They have no enrichment_set and count as version 0 of every derivation, so the first entry in STALE_BELOW makes the whole archive before stamping stale.