Section 19.5.2

Live state transfer and representation invariants

Definition

Live state transfer is the step of a live update, a replacement of code in a running program, that converts the old version’s data in memory into data the new version can use. A representation invariant is a condition that a data structure’s stored values must satisfy for the code using them to work, including what each value means, such as its unit. A transfer is correct when it turns every valid old state into a new state that satisfies the new invariant and means the same thing.

Where it fits

Main entry: Schema migration (§8.36).

The Schema migration entry treats this problem for stored records; live state transfer meets it in memory that a running program is using. Let an abstraction function map a version’s stored bits to the value they stand for: αold\alpha_{\mathrm{old}} for the old version and αnew\alpha_{\mathrm{new}} for the new one. Let ff be the transfer. For every old state ss that satisfies the old invariant, f(s)f(s) must satisfy the new invariant, and αnew(f(s))=αold(s)\alpha_{\mathrm{new}}(f(s)) = \alpha_{\mathrm{old}}(s). The condition has the shape of the simulation relation of a refinement proof: it pairs each old state with a new state that stands for the same value.

A change of unit shows that copying bits can change meaning. The old code stores a timeout §9.34 of 5, meaning 5 ms; the new code reads the same field in microseconds. Copied unchanged, the value means 5 microseconds, 1000 times shorter. The correct transfer multiplies by 1000, because 1 ms is 1000 microseconds: 5×1000=50005 \times 1000 = 5000 microseconds. If the field is a 16-bit unsigned integer in both versions, its largest value is 216−1=65,5352^{16} - 1 = 65{,}535 microseconds, about 65.5 ms. An old timeout of 70 ms would need 70×1000=70,00070 \times 1000 = 70{,}000 microseconds, more than 65,535, and so has no valid image, so the transfer must refuse that state, or the new version must widen the field.

Serialization §4.28, the conversion of values into bytes by a fixed format and back, carries each stored value across unchanged, not its meaning, so it cannot detect the change of unit. A transfer must also handle what is not a plain value.

Limits and common mistakes

The transfer may need extra memory while the old and new state exist side by side. If a fault or a reset can interrupt the transfer while some state survives, for example in flash memory §6.6, the interruption must leave the complete old state or the complete new state; building the new state aside and switching to it in one atomic step achieves this.

Questions answered

Can serialization automatically solve state transfer?

No. Serialization converts values into bytes by a fixed format and back; it carries each stored value across unchanged, but not the value’s meaning, the conditions linking values, or the program’s live relationships. If the old version stores a timeout of 5 meaning 5 ms and the new version reads the field in microseconds, serializing and restoring it yields 5 microseconds, 1000 times too short, whereas the correct transfer computes 5×1000=50005 \times 1000 = 5000 microseconds. Serialization also does not fill new fields consistently, redirect pointers, keep each resource with one owner, carry over operations in progress, or hide a half-converted state from other threads.

Live state transfer, the conversion of a running program’s state for its new code, therefore needs an explicit mapping. The mapping takes each valid old state to a new state that satisfies the new code’s representation invariant, the conditions its data must meet, and that stands for the same value. Old states with no valid image must be rejected, and the switch must look atomic to every observer. Serialization can be a tool inside the mapping, never the whole of it.

Related entries: Ownership (§4.2), Coherent snapshot (§9.23), Refinement (§3.32).