Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

State

Where rivet’s state lives under Airflow, and how to set up a worker for it.

State, by deployment

WorkerStateWhat the operator does
Ephemeral pod (KUBERNETES_SERVICE_HOST in the environment, or deployment="pod")PostgreSQL, requiredrefuses SQLite (RIVET_AIRFLOW_STATE_SQLITE_ON_POD); refuses every CDC export (RIVET_AIRFLOW_CDC_ON_POD: the checkpoint is a file and would live on the pod’s disk) until rivet stores it in the state database; runs the config in place; keeps rivet’s stderr in a private file on the pod’s disk (stderr_temp), never in the task log
Local or long-lived worker (deployment="local" overrides the detection for a long-lived Kubernetes worker with a persistent volume)SQLite in a declared directory, or PostgreSQLrequires state_dir for SQLite state and for CDC; checks it; refuses state or a checkpoint left beside the original config; refuses SQLite state when Airflow’s executor may run tasks on several hosts (Celery, Kubernetes, Edge, …: RIVET_AIRFLOW_STATE_SQLITE_MULTI_HOST) unless the task says single_host=True; warns once per task that SQLite is single-host

A state_dir that is not given where the state needs one is refused with RIVET_AIRFLOW_STATE_DIR_REQUIRED; one that cannot be created or written is refused with RIVET_AIRFLOW_STATE_DIR_NOT_WRITABLE. Creating a PostgreSQL state database needs psycopg2 on the worker, and a worker without it is refused with RIVET_AIRFLOW_STATE_DATABASE.

state_dir is an operator parameter, not an Airflow Variable. The builders pass one value to every task they create, so “the same path for every task” holds by construction, it is visible in the rendered fields, and reading it needs no metadata-database access on the worker.

PostgreSQL state is RIVET_STATE_URL=postgresql://... in the task’s environment: env_from_connections={"RIVET_STATE_URL": "rivet_state"}, or set it on the worker and name it in env_passthrough=["RIVET_STATE_URL"] (a worker variable is not inherited unless it is named).

