# 1. 设计基础

## 1.4 weight

map/reduce阶段都是通过算法计算，如果我们要进行自定义的调整，针对单个算法，我们可以调整其在预选流程中的权重，从而进行定制自己的预选流程

# 2. 源码分析

## 2.1 无锁计算结果保存

// 在计算的时候，会传入nodes []*v1.Node的数组，存储所有的节点，节点索引主要是指的该部分
results := make([]schedulerapi.HostPriorityList, len(priorityConfigs), len(priorityConfigs))


## 2.2 基于节点索引的Map计算

	workqueue.ParallelizeUntil(context.TODO(), 16, len(nodes), func(index int) {
// 根据节点和配置的算法进行计算
nodeInfo := nodeNameToInfo[nodes[index].Name]
// 获取算法的索引
for i := range priorityConfigs {
if priorityConfigs[i].Function != nil {
continue
}

var err error

// 通过节点索引，来进行针对单个node的计算结果的保存
results[i][index], err = priorityConfigs[i].Map(pod, meta, nodeInfo)
if err != nil {
appendError(err)
results[i][index].Host = nodes[index].Name
}
}
})


## 2.3 基于算法索引的Reduce计算

	// 计算策略的分值
for i := range priorityConfigs {
if priorityConfigs[i].Reduce == nil {
continue
}
go func(index int) {
defer wg.Done()
if err := priorityConfigs[index].Reduce(pod, meta, nodeNameToInfo, results[index]); err != nil {
appendError(err)
}
if klog.V(10) {
for _, hostPriority := range results[index] {
klog.Infof("%v -> %v: %v, Score: (%d)", util.GetPodFullName(pod), hostPriority.Host, priorityConfigs[index].Name, hostPriority.Score)
}
}
}(i)
}
// Wait for all computations to be finished.
wg.Wait()


## 2.4 优先级打分结果统计

	// Summarize all scores.
result := make(schedulerapi.HostPriorityList, 0, len(nodes))

for i := range nodes {
result = append(result, schedulerapi.HostPriority{Host: nodes[i].Name, Score: 0})
// 便利所有的算法配置
for j := range priorityConfigs {
result[i].Score += results[j][i].Score * priorityConfigs[j].Weight
}

for j := range scoresMap {
result[i].Score += scoresMap[j][i].Score
}
}


## 2.5 根据优先级随机筛选host

func (g *genericScheduler) selectHost(priorityList schedulerapi.HostPriorityList) (string, error) {
if len(priorityList) == 0 {
return "", fmt.Errorf("empty priorityList")
}
maxScore := priorityList[0].Score
selected := priorityList[0].Host
cntOfMaxScore := 1
for _, hp := range priorityList[1:] {
if hp.Score > maxScore {
maxScore = hp.Score
selected = hp.Host
cntOfMaxScore = 1
} else if hp.Score == maxScore {
cntOfMaxScore++
if rand.Intn(cntOfMaxScore) == 0 {
// Replace the candidate with probability of 1/cntOfMaxScore
selected = hp.Host
}
}
}
return selected, nil
}


# 3. 设计总结

posted @ 2020-01-17 16:43  8小时  阅读(...)  评论(...编辑  收藏