Switching a whole forecast: snapshots, checkpoints and crash recovery

An immutable generation binds a forecast to its checkpoint. Four real process interruptions show when a candidate is prepared and when it is actually active.

Two complete paper scenes illustrate preparing the next generation while keeping the current one intact.

Imagine that a new forecast has already been checked: the required values are present, time positions are correct, and units agree. Now it must become visible to a reader. Replacing the forecast file first and the import checkpoint second introduces a state that belonged to neither completed release. The data is new, while processing still resumes from an old position.

We can investigate that boundary without a real weather provider. We will build a small independent Python store: immutable generations containing two documents, and one file selecting the active generation. Then we will terminate a child process at four controlled points and read what it left behind. This is a local Linux filesystem exercise; it does not reproduce a power failure or distributed storage.

What must switch together

The exercise forecast contains six illustrative temperatures. They are a synthetic sequence, not a meteorological product. The first version is labelled revision-a, the second revision-b. Zero and negative numbers remain ordinary values.

The word “whole” refers to one defined bundle. If cities are stored in separate generations, consistency within each city does not create a shared atomic switch of the entire catalogue. That broader promise requires a separate version of the overall bundle and a reader that respects it.

The checkpoint contains the identity of that same sequence, its revision, the next element position, and a checksum of the forecast document. In this small example, next_index equals the number of stored values. In a real importer, a source position might identify a byte range, a message, or a completed chunk; it cannot simply be replaced with the number of forecast points.

Generation component Purpose
forecast.json Values and their synthetic revision
checkpoint.json A continuation position bound to this forecast
manifest.json Sizes and checksums of both documents
active The identifier of one selected generation

The manifest has a fixed format. Its SHA-256 becomes the generation directory identifier. Changing either document therefore changes its manifest entry and the identity of the bundle. The example uses Python's standard hashlib.sha256.

A checksum binds particular bytes here. It does not authenticate an author or establish the origin or accuracy of weather data. The example uses a trusted local directory and does not defend against another process permitted to rewrite that entire directory.

A completed directory is not yet the active forecast

The writer first creates a temporary directory alongside the generations. It writes the forecast, checkpoint, and manifest there in sequence. After the writes finish, the directory receives its final generation name; the candidate is then read and validated before activation. Until active changes, an ordinary reader continues using the previous bundle.

This provides a useful failure boundary. Terminating after writing only the forecast leaves an unfinished temporary directory. Terminating after preparing the whole generation leaves a new bundle that can be inspected separately. In either case, the presence of files alone does not change the reader's selection.

Preparation must not grant a candidate broader guarantees than it earned. Our validator checks the format, sizes, checksums, and agreement between the pair. It does not repeat every domain check for a real forecast. Completeness of required fields and time intervals must be established before activation by a separate product rule.

Each teaching document is limited to 65,536 bytes, and the sequence to 64 values. A read takes at most the limit plus one byte so that an oversized file can be rejected. These are example bounds, not an estimate of a weather service's memory capacity. Unknown members, duplicate JSON keys, booleans in place of numbers, and infinities are rejected too.

One pointer instead of two independent replacements

To activate a generation, the writer writes its identifier into a temporary file in the same directory, then replaces active with it:

os.replace(temporary, root / "active")

Python's documentation describes successful renaming as atomic and warns about different filesystems. The temporary file is therefore created beside the destination. A reader sees either the old or the new pointer; it should not receive half of one identifier and half of another.

Atomic visibility is not power-loss durability. The example calls flush and fsync on written files and also synchronizes the relevant directories. The Linux fsync manual explains separately that synchronizing a file does not necessarily persist its directory entry. Actual machine-crash behavior still requires testing the particular storage system. Terminating a process does not replace that experiment.

A write or synchronization error must not become a success acknowledgement. This matters especially after replacing the pointer: an exception at a later step no longer means that readers necessarily remained on the previous generation. The actual state must be read again.

A reader pins its generation once

Even a correct atomic switch cannot help a reader that asks for the current version twice: once before reading the forecast, and again before reading the checkpoint. A writer may switch generations between those reads. Each document can be valid on its own while the pair is mixed.

