Skip to content

feat: Migrate HTTP/ECDSA outcall cycles in SubnetMetrics from scaler to per-use-case map - #10844

Merged
mraszyk merged 2 commits into
masterfrom
mraszyk/migrate-scalar-cycles-use-cases
Jul 21, 2026
Merged

mraszyk merged 2 commits into
masterfrom
mraszyk/migrate-scalar-cycles-use-cases

Conversation

@mraszyk

@mraszyk mraszyk commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

The cycles consumed by HTTP and ECDSA outcalls are tracked both in the legacy scalar fields consumed_cycles_http_outcalls / consumed_cycles_ecdsa_outcalls of SubnetMetrics and in the
consumed_cycles_by_use_case map. The scalar fields predate use-case tracking, so they are a superset of the corresponding use-case entries.

This adds a migration that brings the HTTPOutcalls / ECDSAOutcalls entries of consumed_cycles_by_use_case up to the scalar value (via max).

The migration runs whenever any use case is observed (from observe_consumed_cycles_with_use_case), i.e. independently of whether the scalar fields themselves are observed, so the use-case
entries also catch up on subnets that stop performing outcalls. It runs after the increments; since the scalar fields are bumped before the matching use-case observation at the call sites,
the max reconciliation keeps the entries in lockstep with the scalar fields without double counting.

Only the consumed_cycles_by_use_case map is migrated. The monotonic consumed_cycles_by_use_case_as_counters map is intentionally left untouched: backfilling it would introduce a spurious
counter jump (the counters were introduced very recently, unlike the gauges).

The scalar fields are intentionally kept (and kept up to date) rather than zeroed, so they remain the source of truth for the readers that still use them (e.g. consumed_cycles_total) and
so that downgrading to an earlier replica version keeps observing the correct totals. Read paths are left unchanged for now.

mraszyk and others added 2 commits July 21, 2026 08:12
The cycles consumed by HTTP and ECDSA outcalls are tracked both in the
legacy scalar fields `consumed_cycles_http_outcalls` /
`consumed_cycles_ecdsa_outcalls` of `SubnetMetrics` and in the
`consumed_cycles_by_use_case` (and `_as_counters`) map. The scalar fields
predate use-case tracking, so they are a superset of the corresponding
use-case entries.

This adds a migration that, whenever the scalar fields are observed, brings
the `HTTPOutcalls` / `ECDSAOutcalls` use-case entries up to the scalar value
(via `max`). This backfills the history that predates use-case tracking
while avoiding double counting the overlapping period, and keeps the
use-case entries evolving in lockstep with the scalar fields.

The scalar fields are intentionally kept (and kept up to date) rather than
zeroed, so they remain the source of truth for the readers that still use
them (e.g. `consumed_cycles_total`) and so that downgrading to an earlier
replica version keeps observing the correct totals. Read paths are left
unchanged for now.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Refine the HTTP/ECDSA outcall cycles migration so that it:

- Runs from `observe_consumed_cycles_with_use_case` (on every observed use
  case) instead of from the scalar-specific observe methods, so it runs
  independently of whether the scalar fields are observed. This lets the
  use-case entries catch up even on subnets that no longer perform outcalls.
  The migration runs after the increments, and since the scalar fields are
  bumped before the matching use-case observation at the call sites, the
  `max` reconciliation does not double count.

- Only migrates the `consumed_cycles_by_use_case` map. The monotonic
  `consumed_cycles_by_use_case_as_counters` map is left untouched:
  backfilling it would introduce a spurious counter jump, which is exactly
  the transition that #9922 (which introduced the counter map) deliberately
  deferred pending a proper cutoff for in-flight callbacks.

Tests are updated to trigger the migration via an unrelated use case and to
assert the counters map is not modified.

