Skip to content

fix: bound policy ancestors on the fly to avoid O(N^2) translation latency - #9558

Merged
zhaohuabing merged 2 commits into
envoyproxy:mainfrom
zhaohuabing:perf/bound-policy-ancestors
Jul 24, 2026
Merged

fix: bound policy ancestors on the fly to avoid O(N^2) translation latency#9558
zhaohuabing merged 2 commits into
envoyproxy:mainfrom
zhaohuabing:perf/bound-policy-ancestors

Conversation

@zhaohuabing

@zhaohuabing zhaohuabing commented Jul 22, 2026

Copy link
Copy Markdown
Member

What this PR does

Fixes #9539.

When many resources reference the same target, a policy's status.Ancestors list could grow without bound during translation. SetConditionForPolicyAncestor scans the existing ancestors on every call, so building a status with N ancestors was O(N²). For a BackendTLSPolicy shared by ~10k EnvoyExtensionPolicies (one TLS-enabled ext-proc backend), this pushed translation time to ~1 hour.

Fix

Cap status.Ancestors at maxPolicyAncestors + 1 (17) as entries are added. SetConditionForPolicyAncestor now calls boundPolicyAncestors, which evicts the lowest-priority ancestor (same ordering as TruncatePolicyAncestors) once the list exceeds the soft cap. Each insert becomes O(1) and the total O(N).

The single slot above the CRD limit of 16 is intentional: it lets the existing post-processing TruncatePolicyAncestors still observe len > 16, sort, cut to 16, and stamp the Aggregated condition exactly as before.

Behavior change

For policy types that set the Overridden condition in a later pass (SecurityPolicy, BackendTrafficPolicy, EnvoyExtensionPolicy), a policy with more than 16 ancestors may show only Overridden (and lose the redundant Accepted condition) on a truncated ancestor. This happens because eviction runs as ancestors are added — before an ancestor's rank is final — so an ancestor evicted while only Accepted is re-added later carrying just Overridden.

  • Only affects statuses that exceed the CRD limit of 16 ancestors (already truncated).
  • The ancestor set and the meaningful Overridden signal are preserved.
  • Three *-status-conditions-truncated.out.yaml goldens are updated accordingly.

@netlify

netlify Bot commented Jul 22, 2026

Copy link
Copy Markdown

Deploy Preview for cerulean-figolla-1f9435 ready!

Name Link
🔨 Latest commit 9990a2f
🔍 Latest deploy log https://app.netlify.com/projects/cerulean-figolla-1f9435/deploys/6a61a7472f955200089e0e29
😎 Deploy Preview https://deploy-preview-9558--cerulean-figolla-1f9435.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@zhaohuabing
zhaohuabing force-pushed the perf/bound-policy-ancestors branch from 885ec06 to dae129f Compare July 22, 2026 14:26
@guydc

guydc commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Thanks @zhaohuabing! LGTM.
Tested it in a large-scale env and saw the desired improvement in translation time.

@zhaohuabing
zhaohuabing marked this pull request as ready for review July 23, 2026 01:38
@zhaohuabing
zhaohuabing requested a review from a team as a code owner July 23, 2026 01:38
@zhaohuabing
zhaohuabing force-pushed the perf/bound-policy-ancestors branch from de4e8bf to 25fd6de Compare July 23, 2026 01:38
@zhaohuabing zhaohuabing added this to the v1.9.0-rc.1 Release milestone Jul 23, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de4e8bfc4d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/gatewayapi/status/policy.go
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.61%. Comparing base (bae64fd) to head (9990a2f).

Files with missing lines Patch % Lines
internal/gatewayapi/status/policy.go 88.88% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9558      +/-   ##
==========================================
+ Coverage   75.59%   75.61%   +0.01%     
==========================================
  Files         252      252              
  Lines       41747    41758      +11     
==========================================
+ Hits        31559    31575      +16     
+ Misses       8061     8058       -3     
+ Partials     2127     2125       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zhaohuabing
zhaohuabing force-pushed the perf/bound-policy-ancestors branch 3 times, most recently from 061f805 to 63cf84f Compare July 23, 2026 04:27
@zhaohuabing

Copy link
Copy Markdown
Member Author

@codex review

@zhaohuabing
zhaohuabing force-pushed the perf/bound-policy-ancestors branch 2 times, most recently from e2e20e3 to d888858 Compare July 23, 2026 04:30
@zhaohuabing
zhaohuabing requested review from guydc and kkk777-7 July 23, 2026 04:31

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2e20e370c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/gatewayapi/status/policy.go Outdated
@zhaohuabing
zhaohuabing force-pushed the perf/bound-policy-ancestors branch from e73e9ee to c53ece6 Compare July 23, 2026 05:18
…tency

When many resources reference the same target, a policy's status.Ancestors list
could grow without bound during translation. SetConditionForPolicyAncestor scans
the existing ancestors on every call, so building a status with N ancestors was
O(N^2); for a BackendTLSPolicy shared by e.g. 10k EnvoyExtensionPolicies this
pushed translation time to ~1h.

Cap status.Ancestors at maxPolicyAncestors+1 (17) as entries are added:
SetConditionForPolicyAncestor evicts the lowest-priority ancestor once the list
exceeds the soft cap, keeping every insert O(1) and the total O(N). The single
slot above the CRD limit lets the post-processing TruncatePolicyAncestors still
detect overflow, cut to 16, and stamp the Aggregated condition.

Note: because eviction happens as ancestors are added, before an ancestor's rank
is necessarily final, a policy with more than 16 ancestors that later marks a
truncated ancestor Overridden shows only the Overridden condition on it - the
redundant Accepted condition is dropped. This affects only truncated statuses.

Fixes envoyproxy#9539

Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
@zhaohuabing
zhaohuabing force-pushed the perf/bound-policy-ancestors branch from c53ece6 to 6b01f5f Compare July 23, 2026 05:21
@zhaohuabing

Copy link
Copy Markdown
Member Author

@codex review

message: Policy has been accepted.
reason: Accepted
status: "True"
type: Accepted

@zhaohuabing zhaohuabing Jul 23, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This change is a trade-off, as described in the "behavior change" section of the PR description.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

Signed-off-by: Huabing (Robin) Zhao <zhaohuabing@gmail.com>
@kkk777-7

Copy link
Copy Markdown
Member

LGTM, thanks!
Nice optimization :)

@kkk777-7

Copy link
Copy Markdown
Member

/retest

@guydc guydc 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.

Thanks!

@zhaohuabing
zhaohuabing merged commit 6bf0e58 into envoyproxy:main Jul 24, 2026
63 of 65 checks passed
@zhaohuabing
zhaohuabing deleted the perf/bound-policy-ancestors branch July 24, 2026 01:17
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.

perf: translation latency increase when ancestorRefs list is large

3 participants