A RIVET_STATE_URL set on the worker and not passed on is refused. If the worker’s RIVET_STATE_URL names a state rivet would open, by rivet’s own rule (it starts with postgres:// or postgresql://, or it is a sqlite:<path> file), and the task’s environment has no entry for it (not in env_passthrough, env or env_from_connections), the task fails before rivet starts with RIVET_AIRFLOW_STATE_ENV_NOT_PASSED and is not retried: rivet would otherwise run green on an empty SQLite state in state_dir while the real state sits in the database or file the worker names (an incremental export reads from the start again, a CDC stream re-anchors). An empty or blank value, or one with any other scheme, selects no state here and is not refused. The message names the variable and never its value. Fix it one of two ways: inherit the worker’s value with env_passthrough=["RIVET_STATE_URL"], or give the task its own value with env_from_connections={"RIVET_STATE_URL": "<conn_id>"} (or env). If the worker’s variable is not meant for this pipeline, unset it on the worker, or declare env={"RIVET_STATE_URL": ""} on the task: a declared value, also an empty one, is the task’s own decision and rivet then keeps SQLite state in state_dir.

A sqlite:<path> state a task passes on is honoured. rivet opens the file the URL names: an absolute path as written (sqlite:/data/rivet/state.db, sqlite:///data/...), a relative one against the directory of the config it runs, which is state_dir (the copy). The checks for state left beside the original config look for that file instead of state_dir/.rivet_state.db, the single-host warning and the multi-host refusal name it, and a pod refuses it like any SQLite state. The file still lives on this worker’s disk: it is single-host state.

Local worker setup guide

  1. Create the state directory yourself, once, on storage that outlives the worker process. A host path or a named volume, never a container’s own filesystem. One directory per pipeline (per source database), for example /var/lib/rivet/<pipeline>. The operator never creates it: a directory that silently appears empty is exactly the failure this guide exists to prevent, so a missing one is refused (RIVET_AIRFLOW_STATE_DIR_MISSING).

  2. Every task of every DAG that touches the pipeline must see the SAME absolute path. With Docker, mount the same host path at the same container path in every worker. Pass that path as state_dir (the builders do it for all their tasks).

  3. What is in it.

    • .rivet_state.db: rivet’s state, under its default name.
    • <config name>.<hash>.yaml (and <config name>.<hash>--<export>.yaml): the operator’s copy of your config, byte for byte. <hash> is the first 8 hex digits of the SHA-256 of the original’s absolute path, so two configs with one file name never share a copy. rivet resolves the config’s relative paths (its SQLite state, a cdc.checkpoint, a query_file, a local destination.path) against the config’s directory, so running the copy puts all of them here (see 5). Edit your original; the copy is rewritten on every task. The operator does not set RIVET_STATE_URL=sqlite: itself: the copy already keeps the state here on every rivet it runs. A sqlite: value the task passes on moves the state to the file it names (see above); leave it unset for tasks unless the state must live elsewhere.
    • every relative query_file of the config, copied to the same relative path (see 5), and .rivet_airflow_query_files.json, which records the config directory each one came from, as the DAG gave it (the dags/repo of a git-sync link, not the rev-<sha> it points to today), so a new revision of the same config is the same owner. The copy and the record are made under one lock (airflow/locks/state/query-files.lock), so two tasks of two config directories that share this state directory never both pass the check.
    • a relative cdc.checkpoint, which resolves beside the copied config, that is, here.
    • airflow/<dag_id>/: sealed plan artifacts (plans/<run_id>/<export>__<task_id>__s<max_tries>__<inputs>-<hash>.json), the crashed ledger, scratch files; airflow/locks/run/<export>.lock (run, apply, cdc_run), airflow/locks/<export>.lock (load and compact of a whole export) and airflow/locks/table/<export>__<table>-<hash>.lock (load and compact of one table) (Overlap).
    • logs/<dag_id>/<task_id>-<hash>/: rivet’s stdout and stderr, one file per step and try.
    • How a name becomes a path. A DAG id, task id, run id or export name that is one lower-case word of a-z 0-9 _ . - (at most 80 characters, not starting with . or -) is used as it is. Any other name (other characters such as : + or non-ASCII letters, upper case, a mapped task’s task_id[index]) is written as its readable form (each run of other characters replaced by _) followed by - and 12 hex digits of the SHA-256 of the exact name: run id scheduled__2026-10-06T00:00:00+00:00 becomes scheduled__2026-10-06T00_00_00_00_00-b07b0ae9e551. Two different names therefore never share a directory, a sealed plan, a log file or a ledger, also on a file system that ignores case. A task’s log folder always carries the hash, also for a plain task id, so it is never a folder an older layout wrote to.
    • One sealed plan per task, try series, config and export. The artifact’s name is made from the export, the task id (with the map index), the try series (s<max_tries>; Airflow moves max_tries when a task is cleared) and <inputs>: a digest of the config’s absolute path, the config’s bytes and the export’s query_file. Two tasks of one DAG run never share a plan, also when they run two configs that both have an export of one name; a retry of the same task replays its own plan; a clear starts a new series and plans again, so a clear after a successful apply never replays a cursor snapshot that apply already moved past (rivet would refuse it RIVET_PLAN_ARTIFACT_STALE); a config or query file edited between two tries is planned again under a new name.
    • A retry replays its own plan only while the resolved config path and the config’s bytes are unchanged. A config reached through a symlink that is re-pointed between two tries (a new git-sync revision of the DAG folder) is planned again, also when the bytes are the same, and leaves one more config copy and plan artifact in the state directory.
    • The plan digest does not cover the environment. Changing the source between two tries of one task instance (another URL in env, an edited Connection) replays the plan made for the old source against the new one. Clear the task in a new DAG run, or delete its artifact, after such a change.
    • The artifact records the export and the config it was planned from (export_name, config_path). apply refuses a plan object in which either is missing or is not the task’s own (RIVET_AIRFLOW_PLAN_ARTIFACT_FOREIGN), expired or not, and leaves the file as it is, so the retry is refused for the same reason until a person deletes it (or, once it has expired and is 24 h old, the pruning below removes it). The config is compared as a file, not as text: another case, Unicode form or symlink of the same file is the task’s own, and a recorded path that no longer exists is not. A file at that path that is not a JSON object (empty, cut short) is planned over.
    • Upgrading a state directory written by an earlier build of this package: files of an older layout are left alone. They are never read, never pruned and never deleted, so remove logs/<dag_id>/<task_id>/ folders without a -<hash> suffix and airflow/<dag_id>/plans/<run_id>/<export>.json files by hand when you no longer need them. A run in flight during the upgrade plans again, and its crashed budget starts empty (at most one extra crash retry).
    • .rivet_airflow_marker: a random id written by the first task that used the directory; an empty one is refused (RIVET_AIRFLOW_STATE_DIR_MARKER_EMPTY) until a person deletes it.

    The copies and every file under airflow/ and logs/ are created with mode 0600, in directories of mode 0700 (an existing airflow/ or logs/ directory is tightened). Moving an existing pipeline under Airflow: if .rivet_state.db or a relative cdc.checkpoint file already sits beside your original config and not here, the task is refused before rivet starts (RIVET_AIRFLOW_STATE_BESIDE_CONFIG, RIVET_AIRFLOW_CHECKPOINT_BESIDE_CONFIG): the copy would not see it, and rivet would start from an empty state or re-anchor the stream. Stop the runs, move the file into the state directory (the state database with its -wal and -shm files; the checkpoint to the same relative path), and run again. Using the config’s own directory as state_dir needs no copy and no move. After rivet init -o X: init records the source’s primary keys in a .rivet_state.db beside X, which the task then refuses the same way: move it (with -wal and -shm) into state_dir, or run init with RIVET_STATE_URL set to the PostgreSQL state the tasks use. rivet 0.32.2 adds RIVET_STATE_URL=sqlite:<path>, so init can write into <state_dir>/.rivet_state.db, the file the tasks use (README Quickstart).

  4. What breaks when a task sees a different or an empty directory. rivet creates a new, empty state there and treats every export as a first run: an incremental export re-reads from the start, a CDC stream re-anchors and the changes between the lost position and the new anchor are gone, with no error. What the operator can check without rivet’s state identity: the directory exists, is absolute and writable, and its marker equals the marker of each direct upstream rivet task of the same DAG run (taken from their XCom); on SQLite state a difference is refused with RIVET_AIRFLOW_STATE_DIR_DIFFERS. On PostgreSQL state it is not: no task reads what another left in the directory, and several hosts, each with its own directory at one path, are the deployment that state is for (an extract on one host and its load on the other, measured by the live gate’s multi-host leg). It cannot see a directory that was emptied between two DAG runs, or two hosts that each have their own copy.

  5. Relative paths in the config. The copy changes the directory rivet takes as the config’s, and nothing else:

    Keyrivet resolves it againstUnder this package
    query_filethe config’s directory; an absolute path, .. and a symlink out of it are refused by rivetstays as written; the file is copied to the same relative path in the state directory before every task. Refused before rivet starts (RIVET_AIRFLOW_QUERY_FILE) when it is absolute, has .., leaves the directory through a symlink, is missing for the task’s export, or when a config from ANOTHER directory already put a file at that relative path in this state directory (the refusal names the directory and the remedy for an owner that is an earlier revision of this config, or is gone: delete .rivet_airflow_query_files.json in the state directory and the next task claims the path)
    cdc.checkpointthe config’s directoryresolves in the state directory, a subdirectory included (rivet 0.32.2’s init -o <stem>.yaml writes ./cdc/<stem>_<export>.ckpt); a file left beside the original is refused, see 3. The <checkpoint>.owner record rivet 0.32.2 keeps beside it (which config owns the checkpoint) is neither checked nor touched by the operator: move it with the checkpoint or leave it, rivet re-claims a checkpoint whose recorded owner no longer names it
    a local destination.path (or prefix)the config’s directorythe state directory: init’s ./output/orders/ lands in <state_dir>/output/orders/ (from rivet 0.32.2 a CDC scaffold written with -o <stem>.yaml says ./output/<stem>/orders_cdc/, which lands in <state_dir>/output/<stem>/orders_cdc/); cwd does not move it. Write an absolute path to put the files elsewhere
    the other paths rivet opens as written, such as tls.ca_filethe process’s working directoryunchanged: the working directory defaults to the directory of your ORIGINAL config; set cwd to change it
    The preflight reads the config as rivet does: every ${name} is taken from a -p /
    --param in extra_args (a run, CDC run or plan task only: rivet load, compact and apply
    have no -p, and those tasks refuse it with RIVET_AIRFLOW_PARAM_NOT_ACCEPTED), then from the
    task’s environment, and spliced into the text before
    the YAML is parsed, with YAML 1.2 scalars (017 and name: 2024 stay the text written). A
    mode no variable resolves is treated as cdc by the guards; an export name or a
    cdc.checkpoint no variable resolves is refused (RIVET_AIRFLOW_CONFIG_UNRESOLVED), as rivet
    refuses the config.
  6. Single host. SQLite state and the lock files are visible to one host only. Two workers on two hosts need PostgreSQL state and a destination both reach (an object store). Measured with two Celery workers on two hosts, each with its own state_dir at one path (the live gate’s --workers 2):

    • On PostgreSQL state with a GCS destination, extracts of one DAG run on both hosts, each export moving host every run, and the CDC stream moving host, deliver every row once and never move the stored cursor back. A retry of RivetApplyOperator on the other host finds no sealed plan there and plans again from the stored cursor: the delta is extracted once (the killed try on the first host wrote nothing and kept its artifact, pruned after 24 h). Not measured, by the same construction: a plan an earlier try on the first host already applied is not answered already_applied on the other host but planned again, which for an incremental export reads only the rows past the moved cursor.
    • SQLite state: each host has its own .rivet_state.db. An export that runs on the other host reads from the start, exit 0: the whole table again, and on the way back the rows the other host already took (990 of 1000 rows written twice). So SQLite state under an executor that may use several hosts is refused before rivet starts (RIVET_AIRFLOW_STATE_SQLITE_MULTI_HOST); a deployment with one Celery worker says single_host=True.
    • A local destination (rivet init’s default ./output/, which lands in the state_dir): the files are split between the hosts; the first run on the other host writes there without complaint, and a later run on a host whose manifest is behind the stored cursor is refused RIVET_STATE_CURSOR_PAST_MANIFEST by rivet.
    • The lock files guard one host (by construction, not measured): a zombie try on one host and its retry on another are kept apart only by rivet’s own run lease (Overlap).
    • A ledger file would count crashes per host, so on PostgreSQL state the operator keeps none: a crash is retried only within the first crashed_retries tries (Retries).
  7. Moving to PostgreSQL state. Stop the DAGs; create a database; set RIVET_STATE_URL to its postgresql:// URL for every task (the worker’s environment, or env_from_connections). This package does not copy the SQLite state into PostgreSQL: the new state starts empty, so plan the switch as a first run of every export (a full export simply runs again; an incremental export has no cursor to resume from). Keep state_dir for CDC: a checkpoint is a file on every state backend today, and it stays where it is.