Co-Authored-By: Claude Opus 4.8 (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.

Pull request overview

This PR introduces an in-place migration inside SubnetMetrics to reconcile legacy scalar outcall cycle gauges (HTTP/ECDSA) with the newer consumed_cycles_by_use_case map, ensuring the use-case entries catch up to (and stay aligned with) the scalar superset values without double counting.

Changes:

  • Run an outcall reconciliation step after every observe_consumed_cycles_with_use_case update to backfill HTTPOutcalls/ECDSAOutcalls in consumed_cycles_by_use_case using max(scalar, entry).
  • Add tests covering (1) migration triggered by an unrelated use case and (2) lockstep behavior when scalar is bumped before the outcall use-case observation, while ensuring the counters map is not migrated.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
rs/replicated_state/src/metadata_state.rs Adds a reconciliation/migration routine invoked from observe_consumed_cycles_with_use_case to align HTTP/ECDSA use-case gauges with legacy scalar fields.
rs/replicated_state/src/metadata_state/tests.rs Adds unit tests validating migration behavior, lockstep behavior, and that the monotonic counters map remains untouched.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mraszyk
mraszyk marked this pull request as ready for review July 21, 2026 09:37
@mraszyk
mraszyk requested a review from a team as a code owner July 21, 2026 09:37
@zeropath-ai

zeropath-ai Bot commented Jul 21, 2026

Copy link
Copy Markdown

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

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/replicated_state/src/metadata_state.rs
    Implement migration of legacy outcall scalar fields into use-case map for consumed cycles
► rs/replicated_state/src/metadata_state/tests.rs
    Add tests for migrating outcalls scalar fields into use cases and lockstep behavior with scalar fields

@eichhorl

Copy link
Copy Markdown
Contributor

The description claims that consumed_cycles_by_use_case_as_counters is left untouched because backfilling it would introduce a spurious counter jump.

However, it seems that consumed_cycles_by_use_case_as_counters (get_consumed_cycles_by_use_case_as_counters()) is never actually read outside of tests, and the metric consumed_cycles_by_use_case_as_counters is also filled using consumed_cycles_by_use_case:

join_consumed_cycles_by_use_case(
&mut consumed_cycles_total_by_use_case_as_counters,
state
.metadata
.subnet_metrics
.get_consumed_cycles_by_use_case(),
);

So this migration would already lead to a counter jump.

Maybe the problem is that we should call get_consumed_cycles_by_use_case_as_counters() in the code above? But then the metric might actually decrease, since the counters were introduced after the gauges.

@mraszyk

mraszyk commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

it seems that consumed_cycles_by_use_case_as_counters (get_consumed_cycles_by_use_case_as_counters()) is never actually read outside of tests

Good point! I'll consider cleaning that up in a follow-up.

the metric consumed_cycles_by_use_case_as_counters is also filled using consumed_cycles_by_use_case:

join_consumed_cycles_by_use_case(
&mut consumed_cycles_total_by_use_case_as_counters,
state
.metadata
.subnet_metrics
.get_consumed_cycles_by_use_case(),
);

So this migration would already lead to a counter jump.

correct, but the jump would only affect the exported metric, not data (the map consumed_cycles_by_use_case_as_counters) stored in the checkpoint (that I wanted to keep consistent as tracking since the "counters" were introduced, not since the total or gauges were introduced)

Maybe the problem is that we should call get_consumed_cycles_by_use_case_as_counters() in the code above?

The gauges actually already behave as counters at the moment which is likely why they are used to feed the "counter" metric.

@eichhorl

Copy link
Copy Markdown
Contributor

I'll consider cleaning that up in a follow-up.

Fine with me 👍

The gauges actually already behave as counters at the moment which is likely why they are used to feed the "counter" metric.

Then I'm not sure why the _as_counters map was introduced

@mraszyk

mraszyk commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Then I'm not sure why the _as_counters map was introduced

The only reason I can think of is to have them be tracked since the same point in time when the canister-level "counters" were introduced (canister-level "gauges" are really gauges which is why canister-level "counters" were introduced, i.e., they are also decremented unlike subnet-level "gauges" that are never decremented).

@mraszyk
mraszyk added this pull request to the merge queue Jul 21, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 21, 2026
@mraszyk
mraszyk added this pull request to the merge queue Jul 21, 2026
Merged via the queue into master with commit 06ef48c Jul 21, 2026
68 of 69 checks passed
@mraszyk
mraszyk deleted the mraszyk/migrate-scalar-cycles-use-cases branch July 21, 2026 14:08
pull Bot pushed a commit to bit-cook/ic that referenced this pull request Aug 24, 2026
…finity#11219)

The migration added in dfinity#10844, which brings the HTTPOutcalls /
ECDSAOutcalls entries of SubnetMetrics::consumed_cycles_by_use_case up
to the legacy scalar fields, was invoked from
observe_consumed_cycles_with_use_case. That only fires when the subnet
observes a subnet-level use case, and those are only observed on HTTP
outcalls, threshold signature outcalls, canister deletion and cycles
lost to dropped messages. A subnet that does none of these never runs
the migration and its use-case entries stay stale indefinitely.

This is not hypothetical: on mainnet subnet
2fq7c-slacv-26cgz-vzbx2-2jrcs-5edph-i5s2j-tck77-c3rlz-iobzx-mqe,
consumed_cycles_ecdsa_outcalls is 244_900_000_000_000 while the
ECDSAOutcalls use-case entry is 228_370_000_000_000, a gap of
16_530_000_000_000 cycles. Its consumed_cycles_by_use_case_as_counters
map is empty, which (since that map is incremented on the very same code
path) shows that no subnet-level use case has been observed there since
the counters were introduced in dfinity#9922, i.e. well before the migration
was deployed.

The migration now runs from the scheduler's finish_round, which is
documented as running unconditionally after each round and is also
reached by the heap-delta early return and by
checkpoint_round_with_no_execution. Running it once per round instead of
per observation is equivalent, because the call sites bump the scalar
field and the matching use-case entry by the same amount: max(entry,
scalar) + delta == max(entry + delta, scalar + delta). It is also
idempotent, so the extra invocations are harmless.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
pull Bot pushed a commit to bit-cook/ic that referenced this pull request Sep 22, 2026
…finity#11202)

The cycles consumed by HTTP and ECDSA outcalls are tracked both in the
legacy scalar fields consumed_cycles_http_outcalls /
consumed_cycles_ecdsa_outcalls of SubnetMetrics and in the
consumed_cycles_by_use_case map, which a migration introduced in dfinity#10844
keeps in lockstep with those fields. dfinity#11219 fixed that migration to run
once per round from the scheduler's `finish_round`, i.e. regardless of
subnet activity, so the map entries can no longer go stale on a subnet
that performs no outcalls.
I could now successfully confirm that the migration completed on all
subnets with a DFINITY-owned node.

This switches all readers over to the map:

- get_consumed_cycles_http_outcalls / get_consumed_cycles_ecdsa_outcalls
now read the HTTPOutcalls / ECDSAOutcalls entries of
consumed_cycles_by_use_case, via a new helper that also backs the
existing Schnorr / VetKd / dropped messages getters.
- consumed_cycles_total and consumed_cycles_total_v28 add those entries
instead of the scalar fields.

The scalar fields are still observed and serialized, so that downgrading
to an earlier replica version keeps observing the correct totals; only
their reads are gone.

The fixtures that populate only the scalar fields
(`canonical_encoding_subnet_metrics`, the `state_manager` tree hash
fixture and `consumed_cycles_total_is_the_same_across_a_restart`) now
run the migration first, the way the scheduler does once per round. The
expected canonical encodings and state hashes are unchanged.

---------

Co-authored-by: Claude Opus 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