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
Refactor and optimize preferred (anti) pod affinity #85959
Conversation
7f2307b
to
bda0eb2
Compare
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ahg-g 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 |
@@ -143,6 +166,75 @@ func CalculateInterPodAffinityPriorityReduce(pod *v1.Pod, meta interface{}, shar | |||
return nil | |||
} | |||
|
|||
func (p *podAffinityPriorityMap) processExistingPod(existingPod *v1.Pod, existingPodNodeInfo *schedulernodeinfo.NodeInfo) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just moving the processPod to a separate function instead of embedding it. This help showing it better in profiling.
bda0eb2
to
1ce0733
Compare
/cc @alculquicondor |
} | ||
|
||
func (p *podAffinityPriorityMap) processTerm(term *v1.PodAffinityTerm, podDefiningAffinityTerm, podToCheck *v1.Pod, fixedNode *v1.Node, weight int64) error { | ||
namespaces := priorityutil.GetNamespacesFromPodAffinityTerm(podDefiningAffinityTerm, term) | ||
func getProcessedWeightedAffinityTerm(pod *v1.Pod, term *v1.PodAffinityTerm, weight int32) (*processedWeightedAffinityTerm, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newProcessedWeightedAffinityTerm
. And you could accept v1.WeightedPodAffinityTerm
as a parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't accept WeightedPodAffinityTerm as a parameter because of https://github.com/kubernetes/kubernetes/pull/85959/files#diff-ac123860716bbbb4ed0f26cc27062bb7R201
nodes: nodes, | ||
topologyScore: make(topologyPairToScore), | ||
} | ||
type processedWeightedAffinityTerm struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just name it weightedAffinityTerm
, to make it clear that it's our internal representation of the object v1.WeightedAffinityTerm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
pod *v1.Pod | ||
affinityTerms []*processedWeightedAffinityTerm | ||
antiAffinityTerms []*processedWeightedAffinityTerm | ||
sharedLister schedulerlisters.SharedLister |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this being used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
if err := p.processTerm(&term.PodAffinityTerm, podDefiningAffinityTerm, podToCheck, fixedNode, int64(term.Weight*int32(multiplier))); err != nil { | ||
func (p *podAffinityPriorityMap) processTerms(terms []*processedWeightedAffinityTerm, podDefiningAffinityTerm, podToCheck *v1.Pod, fixedNode *v1.Node, multiplier int) error { | ||
for _, term := range terms { | ||
if err := p.processTerm(term, podDefiningAffinityTerm, podToCheck, fixedNode, int64(term.weight*int32(multiplier))); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passing the multiplier seems more readable to me.
Can we rename to updateScoreWithTerms
or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passing the multiplier seems more readable to me.
done.
Can we rename to updateScoreWithTerms or something like that?
then we need to come up with a name for "processTerms" as well, let keep like this.
return processedTerms, nil | ||
} | ||
|
||
func (p *podAffinityPriorityMap) processTerm(term *processedWeightedAffinityTerm, podDefiningAffinityTerm, podToCheck *v1.Pod, fixedNode *v1.Node, weight int64) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
podDefiningAffinityTerm is unused :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, that is because of the refactoring.
return nil | ||
pm := podAffinityPriorityMap{ | ||
topologyScore: make(topologyPairToScore), | ||
pod: pod, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth storing this guy? I feel like it should just be passed as parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are computing this map from the perspective of this pod, so I think it is reasonable to have it. In fact passing it as a parameter means the functions of a specific podAffinityPriorityMap
instance may accept different pods, which is not true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so now we only have a single function relying on it, I removed it :)
9685623
to
36a0743
Compare
/lgtm /hold for rebase |
hardPodAffinityWeight int32 | ||
sync.Mutex | ||
} | ||
|
||
type processedWeightedAffinityTerm struct { | ||
type weightedAffinityTerm struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth adding a comment indicating that this the representation of v1.WeightedPodAffinityTerm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
@@ -292,14 +288,15 @@ func buildTopologyPairToScore( | |||
processNode := func(i int) { | |||
nodeInfo := allNodes[i] | |||
if nodeInfo.Node() != nil { | |||
// Unless the pod itself has affinity terms, We only need to process nodes hosting pods with affinity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are filtering pods, not nodes.
Unless the pod itself has affinity terms, we only need to process pods with affinity in the node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, fixed.
36a0743
to
7b8db19
Compare
7b8db19
to
53be26e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/hold
for rebase
squashed.
/hold cancel /lgtm |
/retest Review the full test history for this PR. Silence the bot with an |
@ahg-g: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. 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. |
/retest |
What type of PR is this?
/kind feature
What this PR does / why we need it:
Optimize preferred (anti) pod affinity by removing redundant creation of Selectors during metadata calculation, and eliminates unnecessary and excessive nodeinfo map lookups. Gain is roughly 25% on 5k clusters.
before
after
Does this PR introduce a user-facing change?:
/cc @Huang-Wei