Example #1
0
func AllocateMem(r CreateRequest) error {
	totalCPuNum := r.TotalCpuNum
	alreadyOccu := 0

	for {
		totalMem := getTotalFreemem()
		avgMem := float64(totalMem) / float64(totalCPuNum)
		newOccu := 0
		for ip, node := range Rres {
			theory := math.Ceil(float64(node.Node.MemInfo.Free) / avgMem)
			fact := util.PositiveNum(node.Docker_nr)
			if float64(fact) < theory {
				alreadyOccu += fact
				newOccu += fact
				delete(Rres, ip)
				for i := 0; i < len(node.Docker_nr); i++ {
					if node.Docker_nr[i] > 0 {
						node.Docker_nr[i] = 1
					} else {
						node.Docker_nr[i] = 0
					}
				}
				Ares[ip] = node
			}
		}

		if newOccu == 0 {
			//hand out accoring to theory
			for ip, node := range Rres {
				theory := math.Ceil(float64(node.Node.MemInfo.Free) / avgMem)
				for i := 0; i < len(node.Docker_nr) && theory > 0; i++ {
					if node.Docker_nr[i] > 0 {
						node.Docker_nr[i] = 1
					} else {
						node.Docker_nr[i] = 0
					}
					theory--
				}
				Ares[ip] = node
			}
			break
		} else if alreadyOccu >= r.TotalCpuNum { //all: fact > theory
			break
		} else {
			totalCPuNum -= newOccu
		}
	}

	if alreadyOccu < r.TotalCpuNum {
		return errors.New("It can provide so much cpu now!")
	}
	return nil
}
Example #2
0
func AllocateCom(r CreateRequest) error {
	var pList util.Ip_Cpus_List

	for ip, res := range Rres {
		cpus := util.PositiveNum(res.Docker_nr)
		node := &util.Ip_Cpus{ip, cpus}
		pList = append(pList, node)
	}

	sort.Sort(pList)
	totalCPuNum := r.TotalCpuNum
	for index := 0; index < len(pList); index++ {
		var HasNozero bool
		ip := pList[index].Ip
		nodeRes, _ := Rres[ip]
		for i := 0; i < len(nodeRes.Docker_nr); i++ {
			HasNozero = false
			if nodeRes.Docker_nr[i] > 0 {
				HasNozero = true
				totalCPuNum--
				if totalCPuNum <= 0 {
					break // from for i
				}
				nodeRes.Docker_nr[i] = 1
			} else {
				nodeRes.Docker_nr[i] = 0
			}
		}
		if !HasNozero {
			continue
		}
		Ares[ip] = nodeRes //there is no all-zero in Ares
		if totalCPuNum <= 0 {
			break //from for index
		}
	}
	return nil
}