Skip to main content
Version: 0.13.0

Migrating state

An application's state outlives the version that wrote it, so a newer version may have to change it before anything reads it, and an older one must not be started over what it cannot read. Three things carry that: a range the descriptor declares, a stamp beside the state, and one condition a unit may carry.

WhereWritten byRead by
datathe descriptor's headthe authorthe gate, before every start
the stampversion, in the instance's state directorythe Core, once per startthe gate before a Core exists; the Core once, before it writes
migrates_froma unit of kind oneshotthe authorthe Core, when the unit would be launched

The data range​

data: ">=2.0"

data is a range over versions of this application: which of them may have written the state this version is about to read.

  • It is optional, and absent means unconstrained.
  • An omitted upper end means this version.
  • Only a descriptor carries it. A composition document in the service form has no version to range over, and the field there is refused with head.present.

The stamp​

One line beside the state, holding the version that last ran against it — 2.1.0 — and neither a store's nor a unit's.

  • The Core writes it once per start, as the instance becomes observable and before any unit can have written anything. It is written to a temporary file in the same directory, flushed, and renamed over the old one, so a reader sees one version or the other and never a partial line.
  • It describes the bytes, so it travels with them. A copy of an instance's state carries the stamp of the version that wrote it, and a restored copy keeps it.
  • It is not the version in the instance record. The record's version is what an operator asked for; the stamp is what actually ran.
  • Clearing an instance's state removes it with everything else.
  • The service form writes none: the deployment has no version of its own, and what is installed is versioned by the host's package manager.

What the gate does with them​

Before every start, and before a Core exists, the gate compares the stamp with the range the version about to start declares.

The state directoryThe gate
has a stamp the range coverspasses; the start proceeds
has a stamp outside the rangerefuses with data.range, naming the stamp and the range
has no stamppasses — a fresh instance, or one whose state was cleared
has a stamp, and the description declares no rangepasses

yoke instance set-version performs the same comparison, so a rollback onto state it cannot read is refused while somebody is at the keyboard. The gate remains the authority: it refuses at every start, whoever asked.

A migration unit​

units:
migrate-store:
kind: oneshot
image: localhost/station-migrate-store@sha256:…
migrates_from: ">=2.0 <2.2"
depends_on: [ provision-store ]

migrates_from is the only condition a unit may carry, and it is a field and not an expression. It is a range over versions of this application, allowed on a unit of kind oneshot and nowhere else.

WrittenThe gate's finding
on a unit that is not a oneshotunit.migrates_from.kind
anywhere in a composition documentunit.migrates_from.present

It is evaluated when the unit would be launched, against the stamp the Core read as it started, before it wrote its own:

The version that last ranThe unit
inside the rangeis launched, as any other unit
outside the rangeis skipped
none — a first start, or cleared stateis skipped

A skipped unit starts no process and begins no incarnation. It passes directly to Completed, the readiness of a unit that runs to completion, so every unit that depends on it starts as though it had run.

It produces one unit.state.changed, to Completed, naming the unit and no incarnation, and carrying skipped with the range and the version it was evaluated against, or none (the catalogue).

An update that migrates once​

StartThe stamp beforemigrate-storeThe stamp after
first start of 2.1.0noneskipped, none2.1.0
first start of 2.2.02.1.0runs2.2.0
any later start of 2.2.02.2.0skipped, 2.2.02.2.0
2.1.0 pointed back at it2.2.0—refused with data.range before a Core exists

The copy taken before the update is what a rollback restores: it carries the stamp 2.1.0, which 2.1.0's range covers (updating and removal).