# state The database, retention. Generated by `make records` from the decisions whose `topic` (or `also`) is this one; edit the decisions, not this page. ## Current ### Stored state - **A task instance records why it failed without an attempt** (D78): `task_instances` has a nullable `reason` for failure without an attempt. It records failures such as exceeding `max_instances` or expanding a value that is not a list. ### Database and schema - **Replace NUL in database JSON strings and keys** (D114): The database layer replaces NUL with U+FFFD in every string and key of JSON it writes. JSON without NUL is written unchanged. - **A role starting waits for a database it can't reach yet** (D496): A master, webserver or standalone that can't reach its database at its start waits for it instead of exiting: while connecting or reading the schema fails transiently (`DbError::is_transient`: a time limit, a connection refused or lost, the database starting, busy, out of connections or a statement timeout), it tries again every `SCHEDR_HIDDEN__WEBSERVER__SCHEMA_RETRY` (5 s), its `/readyz` "starting", until a stop is asked for, which also cuts a try in progress. A wrong host or port waits the same way, so a warning names where it looks (`database_place`, as sqlx reads the URL: host and port, a socket or the file) and why (`DbError::waiting_reason`: codes, never the server's text), never the user or password, at once and every minute while waiting. A wrong password, TLS setting or URL exits at once. Not chosen: exiting and leaving restarts to a supervisor (CrashLoopBackOff kept a master down minutes after its database came back); giving up after a bound. ### Retention - **Retention always keeps each DAG's three newest runs** (D430): Besides unfinished runs, never deleted per DAG: its newest scheduled run (the scheduler's chain: without it a catchup DAG would create its runs again from `start`), its newest run of any kind (a manual run's `since`), and the last that ended `success` or `failed` (`RUN_RECOVERED`). This replaces the archived "the latest run of each DAG". - **A chained run loses its parent link when the parent is deleted** (D431): A run's `parent_run_id` (a `TriggerDagOperator`'s child) is cleared when retention deletes the parent, which usually ages out first; `triggered_by` still names the parent run. A parent isn't kept for its children, nor deleted with them. - **Retention's interval and batch are hidden values** (D432): `SCHEDR_HIDDEN__MASTER__RETENTION_INTERVAL` (1h) and `SCHEDR_HIDDEN__MASTER__RETENTION_BATCH` (100 runs per transaction), as D232's timings; `[retention]` holds only the ages: `runs`, `outputs`, `events`, `commands`. This replaces the archived public `interval` and batch keys. - **Retention is on by default** (D433): `[retention]` defaults as archived: `runs`, `outputs` and `events` 90 days, `commands` 365 days; `outputs` and `events` default to `runs` when only it is set; `"forever"` turns any of them off. An upgraded deployment starts deleting what is older at once. - **Named events go with `[retention] events`** (D434): Events posted through the API (`named_events`, D354) are deleted after `[retention] events`, as trigger events are, by when they were posted; what triggers passed of them goes with them. No key of their own. - **An output deleted by retention answers 410 Gone** (D435): `GET /api/v1/runs/{run}/tasks/{task}/output` answers 410 "output deleted by retention" for a task whose output `[retention] outputs` removed; 404 still means it never had one. The run, its tasks and their outcomes stay; the UI shows that text. - **Retention also keeps the runs catchup counts as done, and runs still read** (D440): Besides D430's three newest runs, kept per DAG: its successful manual and backfill runs planned after its newest scheduled run, and, while it has no scheduled run, its successful backfill runs (catchup counts their fire times as done; deleted, it would run them again). Kept whatever the DAG: a run an open `TriggerDagOperator` attempt waits for (deleted, it would be started again), a run or posted event a waiting step or trigger was handed (its retry gets the same). A removed DAG keeps these until its removal is as old as `[retention] runs` (a file missing for a while costs no history). Found by Codex's review of M4.14; the never-scheduled case and the removed DAG's raised by side notes to the user. ### From other topics - Live events identify what changed (D34, `ui-cli`; built M3.3) - Enable live change notifications by default (D38, `ui-cli`; built M3.4) - A mapped task keeps a whole-task row (D74, `python-api`; built M4.3b) - Mapped outputs are stored per instance (D77, `outcomes`; built M4.3b) - The master keeps concurrency counts in memory (D88, `assignment`; built M4.4) - Inject a failure at each database write in master operation tests (D98, `development`; built M4.3e) - Database trouble ends the worker stream; data faults are handled separately (D108, `workers`; built M4.3e) - Only database trouble ends a worker stream (D115, `workers`; built M4.3e) - A resource cleanup is a visible task instance (D261, `cleanup`; built M4.8c2) - From 1.0.0 the schema and the protocol change only additively (D456, `development`; rule) - The master keeps a sample of each process a minute (D464, `observability`; built M5.1i) ## Replaced None. ## Where in code - `crates/schedr-db/migrations/postgres/0001_initial.sql::reason` (D78) - `crates/schedr-db/migrations/sqlite/0001_initial.sql::reason` (D78) - `crates/schedr-db/src/attempts.rs` (D77, D88) - `crates/schedr-db/src/backend.rs::database_place` (D496) - `crates/schedr-db/src/database.rs` (D98) - `crates/schedr-db/src/database.rs::is_data_fault` (D108, D115) - `crates/schedr-db/src/database.rs::is_transient` (D496) - `crates/schedr-db/src/faults.rs` (D98) - `crates/schedr-db/src/instances.rs` (D74) - `crates/schedr-db/src/instances.rs::reason` (D78) - `crates/schedr-db/src/query.rs::without_nul` (D114) - `crates/schedr-db/src/retention.rs::HELD` (D440) - `crates/schedr-db/src/retention.rs::KEPT` (D440) - `crates/schedr-db/src/samples.rs` (D464) - `crates/schedr-db/src/status.rs` (D456) - `crates/schedr-master/src/core.rs::CoreError` (D108) - `crates/schedr-master/src/dispatch.rs` (D88) - `crates/schedr-master/src/dispatch.rs::RESULT_NOT_STORED` (D115) - `crates/schedr-master/src/faults.rs` (D88) - `crates/schedr-master/src/faults.rs::fail_each_write` (D98) - `crates/schedr-master/src/limits.rs::add` (D88) - `crates/schedr-master/src/limits.rs::load` (D88) - `crates/schedr-master/src/mapping.rs::Sources` (D77) - `crates/schedr-master/src/mapping.rs::coordinates` (D74) - `crates/schedr-master/src/progress.rs::Instances` (D74) - `crates/schedr-master/src/samples.rs::Sampler` (D464) - `crates/schedr-master/src/samples.rs::sample_webserver` (D464) - `crates/schedr-model/src/dag.rs::resource_of_cleanup` (D261) - `crates/schedr-proto/src/version.rs::PROTOCOL_VERSION` (D456) - `crates/schedr-ui/src/live.rs` (D34) - `crates/schedr/src/roles.rs::apply_change_notifications` (D38) - `crates/schedr/src/roles.rs::once_reached` (D496) - `crates/schedr/src/roles.rs::wait_for_schema` (D496) - `scripts/release.py` (D456) ## Open - Q15 (deferred until M4B): Should database tables be partitioned by time? Details and edge cases, with the reasons; read them before changing this: - JOURNAL.md: D430, D440, D496 - archive/PROJECT.md: Native API, Output serialization