← Back to Pull requests

Optimize DDAI profile merge and reconcile paths #3853

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 25, 2026 15:30 UTC
Author

<!-- dd-meta {"pullId":"00000000-0000-0000-0000-000000000000","source":"chat","resourceId":"08d9e3d9-7de6-48d3-9a34-6e78de6ba0c9","workflowId":"81140062-eaff-4824-968b-a6110b243dfe","codeChangeId":"81140062-eaff-4824-968b-a6110b243dfe","sourceType":"chat"} -->

What does this PR do?

Speeds up the DatadogAgent reconcile hot paths, mainly DDAI profile merging. The profile merge is intentionally the complex part; the other changes are kept small.

1. DDAI profile merge (internal/controller/datadogagent/merge.go, profile.go, controller.go)
- Calls TypedValue.Merge directly when objects have no populated ManagedFields, skipping the ManagedFields tracking in managedfields.FieldManager.Apply that we don't use. Outdated-CRD unknown-field stripping and its logging are unchanged.
- Checks the base DDAI against the installed CRD schema once per batch, then merges only the spec subtrees each profile touches and copies the rest directly. The synthetic default profile skips the structured merge entirely.
- Adds a steady-state merge cache on the Reconciler. Entries are checked against the CRD field manager, a fingerprint of the source DDAI and a content hash of the profile. They're evicted when a profile goes away, and callers get DeepCopies. The fingerprint functions document that they must cover every input the merge reads.
- On my machine, 20 profiles take ~4.5 ms cold and ~0.2 ms in steady state (BenchmarkApplyProfilesToDDAISpec_20Profiles_).

2. Profile reconcile (reconcile_agent.go, profile.go, pkg/agentprofile/agent_profile.go, feature/overlay.go)
- labelNodesWithProfiles reuses the node list that's already fetched instead of fetching each node: ~1.8 ms → ~23 µs for 200 nodes in steady state.
- Index-based loops instead of copying Node/Pod/Profile values; overlay registration order is sorted once at registration.

3. Pod mergers (merger/envvars.go, envvarfrom.go, port.go, volume.go, volume_mount.go): index-based loops, so the merge function gets a pointer into the slice instead of a heap-escaping copy of the loop variable.

4. comparison.GenerateMD5ForSpec: pooled JSON encoding buffer. Hashes are unchanged; ~80% fewer bytes allocated per DatadogAgentSpec hash (1780 → 336 B/op), same speed.

5. utils.IsAboveMinVersion: caches the parsed >= minVersion constraint. Every caller passes a package constant, so the cache stays small: ~27 µs → ~5.5 µs and 120 → 26 allocs for 6 checks.

Motivation

Production eBPF CPU profiling across the datadog-operator fleet showed ssaMergeCRD at over 50% of profiled operator CPU, also driving GC overhead, with ~20 profile merges per DatadogAgent reconcile. The other changes target the next user-code hotspots in the same profiles.

Additional Notes

  • Earlier versions of this PR also included a hand-written semver parser, hand-rolled FNV hashing for ConfigMap checksums, a hand-written JSON map encoder and a Private Action Runner YAML change. Review found them not worth the complexity, so they were reverted or replaced by the smaller changes above.
  • Validation on nightly clusters

    Commit 2159d27b ran on 8 operator nightly clusters, starting around 2026-11-26 13:30 UTC, for a ~19h soak. oddish-b stayed on main as a matched control for oddish-c: before the rollout, their CPU, reconcile latency and allocation rate were within ~2% of each other.

    | Signal | Before (main) | After (this PR) | Delta |
    |--|--|--|--|
    | Container CPU, oddish-c vs oddish-b (19h soak) | 28.11 mCPU | 9.64 mCPU | -65.7% |
    | Container CPU, per updated cluster (19h soak vs each cluster's baseline) | 20.7–30.6 mCPU | 6.9–9.7 mCPU | -61% to -69% |
    | datadogagent reconcile latency, oddish-c vs oddish-b (19h soak) | 296.5 ms | 46.4 ms | -84.4% |
    | datadogagent reconcile latency, per updated cluster (initial steady state) | 290–363 ms | 18.6–60.8 ms | -79% to -94% |
    | Fleet eBPF CPU (ebpf-cpu-time) | 1,577 ms/min | 524 ms/min | -66.8% |
    | ↳ managedfields structuredMergeManager.Apply + Update | 603 ms/min (38%) | not in top stacks | gone |
    | ↳ override.PodTemplateSpec (pod mergers) | ~36.6 ms/min | ~4.7 ms/min | -87% |
    | Go profiler CPU (empoleon-a) | 1,310 ms/min | 532 ms/min | -59.4% (Reconcile -79.7%) |
    | Go alloc bytes / alloc count (empoleon-a) | 229 MiB/min / 2.8M/min | 102 MiB/min / 711k/min | -55.7% / -74.7% |
    | Go live heap (empoleon-a) | 79.3 MiB | 73.1 MiB | -7.8% |

    <details>
    <summary>Per-cluster results (baseline → initial steady state → 19h soak)</summary>

    | Cluster | Container CPU (mCPU) | datadogagent reconcile latency (ms) | Working set (MiB) | 19h restarts |
    |--|--|--|--|--|
    | oddish-b (control, main) | 31.07 → 27.50 → 28.11 | 286.7 → 287.1 → 296.5 | 155.2 → 158.8 → 157.9 | 0 |
    | oddish-c | 30.63 → 11.84 → 9.64 (-68.5%) | 289.7 → 60.8 → 46.4 (-84.0%) | 159.0 → 143.5 → 145.6 | 0 |
    | empoleon-a | 23.55 → 8.28 → 9.20 (-60.9%) | 349.0 → 26.4 → 15.2 (-95.6%) | 167.1 → 163.6 → 164.1 | 0 |
    | diglet | 20.70 → 7.16 → 6.90 (-66.7%) | 317.1 → 28.6 → 121.0 (19h avg incl. spikes) | 190.5 → 198.6 → 196.6 | 0 |
    | lagaffe | 21.62 → 7.37 → 7.94 (-63.3%) | 315.7 → 33.2 → 195.7 (19h avg incl. spikes) | 166.9 → 162.0 → 163.5 | 0 |
    | squirtle-a | 22.18 → 8.11 → 7.93 (-64.2%) | 359.3 → 20.6 → 29.6 (-91.8%) | 114.9 → 107.6 → 109.1 | 0 |
    | snowver | 20.69 → 8.12 → 8.04 (-61.1%) | 310.1 → 18.6 → 20.2 (-93.5%) | 123.3 → 121.6 → 126.6 | 0 |
    | entei | 25.78 → 10.33 → 9.72 (-62.3%) | 362.7 → 41.6 → 37.1 (-89.8%) | 201.5 → 173.5 → 187.8 | 0 |

    Windows (UTC): baseline 2026-11-26 10:00–13:20, initial steady state 13:44–14:20, soak 2026-11-26 14:00 – 2026-11-27 08:43.

    </details>

    Metrics (container CPU, reconcile latency, memory):

    ![dz4-hij-p7r](https://ddstaging.datadoghq.com/s/c894ecec5/dz4-hij-p7r)

    ![6vi-tnx-gup](https://ddstaging.datadoghq.com/s/c894ecec5/6vi-tnx-gup)

    Profiles (eBPF fleet CPU and Go profiler on empoleon-a):

    ![xue-3r8-fr5](https://ddstaging.datadoghq.com/s/c894ecec5/xue-3r8-fr5)

    ![tfz-q5s-yn9](https://ddstaging.datadoghq.com/s/c894ecec5/tfz-q5s-yn9)

    ![fna-7bb-h2t](https://ddstaging.datadoghq.com/s/c894ecec5/fna-7bb-h2t)

    ![w4a-ist-ux5](https://ddstaging.datadoghq.com/s/c894ecec5/w4a-ist-ux5)

    Stability over the 19h soak, on every updated cluster:

  • 0 container restarts, 0 reconcile panics, 0 terminal reconcile errors.

  • 0 reconcile errors for datadogagent, datadogagentinternal or datadogagentprofile in the initial steady state.

  • No new error log patterns. The only recurring error is an existing, deliberate profile-conflict test case, at its usual rate.

  • Live heap and working set stayed flat, so the merge cache shows no growth.
  • Next hotspots: reconcileProfiles (getNodeList, enforceCreateStrategy, profile status updates), then the DDAI DaemonSet update and annotateConfigMapsChecksum.

    Minimum Agent Versions

    Are there minimum versions of the Datadog Agent and/or Cluster Agent required?

    Agent: N/A

  • Cluster Agent: N/A
  • Describe your test plan

  • See previous validation on staging clusters
  • Checklist

  • PR has at least one valid label: bug, enhancement, refactoring, documentation, tooling, and/or dependencies

  • PR has a milestone or the qa/skip-qa label

  • All commits are signed (see: signing commits)
  • ---

    PR by Bits - View session in Datadog

    🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: tbavelier Review duration: 7250m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3853"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%