-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathreclaim.go
More file actions
261 lines (216 loc) · 7.92 KB
/
reclaim.go
File metadata and controls
261 lines (216 loc) · 7.92 KB
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
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2018-2025 The Volcano Authors.
Modifications made by Volcano authors:
- Added job validation and preemption policy support
- Enhanced victim selection with priority queue ordering
- Added PrePredicate validation and node filtering
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 reclaim
import (
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"volcano.sh/volcano/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/conf"
"volcano.sh/volcano/pkg/scheduler/framework"
"volcano.sh/volcano/pkg/scheduler/util"
)
type Action struct {
enablePredicateErrorCache bool
}
func New() *Action {
return &Action{
enablePredicateErrorCache: true,
}
}
func (ra *Action) Name() string {
return "reclaim"
}
func (ra *Action) Initialize() {}
func (ra *Action) parseArguments(ssn *framework.Session) {
arguments := framework.GetArgOfActionFromConf(ssn.Configurations, ra.Name())
arguments.GetBool(&ra.enablePredicateErrorCache, conf.EnablePredicateErrCacheKey)
}
func (ra *Action) Execute(ssn *framework.Session) {
klog.V(5).Infof("Enter Reclaim ...")
defer klog.V(5).Infof("Leaving Reclaim ...")
ra.parseArguments(ssn)
queues := util.NewPriorityQueue(ssn.QueueOrderFn)
queueMap := map[api.QueueID]*api.QueueInfo{}
preemptorsMap := map[api.QueueID]*util.PriorityQueue{}
preemptorTasks := map[api.JobID]*util.PriorityQueue{}
klog.V(3).Infof("There are <%d> Jobs and <%d> Queues in total for scheduling.",
len(ssn.Jobs), len(ssn.Queues))
for _, job := range ssn.Jobs {
if job.IsPending() {
continue
}
if vr := ssn.JobValid(job); vr != nil && !vr.Pass {
klog.V(4).Infof("Job <%s/%s> Queue <%s> skip reclaim, reason: %v, message %v", job.Namespace, job.Name, job.Queue, vr.Reason, vr.Message)
continue
}
if queue, found := ssn.Queues[job.Queue]; !found {
klog.Errorf("Failed to find Queue <%s> for Job <%s/%s>", job.Queue, job.Namespace, job.Name)
continue
} else if _, existed := queueMap[queue.UID]; !existed {
klog.V(4).Infof("Added Queue <%s> for Job <%s/%s>", queue.Name, job.Namespace, job.Name)
queueMap[queue.UID] = queue
queues.Push(queue)
}
if ssn.JobStarving(job) {
if _, found := preemptorsMap[job.Queue]; !found {
preemptorsMap[job.Queue] = util.NewPriorityQueue(ssn.JobOrderFn)
}
preemptorsMap[job.Queue].Push(job)
preemptorTasks[job.UID] = util.NewPriorityQueue(ssn.TaskOrderFn)
for _, task := range job.TaskStatusIndex[api.Pending] {
if task.SchGated {
continue
}
preemptorTasks[job.UID].Push(task)
}
}
}
for {
if queues.Empty() {
break
}
queue := queues.Pop().(*api.QueueInfo)
if ssn.Overused(queue) {
klog.V(3).Infof("Queue <%s> is overused, ignore it.", queue.Name)
continue
}
for {
// Pick the starving jobs in this queue.
jobsQ, found := preemptorsMap[queue.UID]
if !found || jobsQ.Empty() {
klog.V(4).Infof("No preemptors in Queue <%s>, break.", queue.Name)
break
}
job := jobsQ.Pop().(*api.JobInfo)
stmt := framework.NewStatement(ssn)
for {
// If job is not request more resource, then stop reclaiming.
if !ssn.JobStarving(job) {
break
}
// Pick up all its candidate tasks.
tasksQ, ok := preemptorTasks[job.UID]
if !ok || tasksQ.Empty() {
klog.V(3).Infof("No preemptor task in job <%s/%s>.",
job.Namespace, job.Name)
break
}
klog.V(3).Infof("Considering reclaim for %d tasks of job <%s/%s>.", tasksQ.Len(), job.Namespace, job.Name)
task := tasksQ.Pop().(*api.TaskInfo)
if task.Pod.Spec.PreemptionPolicy != nil && *task.Pod.Spec.PreemptionPolicy == v1.PreemptNever {
klog.V(3).Infof("Task %s/%s cannot preempt (policy Never)", task.Namespace, task.Name)
continue
}
if !ssn.Preemptive(queue, task) {
klog.V(3).Infof("Queue <%s> cannot reclaim for task <%s>, skip", queue.Name, task.Name)
continue
}
if err := ssn.PrePredicateFn(task); err != nil {
klog.V(3).Infof("PrePredicate failed for task %s/%s: %v", task.Namespace, task.Name, err)
continue
}
ra.reclaimForTask(ssn, stmt, task, job)
}
if ssn.JobPipelined(job) {
stmt.Commit()
} else {
stmt.Discard()
}
if !jobsQ.Empty() {
queues.Push(queue)
}
}
}
}
func (ra *Action) reclaimForTask(ssn *framework.Session, stmt *framework.Statement, task *api.TaskInfo, job *api.JobInfo) {
totalNodes := ssn.FilterOutUnschedulableAndUnresolvableNodesForTask(task)
predicateHelper := util.NewPredicateHelper()
predicateNodes, _ := predicateHelper.PredicateNodes(task, totalNodes, ssn.PredicateForPreemptAction, ra.enablePredicateErrorCache, ssn.NodesInShard)
predicateNodesByShard := util.GetPredicatedNodeByShard(predicateNodes, ssn.NodesInShard)
var predicateNodesByShardFlattened []*api.NodeInfo
for _, nodes := range predicateNodesByShard {
predicateNodesByShardFlattened = append(predicateNodesByShardFlattened, nodes...)
}
for _, n := range predicateNodesByShardFlattened {
klog.V(3).Infof("Considering Task <%s/%s> on Node <%s>.", task.Namespace, task.Name, n.Name)
var reclaimees []*api.TaskInfo
for _, taskOnNode := range n.Tasks {
if taskOnNode.Status != api.Running || !taskOnNode.Preemptable {
continue
}
if j, found := ssn.Jobs[taskOnNode.Job]; !found {
continue
} else if j.Queue != job.Queue {
q := ssn.Queues[j.Queue]
if !q.Reclaimable() {
continue
}
reclaimees = append(reclaimees, taskOnNode.Clone())
}
}
if len(reclaimees) == 0 {
klog.V(4).Infof("No reclaimees on Node <%s>.", n.Name)
continue
}
victims := ssn.Reclaimable(task, reclaimees)
if err := util.ValidateVictims(task, n, victims); err != nil {
klog.V(3).Infof("No validated victims on Node <%s>: %v", n.Name, err)
continue
}
victimsQueue := ssn.BuildVictimsPriorityQueue(victims, task)
resreq := task.InitResreq.Clone()
reclaimed := api.EmptyResource()
// The reclaimed resources should be added to the remaining available resources of the nodes to avoid over-reclaiming.
availableResources := n.FutureIdle()
// Use a per-node statement so that evictions are isolated to this node.
// Only merge into the caller's stmt if Pipeline succeeds; otherwise discard
// so victims on nodes that end up unused are never committed to Kubernetes.
nodeStmt := framework.NewStatement(ssn)
evictionOccurred := false
for !victimsQueue.Empty() {
if resreq.LessEqual(availableResources, api.Zero) {
break
}
reclaimee := victimsQueue.Pop().(*api.TaskInfo)
klog.V(3).Infof("Try to reclaim Task <%s/%s> for Tasks <%s/%s>",
reclaimee.Namespace, reclaimee.Name, task.Namespace, task.Name)
nodeStmt.Evict(reclaimee, "reclaim")
reclaimed.Add(reclaimee.Resreq)
availableResources.Add(reclaimee.Resreq)
evictionOccurred = true
}
klog.V(3).Infof("Reclaimed <%v> for task <%s/%s> requested <%v>, and Node <%s> availableResources <%v>.", reclaimed, task.Namespace, task.Name, task.InitResreq, n.Name, availableResources)
if resreq.LessEqual(availableResources, api.Zero) {
if err := nodeStmt.Pipeline(task, n.Name, evictionOccurred); err != nil {
klog.Errorf("Failed to pipeline Task <%s/%s> on Node <%s>",
task.Namespace, task.Name, n.Name)
if rollbackErr := nodeStmt.UnPipeline(task); rollbackErr != nil {
klog.Errorf("Failed to unpipeline Task %v on %v in Session %v for %v.",
task.UID, n.Name, ssn.UID, rollbackErr)
}
nodeStmt.Discard()
continue
}
stmt.Merge(nodeStmt)
break
}
nodeStmt.Discard()
}
}
func (ra *Action) UnInitialize() {
}