Section 8.22

Staging versus commit versus activation

Definition

Staging, commit and activation are three separate steps in applying an update. Staging stores and checks a candidate, the new version being installed, where it has no effect yet. Commit is the single write, surviving any later loss of power, after which every restart uses the candidate. Activation is the moment the running system starts to use the candidate. The steps happen in this order, often at different times, and each can fail on its own: committed does not mean running.

The idea

A device downloads an update during the day and checks its signature: that is staging. Its owner approves the installation, and the device records that the next start should use the new version: that is commit. The device restarts and runs the new version: that is activation. A report of “updated” after the first step would be false. Each step answers a different question: are the bytes here and checked, will a restart use them, and are they in use now.

The steps need not be separated by a restart. A device that enforces a set of rules, such as a packet filter, which decides which network packets to let through, can switch rule tables while it runs. Here the order hides a hazard. Activation needs resources, above all the memory that holds the new table. If the commit comes first and the memory is requested only at activation, the request can fail after the device has already recorded the new rules as current. Storage then names the new rules, the running system still uses the old ones, and the next restart tries the new rules again, possibly failing the same way at every start.

A restaurant changing its menu is a fair picture. Printing the new menus is staging, announcing that the new menu applies from tomorrow is commit, and cooking from it is activation. Announcing before checking that the kitchen has the ingredients produces a committed menu that cannot be served. The picture stops at recovery: a kitchen improvises, but a device does only what its rules say, so the rules must state what happens to a committed version that cannot be activated.

How it works

Each step has its own work, its own failure rule and its own report.

  1. Stage. Receive the candidate into an area that the running system does not use, such as an inactive slot or a temporary file, and check that it is complete, authentic, well formed and within every rule a candidate must meet. A failure discards the candidate and changes nothing, and a restart while the candidate is only staged runs the current version.

  2. Prepare. Do in advance everything that activation needs and that can fail: allocate the memory the candidate needs and hold it, build the candidate’s data structures in that memory, and check that the candidate can read the stored data. Holding the memory is a reservation: no other part of the system can take it before activation. A check that enough memory is free is not a reservation, because another part of the system can take the memory between the check and the use, a time-of-check to time-of-use §9.26 (TOCTOU) error.

  3. Commit. Once the candidate’s data is durable, meaning certain to survive a loss of power, write the commit record §8.21, the small record that names the candidate as current. From that instant, a restart uses the candidate.

  4. Activate. Switch the running system to the candidate. When preparation did all the work that can fail, activation cannot fail: it is, for example, the storing of one pointer that every user of the rule table follows. For a firmware image, activation is the restart into the new image.

  5. Release. Free the old version’s memory only after nothing uses it any more, for example after every request that started with the old table has finished. Freeing it earlier leaves those requests holding a dangling pointer §4.1, a pointer to memory that has been given back.

The reply to whoever sent the update names the step reached: staged, committed or active. A single word such as “installed” hides which one is meant.

When activation cannot be fully prepared, two other designs are sound.

Worked example

A device keeps its rule table in RAM. The current table occupies 40 KiB. The new table also needs 40 KiB. At 0 ms, 50 KiB of memory is free.

Commit before allocation.

The device is committed to rules it is not running. A report of “installed” sent at 8 ms is true of storage and false of the running system.

Allocation before commit.

The reservation did not create memory; it moved the shortage to the network code. Serving both needs 40+20=6040 + 20 = 60 KiB beyond the old table, 60−50=1060 - 50 = 10 KiB more than the device has. The memory budget must provide those 10 KiB, or the network code must be designed to wait.

A crash before 8 ms restarts with the old table, because the commit record is not yet durable. A crash after 8 ms restarts with the new table; the start-up code then allocates its 40 KiB afresh, a second activation that needs its own failure rule.

Stated precisely

For a candidate version vv and a time tt, define three conditions.

A report about vv sent at time tt is truthful when it names the strongest of these conditions that holds at tt.

Activation after commit is guaranteed to succeed when two conditions hold: every operation of activation that can fail has completed successfully before the commit, and its results, such as reserved memory and built data structures, belong to the update until activation uses them. Activation then consists only of steps that cannot fail. Without these two conditions, the design must state what happens when vv is committed and its activation has failed. The argument rests on three assumptions: no other code frees or uses the reserved resources; the final switch is atomic, meaning that every user sees either the old pointer or the new one and never a mixture; and the start-up path, which activates a committed version after a restart, has its own failure rule.

Limits and common mistakes

Preparation costs memory. The old and new versions coexist from preparation until release, so the peak need is the two together.

Prepared does not mean correct. Activation that cannot fail can still activate a version that behaves wrongly. Detecting that is the job of a confirmation, which the Commit-confirmed update entry treats.

The start-up path is a second activation path. After a crash between commit and activation, start-up code activates the committed version from storage, with its own allocations. Without its own failure rule, a version that cannot start causes a boot loop, a repeating cycle of start, failure and restart.

Memory is one resource among several. Activation can also need hardware set up, keys loaded or stored data converted to a new format; the Schema migration §8.36 entry treats the last case. Each resource needs the same treatment: acquired before the commit, or covered by a recovery rule.

The heap, the memory area from which a program allocates while it runs, can be split into free pieces that are each too small for one large block even when their total is enough. This heap fragmentation §4.9 is a second reason why a free-memory check does not predict a later allocation.

Questions answered

Can a committed object fail to activate because allocation is attempted afterward?

Yes. If the update is committed first, meaning that the durable record which makes every restart use the new version has been written, and the memory the new version needs is requested only afterwards, at activation, that request can fail. Another part of the system may have taken memory since an earlier check, or heap fragmentation, free memory split into pieces too small for one large block, may prevent the allocation. The device is then committed to a version it is not running: storage names the new version, the running system still uses the old one, and the next restart tries to activate the new version again.

The remedy is to allocate and hold the memory, and build the new data structures in it, before the commit, so that activation is reduced to steps that cannot fail, such as switching one pointer. A check that enough memory is free is not enough, because the memory can be taken between the check and the use. Where full preparation is impossible, the design must state what happens to a committed version whose activation fails, for example writing a new commit record that names the old version again and reporting the failure.

Related entries: Commit record and selector (§8.21), Asynchronous completion (§9.28), Quiescent point (§14.34), Object lifetime (§4.1), Prepared state avoids post-commit allocation failure (§17.13), Commit-confirmed update (§8.23), Resource cap versus resource reservation (§13.22), Heap fragmentation (§4.9), Time-of-check to time-of-use (TOCTOU) (§9.26).