Repository navigation
feat: Migrate HTTP/ECDSA outcall cycles in SubnetMetrics from scaler to per-use-case map - #10844
Conversation
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]>
There was a problem hiding this comment.
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_caseupdate to backfillHTTPOutcalls/ECDSAOutcallsinconsumed_cycles_by_use_caseusingmax(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.
|
✅ No security or compliance issues detected. Reviewed everything up to 3186202. Security OverviewDetected Code Changes
|
|
The description claims that However, it seems that ic/rs/replicated_state/src/metrics.rs Lines 518 to 524 in 3186202 So this migration would already lead to a counter jump. Maybe the problem is that we should call |
Good point! I'll consider cleaning that up in a follow-up.
correct, but the jump would only affect the exported metric, not data (the map
The gauges actually already behave as counters at the moment which is likely why they are used to feed the "counter" metric. |
Fine with me 👍
Then I'm not sure why the |
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). |
…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]>
…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]>
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.