Skip to content

Action plan render once - #2067

Merged
sunsingerus merged 6 commits into
Altinity:0.27.4from
erikasb:action-plan-render-once
Sep 7, 2026
Merged

Action plan render once#2067
sunsingerus merged 6 commits into
Altinity:0.27.4from
erikasb:action-plan-render-once

Conversation

@erikasb

@erikasb erikasb commented Aug 18, 2026

Copy link
Copy Markdown

Fixes #2066.

ActionPlan.String() is called on every status update attempt while building the status storage ConfigMap data. Each call re-renders the diff from scratch: reflection-dumps every diffed object (the full value is rendered, then truncated to 256 chars) and iterates the diff maps in random order, so the same plan produces different bytes on every call. During a scale-up of a large CHI this runs thousands of times per reconcile and dominates operator CPU (details and measurements in the issue).

This PR renders the plan once, eagerly in MakeActionPlan, and makes String() return that snapshot. The plan is immutable after construction, and every reconcile already renders it at least once for logging, so this adds no new work — it just stops repeating it. Before the plan was moved into the status path, it was rendered once per reconcile (LogActionPlan); this restores that frequency while keeping the storage feature.

Behavior notes:

  • String() now returns the same bytes on every call. The stored status-actionPlan text becomes a construction-time snapshot. It is write-only (never parsed back), so nothing consumes the difference.
  • Zero-value or JSON-unmarshaled plans (only unexported fields, so they unmarshal empty) render as "", same as before.

Measured on a 156-host CHI, scaling 2 clusters 20 → 39 shards each: 0.28.0 + this change completes in 18–21 min where 0.27.1 took ~1h41m with operator CPU pinned; status update conflict retries drop ~10x.

sunsingerus and others added 6 commits August 12, 2026 00:43
Since the action plan was added to the CR status write path, every status
update re-renders ActionPlan.String() while building the aux status ConfigMap
payload: each call reflection-dumps every diffed object (the full value is
rendered, then truncated to 256 chars) and iterates the diff maps in
randomized order, so the very same plan produces different bytes on every
call. During a horizontal expansion, status updates are issued several times
per host - thousands of renderings of a diff that lists every added shard,
where 0.25.x rendered the plan exactly once per reconcile (LogActionPlan).
This is a major CPU cost on the reconcile hot path (operator CPU observed
pinned for the duration of large expansions).

Render the plan exactly once, eagerly in MakeActionPlan, and make String()
return that stable snapshot. This restores the once-per-reconcile rendering
frequency that existed before the plan moved into the storage path, while
keeping the storage feature. The plan is immutable after construction and
every reconcile already renders it at least once for logs, so eager rendering
adds no new work. Repeated String() calls become byte-stable, which also
allows content comparison on the stored plan downstream. Zero-value or
JSON-unmarshaled plans render as "" exactly as before.

Signed-off-by: Erikas Balynas <ebalynas@paloaltonetworks.com>
@atsarevskiy

Copy link
Copy Markdown

Found this running your patch on top of 0.27.3: render() isn't safe on every
construction path, so moving the render into MakeActionPlan turns a latent nil read
into one that fires.

specDiffReverse is only assigned in the (old != nil) && (new != nil) branch, so the
nil-old, nil-new and both-nil paths leave it nil, and render() reads
len(ap.specDiffReverse.Modified) without a nil check. While String() was lazy that
read only happened where a caller actually wanted the string. Rendering at construction
time runs it on every path, so MakeActionPlan(nil, cr) panics.

Easiest way to see it is with #2070 also applied: its
TestMarkReconcileStartReturnsStatusUpdateError builds a plan with
api.MakeActionPlan(nil, cr), and with this PR in the tree that test panics instead of
failing its assertion. Neither PR does it on its own, which is presumably why CI is
green here.

One line:

if ap.specDiffReverse != nil && len(ap.specDiffReverse.Modified) > 0 {

Happy to push that as a commit if it's easier.

@sunsingerus
sunsingerus changed the base branch from 0.28.0 to 0.27.4 September 4, 2026 12:43
@sunsingerus
sunsingerus merged commit bb75293 into Altinity:0.27.4 Sep 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

planned for review This feature is planned for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants