/
autoscaler.go
444 lines (389 loc) · 12.9 KB
/
autoscaler.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* Copyright (c) 2016 PaddlePaddle Authors All Rights Reserve.
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 autoscaler
import (
"sort"
"sync"
"time"
log "github.com/inconshreveable/log15"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/pkg/api"
padv1 "github.com/baidu/paddle-on-k8s-operator/pkg/apis/paddlepaddle/v1alpha1"
"github.com/baidu/paddle-on-k8s-operator/pkg/updater"
)
const (
defaultLoopDur = time.Second * 30
)
// Autoscaler launches and scales the training jobs.
type Autoscaler struct {
kubeCli kubernetes.Interface
jobUpdater *sync.Map
maxLoadDesired float64
}
// WithMaxLoadDesired init with maxLoadDesired
func WithMaxLoadDesired(maxLoadDesired float64) func(as *Autoscaler) {
return func(as *Autoscaler) {
as.maxLoadDesired = maxLoadDesired
}
}
// NewAutoscaler creates a new Autoscaler.
func NewAutoscaler(kubeClient kubernetes.Interface, jobUpdater *sync.Map, options ...func(*Autoscaler)) *Autoscaler {
c := &Autoscaler{
kubeCli: kubeClient,
jobUpdater: jobUpdater,
maxLoadDesired: 1.0,
}
for _, option := range options {
option(c)
}
return c
}
// InquiryResource returns the idle and total resources of the k8s cluster.
func (a *Autoscaler) InquiryResource() (ClusterResource, error) {
nodes := a.kubeCli.CoreV1().Nodes()
nodeList, err := nodes.List(metav1.ListOptions{})
if err != nil {
return ClusterResource{}, err
}
allocatable := make(v1.ResourceList)
nodesCPUIdleMilli := make(map[string]int64)
nodesMemoryFreeMega := make(map[string]int64)
for _, node := range nodeList.Items {
nodesCPUIdleMilli[node.GetObjectMeta().GetName()] =
node.Status.Allocatable.Cpu().ScaledValue(resource.Milli)
nodesMemoryFreeMega[node.GetObjectMeta().GetName()] =
node.Status.Allocatable.Memory().ScaledValue(resource.Mega)
AddResourceList(allocatable, node.Status.Allocatable)
}
// Get non-terminated pods from all namespaces.
namespace := ""
// FIXME(typhoonzero): scan all pods is not a efficient way.
// NOTE: pending pods need to be caculated for scale down.
// NOTE: "terminating" pods' status is still running, do not
// scale up/down the job if job is still at last scaling
// process.
fieldSelector, err := fields.ParseSelector("status.phase!=" + string(api.PodSucceeded) + ",status.phase!=" + string(api.PodFailed))
if err != nil {
return ClusterResource{}, err
}
allPodsList, err := a.kubeCli.CoreV1().Pods(namespace).List(metav1.ListOptions{FieldSelector: fieldSelector.String()})
if err != nil {
return ClusterResource{}, err
}
allReqs, allLimits, err := getPodsTotalRequestsAndLimits(allPodsList)
if err != nil {
return ClusterResource{}, err
}
err = updateNodesIdleResource(allPodsList, nodesCPUIdleMilli, nodesMemoryFreeMega)
if err != nil {
return ClusterResource{}, err
}
res := ClusterResource{
NodeCount: len(nodeList.Items),
GPUTotal: int(allocatable.NvidiaGPU().Value()),
CPUTotalMilli: allocatable.Cpu().ScaledValue(resource.Milli),
MemoryTotalMega: allocatable.Memory().ScaledValue(resource.Mega),
GPURequest: int(allReqs.NvidiaGPU().Value()),
CPURequestMilli: allReqs.Cpu().ScaledValue(resource.Milli),
MemoryRequestMega: allReqs.Memory().ScaledValue(resource.Mega),
GPULimit: int(allLimits.NvidiaGPU().Value()),
CPULimitMilli: allLimits.Cpu().ScaledValue(resource.Milli),
MemoryLimitMega: allLimits.Memory().ScaledValue(resource.Mega),
Nodes: Nodes{
NodesCPUIdleMilli: nodesCPUIdleMilli,
NodesMemoryFreeMega: nodesMemoryFreeMega,
},
}
return res, nil
}
// elastic job filter.
func elastic(j *padv1.TrainingJob) bool {
return j.Elastic()
}
// gpu job filter
func needGPU(j *padv1.TrainingJob) bool {
return j.NeedGPU()
}
func isRunning(j *padv1.TrainingJob) bool {
if j.Status.Phase == padv1.TrainingJobPhaseRunning || j.Status.Phase == padv1.TrainingJobPhaseScaling {
return true
}
return false
}
func (a *Autoscaler) totalRunningJob(jobName string) bool {
v, ok := a.jobUpdater.Load(jobName)
if !ok {
return false
}
up, ok := v.(*updater.JobUpdater)
if !ok {
return false
}
job := up.Job
totalRunning := job.Status.Phase == padv1.TrainingJobPhaseRunning
log.Debug("totalRunningJob", "jobname", jobName, "totalRunning", totalRunning)
return totalRunning
}
// sortedJobs return the names of sorted jobs by fulfillment and
// tiebreakers in ascending order.
func sortedJobs(j []*padv1.TrainingJob, filters ...func(*padv1.TrainingJob) bool) []*padv1.TrainingJob {
var js trainingjobList
nextJob:
for _, v := range j {
for _, f := range filters {
if !f(v) {
continue nextJob
}
}
js = append(js, v)
}
sort.Sort(js)
return js
}
func searchAssignableNode(r *ClusterResource, j *padv1.TrainingJob) string {
for name, idle := range r.Nodes.NodesCPUIdleMilli {
if j.TrainerCPURequestMilli() <= idle &&
j.TrainerMemRequestMega() <= r.Nodes.NodesMemoryFreeMega[name] {
return name
}
}
return ""
}
func scaleDryRun(r *ClusterResource, j *padv1.TrainingJob, curDiff int32, maxLoadDesired float64, scaleDown bool) (additional int) {
additionalGPUInstance := 0
additionalCPUInstance := 0
cpuRequestMilli := j.TrainerCPURequestMilli()
memRequestMega := j.TrainerMemRequestMega()
gpuLimit := j.TrainerGPULimit()
nodeName := ""
// Adjust resource upon return.
defer func() {
log.Debug("scaleDryRun", "scaledown", scaleDown, "namespace", j.Namespace, "jobname", j.Name, "additional", additional)
r.GPULimit += gpuLimit * additional
r.CPURequestMilli += cpuRequestMilli * int64(additional)
r.MemoryRequestMega += memRequestMega * int64(additional)
if nodeName != "" {
r.Nodes.NodesCPUIdleMilli[nodeName] -= cpuRequestMilli * int64(additional)
r.Nodes.NodesMemoryFreeMega[nodeName] -= memRequestMega * int64(additional)
}
}()
// TODO(helin): j.TrainerJob.Spec.Parallelism may not reflect
// the actual pod running for the trainer job. We need to
// count the pod manually. And calculate the additional value
// based on the running pod count,
// j.TrainerJob.Spec.Parallelism, and curDiff.
plannedInstance := int(*j.Spec.Trainer.ReplicaSpec.Spec.Parallelism) + int(curDiff)
instanceMax := j.Spec.Trainer.MaxInstance
instanceMin := j.Spec.Trainer.MinInstance
log.Debug("scaleDryRun instance num", "min", instanceMin, "max", instanceMax, "planned", plannedInstance)
// TODO(typhoonzero): refine below code to remove direction
// ======================= scaleDown ======================
if scaleDown {
if plannedInstance > instanceMax {
additional = -1
return
}
gpuThreshold := int(float64(r.GPUTotal) * maxLoadDesired)
gpuCondition := r.GPULimit > gpuThreshold
cpuThreshold := int64(float64(r.CPUTotalMilli) * maxLoadDesired)
cpuCondition := r.CPURequestMilli > cpuThreshold
memThreshold := int64(float64(r.MemoryTotalMega) * maxLoadDesired)
memCondition := r.MemoryRequestMega > memThreshold
log.Debug("scaleDryRun", "gpuRequest", r.GPULimit, "threshold", gpuThreshold)
log.Debug("scaleDryRun", "cpuRequest", r.CPURequestMilli, "threshold", cpuThreshold)
log.Debug("scaleDryRun", "memRequest", r.MemoryRequestMega, "threshold", memThreshold)
log.Debug("scaleDryRun conditions", "gpuCondition", gpuCondition, "cpuCondition", cpuCondition, "memCondition", memCondition)
if gpuCondition || cpuCondition || memCondition {
if plannedInstance > instanceMin {
additional = -1
return
}
// can not scale down further
additional = 0
return
}
// do not try to scale up
return
}
// ======================= scaleUp ==========================
if plannedInstance >= instanceMax {
// Do not scale or scale down, don't need to check if
// there are available free resources.
additional = instanceMax - plannedInstance
return
}
if r.MemoryTotalMega-r.MemoryRequestMega <= memRequestMega {
// insufficient memory, do not scale
additional = 0
return
}
// TODO(m3ngyang) this node may not be the one that pod is assigned to in fact.
if nodeName = searchAssignableNode(r, j); nodeName == "" {
additional = 0
return
}
// NOTE: do not scale up to use full cluster resource of CPU
// but we do scale up for GPU.
if int64(float64(r.CPUTotalMilli)*maxLoadDesired)-r.CPURequestMilli >= cpuRequestMilli {
additionalCPUInstance = 1
}
needGPU := gpuLimit > 0
if needGPU && r.GPUTotal-r.GPULimit >= gpuLimit {
additionalGPUInstance = 1
}
if needGPU {
if additionalGPUInstance < additionalCPUInstance {
additional = additionalGPUInstance
} else {
additional = additionalCPUInstance
}
} else {
additional = additionalCPUInstance
}
return
}
func (a *Autoscaler) setAdditional(diff map[string]int32) {
a.jobUpdater.Range(func(k, v interface{}) bool {
key := k.(string)
up := v.(*updater.JobUpdater)
var additional int32
if val, ok := diff[key]; ok {
additional = val
}
up.Additional = additional
log.Debug("setAdditional", "jobname", key, "additional", additional)
a.jobUpdater.Store(k, up)
return true
})
}
// scaleAllJobsDryRun pretends to rescale all jobs in order to find
// out the number of pods should be added/deleted for each job, or
// say, delta. It returns a map from job name to the delta.
func scaleAllJobsDryRun(jobs []*padv1.TrainingJob, r ClusterResource, maxLoadDesired float64) map[string]int32 {
// Iteratively calculate scaling diff until nothing changes.
diff := make(map[string]int32)
for {
noChange := true
sorted := sortedJobs(jobs, elastic)
dryRun := func(j *padv1.TrainingJob, isScaleDown bool) {
name := j.Namespace + "/" + j.Name
additional := scaleDryRun(&r, j, diff[name], maxLoadDesired, isScaleDown)
diff[name] += int32(additional)
if additional != 0 {
noChange = false
}
}
// TODO(typhoonzero): implement GPU priority CFS scheduler from here.
// scale up from the ones that need scaling up the
// most.
for _, j := range sorted {
dryRun(j, false)
}
// scale down from the ones that need scaling up the
// least.
for i := len(sorted) - 1; i >= 0; i-- {
dryRun(sorted[i], true)
}
if noChange {
break
}
}
return diff
}
// Run monitors the cluster resources and training jobs in a loop,
// scales the training jobs according to the cluster resource.
func (a *Autoscaler) Run() {
ticker := time.NewTicker(defaultLoopDur)
defer ticker.Stop()
log.Info("start Autoscaler")
for {
<-ticker.C
r, err := a.InquiryResource()
if err != nil {
log.Error("InquiryResource error", err.Error())
continue
}
log.Info("Cluster.InquiryResource done", "resource", r)
havePending := a.findPendingJob()
log.Debug("pending job", "exist", havePending)
diff := scaleAllJobsDryRun(
a.findTrainingJobsMightBeRescheduled(havePending),
r,
a.maxLoadDesired)
log.Info("Calculated info", "diff", diff)
a.setAdditional(diff)
}
}
func (a *Autoscaler) findPendingJob() bool {
havePending := false
// TODO(m3ngyang) add a pending status for TrainingJob
// how to define a pending job? no trainer pod is scheduled?
traverseFunc := func(k, v interface{}) bool {
log.Debug("Find pendingJob check", "jobname", k)
total := 0
pending := 0
up, ok := v.(*updater.JobUpdater)
if !ok {
log.Debug("Find pendingJob conversion error", "jobname", k)
}
job := up.Job
var labelKey string
if job.Spec.FaultTolerant {
labelKey = "paddle-job-master"
} else {
labelKey = "paddle-job-pserver"
}
pods, err := a.kubeCli.CoreV1().Pods(job.Namespace).List(metav1.ListOptions{LabelSelector: labelKey + "=" + job.Name})
if err != nil {
log.Error("Find pendingJob failed to list job pods", "error", err)
return true
}
for _, pod := range pods.Items {
total++
for _, cond := range pod.Status.Conditions {
if cond.Type == corev1.PodScheduled && cond.Reason == corev1.PodReasonUnschedulable {
pending++
break
}
}
}
if total == pending && total != 0 {
log.Debug("Find pendingJob", "jobName", job.Name, "totalNum", total)
havePending = true
return false
}
return true
}
a.jobUpdater.Range(traverseFunc)
return havePending
}
func (a *Autoscaler) findTrainingJobsMightBeRescheduled(havePending bool) (js trainingjobList) {
traverseFunc := func(k, v interface{}) bool {
jn := k.(string)
log.Debug("findTrainingJobsMightBeRescheduled", "jobname", jn)
up, ok := v.(*updater.JobUpdater)
if !ok {
return false
}
job := up.Job
if a.totalRunningJob(jn) || havePending {
js = append(js, job)
}
return true
}
a.jobUpdater.Range(traverseFunc)
return
}