-
Notifications
You must be signed in to change notification settings - Fork 212
/
sorters.go
231 lines (215 loc) · 7.35 KB
/
sorters.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 objects
import (
"sort"
"time"
"github.com/apache/yunikorn-core/pkg/common/resources"
"github.com/apache/yunikorn-core/pkg/metrics"
"github.com/apache/yunikorn-core/pkg/scheduler/policies"
)
func sortQueue(queues []*Queue, sortType policies.SortPolicy, considerPriority bool) {
sortingStart := time.Now()
if sortType == policies.FairSortPolicy {
if considerPriority {
sortQueuesByPriorityAndFairness(queues)
} else {
sortQueuesByFairnessAndPriority(queues)
}
} else {
if considerPriority {
sortQueuesByPriority(queues)
}
}
metrics.GetSchedulerMetrics().ObserveQueueSortingLatency(sortingStart)
}
func sortQueuesByPriority(queues []*Queue) {
sort.SliceStable(queues, func(i, j int) bool {
l := queues[i]
r := queues[j]
lPriority := l.GetCurrentPriority()
rPriority := r.GetCurrentPriority()
return lPriority > rPriority
})
}
func sortQueuesByPriorityAndFairness(queues []*Queue) {
sort.SliceStable(queues, func(i, j int) bool {
l := queues[i]
r := queues[j]
lPriority := l.GetCurrentPriority()
rPriority := r.GetCurrentPriority()
if lPriority > rPriority {
return true
}
if lPriority < rPriority {
return false
}
comp := resources.CompUsageRatioSeparately(l.GetAllocatedResource(), l.GetGuaranteedResource(),
r.GetAllocatedResource(), r.GetGuaranteedResource())
if comp == 0 {
return resources.StrictlyGreaterThan(resources.Sub(l.GetPendingResource(), r.GetPendingResource()), resources.Zero)
}
return comp < 0
})
}
func sortQueuesByFairnessAndPriority(queues []*Queue) {
sort.SliceStable(queues, func(i, j int) bool {
l := queues[i]
r := queues[j]
comp := resources.CompUsageRatioSeparately(l.GetAllocatedResource(), l.GetGuaranteedResource(),
r.GetAllocatedResource(), r.GetGuaranteedResource())
if comp == 0 {
lPriority := l.GetCurrentPriority()
rPriority := r.GetCurrentPriority()
if lPriority > rPriority {
return true
}
if lPriority < rPriority {
return false
}
return resources.StrictlyGreaterThan(resources.Sub(l.GetPendingResource(), r.GetPendingResource()), resources.Zero)
}
return comp < 0
})
}
func sortApplications(apps map[string]*Application, sortType policies.SortPolicy, considerPriority bool, globalResource *resources.Resource) []*Application {
sortingStart := time.Now()
var sortedApps []*Application
switch sortType {
case policies.FairSortPolicy:
sortedApps = filterOnPendingResources(apps)
if considerPriority {
sortApplicationsByPriorityAndFairness(sortedApps, globalResource)
} else {
sortApplicationsByFairnessAndPriority(sortedApps, globalResource)
}
case policies.FifoSortPolicy:
sortedApps = filterOnPendingResources(apps)
if considerPriority {
sortApplicationsByPriorityAndSubmissionTime(sortedApps)
} else {
sortApplicationsBySubmissionTimeAndPriority(sortedApps)
}
case policies.StateAwarePolicy:
sortedApps = stateAwareFilter(apps)
if considerPriority {
sortApplicationsByPriorityAndSubmissionTime(sortedApps)
} else {
sortApplicationsBySubmissionTimeAndPriority(sortedApps)
}
}
metrics.GetSchedulerMetrics().ObserveAppSortingLatency(sortingStart)
return sortedApps
}
func sortApplicationsByFairnessAndPriority(sortedApps []*Application, globalResource *resources.Resource) {
sort.SliceStable(sortedApps, func(i, j int) bool {
l := sortedApps[i]
r := sortedApps[j]
if comp := resources.CompUsageRatio(l.GetAllocatedResource(), r.GetAllocatedResource(), globalResource); comp != 0 {
return comp < 0
}
return l.GetAskMaxPriority() > r.GetAskMaxPriority()
})
}
func sortApplicationsByPriorityAndFairness(sortedApps []*Application, globalResource *resources.Resource) {
sort.SliceStable(sortedApps, func(i, j int) bool {
l := sortedApps[i]
r := sortedApps[j]
leftPriority := l.GetAskMaxPriority()
rightPriority := r.GetAskMaxPriority()
if leftPriority > rightPriority {
return true
}
if leftPriority < rightPriority {
return false
}
return resources.CompUsageRatio(l.GetAllocatedResource(), r.GetAllocatedResource(), globalResource) < 0
})
}
func sortApplicationsBySubmissionTimeAndPriority(sortedApps []*Application) {
sort.SliceStable(sortedApps, func(i, j int) bool {
l := sortedApps[i]
r := sortedApps[j]
if l.SubmissionTime.Before(r.SubmissionTime) {
return true
}
if r.SubmissionTime.Before(l.SubmissionTime) {
return false
}
return l.GetAskMaxPriority() > r.GetAskMaxPriority()
})
}
func sortApplicationsByPriorityAndSubmissionTime(sortedApps []*Application) {
sort.SliceStable(sortedApps, func(i, j int) bool {
l := sortedApps[i]
r := sortedApps[j]
leftPriority := l.GetAskMaxPriority()
rightPriority := r.GetAskMaxPriority()
if leftPriority > rightPriority {
return true
}
if leftPriority < rightPriority {
return false
}
return l.SubmissionTime.Before(r.SubmissionTime)
})
}
func filterOnPendingResources(apps map[string]*Application) []*Application {
filteredApps := make([]*Application, 0)
for _, app := range apps {
// Only look at app when pending-res > 0
if resources.StrictlyGreaterThanZero(app.GetPendingResource()) {
filteredApps = append(filteredApps, app)
}
}
return filteredApps
}
// This filter only allows one (1) application with a state that is not running in the list of candidates.
// The preference is a state of Starting. If we can not find an app with a starting state we will use an app
// with an Accepted state. However if there is an app with a Starting state even with no pending resource
// requests, no Accepted apps can be scheduled. An app in New state does not have any asks and can never be
// scheduled.
func stateAwareFilter(apps map[string]*Application) []*Application {
filteredApps := make([]*Application, 0)
var acceptedApp *Application
var foundStarting bool
for _, app := range apps {
// found a starting app clear out the accepted app (independent of pending resources)
if app.IsStarting() {
foundStarting = true
acceptedApp = nil
}
// Now just look at app when pending-res > 0
if resources.StrictlyGreaterThanZero(app.GetPendingResource()) {
// filter accepted apps
if app.IsAccepted() {
// check if we have not seen a starting app
// replace the currently tracked accepted app if this is an older one
if !foundStarting && (acceptedApp == nil || acceptedApp.SubmissionTime.After(app.SubmissionTime)) {
acceptedApp = app
}
continue
}
// this is a running or starting app add it to the list
filteredApps = append(filteredApps, app)
}
}
// just add the accepted app if we need to: apps are not sorted yet
if acceptedApp != nil {
filteredApps = append(filteredApps, acceptedApp)
}
return filteredApps
}