Optimize the suboptimal image locality algorithm

This commit is contained in:
Yongkun Anfernee Gui 2017-10-31 11:23:44 -07:00
parent 35e9784196
commit 8465625bd7

View File

@ -35,10 +35,8 @@ func ImageLocalityPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *scheduler
return schedulerapi.HostPriority{}, fmt.Errorf("node not found") return schedulerapi.HostPriority{}, fmt.Errorf("node not found")
} }
var sumSize int64 sumSize := totalImageSize(node, pod.Spec.Containers)
for i := range pod.Spec.Containers {
sumSize += checkContainerImageOnNode(node, &pod.Spec.Containers[i])
}
return schedulerapi.HostPriority{ return schedulerapi.HostPriority{
Host: node.Name, Host: node.Name,
Score: calculateScoreFromSize(sumSize), Score: calculateScoreFromSize(sumSize),
@ -49,31 +47,35 @@ func ImageLocalityPriorityMap(pod *v1.Pod, meta interface{}, nodeInfo *scheduler
// 1. Split image size range into 10 buckets. // 1. Split image size range into 10 buckets.
// 2. Decide the priority of a given sumSize based on which bucket it belongs to. // 2. Decide the priority of a given sumSize based on which bucket it belongs to.
func calculateScoreFromSize(sumSize int64) int { func calculateScoreFromSize(sumSize int64) int {
var score int
switch { switch {
case sumSize == 0 || sumSize < minImgSize: case sumSize == 0 || sumSize < minImgSize:
// score == 0 means none of the images required by this pod are present on this // 0 means none of the images required by this pod are present on this
// node or the total size of the images present is too small to be taken into further consideration. // node or the total size of the images present is too small to be taken into further consideration.
score = 0 return 0
// If existing images' total size is larger than max, just make it highest priority.
case sumSize >= maxImgSize: case sumSize >= maxImgSize:
score = schedulerapi.MaxPriority // If existing images' total size is larger than max, just make it highest priority.
default: return schedulerapi.MaxPriority
score = int((int64(schedulerapi.MaxPriority) * (sumSize - minImgSize) / (maxImgSize - minImgSize)) + 1)
}
// Return which bucket the given size belongs to
return score
} }
// checkContainerImageOnNode checks if a container image is present on a node and returns its size. return int((int64(schedulerapi.MaxPriority) * (sumSize - minImgSize) / (maxImgSize - minImgSize)) + 1)
func checkContainerImageOnNode(node *v1.Node, container *v1.Container) int64 { }
// totalImageSize returns the total image size of all the containers that are already on the node.
func totalImageSize(node *v1.Node, containers []v1.Container) int64 {
imageSizes := make(map[string]int64)
for _, image := range node.Status.Images { for _, image := range node.Status.Images {
for _, name := range image.Names { for _, name := range image.Names {
if container.Image == name { imageSizes[name] = image.SizeBytes
// Should return immediately.
return image.SizeBytes
} }
} }
var total int64
for _, container := range containers {
if size, ok := imageSizes[container.Image]; ok {
total += size
} }
return 0 }
return total
} }