Backfill planning and recovery
The local backfill workflow turns one historical time range into independent partition runs. It plans the work, records progress on disk, starts each partition through the normal run workflow, and lets an engineer resume only the unfinished work after a failure or interruption.
Backfill lifecycle
The bruin backfill command coordinates planning, execution, and review around one saved backfill identity. A backfill can target a pipeline or one asset, and it carries the run context needed to make every child run behave as part of the same recovery unit. See CLI workflows for the wider command lifecycle, command families and operational workflows for the place of backfill beside normal local runs, and project initialization and templates for the project boundary that supplies the pipeline and configuration.
This diagram shows how a historical request becomes resumable child work and then returns as a summary.
flowchart LR
A[Range and run context] --> B[Plan partitions]
B --> C[Preview or save manifest]
C --> D[Select eligible partitions]
D --> E[Start child bruin run]
E --> F[Save attempt and partition result]
F --> G[Summary and logs]
F --> H[Resume unfinished work]
H --> D
Plan inputs
The plan fixes the target, date range, timezone, partition rule, environment, and run options for the life of the backfill. Date-only end values include that calendar date. Timestamp end values mark the exclusive end of the range. Calendar partitions follow local boundaries, while duration partitions start at the requested instant. The plan keeps intervals half-open as [start, end) so adjacent partitions do not overlap.
The planner supports hourly, daily, weekly, monthly, yearly, and positive duration partitions. It generates partition identities from the plan context and interval. The identity stays the same when the engineer changes execution order, so --reverse changes traversal without changing which saved record belongs to each interval.
The selected environment also shapes the safe active partition count. The command reduces the requested parallelism when connection limits require it, and it runs sequentially when a writable local DuckDB file could receive concurrent writer processes. Read-only DuckDB connections can support parallel partitions.
References: cmd/backfill.go• L44-85 cmd/backfill.go• L120-145 cmd/backfill.go• L185-245 cmd/backfill.go• L314-333 pkg/…/plan.go• L27-38 pkg/…/plan.go• L46-92 pkg/…/plan.go• L95-120 pkg/…/plan.go• L132-180 pkg/…/plan.go• L235-275
Plan preview
Dry-run mode lets an engineer inspect the complete plan and current saved state without running assets or creating a backfill store. Text output gives a compact human view. JSON output gives the normalized manifest, execution options, summary, visible partition records, and pagination state for scripts or tools.
Pagination limits only the displayed partition records. It does not reduce the work that execution covers or the counts in the summary. This distinction lets an engineer inspect a very large backfill in pages without accidentally running only one page.
References: cmd/backfill.go• L245-255 cmd/backfill.go• L403-460 cmd/backfill.go• L463-487 docs/…/backfill.md• L17-27
Backfill state
The backfill store is the local record of planned work and its attempts. It separates immutable plan inputs from independently saved partition results, which lets recovery rebuild the same plan and inspect only the records that already exist.
The default store is under <repository>/logs/backfills/<backfill-id>. An explicit --state-dir changes the root and must be supplied again for a resume. The command adds an in-repository state path to .gitignore because the store can contain saved variable overrides and run details.
| State | Purpose |
|---|---|
| Manifest | Keeps the backfill identity and the inputs that define the plan. |
| Partition record | Keeps one interval, its current status, and its attempt history. |
| Child log | Keeps the output for one child run attempt. |
| Execution coordination | Prevents another executor from taking over while old child processes remain active. |
Queued partitions are implicit in the plan until their first attempt. This keeps a large backfill light on disk and memory. Partition records are written as separate atomic snapshots, while a process lock prevents two invocations from executing the same backfill at the same time.
References: cmd/backfill.go• L139-159 cmd/backfill.go• L259-284 cmd/backfill.go• L289-312 pkg/…/store.go• L20-35 pkg/…/store.go• L45-75 pkg/…/store.go• L131-183 pkg/…/store.go• L201-267 docs/…/backfill.md• L97-106
Partition states
Each partition moves through a small state model. An attempt has its own child run ID, start and finish times, status, and error text. The partition keeps all attempts, so a retry or recovery run does not erase the earlier reason for failure.
| Partition status | Meaning in recovery |
|---|---|
queued | The interval has not completed an attempt. |
running | A child run is active or was active when the prior executor stopped. |
succeeded | The interval completed successfully and is skipped by a normal resume. |
failed | The last attempt failed after its allowed retries. |
cancelled | Execution stopped while the interval was active. |
On a new invocation, all partitions are eligible. On a resume, successful partitions are skipped by default. failed selects only failed partitions, missing selects queued, interrupted, or cancelled work, and all repeats every partition. The summary marks partitions excluded from the current invocation as skipped without changing their stored status.
References: pkg/…/store.go• L27-49 pkg/…/execute.go• L42-63 pkg/…/execute.go• L65-104 pkg/…/execute.go• L190-237 docs/…/backfill.md• L43-74
Child run coordination
The parent backfill process does not execute asset work itself. It starts one child bruin run for each selected interval and passes the saved run context, partition dates, backfill ID, and total partition count to that child. The normal run system then handles the asset graph, workers, connection use, logs, and pipeline state for that interval.
The child receives the interval as an inclusive end timestamp one microsecond before the plan’s exclusive end. This preserves the normal run variables while keeping neighboring partitions separate. Each child also receives a unique run ID, so run logs and destination writes can be traced to one partition attempt.
The parent limits the number of active child runs. Each child has its own asset worker limit, and the parent’s connection and DuckDB checks prevent the combination from exceeding known local limits. The coordination applies only to children of one backfill invocation. It does not coordinate separately launched backfills or other CLI processes.
References: cmd/backfill.go• L336-361 cmd/backfill.go• L363-400 pkg/…/scheduler.go• L117-153 pkg/…/scheduler.go• L1152-1187 docs/…/backfill.md• L89-106
Failure policies
Retries apply within the current invocation. After a partition uses its allowed attempts, the failure policy decides whether the parent starts more work or stops.
| Policy | Parent behavior | Recovery result |
|---|---|---|
continue | Keep launching remaining eligible partitions. | Other partitions can finish while the failed partition remains failed. |
stop | Stop launching new partitions and let active children finish. | Queued work remains available for a later resume. |
fail-fast | Stop launching work and cancel active children. | Active work becomes cancelled or failed and remains inspectable. |
The executor saves a running attempt before starting the child and saves its final result after the child exits. If a later invocation finds an attempt still marked running, it records that attempt as interrupted before making the partition eligible again. This preserves evidence of the earlier executor without treating an incomplete run as a success.
References: cmd/backfill.go• L50-58 pkg/…/execute.go• L13-39 pkg/…/execute.go• L111-187 pkg/…/execute.go• L190-237 integration-tests/backfill_test.go• L184-229 integration-tests/backfill_test.go• L437-490
Process boundaries
The parent gives each child a private log file and sends both child output streams through a locked writer. This keeps concurrent child output readable and leaves the terminal display separate from the detailed logs.
The parent and child also share an execution generation. A resumed executor changes that generation only after it confirms that no old child still holds the child lock. A child from an older generation therefore cannot continue work after a new executor takes control.
Unix starts each child in a new process group and sends an interrupt to that group during cancellation. Windows starts a new process group and terminates the full child tree with the platform task control. Both platforms wait up to 30 seconds for child shutdown before the command returns.
References: cmd/backfill.go• L363-400 cmd/backfill_process_unix.go• L13-23 cmd/backfill_process_windows.go• L10-17 pkg/…/store.go• L79-128 integration-tests/backfill_test.go• L261-317
Recovery workflow
Recovery uses the saved manifest as the source of truth for the plan. The command re-reads pipeline files and connection credentials, but it does not accept a changed target, range, timezone, partition rule, or saved run input. Execution controls such as concurrency, worker count, retry count, order, and failure policy can change on resume.
When an engineer fixes an asset or credential, the next --continue invocation can use that updated runtime input while still addressing the same partition identities. This keeps the recovery set stable while allowing the cause of the failure to be repaired.
The normal recovery sequence is:
- Read the final summary and child logs to identify failed, cancelled, or queued partitions.
- Fix the pipeline or connection issue that caused the child failure.
- Run
bruin backfill --continue <backfill-id>to retry failed and unfinished work. - Use
--rerun failed,--rerun missing, or--rerun allwhen the recovery scope must be explicit. - Use
--dry-runto inspect the saved state before starting another execution.
sequenceDiagram
participant E as Engineer
participant P as Backfill parent
participant S as State store
participant C as Child run
E->>P: --continue backfill ID
P->>S: Read manifest and partition records
P->>P: Select eligible states
P->>C: Start repaired interval
C-->>P: Result and child log
P->>S: Save attempt and partition status
P-->>E: Summary or next resume command
References: cmd/backfill.go• L151-213 cmd/backfill.go• L256-286 pkg/…/execute.go• L42-52 pkg/…/execute.go• L111-187 docs/…/backfill.md• L43-74
Cancellation recovery
Ctrl+C, SIGTERM, q, and fail-fast stop new scheduling and cancel active children. The terminal workflow waits for those children to exit before it returns, so the final summary reflects the known state instead of leaving the display detached from running work.
After cancellation, queued work remains queued and active work becomes cancelled when its child observes the cancellation. A later resume can select that missing work. Engineers should use idempotent materializations because a crash can occur after a data commit but before the matching partition record is saved. That partition can then run again.
References: cmd/backfill.go• L129-130 cmd/backfill_ui.go• L138-169 cmd/backfill_ui.go• L401-445 pkg/…/execute.go• L181-187 pkg/…/execute.go• L212-237 cmd/backfill_process_unix.go• L13-23 cmd/backfill_process_windows.go• L10-17 docs/…/backfill.md• L72-74
Terminal workflows
An interactive terminal uses a live dashboard. It shows the plan range, environment, partition rule, effective parallelism, completion progress, status counts, and a visible window of partition rows. The display follows the active partition until the engineer enters manual browsing mode.
The dashboard keeps only visible rows in its view, which allows a large plan to remain usable. It reads saved records for the visible range and overlays active and recent results as they arrive.
| Input | Workflow |
|---|---|
| Arrow keys | Move through the partition list. |
| Page Up/Page Down | Move by one screen. |
| Home | Return to the first visible range. |
f | Return to automatic following of active work. |
q or Ctrl+C | Stop scheduling and cancel active children. |
References: cmd/backfill_ui.go• L23-33 cmd/backfill_ui.go• L134-197 cmd/backfill_ui.go• L220-269 cmd/backfill_ui.go• L271-387 cmd/backfill_ui_test.go• L42-103 cmd/backfill_ui_test.go• L131-201
Non-interactive output
Pipes, redirected output, TERM=dumb, and --no-progress use plain progress lines instead of the dashboard. --no-color and NO_COLOR remove terminal color. JSON execution keeps standard output as one final JSON object and does not mix child output into that object.
The final text summary reports completion, failure, cancellation, queued, running, and skipped counts. It also shows the child log path and a resume command when the backfill is incomplete. A nonzero command result signals failed partitions or cancellation.
References: cmd/backfill_ui.go• L401-445 cmd/backfill_ui.go• L448-496 cmd/backfill.go• L403-460 docs/…/backfill.md• L108-128
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to date parsing or interval boundaries must keep the half-open plan, inclusive child run end, timezone rules, DST behavior, and partition IDs aligned.
- Changes to the manifest or run flags must preserve resume validation so a saved backfill cannot silently run a different target or range.
- Changes to partition status selection must keep default resume,
failed,missing, andallbehavior consistent with summary counts and stored attempts. - Changes to child arguments must keep the parent interval, child run dates, backfill ID, partition total, saved variables, and run logs linked.
- Changes to state writes or locks must preserve atomic partition records and prevent a resumed executor from overlapping orphaned children.
- Changes to connection limits or local DuckDB handling must keep effective parallelism safe for both the parent and each child run.
- Changes to cancellation must preserve platform-specific process-tree handling and the wait-before-return contract used by the dashboard.
- Changes to text or JSON output must keep machine-readable stdout separate from progress and child logs.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Resume uses a different target, date range, timezone, partition rule, or saved run input | The command rejects the resume instead of mixing records from different plans | Create a new backfill when the plan inputs must change |
| A backfill uses a writable local DuckDB connection with active parallelism | Child processes can contend for the same database writer | Let the command reduce effective partition parallelism to one |
A child fails after all retries and on-failure is stop or fail-fast | New partitions stop and the command returns incomplete work | Fix the cause, then resume the saved backfill |
| A parent exits while a child is still active | A later executor could overlap old work or duplicate side effects | Wait for child exit, or let generation and child locks block resume until safe |
| A crash occurs after a data commit but before the partition record is saved | Resume can execute that partition again | Use idempotent materializations for backfill writes |
| A saved state directory is not supplied on resume | The command cannot find the manifest and partition records | Reuse the original --state-dir value |
| An engineer treats output pagination as execution pagination | The displayed page does not represent the full work that ran | Remember that --offset and --limit affect output only |
Key files
| File | Why to read it |
|---|---|
| cmd/backfill.go | Defines the command contract, plan creation, saved-input rules, effective parallelism, child launch, and output modes. |
| pkg/…/plan.go | Defines date interpretation, timezone-aware partition boundaries, deterministic identities, and lazy plan generation. |
| pkg/…/store.go | Defines the manifest, partition records, atomic state writes, process locks, and child execution generations. |
| pkg/…/execute.go | Defines eligibility, retries, failure policies, cancellation outcomes, and attempt history. |
| cmd/backfill_ui.go | Defines the interactive dashboard, plain progress workflow, cancellation wait, and final recovery summary. |
| cmd/backfill_process_unix.go and cmd/backfill_process_windows.go | Define the platform-specific child process groups and cancellation behavior. |
| pkg/…/scheduler.go | Shows how each child run carries backfill identity and total-partition progress into normal pipeline state. |
| integration-tests/backfill_test.go | Demonstrates end-to-end planning, resume, retries, cancellation, parallelism, DST ranges, and output contracts. |
| docs/…/backfill.md | Provides the user-facing command reference for dates, recovery flags, state layout, and terminal behavior. |