Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPBUGS-26498: Make ingressConditionsEqual more efficient #588

Merged

Conversation

gcs278
Copy link
Contributor

@gcs278 gcs278 commented Apr 29, 2024

We found that the router's contention tracker was slower after updates were made to support the UnservableInFutureVersions condition. This lag sometimes causes routers to update the route status before the contention tracker detected an update, leading to the per-route contention logic not activating.

In such scenarios, the maxContentions logic eventually activates, temporarily preventing the router from making any updates to the routes. This commit improves the efficiency of the ingressConditionsEqual function to help mitigate this type of issues.

Related slack thread.

Current PR Benchmark Results (Nested Loops):

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...] 
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         		1000000000	         4.936 ns/op	       0 B/op	       0 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	1000000000	         2.449 ns/op	       0 B/op	       0 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	433452776	        27.60 ns/op	       0 B/op	       0 allocs/op
PASS

Using sort.Slice Benchmark Results (Previous PR Revision b1bea4d):

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...]
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         		88920192	       121.0 ns/op	      48 B/op	       2 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	1000000000	         2.352 ns/op	       0 B/op	       0 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	30747393	       379.5 ns/op	     304 B/op	       6 allocs/op
PASS

Post-#555 Benchmark Results (Not this PR):

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...]
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         	  	  226267	     52844 ns/op	    7878 B/op	     251 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	   84859	    140895 ns/op	   18302 B/op	     674 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	   68632	    174946 ns/op	   23050 B/op	     863 allocs/op
PASS

Pre-#555 Benchmark Results:

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...]
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         	 	 7699280	      1549 ns/op	    1008 B/op	      19 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	 2992860	      3983 ns/op	    2336 B/op	      35 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	 2984841	      4028 ns/op	    2336 B/op	      35 allocs/op
PASS

Also, I have included some trivial logging improvements that will help debugging racy conditions.

Add missing key to log statement and log when a worker becomes elected
to leader or demoted to a follower for better clarity in router logs.
@openshift-ci-robot openshift-ci-robot added jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. labels Apr 29, 2024
@openshift-ci-robot
Copy link
Contributor

@gcs278: This pull request references Jira Issue OCPBUGS-26498, which is valid.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.16.0) matches configured target version for branch (4.16.0)
  • bug is in the state POST, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @ShudiLi

The bug has been updated to refer to the pull request using the external bug tracker.

In response to this:

We found that the router's contention tracker was slower after updates were made to support the UnservableInFutureVersions condition. This lag sometimes causes routers to update the route status before the contention tracker detected an update, leading to the per-route contention logic not activating.

In such scenarios, the maxContentions logic eventually activates, temporarily preventing the router from making any updates to the routes. This commit improves the efficiency of the ingressConditionsEqual function to help mitigate this type of issues.

Also, I have included some trivial logging improvements that will help debugging racy conditions.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

Introduces verbose (level 10) logging to trace plugin chain execution.
A bug was identified where the plugin chain halted unexpectedly, and
logging the plugin chain steps aids in debugging.
Add verbose logging to help indicate the router is attempting to update
a route status. This is helpful for debugging racy router status logic.
@gcs278 gcs278 force-pushed the efficient-contention-comparison branch from 27cfb9a to b1bea4d Compare April 29, 2024 22:50
Comment on lines 265 to 267
// Sort both slices by Type to ensure they are in the same order for comparison.
sort.Slice(a, func(i, j int) bool { return a[i].Type < a[j].Type })
sort.Slice(b, func(i, j int) bool { return b[i].Type < b[j].Type })
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you done benchmarks on this code? sort.Slice uses reflection, albeit the lighter-weight reflectlite rather than the standard reflect package, and even without that, the underlying quicksort algorithm implementation is probably slower than a nested loop for the expected number of items (namely 0 or 1 in the usual case, 2 in the exceptional case). If benchmarks show that sort.Slice is fast enough, though, then it doesn't really matter. (If you do write a benchmark, consider using testing.B and adding the benchmark to pkg/router/controller/status_test.go.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I need to back this up with some data. I added a benchmark function (first time I've ever done that) and provided various results in the PR description.

The benchmark with sort.Slice results show it's ~500x faster (I'm comparing ns/op). I stopped there.

BUT I just tried out nested loops, and I see it's ~5000-7000x faster. You are right, where 2 conditions are our max, nested loops are faster than sort.Slice..

@gcs278 gcs278 force-pushed the efficient-contention-comparison branch from b1bea4d to c97331f Compare April 30, 2024 04:09
@Miciah
Copy link
Contributor

Miciah commented Apr 30, 2024

/approve
/lgtm
/hold
for @frobware to review.

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 30, 2024
@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Apr 30, 2024
Copy link
Contributor

openshift-ci bot commented Apr 30, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Miciah

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 30, 2024
@frobware
Copy link
Contributor

A suggestion for improved performance:

% git diff
diff --git a/pkg/router/controller/contention.go b/pkg/router/controller/contention.go
index 0d866406..0f159bf7 100644
--- a/pkg/router/controller/contention.go
+++ b/pkg/router/controller/contention.go
@@ -263,10 +263,10 @@ func ingressConditionsEqual(a, b []routev1.RouteIngressCondition) bool {

        // Compare each condition in a with every condition in b.
        // Given the current max of only two conditions, nested loops are more efficient than sorting.
-       for _, condA := range a {
+       for i := 0; i < len(a); i++ {
                matchFound := false
-               for _, condB := range b {
-                       if conditionsEqual(condA, condB) {
+               for j := 0; j < len(b); j++ {
+                       if conditionsEqual(&a[i], &b[j]) {
                                matchFound = true
                                break
                        }
@@ -280,7 +280,7 @@ func ingressConditionsEqual(a, b []routev1.RouteIngressCondition) bool {
 }

 // conditionsEqual compares two RouteIngressConditions, ignoring LastTransitionTime.
-func conditionsEqual(a, b routev1.RouteIngressCondition) bool {
+func conditionsEqual(a, b *routev1.RouteIngressCondition) bool {
        return a.Type == b.Type &&
                a.Status == b.Status &&
                a.Reason == b.Reason &&

Benchmarking shows:

% go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: 11th Gen Intel(R) Core(TM) i9-11900 @ 2.50GHz
Benchmark_ingressConditionsEqual/single-16              1000000000               3.422 ns/op
Benchmark_ingressConditionsEqual/mismatch_length-16             1000000000               1.487 ns/op
Benchmark_ingressConditionsEqual/double-16                      880706917               13.74 ns/op

},
},
}
for _, tc := range testCases {
Copy link
Contributor

Choose a reason for hiding this comment

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

Although there's very minimal setup here you could call b.ResetTimer() to take any current or future setup costs out of the numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

We found that the router's contention tracker was slower after updates
were made to support the UnservableInFutureVersions condition. This
lag sometimes causes routers to update the route status before the
contention tracker detected an update, leading to the per-route
contention logic not activating.

In such scenarios, the maxContentions logic eventually activates, temporarily
preventing the router from making any updates to the routes. This commit
improves the efficiency of the ingressConditionsEqual function to help
mitigate this type of issues.
@gcs278 gcs278 force-pushed the efficient-contention-comparison branch from c97331f to 44f217f Compare April 30, 2024 13:39
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Apr 30, 2024
@gcs278
Copy link
Contributor Author

gcs278 commented Apr 30, 2024

A suggestion for improved performance:

% git diff
diff --git a/pkg/router/controller/contention.go b/pkg/router/controller/contention.go
index 0d866406..0f159bf7 100644
--- a/pkg/router/controller/contention.go
+++ b/pkg/router/controller/contention.go
@@ -263,10 +263,10 @@ func ingressConditionsEqual(a, b []routev1.RouteIngressCondition) bool {

        // Compare each condition in a with every condition in b.
        // Given the current max of only two conditions, nested loops are more efficient than sorting.
-       for _, condA := range a {
+       for i := 0; i < len(a); i++ {
                matchFound := false
-               for _, condB := range b {
-                       if conditionsEqual(condA, condB) {
+               for j := 0; j < len(b); j++ {
+                       if conditionsEqual(&a[i], &b[j]) {
                                matchFound = true
                                break
                        }
@@ -280,7 +280,7 @@ func ingressConditionsEqual(a, b []routev1.RouteIngressCondition) bool {
 }

 // conditionsEqual compares two RouteIngressConditions, ignoring LastTransitionTime.
-func conditionsEqual(a, b routev1.RouteIngressCondition) bool {
+func conditionsEqual(a, b *routev1.RouteIngressCondition) bool {
        return a.Type == b.Type &&
                a.Status == b.Status &&
                a.Reason == b.Reason &&

Benchmarking shows:

% go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: 11th Gen Intel(R) Core(TM) i9-11900 @ 2.50GHz
Benchmark_ingressConditionsEqual/single-16              1000000000               3.422 ns/op
Benchmark_ingressConditionsEqual/mismatch_length-16             1000000000               1.487 ns/op
Benchmark_ingressConditionsEqual/double-16                      880706917               13.74 ns/op

Done, thanks @frobware

@frobware
Copy link
Contributor

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Apr 30, 2024
@gcs278
Copy link
Contributor Author

gcs278 commented Apr 30, 2024

/hold cancel

@openshift-ci openshift-ci bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 30, 2024
@openshift-ci-robot
Copy link
Contributor

/retest-required

Remaining retests: 0 against base HEAD 8762aa4 and 2 for PR HEAD 44f217f in total

@gcs278
Copy link
Contributor Author

gcs278 commented Apr 30, 2024

I see something funny in my origin E2E test, just going to hold out of precaution until I understand.
/hold

@openshift-ci openshift-ci bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 30, 2024
@gcs278
Copy link
Contributor Author

gcs278 commented Apr 30, 2024

Never mind, I mistakenly was testing with the wrong router image and thought there was a regression...
/hold cancel

@openshift-ci openshift-ci bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 30, 2024
Copy link
Contributor

openshift-ci bot commented Apr 30, 2024

@gcs278: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-metal-ipi-ovn-ipv6 44f217f link false /test e2e-metal-ipi-ovn-ipv6

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@openshift-merge-bot openshift-merge-bot bot merged commit b6f7c63 into openshift:master Apr 30, 2024
7 of 8 checks passed
@openshift-ci-robot
Copy link
Contributor

@gcs278: Jira Issue OCPBUGS-26498: Some pull requests linked via external trackers have merged:

The following pull requests linked via external trackers have not merged:

These pull request must merge or be unlinked from the Jira bug in order for it to move to the next state. Once unlinked, request a bug refresh with /jira refresh.

Jira Issue OCPBUGS-26498 has not been moved to the MODIFIED state.

In response to this:

We found that the router's contention tracker was slower after updates were made to support the UnservableInFutureVersions condition. This lag sometimes causes routers to update the route status before the contention tracker detected an update, leading to the per-route contention logic not activating.

In such scenarios, the maxContentions logic eventually activates, temporarily preventing the router from making any updates to the routes. This commit improves the efficiency of the ingressConditionsEqual function to help mitigate this type of issues.

Related slack thread.

Current PR Benchmark Results (Nested Loops):

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...] 
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         		1000000000	         4.936 ns/op	       0 B/op	       0 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	1000000000	         2.449 ns/op	       0 B/op	       0 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	433452776	        27.60 ns/op	       0 B/op	       0 allocs/op
PASS

Using sort.Slice Benchmark Results (Previous PR Revision b1bea4d):

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...]
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         		88920192	       121.0 ns/op	      48 B/op	       2 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	1000000000	         2.352 ns/op	       0 B/op	       0 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	30747393	       379.5 ns/op	     304 B/op	       6 allocs/op
PASS

Post-#555 Benchmark Results (Not this PR):

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...]
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         	  	  226267	     52844 ns/op	    7878 B/op	     251 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	   84859	    140895 ns/op	   18302 B/op	     674 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	   68632	    174946 ns/op	   23050 B/op	     863 allocs/op
PASS

Pre-#555 Benchmark Results:

$ go test -bench=. -run=Benchmark_ingressConditionsEqual ./... -benchtime=10s -benchmem
[...]
goos: linux
goarch: amd64
pkg: github.com/openshift/router/pkg/router/controller
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ingressConditionsEqual/single-12         	 	 7699280	      1549 ns/op	    1008 B/op	      19 allocs/op
Benchmark_ingressConditionsEqual/mismatch_length-12         	 2992860	      3983 ns/op	    2336 B/op	      35 allocs/op
Benchmark_ingressConditionsEqual/double-12                  	 2984841	      4028 ns/op	    2336 B/op	      35 allocs/op
PASS

Also, I have included some trivial logging improvements that will help debugging racy conditions.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-bot
Copy link
Contributor

[ART PR BUILD NOTIFIER]

This PR has been included in build ose-haproxy-router-base-container-v4.17.0-202404302014.p0.gb6f7c63.assembly.stream.el9 for distgit ose-haproxy-router-base.
All builds following this will include this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants