Skip to content

feat: use the monotonic consumed cycles by use case in metrics, deletion and install_code - #11776

Merged
mraszyk merged 5 commits into
masterfrom
mraszyk/consumed-cycles-monotonic-use-sites
Oct 7, 2026
Merged

mraszyk merged 5 commits into
masterfrom
mraszyk/consumed-cycles-monotonic-use-sites

Conversation

@mraszyk

@mraszyk mraszyk commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Switches the remaining production readers of the CanisterMetrics::consumed_cycles_by_use_cases gauges to consumed_cycles_by_use_cases_monotonic:

  • rs/replicated_state/src/metrics.rs: the canister part of the per-use-case consumed cycles gauge exported to Prometheus now comes from the monotonic map, with HTTPOutcalls removed, just like the counter's. The gauge (replicated_state_consumed_cycles_from_replica_start) and the counter (..._as_counters) are therefore built from exactly the same inputs, so observe() builds a single per-use-case map and feeds it to both, instead of building an identical second map over every canister after each round.
  • rs/execution_environment/src/canister_manager.rs: on canister deletion, the canister's per-use-case consumption is moved into SubnetMetrics from the monotonic map.
  • rs/execution_environment/src/execution/install_code.rs: the burned cycles delta in finish reads BurnedCycles from the monotonic map. Burned cycles are charged directly without a prepayment, so both maps move together and the balance debug_assert is unaffected.

The monotonic map has an HTTPOutcalls entry, which the gauge map lacks because HTTPS outcalls are recorded at the subnet level when they are charged. The metrics and deletion paths skip it. Without the skip, deletion would double count outcall cycles in the subnet-level HTTPOutcalls entry, and through it in SubnetMetrics::consumed_cycles_total(). That subnet-level part goes into the certified /subnet/<subnet_id>/metrics total from V29 on (consumed_cycles_total_including_canisters) and also into the V30 total (consumed_cycles_total_including_canisters_monotonic, #11777).

For the other use cases, the deletion path should behave as before once the monotonic amounts have been backfilled (which is the case in production by now). A stopped canister has no outstanding prepayments, so its gauges equal their monotonic counterparts.

Tests:

  • New http_outcalls_consumed_cycles_are_not_double_counted_on_canister_deletion: deleting a canister that made an HTTPS outcall leaves the subnet-level HTTPOutcalls entry, the subnet total (beyond the leftover balance), and the exported gauge and counter unchanged. It fails without the skip (subnet HTTPOutcalls entry 2,000,000 instead of 1,000,000).
  • consumed_cycles_for_instructions_are_updated_from_valid_canisters first observes the metrics while the instruction prepayment is outstanding and expects 0.0 (it fails with 1000.0 if metrics.rs reads the gauges again), then settles the prepayment with a zero refund, as finishing an execution would, and expects the full amount.
  • consumed_cycles_are_updated_from_deleted_canisters settles the prepayment the same way before deleting the canister, so the cycles count as consumed in the monotonic map.

🤖 Generated with Claude Code

…ion and install_code

Switch the remaining readers of the `CanisterMetrics::consumed_cycles_by_use_cases`
gauges to `consumed_cycles_by_use_cases_monotonic`:

- the per-use-case consumed cycles gauge exported by `ReplicatedStateMetrics`;
- the per-use-case consumption folded into `SubnetMetrics` on canister deletion;
- the burned cycles delta computed when finishing `install_code`.

The monotonic map has an `HTTPOutcalls` entry, which the gauges lack because HTTPS
outcalls are recorded at the subnet level. It is skipped in the first two places so
that HTTPS outcalls are not double counted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Existing scheduler metric tests will fail under the new prepayment semantics, and HTTPS deletion needs regression coverage.

Review effort: Balanced
Findings: 2 High severity · 1 Low severity

Open (3)
What changed in this PR

Switches remaining production consumers to monotonic per-use-case cycle accounting.

Changes:

  • Exports monotonic canister cycle metrics, excluding HTTPS outcalls.
  • Preserves monotonic accounting during canister deletion.
  • Uses monotonic burned-cycle totals during install_code.
File Description
rs/​replicated_state/​src/​metrics.rs Uses monotonic values for Prometheus totals.
rs/​execution_environment/​src/​canister_manager.rs Transfers monotonic values on deletion.
rs/​execution_environment/​src/​execution/​install_code.rs Calculates burned-cycle deltas monotonically.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread rs/replicated_state/src/metrics.rs Resolved
…cs tests

The per-use-case consumed cycles gauge and the deleted canisters accounting now read
the monotonic amounts, which only count an instructions prepayment once its refund is
known. Settle the prepayment with a zero refund, as finishing the execution would.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Regression coverage does not distinguish monotonic metrics from gauges or verify HTTP-outcall deletion deduplication.

Review effort: Balanced
Findings: 2 High severity · 1 Low severity

Open (3)

Regression test for skipping the `HTTPOutcalls` entry of the canister's monotonic
consumed cycles when folding them into `SubnetMetrics` on deletion: the subnet-level
entries, the consumed cycles total and the exported metrics must count the outcall
only once.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The accounting changes are consistent with existing invariants, and prior test coverage concerns have been addressed.

Review effort: Balanced
Findings: None

Resolved since last review (3)

@mraszyk
mraszyk marked this pull request as ready for review October 7, 2026 12:35
@mraszyk
mraszyk requested a review from a team as a code owner October 7, 2026 12:35
@zeropath-ai

zeropath-ai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

✅ No security or compliance issues detected. Reviewed everything up to d0d7f52.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/execution_environment/src/canister_manager.rs
     Monotonic consumption cycles handling: exclude HTTPS outcalls from per-canister metrics when aggregating to avoid double counting
Enhancement ► rs/execution_environment/src/execution/install_code.rs
     Switch to monotonic consumed cycles per use case when computing BurnedCycles contributions
Enhancement ► rs/execution_environment/src/scheduler/tests/metrics.rs
     Add tests and metrics adjustments for consumed cycles, including HTTPS outcalls handling and prepayment settlement checks
Enhancement ► rs/replicated_state/src/metrics.rs
     Update metrics aggregation to use monotonic per-use-case data and ensure proper filtering of HTTP outcalls in canister-level metrics formatting

Comment thread rs/replicated_state/src/metrics.rs Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The accounting changes are consistent with existing invariants and adequately covered by regression tests.

0 open findings

🧠 Review effort: Balanced

- Feed the same per-use-case totals to both the consumed cycles gauge and
  counter instead of building an identical second map.
- Observe the metrics once while the instruction prepayment is still
  outstanding, so the test fails if the metrics read the gauges again.
- Drop the redundant subnet map assertion from the HTTPS outcalls deletion
  test and build its labels with `labels`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The accounting changes are consistent with existing invariants and covered by targeted regression tests.

0 open findings

🧠 Review effort: Balanced

@mraszyk
mraszyk enabled auto-merge October 7, 2026 19:25
@mraszyk
mraszyk added this pull request to the merge queue Oct 7, 2026
Merged via the queue into master with commit 9debdbf Oct 7, 2026
42 checks passed
@mraszyk
mraszyk deleted the mraszyk/consumed-cycles-monotonic-use-sites branch October 7, 2026 19:58
AleDema pushed a commit to AleDema/ic that referenced this pull request Oct 11, 2026
…#11782)

On canister deletion, `CanisterManager::delete_canister` now adds the
canister's `CanisterMetrics::consumed_cycles_monotonic` (instead of the
`consumed_cycles` gauge) to
`SubnetMetrics::consumed_cycles_by_deleted_canisters`. This matches the
per-use-case consumption, which is already taken from the monotonic map
(dfinity#11776).

The monotonic total has been backfilled for all canisters (dfinity#11490). A
stopped canister has no outstanding prepayments, so its gauge equals its
monotonic counterpart.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants