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.
| Where | Written by | Read by | |
|---|---|---|---|
data | the descriptor's head | the author | the gate, before every start |
| the stamp | version, in the instance's state directory | the Core, once per start | the gate before a Core exists; the Core once, before it writes |
migrates_from | a unit of kind oneshot | the author | the 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 directory | The gate |
|---|---|
| has a stamp the range covers | passes; the start proceeds |
| has a stamp outside the range | refuses with data.range, naming the stamp and the range |
| has no stamp | passes — a fresh instance, or one whose state was cleared |
| has a stamp, and the description declares no range | passes |
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.
| Written | The gate's finding |
|---|---|
on a unit that is not a oneshot | unit.migrates_from.kind |
| anywhere in a composition document | unit.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 ran | The unit |
|---|---|
| inside the range | is launched, as any other unit |
| outside the range | is skipped |
| none — a first start, or cleared state | is 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
| Start | The stamp before | migrate-store | The stamp after |
|---|---|---|---|
| first start of 2.1.0 | none | skipped, none | 2.1.0 |
| first start of 2.2.0 | 2.1.0 | runs | 2.2.0 |
| any later start of 2.2.0 | 2.2.0 | skipped, 2.2.0 | 2.2.0 |
| 2.1.0 pointed back at it | 2.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).