Commit f6659e45 authored by Ahmad Diaa's avatar Ahmad Diaa

further enhancements removing matchingTerms from metadata

parent 3fb6912d
...@@ -38,6 +38,12 @@ type PredicateMetadataFactory struct { ...@@ -38,6 +38,12 @@ type PredicateMetadataFactory struct {
podLister algorithm.PodLister podLister algorithm.PodLister
} }
// AntiAffinityTerm's topology key value used in predicate metadata
type topologyPair struct {
key string
value string
}
// Note that predicateMetadata and matchingPodAntiAffinityTerm need to be declared in the same file // Note that predicateMetadata and matchingPodAntiAffinityTerm need to be declared in the same file
// due to the way declarations are processed in predicate declaration unit tests. // due to the way declarations are processed in predicate declaration unit tests.
type matchingPodAntiAffinityTerm struct { type matchingPodAntiAffinityTerm struct {
...@@ -52,11 +58,11 @@ type predicateMetadata struct { ...@@ -52,11 +58,11 @@ type predicateMetadata struct {
podBestEffort bool podBestEffort bool
podRequest *schedulercache.Resource podRequest *schedulercache.Resource
podPorts []*v1.ContainerPort podPorts []*v1.ContainerPort
//key is a pod full name with the anti-affinity rules. // A map of antiffinity terms' topology pairs to the pods'
matchingAntiAffinityTerms map[string][]matchingPodAntiAffinityTerm
// A map of antiffinity terms' topology ke values to the pods' names
// that can potentially match the affinity rules of the pod // that can potentially match the affinity rules of the pod
topologyValueToAntiAffinityPods map[string][]string topologyPairToAntiAffinityPods map[topologyPair][]*v1.Pod
// Reverse map for topologyPairToAntiAffinityPods to reduce deletion time
antiAffinityPodToTopologyPairs map[string][]topologyPair
// A map of node name to a list of Pods on the node that can potentially match // A map of node name to a list of Pods on the node that can potentially match
// the affinity rules of the "pod". // the affinity rules of the "pod".
nodeNameToMatchingAffinityPods map[string][]*v1.Pod nodeNameToMatchingAffinityPods map[string][]*v1.Pod
...@@ -116,7 +122,7 @@ func (pfactory *PredicateMetadataFactory) GetMetadata(pod *v1.Pod, nodeNameToInf ...@@ -116,7 +122,7 @@ func (pfactory *PredicateMetadataFactory) GetMetadata(pod *v1.Pod, nodeNameToInf
if pod == nil { if pod == nil {
return nil return nil
} }
matchingTerms, topologyValues, err := getMatchingAntiAffinityTerms(pod, nodeNameToInfoMap) podToTopolgyPair, topologyPairToPods, err := getMatchingTopologyPairs(pod, nodeNameToInfoMap)
if err != nil { if err != nil {
return nil return nil
} }
...@@ -130,10 +136,10 @@ func (pfactory *PredicateMetadataFactory) GetMetadata(pod *v1.Pod, nodeNameToInf ...@@ -130,10 +136,10 @@ func (pfactory *PredicateMetadataFactory) GetMetadata(pod *v1.Pod, nodeNameToInf
podBestEffort: isPodBestEffort(pod), podBestEffort: isPodBestEffort(pod),
podRequest: GetResourceRequest(pod), podRequest: GetResourceRequest(pod),
podPorts: schedutil.GetContainerPorts(pod), podPorts: schedutil.GetContainerPorts(pod),
matchingAntiAffinityTerms: matchingTerms,
nodeNameToMatchingAffinityPods: affinityPods, nodeNameToMatchingAffinityPods: affinityPods,
nodeNameToMatchingAntiAffinityPods: antiAffinityPods, nodeNameToMatchingAntiAffinityPods: antiAffinityPods,
topologyValueToAntiAffinityPods: topologyValues, topologyPairToAntiAffinityPods: topologyPairToPods,
antiAffinityPodToTopologyPairs: podToTopolgyPair,
} }
for predicateName, precomputeFunc := range predicateMetadataProducers { for predicateName, precomputeFunc := range predicateMetadataProducers {
glog.V(10).Infof("Precompute: %v", predicateName) glog.V(10).Infof("Precompute: %v", predicateName)
...@@ -149,23 +155,22 @@ func (meta *predicateMetadata) RemovePod(deletedPod *v1.Pod) error { ...@@ -149,23 +155,22 @@ func (meta *predicateMetadata) RemovePod(deletedPod *v1.Pod) error {
if deletedPodFullName == schedutil.GetPodFullName(meta.pod) { if deletedPodFullName == schedutil.GetPodFullName(meta.pod) {
return fmt.Errorf("deletedPod and meta.pod must not be the same") return fmt.Errorf("deletedPod and meta.pod must not be the same")
} }
// Delete pod from matching topology pairs map
// Delete pod from matching topology values map for _, pair := range meta.antiAffinityPodToTopologyPairs[deletedPodFullName] {
for _, term := range meta.matchingAntiAffinityTerms[deletedPodFullName] { for index, pod := range meta.topologyPairToAntiAffinityPods[pair] {
if topologyValue, ok := term.node.Labels[term.term.TopologyKey]; ok { if schedutil.GetPodFullName(pod) == deletedPodFullName {
for index, podName := range meta.topologyValueToAntiAffinityPods[topologyValue] { podsList := meta.topologyPairToAntiAffinityPods[pair]
if podName == deletedPodFullName { podsList[index] = podsList[len(podsList)-1]
podsList := meta.topologyValueToAntiAffinityPods[topologyValue] if len(podsList) <= 1 {
meta.topologyValueToAntiAffinityPods[topologyValue] = append(podsList[:index], delete(meta.topologyPairToAntiAffinityPods, pair)
podsList[index+1:]...) } else {
break meta.topologyPairToAntiAffinityPods[pair] = podsList[:len(podsList)-1]
} }
break
} }
} }
} }
delete(meta.antiAffinityPodToTopologyPairs, deletedPodFullName)
// Delete any anti-affinity rule from the deletedPod.
delete(meta.matchingAntiAffinityTerms, deletedPodFullName)
// Delete pod from the matching affinity or anti-affinity pods if exists. // Delete pod from the matching affinity or anti-affinity pods if exists.
affinity := meta.pod.Spec.Affinity affinity := meta.pod.Spec.Affinity
podNodeName := deletedPod.Spec.NodeName podNodeName := deletedPod.Spec.NodeName
...@@ -222,21 +227,16 @@ func (meta *predicateMetadata) AddPod(addedPod *v1.Pod, nodeInfo *schedulercache ...@@ -222,21 +227,16 @@ func (meta *predicateMetadata) AddPod(addedPod *v1.Pod, nodeInfo *schedulercache
return fmt.Errorf("invalid node in nodeInfo") return fmt.Errorf("invalid node in nodeInfo")
} }
// Add matching anti-affinity terms of the addedPod to the map. // Add matching anti-affinity terms of the addedPod to the map.
podMatchingTerms, podTopologyValuesToMatchingPods, err := getMatchingAntiAffinityTermsOfExistingPod(meta.pod, addedPod, nodeInfo.Node()) matchingPodToTopologyPairs, podTopologyPairToMatchingPods, err := getMatchingTopologyPairsOfExistingPod(meta.pod, addedPod, nodeInfo.Node())
if err != nil { if err != nil {
return err return err
} }
if len(podMatchingTerms) > 0 { if len(matchingPodToTopologyPairs) > 0 {
existingTerms, found := meta.matchingAntiAffinityTerms[addedPodFullName] for pair, pods := range podTopologyPairToMatchingPods {
if found { meta.topologyPairToAntiAffinityPods[pair] = append(meta.topologyPairToAntiAffinityPods[pair], pods...)
meta.matchingAntiAffinityTerms[addedPodFullName] = append(existingTerms,
podMatchingTerms...)
} else {
meta.matchingAntiAffinityTerms[addedPodFullName] = podMatchingTerms
} }
for pod, pairs := range matchingPodToTopologyPairs {
for topologyValue, pods := range podTopologyValuesToMatchingPods { meta.antiAffinityPodToTopologyPairs[pod] = append(meta.antiAffinityPodToTopologyPairs[pod], pairs...)
meta.topologyValueToAntiAffinityPods[topologyValue] = append(meta.topologyValueToAntiAffinityPods[topologyValue], pods...)
} }
} }
// Add the pod to nodeNameToMatchingAffinityPods and nodeNameToMatchingAntiAffinityPods if needed. // Add the pod to nodeNameToMatchingAffinityPods and nodeNameToMatchingAntiAffinityPods if needed.
...@@ -291,10 +291,6 @@ func (meta *predicateMetadata) ShallowCopy() algorithm.PredicateMetadata { ...@@ -291,10 +291,6 @@ func (meta *predicateMetadata) ShallowCopy() algorithm.PredicateMetadata {
ignoredExtendedResources: meta.ignoredExtendedResources, ignoredExtendedResources: meta.ignoredExtendedResources,
} }
newPredMeta.podPorts = append([]*v1.ContainerPort(nil), meta.podPorts...) newPredMeta.podPorts = append([]*v1.ContainerPort(nil), meta.podPorts...)
newPredMeta.matchingAntiAffinityTerms = map[string][]matchingPodAntiAffinityTerm{}
for k, v := range meta.matchingAntiAffinityTerms {
newPredMeta.matchingAntiAffinityTerms[k] = append([]matchingPodAntiAffinityTerm(nil), v...)
}
newPredMeta.nodeNameToMatchingAffinityPods = make(map[string][]*v1.Pod) newPredMeta.nodeNameToMatchingAffinityPods = make(map[string][]*v1.Pod)
for k, v := range meta.nodeNameToMatchingAffinityPods { for k, v := range meta.nodeNameToMatchingAffinityPods {
newPredMeta.nodeNameToMatchingAffinityPods[k] = append([]*v1.Pod(nil), v...) newPredMeta.nodeNameToMatchingAffinityPods[k] = append([]*v1.Pod(nil), v...)
...@@ -303,15 +299,18 @@ func (meta *predicateMetadata) ShallowCopy() algorithm.PredicateMetadata { ...@@ -303,15 +299,18 @@ func (meta *predicateMetadata) ShallowCopy() algorithm.PredicateMetadata {
for k, v := range meta.nodeNameToMatchingAntiAffinityPods { for k, v := range meta.nodeNameToMatchingAntiAffinityPods {
newPredMeta.nodeNameToMatchingAntiAffinityPods[k] = append([]*v1.Pod(nil), v...) newPredMeta.nodeNameToMatchingAntiAffinityPods[k] = append([]*v1.Pod(nil), v...)
} }
newPredMeta.topologyValueToAntiAffinityPods = make(map[string][]string) newPredMeta.topologyPairToAntiAffinityPods = make(map[topologyPair][]*v1.Pod)
for k, v := range meta.topologyValueToAntiAffinityPods { for k, v := range meta.topologyPairToAntiAffinityPods {
newPredMeta.topologyValueToAntiAffinityPods[k] = append([]string(nil), v...) newPredMeta.topologyPairToAntiAffinityPods[k] = append([]*v1.Pod(nil), v...)
}
newPredMeta.antiAffinityPodToTopologyPairs = make(map[string][]topologyPair)
for k, v := range meta.antiAffinityPodToTopologyPairs {
newPredMeta.antiAffinityPodToTopologyPairs[k] = append([]topologyPair(nil), v...)
} }
newPredMeta.serviceAffinityMatchingPodServices = append([]*v1.Service(nil), newPredMeta.serviceAffinityMatchingPodServices = append([]*v1.Service(nil),
meta.serviceAffinityMatchingPodServices...) meta.serviceAffinityMatchingPodServices...)
newPredMeta.serviceAffinityMatchingPodList = append([]*v1.Pod(nil), newPredMeta.serviceAffinityMatchingPodList = append([]*v1.Pod(nil),
meta.serviceAffinityMatchingPodList...) meta.serviceAffinityMatchingPodList...)
return (algorithm.PredicateMetadata)(newPredMeta) return (algorithm.PredicateMetadata)(newPredMeta)
} }
......
...@@ -113,11 +113,6 @@ func predicateMetadataEquivalent(meta1, meta2 *predicateMetadata) error { ...@@ -113,11 +113,6 @@ func predicateMetadataEquivalent(meta1, meta2 *predicateMetadata) error {
for !reflect.DeepEqual(meta1.podPorts, meta2.podPorts) { for !reflect.DeepEqual(meta1.podPorts, meta2.podPorts) {
return fmt.Errorf("podPorts are not equal") return fmt.Errorf("podPorts are not equal")
} }
sortAntiAffinityTerms(meta1.matchingAntiAffinityTerms)
sortAntiAffinityTerms(meta2.matchingAntiAffinityTerms)
if !reflect.DeepEqual(meta1.matchingAntiAffinityTerms, meta2.matchingAntiAffinityTerms) {
return fmt.Errorf("matchingAntiAffinityTerms are not euqal")
}
sortNodePodMap(meta1.nodeNameToMatchingAffinityPods) sortNodePodMap(meta1.nodeNameToMatchingAffinityPods)
sortNodePodMap(meta2.nodeNameToMatchingAffinityPods) sortNodePodMap(meta2.nodeNameToMatchingAffinityPods)
if !reflect.DeepEqual(meta1.nodeNameToMatchingAffinityPods, meta2.nodeNameToMatchingAffinityPods) { if !reflect.DeepEqual(meta1.nodeNameToMatchingAffinityPods, meta2.nodeNameToMatchingAffinityPods) {
...@@ -465,19 +460,25 @@ func TestPredicateMetadata_ShallowCopy(t *testing.T) { ...@@ -465,19 +460,25 @@ func TestPredicateMetadata_ShallowCopy(t *testing.T) {
HostIP: "1.2.3.4", HostIP: "1.2.3.4",
}, },
}, },
matchingAntiAffinityTerms: map[string][]matchingPodAntiAffinityTerm{ topologyPairToAntiAffinityPods: map[topologyPair][]*v1.Pod{
"term1": { {key: "name", value: "machine1"}: {
{ &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "p2", Labels: selector1},
term: &v1.PodAffinityTerm{TopologyKey: "node"}, Spec: v1.PodSpec{NodeName: "nodeC"},
node: &v1.Node{ },
ObjectMeta: metav1.ObjectMeta{Name: "machine1"}, },
}, {key: "name", value: "machine2"}: {
&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "p1", Labels: selector1},
Spec: v1.PodSpec{NodeName: "nodeA"},
}, },
}, },
}, },
topologyValueToAntiAffinityPods: map[string][]string{ antiAffinityPodToTopologyPairs: map[string][]topologyPair{
"machine1": {"p1", "p2"}, "p2": {
"machine2": {"p3"}, topologyPair{key: "name", value: "machine1"},
},
"p1": {
topologyPair{key: "name", value: "machine2"},
},
}, },
nodeNameToMatchingAffinityPods: map[string][]*v1.Pod{ nodeNameToMatchingAffinityPods: map[string][]*v1.Pod{
"nodeA": { "nodeA": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment