Repository navigation
feat: Backfill the canister-level monotonic consumed cycles by use case - #11585
Conversation
`CanisterMetrics::consumed_cycles_by_use_cases_monotonic` was introduced in May 2026 (#9922) and decodes as empty from checkpoints written before that, so it starts from zero rather than from what the canister has really consumed. Its gauge counterpart, `consumed_cycles_by_use_cases`, predates it and holds the full history. This backfills the monotonic amounts from those gauges, the way #11490 already does for the scalar `consumed_cycles_monotonic`, and in the same checkpoint-round pass: the scalar amount is defined as the sum of the by-use-case amounts over the use cases the scalar gauge covers, so backfilling one without the other would break that. The derivation is exact per use case, thanks to the invariant on `SystemState::outstanding_prepayments`: a gauge exceeds its monotonic counterpart by exactly the prepayments still outstanding for it, all of which are recorded in the replicated state. `outstanding_prepayments()` therefore now returns an `OutstandingPrepayments` breakdown -- `Instructions` and `RequestAndResponseTransmission`, the only two `CyclesUseCaseRefundableKind`s -- with `total()` for the scalar check and `for_use_case()` for the per-use-case one. `HTTPOutcalls` is deliberately left out: it is the one monotonic entry with no canister-level gauge to derive it from, as HTTPS outcalls are only tracked as a gauge at the subnet level. The backfill is driven by the gauge map, which skips it structurally, and the comments now say so -- including on the public `CyclesConsumed::http_outcalls`, which the `canister_metrics` endpoint serves and which therefore keeps covering only the outcalls made since May 2026 while every other field covers the canister's full history. The comments on both pairs of fields (proto and Rust) now state since when each amount is tracked and that the monotonic ones cover the full history from the first checkpoint round after an upgrade onwards. Note on the one visible effect: unlike the scalar monotonic total, which nothing exports yet, `consumed_cycles_by_use_cases_monotonic` feeds `replicated_state_consumed_cycles_from_replica_start_as_counters`. That counter will therefore step up once per canister on the first checkpoint round after the upgrade, by the amount it has been under-reporting all along. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The replicated-state accounting migration affects live exported counters and warrants final human review.
Pull request overview
Backfills per-use-case monotonic cycle metrics from historical gauges during checkpoint rounds.
Changes:
- Adds per-use-case outstanding prepayment accounting and migration.
- Preserves
HTTPOutcallssemantics. - Expands tests and metric documentation.
File summaries
| File | Description |
|---|---|
rs/replicated_state/src/canister_state/tests.rs |
Tests migration invariants. |
rs/replicated_state/src/canister_state/system_state.rs |
Implements per-use-case backfill. |
rs/protobuf/src/gen/state/state.canister_state_bits.v1.rs |
Updates generated documentation. |
rs/protobuf/def/state/canister_state_bits/v1/canister_state_bits.proto |
Documents persisted metrics. |
rs/execution_environment/src/scheduler/tests/metrics.rs |
Tests checkpoint migration. |
rs/execution_environment/src/scheduler.rs |
Runs and validates backfill. |
rs/execution_environment/src/execution/response/tests.rs |
Tests legacy callback accounting. |
rs/execution_environment/src/canister_manager.rs |
Documents endpoint behavior. |
packages/ic-management-canister-types/src/lib.rs |
Documents public metric history. |
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 1
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Address Copilot review on #11585. The public doc on `CyclesConsumed::http_outcalls` claimed the other fields cover the canister's full history, contrasting them with that one. That only holds once the canister's subnet has taken a checkpoint under a replica version that backfills them: until then they, too, only report what has been recorded since May 2026. The `http_outcalls` field is still the one that never catches up, and that is now what the contrast says. The same looseness was in the note on `get_canister_metrics`, which read as if the amounts it serves cover the full history outright. Documentation only. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
Replicated-state accounting migration and externally visible metric changes warrant final human validation.
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 0 new
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
The comments claimed that backfilling `consumed_cycles_by_use_cases_monotonic` from `consumed_cycles_by_use_cases` recovers the canister's full history. It does not: the gauge map itself was only introduced in March 2023 (EXC-1345), so that is as far as any per-use-case record goes, and the backfill cannot reach further back than its source. Only the scalar `consumed_cycles`, tracked since the initial import in 2021, holds the full history -- so the claim survives for the scalar amount alone, and the by-use-case comments now say March 2023 instead. This also makes the pre-existing claim that `consumed_cycles_monotonic` is "exactly" the scalar equivalent of the by-use-case amounts summed over the use cases the scalar gauge covers too strong, in a way the backfill makes load-bearing: it derives the scalar amount from a gauge reaching back to 2021 and the by-use-case amounts from gauges reaching back to March 2023, so a canister that consumed cycles before then ends up with a scalar total above what its by-use-case amounts add up to. The equivalence is now stated as holding for everything consumed from March 2023 onwards, and the justification for backfilling both in one go no longer rests on the sum identity alone. `HTTPOutcalls` is unchanged: still never backfilled, still only covering the outcalls made since May 2026. Documentation only. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The change alters persisted replicated-state accounting and checkpoint migration semantics across multiple execution paths.
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 0 new
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
The backfill only took a mutable reference to a canister when one of its amounts was actually below what its gauge accounts for, to keep the pass read-only once every canister had been backfilled and so avoid the `Arc::make_mut` clone. That is not worth the machinery: the pass runs on checkpoint rounds only, so paying for the clone once per canister per checkpoint is fine. Dropping the guard collapses the checking function. It no longer has to report whether a backfill is due, so its three-way comparison becomes a single assertion that a monotonic amount is never above its gauge net of the outstanding prepayments, and `needs_monotonic_backfill` becomes `check_monotonic_consumed_cycles`. Calling the backfill right after such an assertion has tripped is harmless: `SystemState::migrate_consumed_cycles_to_monotonic` takes the `max`, so it only ever raises a monotonic amount and cannot write back a derived amount that saturated to zero. One observable detail changes: the backfill materializes a monotonic entry for every use case in the gauge map, so a use case whose whole gauge is still outstanding now gets a zero entry on the first checkpoint round instead of staying absent. An absent entry and a zero entry mean the same thing, and the test helper reads them that way either way; only its comment on why an entry can be absent needed correcting. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
The checkpoint pass unnecessarily triggers copy-on-write cloning for every canister after migration is complete.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 1
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
The note on the deletion path explained at length that a canister decoded from a checkpoint predating the monotonic amounts carries zeroes in them, and that deletion can happen in an ordinary round before a checkpoint round backfills them. That is all beside the point being made: the gauges are moved into the subnet metrics because they are the ones that reach furthest back, which stands on its own. It is also the monotonic amounts that get backfilled, not the canister. Fixed here and in the two other comments that said it the same way. Documentation only. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
The migration is consistent with the accounting invariants and has comprehensive focused coverage.
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 0 new
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
…April 2023 The by-use-case consumed cycles gauges were introduced in March 2023 (EXC-1345), but EXC-1376 moved them from proto field 35 to 36 in April 2023, discarding what the first month had recorded; the comments and the PR description now say April 2023 accordingly. Both the check and the backfill now skip the `HTTPOutcalls` use case explicitly instead of relying on the gauge map never holding such an entry, which `observe_consumed_cycles_with_use_case` only asserts in debug builds. The public `CyclesConsumed` struct doc now carries one time-stable sentence on how far back the amounts reach; the `http_outcalls` field doc is reduced to its own exception. Co-Authored-By: Claude Fable 5.1 <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Pre-April 2023 callbacks can make the per-use-case derivation incorrect, and public API coverage remains overstated.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 2
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
…grade Until the canister's subnet has taken a checkpoint under a replica version that backfills the by-use-case amounts, they only cover what was recorded since May 2026; the struct doc now says so alongside the `http_outcalls` exception. Co-Authored-By: Claude Fable 5.1 <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
Pre-April-2023 callbacks can cause incorrect per-use-case backfills because their prepayments are absent from the source gauge.
Review details
Suppressed comments (1)
rs/replicated_state/src/canister_state/system_state.rs:2542
- This subtracts every currently outstanding prepayment, but the source gauge explicitly starts only in April 2023. A guaranteed-response callback created before field 36 was introduced can still be present because
NO_DEADLINEcallbacks are excluded from expiration (call_context_manager.rs:893-903); its prepayment was discarded with field 35 and is therefore not part of this gauge. Subtracting it either raises a false invariant error or, when later consumption makes the gauge large enough, silently under-backfills this use case. The migration must subtract only prepayments known to have been recorded in field 36 (or adopt and document a conservative legacy-callback policy if that provenance cannot be recovered).
*monotonic = (*monotonic).max(*gauge - outstanding.for_use_case(*use_case));
- Files reviewed: 9/9 changed files
- Comments generated: 0 new
- Review effort level: Balanced (auto)
Note
Copilot is running an experiment and ran this review at Balanced.
… doc The struct-level doc of `CyclesConsumed` in the published `ic-management-canister-types` crate described the window between a replica upgrade and the first checkpoint round that backfills the amounts. That is a replica implementation detail which goes stale within weeks of the rollout, while the published doc lives on. Keep only the durable facts there: the amounts cover consumption since April 2023, `http_outcalls` since May 2026. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…elds The doc comments on the `CanisterMetrics` getters, the proto fields, the `canister_metrics` endpoint and the canister deletion path all described what the checkpoint-round backfill does to the monotonic amounts. That ties code that is not the backfill to it, so dropping the backfill one day would leave stale traces behind. Those comments now state only what is durable: what each amount is, since when it is tracked, and which use cases it covers. What the backfill establishes is described on the backfill itself (`SystemState::migrate_consumed_cycles_to_monotonic` and the scheduler pass). Co-Authored-By: Claude Fable 5.1 <[email protected]>
… or moot Once a canister has been backfilled, its by-use-case monotonic amounts reach back as far as the gauges, and further for `HTTPOutcalls`, so the gauges no longer "hold the longer history"; and since when the monotonic fields were introduced no longer says anything about how far back they reach. Drop those claims from the getters, the proto fields and the canister deletion path. Also drop the backfill's own "two faces of the same metric" rationale: the `HTTPOutcalls` entry makes the by-use-case amounts and the scalar asymmetric. Co-Authored-By: Claude Fable 5.1 <[email protected]>
`checkpoint_round_backfills_consumed_cycles_monotonic_of_paused_canister` only asserted that something was outstanding after the checkpoint round. As the canister had consumed nothing but the paused execution's prepayment, the backfilled amount was zero, indistinguishable from the zero the test had just reset it to: the test never showed the backfill writing anything. Now the canister first completes an ingress execution, so it has settled consumption to recover; and after the checkpoint round the test destructures the aborted task, checking that it is the ingress execution, that exactly its prepayment is outstanding (for `Instructions` only), and that the backfilled amount is exactly what was settled, the way the aborted response test does. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
✅ No security or compliance issues detected. Reviewed everything up to 40ebc17. Security Overview
Detected Code Changes
|
alin-at-dfinity
left a comment
There was a problem hiding this comment.
Still have a couple of large files to go through.
alin-at-dfinity
left a comment
There was a problem hiding this comment.
LGTM, but only because this will all be dropped soon. The code is nice and clean, but the torrent of comments is truly scary.
Address Alin's review: drop the redundant claim that the `consumed_cycles` gauge is the field holding the canister's full history (both it and its monotonic counterpart do now), cut the EXC-1345/EXC-1376 backstory behind the by-use-case gauges' April 2023 start, shorten the `OutstandingPrepayments` docs, and reword the two comments that were phrased relative to the gauges so they don't age badly once those are dropped. Regenerates `state.canister_state_bits.v1.rs` for the `.proto` comment edits. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…se (#11585) `CanisterMetrics::consumed_cycles_by_use_cases_monotonic` was introduced in May 2026 (#9922) and decodes as empty from checkpoints written before that, so it starts from zero rather than from what the canister has really consumed. Its gauge counterpart, `consumed_cycles_by_use_cases`, predates it and reaches further back. This backfills the monotonic amounts from those gauges, the way #11490 already does for the scalar `consumed_cycles_monotonic`, and in the same checkpoint-round pass. The derivation is exact per use case, by the same invariant as for the scalar: a gauge exceeds its monotonic counterpart by exactly the prepayments still outstanding for it, all of which are recorded in the replicated state. `SystemState::outstanding_prepayments` now returns that breakdown per use case. ## How far back each amount reaches The backfill can only recover as much as its source holds, and the two sources differ: | Field | Tracked since | | ----- | ------------- | | `consumed_cycles` (scalar gauge, tag 22) | the initial import, 2021 — the canister's full history | | `consumed_cycles_by_use_cases` (gauge map, tag 36) | April 2023 (EXC-1376) | | `consumed_cycles_by_use_cases_monotonic` (tag 65) | May 2026, and April 2023 once backfilled | | ⤷ its `HTTPOutcalls` entry | May 2026, permanently | The by-use-case breakdown was introduced in March 2023 (EXC-1345, deployed 2023-03-13 to 2023-03-20), but the fix that followed (EXC-1376, elected on 2023-04-17 by [proposal 118023](https://dashboard.internetcomputer.org/proposal/118023) and deployed 2023-04-18 to 2023-04-24) moved it from proto field 35 to field 36, discarding what the first five weeks had recorded. Nothing records what a canister consumed per use case before April 2023, so that is how far back the backfilled amounts reach. A consequence: the scalar `consumed_cycles_monotonic` is backfilled from a gauge reaching back to 2021, the by-use-case amounts from gauges reaching back to April 2023, so a canister that consumed cycles before then holds more in the scalar than its by-use-case amounts add up to. The pre-existing doc calling the scalar *exactly* their sum is qualified accordingly. `HTTPOutcalls` is never backfilled: HTTPS outcalls are only tracked as a gauge at the subnet level, so this monotonic entry has no canister-level gauge to derive it from and keeps covering only what it has recorded since May 2026. The public `CyclesConsumed` docs served by `canister_metrics` state how far back the amounts reach. Only the backfill's own comments describe the backfill. The comments on the fields, their getters, the `canister_metrics` endpoint and the canister deletion path state just what each amount is and which use cases it covers, so that dropping the backfill one day leaves no stale traces. ## Backfilling unconditionally The backfill runs for every canister on every checkpoint round, rather than only for canisters that need it, in exchange for dropping the "is a backfill due" bookkeeping, which with the by-use-case amounts would have to span the scalar total and every use case. That costs an `Arc::make_mut` per canister per checkpoint round, and it is a real copy: the tip a round works on shares every canister's `Arc` with the last committed state, so every canister not already modified earlier in the round gets copied. The cost is small next to what already happens: the resource-charging pass does the same to every canister every 10 seconds, i.e. dozens of times per checkpoint interval. ## Visible effects Unlike the scalar monotonic total, which nothing exports yet, `consumed_cycles_by_use_cases_monotonic` is served in two places, both of which will step up once, on the first checkpoint round after the upgrade: - The `canister_metrics` management canister endpoint reports its `cycles_consumed` from it. Every field except `http_outcalls` will jump from what the canister consumed since May 2026 to what it consumed since April 2023. - The `replicated_state_consumed_cycles_from_replica_start_as_counters` Prometheus counter, by the sum over the subnet's canisters of what each has been under-reporting since May 2026. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Bump the crate version to 0.11.0 and close out the CHANGELOG's [Unreleased] section, which collects since v0.10.0: - the `Secp256r1` variant of `EcdsaCurve`, selecting threshold ECDSA on the NIST P-256 curve (dfinity#11573) - the `million_round_instructions_total` field of `SubnetMetricsResult` (dfinity#11586) It also records, as a doc change, how far back the `CyclesConsumed` amounts served by `canister_metrics` reach (dfinity#11585). Neither `EcdsaCurve` nor `SubnetMetricsResult` is `#[non_exhaustive]`, so the new variant breaks exhaustive matches downstream and the new field breaks struct literals, hence the minor bump. --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
CanisterMetrics::consumed_cycles_by_use_cases_monotonicwas introduced in May 2026 (#9922) and decodes as empty from checkpoints written before that, so it starts from zero rather than from what the canister has really consumed. Its gauge counterpart,consumed_cycles_by_use_cases, predates it and reaches further back.This backfills the monotonic amounts from those gauges, the way #11490 already does for the scalar
consumed_cycles_monotonic, and in the same checkpoint-round pass. The derivation is exact per use case, by the same invariant as for the scalar: a gauge exceeds its monotonic counterpart by exactly the prepayments still outstanding for it, all of which are recorded in the replicated state.SystemState::outstanding_prepaymentsnow returns that breakdown per use case.How far back each amount reaches
The backfill can only recover as much as its source holds, and the two sources differ:
consumed_cycles(scalar gauge, tag 22)consumed_cycles_by_use_cases(gauge map, tag 36)consumed_cycles_by_use_cases_monotonic(tag 65)HTTPOutcallsentryThe by-use-case breakdown was introduced in March 2023 (EXC-1345, deployed 2023-03-13 to 2023-03-20), but the fix that followed (EXC-1376, elected on 2023-04-17 by proposal 118023 and deployed 2023-04-18 to 2023-04-24) moved it from proto field 35 to field 36, discarding what the first five weeks had recorded. Nothing records what a canister consumed per use case before April 2023, so that is how far back the backfilled amounts reach.
A consequence: the scalar
consumed_cycles_monotonicis backfilled from a gauge reaching back to 2021, the by-use-case amounts from gauges reaching back to April 2023, so a canister that consumed cycles before then holds more in the scalar than its by-use-case amounts add up to. The pre-existing doc calling the scalar exactly their sum is qualified accordingly.HTTPOutcallsis never backfilled: HTTPS outcalls are only tracked as a gauge at the subnet level, so this monotonic entry has no canister-level gauge to derive it from and keeps covering only what it has recorded since May 2026. The publicCyclesConsumeddocs served bycanister_metricsstate how far back the amounts reach.Only the backfill's own comments describe the backfill. The comments on the fields, their getters, the
canister_metricsendpoint and the canister deletion path state just what each amount is and which use cases it covers, so that dropping the backfill one day leaves no stale traces.Backfilling unconditionally
The backfill runs for every canister on every checkpoint round, rather than only for canisters that need it, in exchange for dropping the "is a backfill due" bookkeeping, which with the by-use-case amounts would have to span the scalar total and every use case. That costs an
Arc::make_mutper canister per checkpoint round, and it is a real copy: the tip a round works on shares every canister'sArcwith the last committed state, so every canister not already modified earlier in the round gets copied. The cost is small next to what already happens: the resource-charging pass does the same to every canister every 10 seconds, i.e. dozens of times per checkpoint interval.Visible effects
Unlike the scalar monotonic total, which nothing exports yet,
consumed_cycles_by_use_cases_monotonicis served in two places, both of which will step up once, on the first checkpoint round after the upgrade:canister_metricsmanagement canister endpoint reports itscycles_consumedfrom it. Every field excepthttp_outcallswill jump from what the canister consumed since May 2026 to what it consumed since April 2023.replicated_state_consumed_cycles_from_replica_start_as_countersPrometheus counter, by the sum over the subnet's canisters of what each has been under-reporting since May 2026.🤖 Generated with Claude Code