The correct sequence in this model is short:

generation = active_generation(root)
pair = read_generation(root, generation)

Both documents are read using the same pinned identifier. The reader checks the manifest, sizes, checksums, and the checkpoint's relationship to the forecast. A request that already selected revision-a may correctly finish using it after revision-b is activated. This is a consistent answer at the generation-selection point, not a promise that every request everywhere switches versions simultaneously.

Old generations are not deleted in this exercise, so a directory pinned by a reader remains available. Cleanup in a working store needs its own policy: reader leases, sufficient retention, a read retry, or another coordinated mechanism. The rule “keep the last two” alone does not guarantee that a slow reader has released the third.

The checkpoint must belong to these exact data

Checksum verification does not replace checking the semantic relationship. A new manifest could contain correct checksums for a forecast from one revision and a checkpoint from another. Every byte would match the manifest, yet resuming processing from that pair would be wrong.

The reader therefore also compares the revision, source identity, position, and forecast checksum recorded in the checkpoint. One test deliberately creates a mixed pair, recalculates its checksums, and still receives a rejection. This distinguishes byte integrity from a state suitable for continuing work.

In a real system, this check might require restoring internal state and rebuilding its reader-facing representation. Our six numbers do not model merging several releases, forecast horizons, or every provenance rule. The small format keeps the activation boundary visible.

Four actual process interruptions

The lab runner creates a separate temporary directory for each scenario and activates the old revision. A real child process writes the new one. At the selected point, it calls os._exit with status 73, without normal termination or a success message.

This deliberately interrupts the teaching writer. It lets us observe files after process termination, but does not simulate losing kernel caches, damaging a storage device, or disconnecting a network.

Interruption point Forecast afterward Checkpoint New candidate
After writing only the forecast revision-a revision-a No completed generation
After preparing the entire generation revision-a revision-a Available, but not current
Before replacing active revision-a revision-a Available, but not current
After replacing and synchronizing active revision-b revision-b Current

In all four scenarios, the process exited with 73 and printed no success acknowledgement. In the last one, however, the new generation was already current. Process status alone therefore does not determine the effect of the write.

After each interruption, the runner performs another write and checks that the new revision can be read. In the final scenario, the repeat returns already_current: it does not create another logical change. In the others, it completes preparation and activation against the known current pointer.

Recovery starts with a read

The resolve function distinguishes three present observations: the requested generation is current, available without being current, or unavailable. The second result does not mean “never active.” The generation may already have served requests before a successor replaced it. A separate journal is needed to answer historical questions.

Likewise, an unavailable candidate does not give universal permission to repeat any external action. Our repeat uses immutable synthetic input and local files. A provider interaction, a message, or a payment would require its own identity and retry rules.

Before a new activation, the writer checks the expected current generation. To prevent two cooperating local writers from passing that check simultaneously, the change is protected with fcntl.flock. Every participant must follow that protocol. It is neither a distributed lease nor protection against arbitrary writes by another process.

An old revision is not restored automatically just because its files remain. The test rejects activation with a stale expectation. Deliberately returning to a previous forecast would require a new decision about its suitability and freshness; a checksum alone does not make that decision.

Scope of the local check

The local check used the Python standard library.

On Python 3.12.14, 19 test methods and four separate child-process interruption scenarios were executed. They cover ordinary switching, a pinned reader, deliberately incorrect repeated pointer reads, stale expectations, replay of the current generation, changed files, a missing checkpoint, and a pair with internally consistent checksums but inconsistent meaning.

A real weather service adds its storage system, writer authority, retention periods, actual checkpoint, product completeness and freshness, and revision observation through the site's reader. The local result is narrower: at the tested interruption points, the reader obtained a complete old or new pair, and the absence of a success acknowledgement could be distinguished from the absence of activation itself.

  1. Data & Infrastructure Rebuilding a search index without mixing old and new schemas
  2. Platform Engineering A long task stopped: checkpoints and safe resumption
  3. Weather The process finished, but was the whole forecast updated?
  4. Weather Importing weather data again: checkpoints and revisions instead of duplicates