Eloverblik

Dokumentation · Data og kilder

The runner, the first load and the replay (B08b)

How history enters the database: docs/architecture/phase-1.md §5.2 (steps 2-5), §6 and §6.4 are the specification; this page says where each part is and how to use it. The planner (docs/planner.md) decides; the merge (docs/readings.md) stores; the runner connects them through ingest.chunk.

PartWhere
ingest.chunk as the planner's store: read, write in the order of C3, first tiling, re-fetch, a probe stepelo_worker.ingest.chunks
The runner: recovery, tiling, one request after another; the senderselo_worker.ingest.runner
The live sender's transactions A and B inside the pace store'selo_worker.eloverblik.observer.WorkerObserver.for_chunks
The backfill jobelo_worker.jobs.eloverblik_backfill (eloverblik-backfill)
The replay of a captureelo_worker.replay (make load-capture)
make refetch, make probe-windowelo_worker.commands
Test tooling: kills at a statement, recording a load as a capture, faults by windowelo_devtools.runner_harness

One request

  1. Recovery. A gettimeseries row of raw.fetch (not a replay's) with no response and no item, whose request key names chunks, is a request whose process died after transaction A, or whose transaction B failed. It is merged as a request that got no response (http_status NULL): the planner applies Failure(TRANSIENT) with that fetch_id, so the ladder advances on attempts (CT-05), before any of its chunks is requested again (the contract of B06av-02). The row keeps http_status NULL (§3.1) and gets its items (outcome 5). 2. Tiling (chunks.tile_new): every MP that may be requested, with no chunk of purpose 1 or 2 and no first_load_to, gets the calendar tiling of §6.1 and first_load_to = today. An MP loaded from a capture has a first_load_to (N8) and is not tiled again; later days are the daily run's (§6.5 step 2, B09: docs/daily-run.md). 3. The next request: plan.next_request over the chunks of the MPs that may be requested: status 1 or 3 (an MP without current access still has its history loaded, §6.5 step 1), of a type with time series. An MP no longer listed (2) or with an invalid ID (4) is not requested. 4. Transaction A, committed before the send: attempts + 1 on every chunk of the batch (plan.start_request, C4), and the raw.fetch row. Its request key is gettimeseries/<from>/<to>/Actual chunks=<id>,<id>…: internal IDs, never a GSRN (§3.1). 5. The call, then transaction B: merge(…, ChunkRequest(request), now=…), which stores the readings and moves the chunks through the outcome table, the ladder, the edge search and the readable anchor.

Two senders. SourceSender (the demo source, raw.fetch.source 9) writes A and B itself, on one connection that holds the session lock elo.ingest from before A until B has committed (R9). LiveSender asks the Customer API client; the client's WorkerObserver, while for_chunks(request) is held, counts the attempts and names the chunks in transaction A and runs the merge in transaction B, both inside the PostgreSQL pace store's transactions, on its connection (the merge's transaction lock is the store's session lock; on another connection it would deadlock). A 401 retried with a new token is a second send and a second attempt.

The clock. The planner reckons waits and what is due from one instant: the live worker passes the database's clock (PostgresPaceStore.now), the tests a simulated one. merge(…, now=…) takes the same instant for next_try_at and closed_at (by default the database's now()).

A kill at any point leaves the chunks and raw.fetch as the last committed transaction left them: a transaction A without its B is recovered as above, a transaction B is whole or absent (R8), a split is whole or absent (C3). The next run continues from there. This is tested as a property (below).

The backfill job

eloverblik-backfill runs isalive first and every 5 minutes, then the runner, until nothing is due or 300 seconds have passed: the queue runs a job inside its claim's transaction, which must not stay open for the hours a first load can take. A run that leaves work queues its successor in the claim's transaction for the instant the next chunk is due (next_due_at), so the queue drains without a scheduler; the daily run (B09) may queue runs too. A run in which every request failed (not a 200, or every item to be retried) raises EveryRequestFailed and the queue retries it by its ordinary rule; a run in which some failed is done and says how many (B07v-05). isalive not true, or a client Deferred (a cooldown, a token call that did not succeed), ends the run and queues its successor for when it may call again. While the worker does not own the egress, has no token, or its account is invalid, the job is refused (NotReady) as the weekly job is. A refused refresh token makes the account invalid (§5.2), as the weekly job's does, so it is not sent again until the token changes (B08bv-03).

No wait inside the claim (B08bv-02). Before isalive and before every request the job asks the pace store when the next data call may start (data_start_at: next_start_at, cooldown_until and the process's elapsed floors). More than 5 s ahead (a 429, five 503 in a row), the run ends and its successor is due at that instant (paused_until in the result); the spacing of two calls is waited for as before. Not covered: a /token call is spaced by token_spacing_s (60 s by default, at least 30 s), once per access token's life.

The chain does not depend on a run surviving (B08bv-01). A successor is queued in its run's claim and is lost with a run that fails or is killed; a job whose three attempts are used up is failed. So the worker's loop, at every idle tick while the Eloverblik runtime may call (it owns the egress and the account is active), runs eloverblik_backfill.ensure_queued: one statement that queues a run, due at once, when work is due (a pending chunk, a no_data chunk whose next_try_at has come, an MP waiting for its first tiling, or an ANALYZE that is owed) and no backfill run is queued or running **that is due no later than the work could start anyway**: now, a retry's back-off (90 s: the queue's 30 s and 60 s) or the end of the pacer's pause, whichever is latest (B08bv2-03). A successor queued for a no_data retry 30 days away does not hide a metering point the weekly list registers meanwhile. During the pacer's pause (more than 5 s ahead) the check queues nothing. A running job's row stays queued until it completes; the dedup key makes two checks at once queue one run.

The check waits for a deferred run's successor (B08bv3-04). When the latest finished run ended deferred (isalive not true, or the API's deferral), the successor it queued counts as due in time: the check does not start a run of its own before it. So while the API is not alive one run asks isalive every five minutes (NOT_ALIVE_WAIT), not one per tick, and the load starts up to five minutes after the API returns.

Back-off after jobs that failed before their first request (B08bv3-03). After n backfill jobs in a row that failed with FailedBeforeRequest (a fault before any request was sent, such as a database that refuses raw.fetch), the check queues nothing until the last one's end plus 1 hour, 6 hours, 1 day, then a day each time (the waits of a parked chunk, §6.2), by the queue's clock. The back-off is waited out even when the fault has cleared meanwhile; make status says so (backfill: failing before its first request, B09). B09's daily run and make refetch queue a run directly, and a run that does not fail so ends the back-off.

A parked chunk is not work for the check (B08bv2-04, the lead's ruling). The chain's successors (due at the earliest next_try_at) and the daily run (B09) retry it. A fault that strikes before a request is sent (a transaction A that fails, so no raw.fetch row and no attempt with it) counts as an attempt all the same, in a transaction of its own (plan.apply_unsent: the ladder of §6.2 without consuming a fetch): the chunk is solo at 3, split at 5, parked at 8 and, after its 8 parked tries, unresolved. So a fault that never clears parks its chunks and queues nothing more, instead of one failing job every 95 s. How many chunks are parked: parked in every run's result (ops.job_run.detail->'parked'), and for make status (B09) the same count from ingest.chunk.state (2 = parked; 9 = unresolved).

ANALYZE when it is owed (B15v-02, B08bv2-07). Transaction A of a request for a window of more than a day (a first load, a re-fetch of more than a day; not a daily run's single day) writes one row to ingest.analyze_owed (0061), before the send. A run pays the debt when nothing is due now, whatever still waits for days (no_data, parked): ts.analyze_loaded(), the owner's function (0061) that analyses ts.series, ts.reading and ts.day_load, and the row's deletion, in one transaction. A worker killed after the last answer and before the ANALYZE still owes it, and the idle check queues a run to pay it. The replay analyses at its end.

Transaction A checks the chunks again (B08bv-08). The request is planned from rows read without a lock; under the rows' locks every chunk of it must still be due, or nothing is written or sent (on the live path the reservation rolls back with transaction A) and the step plans again from the rows as they are.

The job's result: requests, failed_requests, recovered, tiled, chunks by state, parked (the count of parked chunks, B08bv2-04), next_due_at, statuses by HTTP status, successor_at, paused_until when the pacer ended it, analyzed when it ran ANALYZE, and for a probe probe_complete.

The runner reads open chunks only (B08bv2-05). Planning the next request reads the due chunks, the queue's state the open ones; closed chunks, most of the table, are never loaded into the process.

The commands

MP is the mp_id (as make status prints it); FROM and TO are local dates and TO is exclusive (§2: every interval is half-open). Both run inside the running worker container (docker compose exec) and make no call: they plan, and the backfill job sends.

The replay: make load-capture CAPTURE=data/capture/<run>

It runs python -m elo_worker.replay /capture in a one-shot container of the worker's image (docker compose run), with the capture directory mounted read-only and as the user who runs make (who owns its 0600 files, §12.2): the database is not published on the host (§12.1), and the capture is never copied into an image or a volume. It makes no call to Eloverblik and needs no token: the token variable is blanked for it, as for make up's mount check. Since B02b the service's user: is ELO_UID:ELO_GID, which the Makefile exports for every target, so the recipe passes no --user. Its command replaces the worker's, so no start check runs and no start ticket is taken (the launch contract of docs/stack.md is the worker's). The service's bind of data/state/ comes with it, unused; Compose does not create it, so the stack is started with make up first (the database must run anyway: --no-deps).

  1. Master data: each captured list and getdetails response, in the order of requested_at, through B07's appliers, raw.fetch.source 8, its raw.fetch row in the same transaction. A response whose row is already there (a run that died before the checkpoint) is not applied again. 2. The checkpoint, one transaction: ingest.capture_import (the capture ID, the SHA-256 of chunks.jsonl then requests.jsonl, the MP map, the chunk map) and the captured chunks in their captured states with new IDs from the identity. A captured chunk overlapping an existing chunk of the MP in any state but split is not imported (null in the map). A captured parent that was not imported leaves its child a root of its own lineage. last_fetch_id is NULL (the captured request has no raw.fetch row yet; any later live fetch is newer). first_load_to comes from manifest.json, only where ingest.mp_state has none (N8, LT-06). 3. Content: each captured Actual time series response in the order of requested_at, through the merge with a ReplayRequest and the captured requested_at; its raw.fetch row, the merge and applied_through in one transaction, so a kill leaves no response applied twice and no row without its response (LT-02). Month answers are not readings (§4.5) and are not replayed. 4. Close: readable_from by the anchor rule where it is NULL, then completed_at.

The same capture again: skipped when complete (LT-01: nothing written), resumed after applied_through otherwise. The same capture ID with another hash: refused before anything is written (LT-03).

A damaged capture changes nothing (B08bv-04). Before step 1 (and before a resumed or skipped run does anything) the whole capture is read and checked: every body decompressed and checked against its SHA-256, every 200 answer of a data call present and JSON, every request and chunk naming MPs of the manifest, every chunk row readable, and every done chunk covered by a captured 200 Actual answer for its MP (so a requests.jsonl cut at a line end is not loaded as complete). A fault is a refusal (refused: …, exit 3), naming the file and line, never a value; nothing is written.

The report names MPs by mp_id and prints no quantity: the counts (master data and time series responses applied by this run; the capture's chunks imported and not imported, from chunk_map); each readable_from set; every series-day whose captured content was held back as stale (the database held a newer observation: ts.apply_day answered stale for a delivered day), for make refetch (B08av-08; a capture is meant to be loaded before live ingestion begins). The days are stored with the response that brought them (ingest.capture_held_back, 0061), so a resumed run and a later (skipped) run list all of them (B08bv-05).

The monthly control (B08bv-06, B08bv2-01, -02, B08bv3-01, -02, -05, B08bv4-01 … -06), the comparison of §9.4 and of B08b's last criterion; the lead runs it on the owner's capture as the phase's proof, so it must be right in both directions. **What is compared comes from the capture's own evidence, never from what was stored:** per E17 root of the manifest and business type, every month that a captured chunk meets (in any state but split), every month the capture holds a Month value for, every month a captured answer delivered a day of, and every month with a day in a hole of the root's tiling. For each month:

Windows, listed with their dates. Adjacent chunks the API refused (outside, not_available) form a run. A run with a day of a done window or a delivered day on both sides lies inside the root's history: "not compared: refused inside delivered history", a bar to a pass, unless every day of it is accepted by name in the manifest's accepted_refusals ({"number", "day"}, written by the capture tool, B06b); it is then listed as accepted and no bar. A run at the start or the end of the history is "refused by the API at the edge of its history" and no bar. So **the months between two access periods bar a pass until each of their days is accepted by name.** Runs of empty chunks (still 30015 after every retry, §6.2) are "not compared: nothing captured" and bar a pass, unless they lie outside every access period the capture knows (a done window or a delivered day on one side only): then they are listed and no bar; a root with no such day at all has no access period to lie outside of, so its empty runs bar. Acceptance by name applies to refused runs only.

Every metering point is named (B08bv4-01). One list, monthly control by metering point, names every metering point of the capture's manifest and every E17 root of the database, each with its verdict: E17 root, compared and equal: n months; failed: n of m months; not compared: with the reasons and the counts; nothing to compare (a root with chunks and no month, such as one whose every window was refused at the edge; its windows are still listed); left out: a child of its root, a root of another type, or an E17 root of the database that the manifest does not name. A metering point of the manifest that the database does not hold is FAILED: not in the database (exit 4, B06bv5a-22), and a root of the manifest without any chunk is not planned (not compared): both bar a pass. A pass says how many roots it covered of how many (1 of 18 roots covered, the other 17 left out (listed above)); a root left out is never silent, and does not bar a pass.

What is printed. Per root a line with its chunks of the capture by state; per root and year n of m months equal and what each other month lacks; the windows; the days not delivered and refused; the list by metering point; the last line: passed, every month equal; …roots covered…, FAILED, n of m months failed (with what is not compared beside it), NOT A PASS, … (what is not compared and what bars) or NOT VERIFIED (nothing to compare at all). A month that differs shows the difference as a signed count of units of the Month value's last decimal when it is fewer than ten (differs by -1 unit of the Month value's last decimal (0.001)), else by 10 or more units, and the ratio stored/Month - 1 to eight significant digits, and its count of stored days; never a kWh figure of a month. Beware: a small count with its ratio gives the month's kWh by one division (the lead's ruling keeps both); do not copy such a line anywhere. The summary counts the months that differ in the last decimal only, so that a systematic last-decimal difference of the source is recognisable as one. "Equal" is equal at the last decimal the Month value carries: two decimals hide up to 0.005 a month.

The owner's capture. Expect exit 5 at first: any no_data chunk (a window answered 30015, a new metering point awaiting its first data) is "not captured", and the months between two access periods are refused inside delivered history, until accepted by name. Then the proof is the list, read line by line. The lead (1) reads every not compared line and every edge line against what the owner knows of the metering point; (2) for each window between two access periods that the owner confirms had no access, records its days in the manifest's accepted_refusals (the capture tool's record, B06b) and runs make load-capture again: the capture is skipped (nothing is written; manifest.json is not in the checkpoint's hash) and the control runs again; (3) takes the months still "not captured" as days the daily run (B09) or make refetch must fetch, not as proof; (4) runs make verify beside it (the control compares day summaries, not readings). Run the control directly after the load, before live ingestion: a later withdrawal or correction then fails it, rightly. Exit 4 is always a true difference to investigate.

On the demo organisation, the generator's full capture fails for its real gaps only (the test asserts exactly them): KITCHEN's two quarters refused for conflicting duplicates, the lighting's months delivered as P1M, and the months that the generator, which does no edge search, has no done chunk for; the KWT heat pump is not compared; the root listed at the anchor has nothing to compare, its refused windows listed.

The capture is checked before anything is written (B08bv-04): among other things, every done chunk needs a captured 200 Actual answer covering its window **whose body holds a successful item for the chunk's metering point** (B08bv2-06), not only a request line that names it.

Exit codes of make load-capture (the command's; make prints them as Error 4 and itself exits 2): 0 loaded and the control passed; 2 no DATABASE_URL; 3 refused; 4 loaded, the control failed (a delivered day not stored, a day counted twice, a sum that differs, the retention gate, a metering point of the manifest not in the database); 5 loaded, nothing differs but the control is not a pass (a month not compared: no Month value, not captured, not planned or partly compared; a window refused inside delivered history and not accepted; a window with nothing captured; a root not planned; or nothing to compare at all); 6 an incomplete capture loaded with INCOMPLETE=1 and nothing differs (below); 1 any other error, printed as its class name only (B08bv-07).

The capture tool's verdict comes first (B06bv2-04, B06bf3). Before anything is written, make load-capture reads the capture's manifest.json complete (the capture tool's one predicate, docs/capture.md). A capture that is not complete (false, missing, or a manifest that cannot be read) is refused (exit 3, refused: the capture is INCOMPLETE …, nothing written). make load-capture CAPTURE=… INCOMPLETE=1 loads it anyway, for a capture that can never be completed further (an item or a Month control refused for good), runs the control, and ends with the line the capture is INCOMPLETE by the capture tool's own verdict …: exit 6 in place of 0 or 5, never a pass; exit 4 stays 4 (a true difference). The days the lead accepted by name (accepted_refusals) are read by the control as above; the capture tool removes a day from them once it is stored (B06bv3-09).

The address, and the edge controls (B06bv4-06, B06bv4a-02, B06bf4). Before the verdict, make load-capture reads manifest.json api_base_url: a capture not made at the API (https://api.eloverblik.dk/customerapi/api/, or no address recorded) is a rehearsal's and is refused (exit 3, nothing written) unless REHEARSAL=1 (--rehearsal), which says rehearsal: … in the first and the last line printed. The capture tool asks an edge control (one Month request per calendar year) over every span it takes as an access edge; the monthly control reads it: a Month answer for a root whose items carry no value, or a refusal or 30015 code, over a window that holds no delivered day of a root that has some (where the capture tool asks its edge controls; it asks none for a series without a stored day), says the window holds nothing; any other value-less answer proves nothing (B08bv4-04 stands). A month wholly of such days, with nothing delivered and nothing stored, is compared and equal (not "not captured"); no_data or invalid chunks all of whose days such an answer covers, or the capture's lead accepted by name, are not "not captured"; an empty run so covered is no bar. So a capture the tool calls COMPLETE passes the control when every stored day equals its month, and fails only on a true difference or a month nothing answered for. make prints the statuses as make: *** [load-capture] Error 3 and so on, and its own status is then 2.

What a replay equals. The content of the load it recorded: the readings and day summaries. Not its absence handling: a replayed absence never marks or withdraws (step B3), so a day that live responses withdrew keeps its captured values after a replay of the same responses (LT-05).

Tests

services/worker/tests/test_runner.py, test_replay.py, test_backfill_live.py, test_backfill_chain.py. ELO_PROPERTY_SEEDS (10 by default) sets the number of seeds of the three properties.

Kilde: docs/runner.md i løsningens repository.