Skip to content

ROX-33758: Fix quadratic runtime in endpointsStore.addToHistory#19566

Merged
vikin91 merged 1 commit intomasterfrom
piotr/ROX-33758-addToHistory
Mar 24, 2026
Merged

ROX-33758: Fix quadratic runtime in endpointsStore.addToHistory#19566
vikin91 merged 1 commit intomasterfrom
piotr/ROX-33758-addToHistory

Conversation

@vikin91
Copy link
Contributor

@vikin91 vikin91 commented Mar 24, 2026

Description

Fix quadratic runtime in endpointsStore.addToHistory that caused elevated mutex hold times during Apply() in large clusters.

Problem: The legacy addToHistory iterates the entire reverseEndpointMap[deploymentID] (all M endpoints of a deployment) to rebuild the reverse-history map on every call. During purgeNoLock, this is invoked once per endpoint, resulting in O(M^2) total work. In clusters with many nodes and NodePort/LoadBalancer expansions, M grows large enough to cause noticeable event-processing latency.

Fix: Replace the full deployment-endpoint scan with a targeted O(T) insert that records only the specific endpoint being moved to history. During a full purge of M endpoints, total work drops from O(sum(T_i) + M^2) to O(sum(T_i) + M).

AI note: Implementation was partially AI-assisted; all code was reviewed, corrected, and benchmarked by the author.

User-facing documentation

Testing and quality

  • the change is production ready: the change is GA, or otherwise the functionality is gated by a feature flag
  • CI results are inspected

Automated testing

  • added unit tests
  • added e2e tests
  • added regression tests
  • added compatibility tests
  • modified existing tests

How I validated my change

  • Unit tests
  • Benchmark

Benchmark results (Apple M3 Pro) show constant-time behavior for the fast path regardless of endpoint count, vs linear growth for legacy:

Scenario Fast path (ns/op) Legacy (ns/op) Speedup
100 endpoints ~696 ~9,212 13x
1,000 endpoints ~650 ~115,167 177x
5,000 endpoints ~704 ~535,328 761x

Allocations drop proportionally (12 allocs/op vs 1k-5k allocs/op).

@vikin91
Copy link
Contributor Author

vikin91 commented Mar 24, 2026

This change is part of the following stack:

Change managed by git-spice.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In benchmarkSeedEndpointsStore, for i := range numEndpoints is invalid for an int and won't compile; switch to a standard counter loop (e.g., for i := 0; i < numEndpoints; i++ { ... }) to correctly populate the benchmark data.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `benchmarkSeedEndpointsStore`, `for i := range numEndpoints` is invalid for an `int` and won't compile; switch to a standard counter loop (e.g., `for i := 0; i < numEndpoints; i++ { ... }`) to correctly populate the benchmark data.

## Individual Comments

### Comment 1
<location path="sensor/common/clusterentities/store_endpoints_benchmark_test.go" line_range="17" />
<code_context>
+	epSet := set.NewSet[net.NumericEndpoint]()
+
+	var firstEndpoint net.NumericEndpoint
+	for i := range numEndpoints {
+		ep := buildEndpoint(fmt.Sprintf("10.%d.%d.%d", (i/65536)%256, (i/256)%256, i%256), 80)
+		if i == 0 {
</code_context>
<issue_to_address>
**issue (bug_risk):** The `for i := range numEndpoints` loop is invalid and will prevent the benchmark from compiling

`numEndpoints` is an `int`, so `for i := range numEndpoints {` will not compile and will break `go test ./...`. Use a counted loop instead, e.g. `for i := 0; i < numEndpoints; i++ { ... }`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@vikin91 vikin91 changed the base branch from piotr/ROX-33757-moveToHistory to master March 24, 2026 12:46
@vikin91 vikin91 force-pushed the piotr/ROX-33758-addToHistory branch from de03fd3 to 8eb0a35 Compare March 24, 2026 12:46
@vikin91 vikin91 requested review from a team as code owners March 24, 2026 12:46
@vikin91 vikin91 requested review from vladbologa and removed request for a team and vladbologa March 24, 2026 12:46
@vikin91
Copy link
Contributor Author

vikin91 commented Mar 24, 2026

Sorry for the review request spam - I was changing base branch automatically and I didn't mean to ping you all.

@rhacs-bot
Copy link
Contributor

rhacs-bot commented Mar 24, 2026

Images are ready for the commit at 8eb0a35.

To use with deploy scripts, first export MAIN_IMAGE_TAG=4.11.x-422-g8eb0a35414.

@vikin91
Copy link
Contributor Author

vikin91 commented Mar 24, 2026

/retest

@codecov
Copy link

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.27%. Comparing base (24592b6) to head (8eb0a35).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19566   +/-   ##
=======================================
  Coverage   49.26%   49.27%           
=======================================
  Files        2727     2727           
  Lines      205821   205819    -2     
=======================================
+ Hits       101396   101408   +12     
+ Misses      96889    96879   -10     
+ Partials     7536     7532    -4     
Flag Coverage Δ
go-unit-tests 49.27% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vikin91 vikin91 enabled auto-merge (squash) March 24, 2026 13:33
@vikin91 vikin91 added backport release-4.9 https://spaces.redhat.com/spaces/StackRox/pages/558727298 backport release-4.10 labels Mar 24, 2026
@vikin91 vikin91 merged commit bfd67f2 into master Mar 24, 2026
156 of 160 checks passed
@vikin91 vikin91 deleted the piotr/ROX-33758-addToHistory branch March 24, 2026 14:57
rhacs-bot pushed a commit that referenced this pull request Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review area/sensor backport release-4.9 https://spaces.redhat.com/spaces/StackRox/pages/558727298 backport release-4.10

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants