forked from volcano-sh/volcano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binpack.go
260 lines (222 loc) · 7.4 KB
/
binpack.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright 2019 The Volcano Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package binpack
import (
"fmt"
"strings"
"k8s.io/api/core/v1"
"k8s.io/klog"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/framework"
)
const (
// PluginName indicates name of volcano scheduler plugin.
PluginName = "binpack"
)
const (
// BinpackWeight is the key for providing Binpack Priority Weight in YAML
BinpackWeight = "binpack.weight"
// BinpackCPU is the key for weight of cpu
BinpackCPU = "binpack.cpu"
// BinpackMemory is the key for memory of cpu
BinpackMemory = "binpack.memory"
// BinpackResources is the key for additional resource key name
BinpackResources = "binpack.resources"
// BinpackResourcesPrefix is the key prefix for additional resource key name
BinpackResourcesPrefix = BinpackResources + "."
resourceFmt = "%s[%d]"
)
type priorityWeight struct {
BinPackingWeight int
BinPackingCPU int
BinPackingMemory int
BinPackingResources map[v1.ResourceName]int
}
func (w *priorityWeight) String() string {
length := 3
if extendLength := len(w.BinPackingResources); extendLength == 0 {
length++
} else {
length += extendLength
}
msg := make([]string, 0, length)
msg = append(msg,
fmt.Sprintf(resourceFmt, BinpackWeight, w.BinPackingWeight),
fmt.Sprintf(resourceFmt, BinpackCPU, w.BinPackingCPU),
fmt.Sprintf(resourceFmt, BinpackMemory, w.BinPackingMemory),
)
if len(w.BinPackingResources) == 0 {
msg = append(msg, "no extend resources.")
} else {
for name, weight := range w.BinPackingResources {
msg = append(msg, fmt.Sprintf(resourceFmt, name, weight))
}
}
return strings.Join(msg, ", ")
}
type binpackPlugin struct {
// Arguments given for the plugin
weight priorityWeight
}
//New function returns prioritizePlugin object
func New(aruguments framework.Arguments) framework.Plugin {
weight := calculateWeight(aruguments)
return &binpackPlugin{weight: weight}
}
func calculateWeight(args framework.Arguments) priorityWeight {
/*
User Should give priorityWeight in this format(binpack.weight, binpack.cpu, binpack.memory).
Support change the weight about cpu, memory and additional resource by arguments.
actions: "enqueue, reclaim, allocate, backfill, preempt"
tiers:
- plugins:
- name: binpack
arguments:
binpack.weight: 10
binpack.cpu: 5
binpack.memory: 1
binpack.resources: nvidia.com/gpu, example.com/foo
binpack.resources.nvidia.com/gpu: 2
binpack.resources.example.com/foo: 3
*/
// Values are initialized to 1.
weight := priorityWeight{
BinPackingWeight: 1,
BinPackingCPU: 1,
BinPackingMemory: 1,
BinPackingResources: make(map[v1.ResourceName]int),
}
// Checks whether binpack.weight is provided or not, if given, modifies the value in weight struct.
args.GetInt(&weight.BinPackingWeight, BinpackWeight)
// Checks whether binpack.cpu is provided or not, if given, modifies the value in weight struct.
args.GetInt(&weight.BinPackingCPU, BinpackCPU)
if weight.BinPackingCPU < 0 {
weight.BinPackingCPU = 1
}
// Checks whether binpack.memory is provided or not, if given, modifies the value in weight struct.
args.GetInt(&weight.BinPackingMemory, BinpackMemory)
if weight.BinPackingMemory < 0 {
weight.BinPackingMemory = 1
}
resourcesStr := args[BinpackResources]
resources := strings.Split(resourcesStr, ",")
for _, resource := range resources {
resource = strings.TrimSpace(resource)
if resource == "" {
continue
}
// binpack.resources.[ResourceName]
resourceKey := BinpackResourcesPrefix + resource
resourceWeight := 1
args.GetInt(&resourceWeight, resourceKey)
if resourceWeight < 0 {
resourceWeight = 1
}
weight.BinPackingResources[v1.ResourceName(resource)] = resourceWeight
}
return weight
}
func (bp *binpackPlugin) Name() string {
return PluginName
}
func (bp *binpackPlugin) OnSessionOpen(ssn *framework.Session) {
klog.V(4).Infof("Enter binpack plugin ...")
if klog.V(4) {
defer func() {
klog.V(4).Infof("Leaving binpack plugin. %s ...", bp.weight.String())
}()
notFoundResource := []string{}
for resource := range bp.weight.BinPackingResources {
found := false
for _, nodeInfo := range ssn.Nodes {
if nodeInfo.Allocatable.Get(resource) > 0 {
found = true
break
}
}
if !found {
notFoundResource = append(notFoundResource, string(resource))
}
}
klog.V(4).Infof("resources [%s] record in weight but not found on any node", strings.Join(notFoundResource, ", "))
}
nodeOrderFn := func(task *api.TaskInfo, node *api.NodeInfo) (float64, error) {
binPackingScore := BinPackingScore(task, node, bp.weight)
klog.V(4).Infof("Binpack score for Task %s/%s on node %s is: %v", task.Namespace, task.Name, node.Name, binPackingScore)
return binPackingScore, nil
}
if bp.weight.BinPackingWeight != 0 {
ssn.AddNodeOrderFn(bp.Name(), nodeOrderFn)
} else {
klog.Infof("binpack weight is zero, skip node order function")
}
}
func (bp *binpackPlugin) OnSessionClose(ssn *framework.Session) {
}
// BinPackingScore use the best fit polices during scheduling.
// Goals:
// - Schedule Jobs using BestFit Policy using Resource Bin Packing Priority Function
// - Reduce Fragmentation of scarce resources on the Cluster
func BinPackingScore(task *api.TaskInfo, node *api.NodeInfo, weight priorityWeight) float64 {
score := 0.0
weightSum := 0
requested := task.Resreq
allocatable := node.Allocatable
used := node.Used
for _, resource := range requested.ResourceNames() {
request := requested.Get(resource)
if request == 0 {
continue
}
allocate := allocatable.Get(resource)
nodeUsed := used.Get(resource)
resourceWeight := 0
found := false
switch resource {
case v1.ResourceCPU:
resourceWeight = weight.BinPackingCPU
found = true
case v1.ResourceMemory:
resourceWeight = weight.BinPackingMemory
found = true
default:
resourceWeight, found = weight.BinPackingResources[resource]
}
if !found {
continue
}
resourceScore := ResourceBinPackingScore(request, allocate, nodeUsed, resourceWeight)
klog.V(5).Infof("task %s/%s on node %s resource %s, need %f, used %f, allocatable %f, weight %d, score %f", task.Namespace, task.Name, node.Name, resource, request, nodeUsed, allocate, resourceWeight, resourceScore)
score += resourceScore
weightSum += resourceWeight
}
// mapping the result from [0, weightSum] to [0, 10(MaxPriority)]
if weightSum > 0 {
score = score / float64(weightSum)
}
score *= schedulerapi.MaxPriority * float64(weight.BinPackingWeight)
return score
}
// ResourceBinPackingScore calculate the binpack score for resource with provided info
func ResourceBinPackingScore(requested, capacity, used float64, weight int) float64 {
if capacity == 0 || weight == 0 {
return 0
}
usedFinally := requested + used
if usedFinally > capacity {
return 0
}
score := usedFinally * float64(weight) / capacity
return score
}