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

Use GroupMemberSet.Merge to reduce CPU usage and memory footprint #2467

Merged
merged 1 commit into from Jul 30, 2021

Conversation

tnqn
Copy link
Member

@tnqn tnqn commented Jul 26, 2021

When getting the union of multiple AddressGroups or AppliedToGroups,
agent used the GroupMemberSet.Union method, which always creates an
intermediate set and copies the original items when merging another
set. This wasted significant time and memory when the groups had
massive members. This patch adds the GroupMemberSet.Merge method which
avoids the above overhead.

benchmark comparison when merging two groups with 10K members and 100
members:

name                            old time/op    new time/op    delta
RuleCacheUnionAddressGroups-48    4.27ms ±15%    2.15ms ±15%  -49.72%  (p=0.008 n=5+5)

name                            old alloc/op   new alloc/op   delta
RuleCacheUnionAddressGroups-48    1.95MB ± 0%    0.97MB ± 0%  -50.00%  (p=0.008 n=5+5)

name                            old allocs/op  new allocs/op  delta
RuleCacheUnionAddressGroups-48       565 ± 0%       282 ± 0%  -50.16%  (p=0.000 n=4+5)

Signed-off-by: Quan Tian qtian@vmware.com

For #2457

@codecov-commenter
Copy link

codecov-commenter commented Jul 26, 2021

Codecov Report

Merging #2467 (5856d64) into main (cdc8453) will increase coverage by 5.14%.
The diff coverage is 68.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2467      +/-   ##
==========================================
+ Coverage   59.82%   64.97%   +5.14%     
==========================================
  Files         284      284              
  Lines       22168    26080    +3912     
==========================================
+ Hits        13263    16946    +3683     
- Misses       7483     7547      +64     
- Partials     1422     1587     +165     
Flag Coverage Δ
e2e-tests 55.89% <57.89%> (?)
kind-e2e-tests 47.02% <30.76%> (-0.06%) ⬇️
unit-tests 42.02% <55.55%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
pkg/apis/controlplane/v1beta1/sets.go 0.00% <0.00%> (ø)
pkg/controller/egress/store/egressgroup.go 2.29% <0.00%> (+2.29%) ⬆️
pkg/agent/controller/networkpolicy/cache.go 86.61% <100.00%> (-0.37%) ⬇️
pkg/apis/controlplane/sets.go 34.84% <100.00%> (+11.77%) ⬆️
pkg/apis/controlplane/v1beta2/sets.go 63.63% <100.00%> (-5.60%) ⬇️
...ntroller/networkpolicy/networkpolicy_controller.go 84.63% <100.00%> (+3.69%) ⬆️
...g/controller/networkpolicy/store/appliedtogroup.go 86.25% <100.00%> (-0.12%) ⬇️
pkg/controller/egress/ipallocator/allocator.go 67.82% <0.00%> (-15.16%) ⬇️
pkg/agent/agent_linux.go 85.71% <0.00%> (-14.29%) ⬇️
pkg/controller/networkpolicy/endpoint_querier.go 77.64% <0.00%> (-13.79%) ⬇️
... and 275 more

antoninbas
antoninbas previously approved these changes Jul 26, 2021
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

LGTM

// s1.Merge(s2) = {a1, a2, a3, a4, a5}
// s1 = {a1, a2, a3, a4, a5}
//
// It supersedes s1.Union(s2) when constructing a new set is not the intention.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// It supersedes s1.Union(s2) when constructing a new set is not the intention.
// It should be used instead of s1.Union(s2) when constructing a new set is not required.

same below

Copy link
Contributor

@Dyanngg Dyanngg left a comment

Choose a reason for hiding this comment

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

Why do we not simply re-write Union? Is there any usage where the original GroupMemberSets must be perserved?

@antoninbas
Copy link
Contributor

Why do we not simply re-write Union? Is there any usage where the original GroupMemberSets must be perserved?

Union and merge mean different things IMO, so we shouldn't just change the implementation of union

@tnqn
Copy link
Member Author

tnqn commented Jul 27, 2021

Why do we not simply re-write Union? Is there any usage where the original GroupMemberSets must be perserved?

Union and merge mean different things IMO, so we shouldn't just change the implementation of union

Yes, also Union is the original method of K8s util package. It's widely used in the scenario where constructing a new set is required.

@tnqn
Copy link
Member Author

tnqn commented Jul 27, 2021

/test-all

When getting the union of multiple AddressGroups or AppliedToGroups,
agent used the GroupMemberSet.Union method, which always creates an
intermediate set and copies the original items when merging another
set. This wasted significant time and memory when the groups had
massive members. This patch adds the GroupMemberSet.Merge method which
avoids the above overhead.

benchmark comparison when merging two groups with 10K members and 100
members:

name                            old time/op    new time/op    delta
RuleCacheUnionAddressGroups-48    4.27ms ±15%    2.15ms ±15%  -49.72%  (p=0.008 n=5+5)

name                            old alloc/op   new alloc/op   delta
RuleCacheUnionAddressGroups-48    1.95MB ± 0%    0.97MB ± 0%  -50.00%  (p=0.008 n=5+5)

name                            old allocs/op  new allocs/op  delta
RuleCacheUnionAddressGroups-48       565 ± 0%       282 ± 0%  -50.16%  (p=0.000 n=4+5)

Signed-off-by: Quan Tian <qtian@vmware.com>
@Dyanngg
Copy link
Contributor

Dyanngg commented Jul 27, 2021

Yes, also Union is the original method of K8s util package. It's widely used in the scenario where constructing a new set is required.

Thanks for the explanations. LGTM

@tnqn
Copy link
Member Author

tnqn commented Jul 29, 2021

/test-integration
/test-conformance
/test-e2e
/test-networkpolicy

@tnqn tnqn merged commit 1a84b66 into antrea-io:main Jul 30, 2021
@tnqn tnqn deleted the improve-union-group branch July 30, 2021 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants