DAG builders
build_batch_dag, build_cdc_dag and build_config_dag build whole DAGs from the operators.
DAG builders
Batch: build_batch_dag
from airflow_provider_rivet.dags import build_batch_dag
dag = build_batch_dag(
"rivet_postgres",
config="/opt/pipelines/postgres.yaml",
state_dir="/var/lib/rivet/postgres",
plan_file="/opt/pipelines/postgres.plan.json", # or exports=["users", "orders"]
compact_exports=["orders"],
operator_kwargs={"env_from_connections": {"RIVET_PG_URL": "rivet_postgres"}},
)
plan ──> apply: [a] [b] ─> wave_2_done ─> [big] ─> after_big ─> [huge]
│ │ │ │ │
└─> (every │ │ │ │
apply) [a] [b] load: [big] [huge]
load: │
compact: [big] every task ─> watcher
- Three TaskGroups,
apply,load,compact. They are the picture; dependencies run per table:apply.X >> load.X >> compact.X, allall_success. A failedapply.aleavesapply.b,load.band every later wave runnable; it blocksload.aandcompact.a. - Waves come from the plan file, as in the recipe, and each wave is ordered the way
rivet apply’s cost gate orders it: every heavy export alone, one after another, then the cheap exports of the wave together; wave N+1 starts after wave N. Ordering goes through tasks that do nothing,apply.wave_<n>_doneandapply.after_<export>, withtrigger_rule="all_done": the next export waits for the earlier one to finish, not to succeed. - Every rivet task is
trigger_rule="all_success"whateverdefault_argssays; atrigger_ruleinoperator_kwargsis refused, and so ismax_active_tis_per_dag(it is 1 on every rivet task, see limits).default_argsyou pass are merged over the builders’ (retries=2,retry_delay5 minutes), so adding a callback keeps the retries. exports,compact_exportsandtablesare lists; a bare string is refused. A name that is not an ASCII task id (daily orders,orders:v2,заказы) gets a task id with each character outside[A-Za-z0-9_.-]replaced by_and an 8-character hash of the name (apply.daily_orders_<hash>); rivet still receives the real name.planis a direct upstream of everyapplytask: a failed plan refresh blocks the whole run (upstream_failed), because a run that could not plan has nothing it should extract.- A run with a failed task is a failed run. Airflow colours a run by its leaf tasks, and an
ordering task that succeeds after a failure would otherwise leave the run green. Every DAG the
builders make ends in one task,
watcher(trigger_rule="one_failed", no retries), downstream of every other task: it is skipped when nothing failed and fails when anything did (the watcher pattern of Airflow’s documentation).
| What fails | Its own table | Other tables | Run |
|---|---|---|---|
plan | every apply, load, compact: upstream_failed; nothing reaches rivet | the same | failed |
apply.X | load.X, compact.X: upstream_failed | run; the next of a heavy chain and the next wave still start | failed |
load.X | compact.X: upstream_failed | run | failed |
compact.X | run | failed | |
| nothing | success (watcher skipped) |
plan_fileis read at parse time (no database access); without one, passexports=[...].- With
plan_file, aplantask rewrites that file fromrivet planat the start of every run;refresh_plan=Falsedrops the task and keeps the file as it is. extract="run"usesrivet run -e Xinstead of plan + apply.- Compact tasks are created only for the exports named in
compact_exports. Whether a table compacts depends on the load mode,load.layoutand the warehouse, which only rivet resolves (fullnever compacts; see ADR-0039 D3 for the skip reasons). A named export that rivet skips shows as an Airflow skip with itsskip_reasonin XCom; an export left out ofcompact_exportshas no compact task to show it. load=Falsedrops the load group (a config with noload:block).
CDC: build_cdc_dag
from airflow_provider_rivet.dags import build_cdc_dag
dag = build_cdc_dag(
"rivet_orders_cdc",
config="/opt/pipelines/cdc.yaml",
export="app_cdc",
tables=["orders", "users"],
state_dir="/var/lib/rivet/cdc",
)
runis ONE task for the stream.load.Tandcompact.Tare separate tasks per table.tablesare written as the config names them:dbo.ordersis loaded and compacted as the warehouse tabledbo_orders(the schema’s dot folded to_, as rivet names it).run >> load.T >> compact.T, allall_success: nothing is loaded or compacted after a drain that did not succeed (a refusal such as a log gap means the files of this cycle are not the stream), and a failedload.Tblockscompact.Tbut not another table. The parts of an earlier cycle whose load failed are loaded by the next run whose drain succeeds: the load consumes what rivet’s ledger says is unloaded. A load that finds nothing to do is a success (never an Airflow skip: a skip would propagate tocompact.T).- The same
watchertask ends the DAG: a failedrun,load.Torcompact.Tfails the run. - On
stop_reason: max_eventsthe run task succeeds, puts it in XCom and logs anotelinestopped at max_events (...)(recordrivet.cdc.max_events). It does not loop. load=Falsedrops the load and compact tasks (the stream’s files are the product);compact=Falsekeepsload.Tand dropscompact.T. Usecompact=Falseon Snowflake and ClickHouse, where there is nothing to compact (see “Storage and warehouse connections”).operator_kwargsgo to every task, as in the batch builder.
From a config: build_config_dag
build_config_dag builds the DAG of one rivet config the way build_source_dags builds a source’s DAG from its
config, with no Connection or init step of its own; the quickstart builds each pipeline through it. A config that cannot be built into a DAG at all is refused with RIVET_AIRFLOW_CONFIG_UNBUILDABLE, naming the exception.
from airflow_provider_rivet.dags import build_config_dag
dag = build_config_dag(
"rivet_erp_cdc",
config="/opt/pipelines/erp.yaml",
state_dir="/var/lib/rivet/erp",
operator_kwargs={"env_from_connections": {"RIVET_SOURCE_URL": "erp", "RIVET_STATE_URL": "rivet_state_erp"}},
)
modeis"cdc"or"batch"; left out, it is"cdc"when the config has amode: cdcexport. A CDC DAG of a config with nomode: cdcexport, a batch DAG of one with any (or with amode: ${VAR}that may resolve tocdc), and a config with no export are refused, coded (RIVET_AIRFLOW_CONFIG_NO_CDC_EXPORT,RIVET_AIRFLOW_CONFIG_HAS_CDC_EXPORT,RIVET_AIRFLOW_CONFIG_NO_EXPORT), as is a${...}inload.targetorload.layoutthat may decide whether an export compacts (RIVET_AIRFLOW_CONFIG_UNRESOLVED), and a stream namedwatcherbeside other streams (RIVET_AIRFLOW_CONFIG_RESERVED_EXPORT_NAME). As inbuild_source_dags, a refused or unreadable config is a DAG of one task,config, that fails naming the refusal; it never ends the DAG file, so a loop over several configs (the quickstart’s) still builds the others. Amodeother than these is aValueError.- Load tasks are built when the config has a
load:section; a compact task follows a table’s load only where rivet resolves it to a base and a buffer (a compacting warehouse withlayout: base_buffer, or a stream withbackfill:). ClickHouse and Snowflake get none. Task ids name the warehouse table:dbo.ordersisload.dbo_orders. - One CDC stream is built as
build_cdc_dagbuilds it:run,load.T,compact.Tat the top. Several streams (rivet initwrites one per table on SQL Server, Oracle, MongoDB and PostgreSQL outsidepublic) are aTaskGroupeach, named after the export (dbo_orders.run,dbo_orders.load.dbo_orders); they have no edges between them and run side by side, each under its own export’s lock, and the onewatcherends the DAG. build_cdc_dagnames a table’s tasks asbuild_config_dagdoes:dbo.ordersisload.dbo_orders(until this release it wasload.dbo.orders); rivet is still given the captured name.- Adding a second stream renames the first stream’s task ids (
runbecomes<export>.run): Airflow keeps the old ids’ history under the old names, and the new ids start with none.
One DAG per database: build_source_dags
Name the databases; each gets a DAG, created paused, that writes its own config the first time it runs. The operator’s work is to unpause it.
from datetime import datetime
from airflow_provider_rivet.dags import build_source_dags
globals().update(
build_source_dags(
["shop", "billing"], # Airflow Connection ids, else data sources of odbc.ini
mode="cdc", # or "batch"
config_dir="/opt/airflow/dags/rivet",
state_root="/opt/airflow/rivet-state",
state_connection="rivet_state", # a PostgreSQL server; each source gets the database rivet_state_<name>
init_args=[
"--tls",
"verify-full",
"--gcs-bucket",
"my-bucket",
"--bigquery-project",
"my-project",
"--bigquery-dataset",
"{name}",
],
operator_kwargs={"cloud_credentials": ["gcp"]},
schedule="0 */2 * * *",
start_date=datetime(2026, 1, 1),
catchup=False,
)
)
For each name <name>:
| DAG | rivet_<name>_cdc / rivet_<name>_batch, paused at creation (is_paused_upon_creation) |
| source | the Airflow Connection source_connection (default {name}; rivet_{name} for the layout of the cron recipe); without one, the data source [<name>] of odbc_ini (default /etc/odbc.ini; None turns it off). The task log says when odbc.ini was used, and the password of the data source is masked in it. The file is read as unixODBC reads it (the name in any case, the first section and the first value win, ; and # comments); a file that is missing or unreadable on the worker is refused with that cause, not as a missing data source. odbc.ini: MySQL, PostgreSQL, SQL Server (Server = host,port, a SQL login) and Oracle (ServerName = host:port/service) drivers; MongoDB only through a Connection |
| config | <config_dir>/<name>.yaml |
| state | the database rivet_state_<name> on the server of state_connection, and the folder <state_root>/<name> |
Before the config exists, the DAG has one task, init. When it runs:
- it creates
rivet_state_<name>if missing (the state Connection’s user needsCREATEDB) and<state_root>/<name>(state_rootmust exist: a missing one is a volume that is not mounted, and is refused); - runs
rivet init --source-env RIVET_SOURCE_URL [--mode cdc] -o <state_root>/<name>/.init/<name>.yaml <init_args>,{name}ininit_argsreplaced (--source,--source-env,--source-file,-o/--outputand, in a CDC DAG,--modeare the operator’s: refused in any spelling,--output=xand-oXtoo;--mode cdcin a batch DAG is refused, a value that readscdcis not).initnames the stream’sserver_id, slot, checkpoint and bucket prefix after the file name, so two sources never share them; - runs
rivet checkon it; - refuses a config that writes to a bucket prefix or a warehouse dataset another config in
config_dirwrites to (give each source its own, e.g.--bigquery-dataset {name}); - writes
<config_dir>/<name>.yaml, never over an existing file, and logs each export and its tables.
A failed step writes no config and is not retried: read the log, fix the cause, and clear the task in the latest run
only (an init cleared in several runs at once fails in each run that starts after the first one wrote the config,
since the DAG has no init task then). The bucket and the
warehouse dataset are not created: they must exist.
Once the config exists, the next parse builds the same DAG from it: for cdc, each mode: cdc export of the
config as a stream (run, then a load per table when the config has a load: block and a compaction of each table
whose layout rivet resolves to a base and a buffer); for batch, every export through build_batch_dag, with a load
when it has load: and a compaction of each export whose layout rivet resolves the same way. The layout is read as
rivet resolves it (ADR-0034 D7), from the config alone: BigQuery (the one warehouse that compacts), a load that
accumulates (incremental, cdc; never full, and an export without mode: is full), and layout: base_buffer
written at load:, over it at the export’s load:, over it at the captured table’s load.tables.<t>; with no layout
written, a stream with cdc.backfill is a base and a buffer, anything else a change log with no compaction.
rivet init --mode cdc writes one stream over all tables on MySQL and PostgreSQL’s public, which keeps the
build_cdc_dag shape (run, load.T, compact.T), and one stream per table on SQL Server, Oracle, MongoDB and other
PostgreSQL schemas: each stream is then a group named after its export (dbo_orders.run, dbo_orders.load.dbo_orders: a load takes the warehouse name, the schema’s dot folded to _),
the streams running side by side. A batch DAG takes init’s suggested mode per table (full for a small one), or the one init_args names
(--mode incremental). One name is one config and one state database: a source in both a CDC and a batch DAG needs
two names, or a config_dir and state_root per mode, and a batch DAG over a config that has a mode: cdc export is
refused (below). Oracle CDC signs in as a common user (c##…) that LogMiner
lets mine, with --schema naming the tables’ owner. init_args are one list for every name of a call: sources that need other flags
(--tls require for SQL Server, --schema) go in a call of their own. So
unpausing runs init at once, and the stream starts at the next scheduled run (or trigger one).
- The config folder must be one the scheduler (Airflow 3: the dag-processor) and every worker read, as the DAGs
folder. If
initkeeps running, the file it wrote is not where the scheduler reads. - Edit the config like any other: the DAG follows it at the next parse. Delete it to have
initwrite it again. - A config the builder cannot build a DAG from (not YAML, a wrong shape, no
mode: cdcexport for a CDC DAG, amode: cdcexport under a batch DAG, no export at all) never ends the DAG file: that source’s DAG keeps its id and has one task,config, which fails without retry naming the cause (RIVET_AIRFLOW_CONFIG_UNREADABLEwith the line and column of a YAML error,..._NO_CDC_EXPORT,..._HAS_CDC_EXPORT,..._NO_EXPORT,..._UNBUILDABLE) and the remedy; the other sources of the call are built as they are. Fix the config (or delete it forinit) and the next parse builds the real DAG. A wrong argument of the call itself (a bad name,operator_kwargs, a DAG setting) is still an import error, as it is the same for every source. - An export whose id in the
applygroup is the id of an ordering point of a plan (after_<heavy export>,wave_<n>_done) keeps its id; the ordering point gets_barrierappended. - A config moved from cron (
docs/cron-to-airflow-cdc.md) works as it is: the source goes to the variable itsurl_envnames. operator_kwargsgo to every task,initincluded;env_from_connections,env_from_variables,configandstate_dirare the builder’s.env_from_variables({ENV_NAME: "<variable key>"},{name}replaced in the key) fills an environment variable from an Airflow Variable per source, read when each task runs and never at parse. For a config withurl_secret: ${RIVET_SOURCE_URL_SECRET}:env_from_variables={"RIVET_SOURCE_URL_SECRET": "rivet_{name}_url_secret"}, one Variablerivet_<name>_url_secretper source. A missing Variable fails that source’s tasks withRIVET_AIRFLOW_VARIABLE_MISSING, before rivet starts, and no other source. The walk-through, the IAM grant and the comparison with Airflow’s Secret Manager backend are in Connections and secrets.- The DAG file reads only files: no database is reached while the scheduler parses it.