/
scheduler.go
89 lines (76 loc) · 2.51 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package devicemanager
import (
"math"
"sync"
faasv1 "github.com/Interstellarss/faas-share/pkg/apis/faasshare/v1"
corev1 "k8s.io/api/core/v1"
)
func scheduleSharePod(isGPUPod bool, gpu_request float64, gpu_mem int64, gpupod *faasv1.SharePod, nodeList []*corev1.Node, podList []*corev1.Pod, sharePodList []*faasv1.SharePod) (string, string) {
// Implement custom scheduling algorithm and replace the function assignment
// Prototype: FUNC(bool, string, *kubesharev1.SharePod, NodeResources) (string, string, error)
ap := ScheduleAlgorithmBestFit
nodeResources := syncClusterResources(nodeList, podList, sharePodList)
for _, filter := range filters {
filter(nodeResources, gpupod)
}
return ap(isGPUPod, gpu_request, gpu_mem, gpupod, nodeResources)
}
func ScheduleAlgorithmBestFit(isGPUPod bool, gpu_request float64, gpu_mem int64, sharepod *faasv1.SharePod, nodeResources NodeResources) (schedNodeName string, schedGPUID string) {
type candidateNodeGPU struct {
NodeName string
GPUID string
Point int64
}
bestNode := candidateNodeGPU{
Point: 2147483647,
NodeName: "",
GPUID: "",
}
var bestNodeMux sync.Mutex
tryBestNode := func(point int64, nodeName, GPUID string) {
bestNodeMux.Lock()
if point < bestNode.Point {
bestNode.Point = point
bestNode.NodeName = nodeName
bestNode.GPUID = GPUID
}
bestNodeMux.Unlock()
}
var cpuReqTotal, memReqTotal int64 = 0, 0
for _, container := range sharepod.Spec.PodSpec.Containers {
cpuReqTotal += container.Resources.Requests.Cpu().MilliValue()
memReqTotal += container.Resources.Requests.Memory().MilliValue()
}
gpu_request_millivalue := int64(math.Ceil(gpu_request * (float64)(1000.0)))
var wait sync.WaitGroup
scheduleNode := func(nodeName string, nodeRes *NodeResource) {
if nodeRes.CpuFree < cpuReqTotal || nodeRes.MemFree < memReqTotal {
wait.Done()
return
}
if isGPUPod {
findTheHole := false
for id, gpu := range nodeRes.GpuFree {
if gpu.GPUFreeReq < gpu_request_millivalue || gpu.GPUFreeMem < gpu_mem {
continue
}
findTheHole = true
tryBestNode(gpu.GPUFreeReq-gpu_request_millivalue, nodeName, id)
}
if !findTheHole {
if nodeRes.GpuFreeCount > 0 {
tryBestNode(1000-gpu_request_millivalue, nodeName, faasv1.NewGPUID(5))
}
}
} else {
tryBestNode(nodeRes.CpuFree-cpuReqTotal, nodeName, "")
}
wait.Done()
}
wait.Add(len(nodeResources))
for nodeName, nodeRes := range nodeResources {
go scheduleNode(nodeName, nodeRes)
}
wait.Wait()
return bestNode.NodeName, bestNode.GPUID
}