forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
356 lines (294 loc) · 9.66 KB
/
context.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
package scheduler
import (
"regexp"
log "github.com/hashicorp/go-hclog"
memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/nomad/structs"
)
// Context is used to track contextual information used for placement
type Context interface {
// State is used to inspect the current global state
State() State
// Plan returns the current plan
Plan() *structs.Plan
// Logger provides a way to log
Logger() log.Logger
// Metrics returns the current metrics
Metrics() *structs.AllocMetric
// Reset is invoked after making a placement
Reset()
// ProposedAllocs returns the proposed allocations for a node which are
// the existing allocations, removing evictions, and adding any planned
// placements.
ProposedAllocs(nodeID string) ([]*structs.Allocation, error)
// RegexpCache is a cache of regular expressions
RegexpCache() map[string]*regexp.Regexp
// VersionConstraintCache is a cache of version constraints
VersionConstraintCache() map[string]VerConstraints
// SemverConstraintCache is a cache of semver constraints
SemverConstraintCache() map[string]VerConstraints
// Eligibility returns a tracker for node eligibility in the context of the
// eval.
Eligibility() *EvalEligibility
}
// EvalCache is used to cache certain things during an evaluation
type EvalCache struct {
reCache map[string]*regexp.Regexp
versionCache map[string]VerConstraints
semverCache map[string]VerConstraints
}
func (e *EvalCache) RegexpCache() map[string]*regexp.Regexp {
if e.reCache == nil {
e.reCache = make(map[string]*regexp.Regexp)
}
return e.reCache
}
func (e *EvalCache) VersionConstraintCache() map[string]VerConstraints {
if e.versionCache == nil {
e.versionCache = make(map[string]VerConstraints)
}
return e.versionCache
}
func (e *EvalCache) SemverConstraintCache() map[string]VerConstraints {
if e.semverCache == nil {
e.semverCache = make(map[string]VerConstraints)
}
return e.semverCache
}
// EvalContext is a Context used during an Evaluation
type EvalContext struct {
EvalCache
state State
plan *structs.Plan
logger log.Logger
metrics *structs.AllocMetric
eligibility *EvalEligibility
}
// NewEvalContext constructs a new EvalContext
func NewEvalContext(s State, p *structs.Plan, log log.Logger) *EvalContext {
ctx := &EvalContext{
state: s,
plan: p,
logger: log,
metrics: new(structs.AllocMetric),
}
return ctx
}
func (e *EvalContext) State() State {
return e.state
}
func (e *EvalContext) Plan() *structs.Plan {
return e.plan
}
func (e *EvalContext) Logger() log.Logger {
return e.logger
}
func (e *EvalContext) Metrics() *structs.AllocMetric {
return e.metrics
}
func (e *EvalContext) SetState(s State) {
e.state = s
}
func (e *EvalContext) Reset() {
e.metrics = new(structs.AllocMetric)
}
func (e *EvalContext) ProposedAllocs(nodeID string) ([]*structs.Allocation, error) {
// Get the existing allocations that are non-terminal
ws := memdb.NewWatchSet()
proposed, err := e.state.AllocsByNodeTerminal(ws, nodeID, false)
if err != nil {
return nil, err
}
// Determine the proposed allocation by first removing allocations
// that are planned evictions and adding the new allocations.
if update := e.plan.NodeUpdate[nodeID]; len(update) > 0 {
proposed = structs.RemoveAllocs(proposed, update)
}
// Remove any allocs that are being preempted
nodePreemptedAllocs := e.plan.NodePreemptions[nodeID]
if len(nodePreemptedAllocs) > 0 {
proposed = structs.RemoveAllocs(proposed, nodePreemptedAllocs)
}
// We create an index of the existing allocations so that if an inplace
// update occurs, we do not double count and we override the old allocation.
proposedIDs := make(map[string]*structs.Allocation, len(proposed))
for _, alloc := range proposed {
proposedIDs[alloc.ID] = alloc
}
for _, alloc := range e.plan.NodeAllocation[nodeID] {
proposedIDs[alloc.ID] = alloc
}
// Materialize the proposed slice
proposed = make([]*structs.Allocation, 0, len(proposedIDs))
for _, alloc := range proposedIDs {
proposed = append(proposed, alloc)
}
return proposed, nil
}
func (e *EvalContext) Eligibility() *EvalEligibility {
if e.eligibility == nil {
e.eligibility = NewEvalEligibility()
}
return e.eligibility
}
type ComputedClassFeasibility byte
const (
// EvalComputedClassUnknown is the initial state until the eligibility has
// been explicitly marked to eligible/ineligible or escaped.
EvalComputedClassUnknown ComputedClassFeasibility = iota
// EvalComputedClassIneligible is used to mark the computed class as
// ineligible for the evaluation.
EvalComputedClassIneligible
// EvalComputedClassIneligible is used to mark the computed class as
// eligible for the evaluation.
EvalComputedClassEligible
// EvalComputedClassEscaped signals that computed class can not determine
// eligibility because a constraint exists that is not captured by computed
// node classes.
EvalComputedClassEscaped
)
// EvalEligibility tracks eligibility of nodes by computed node class over the
// course of an evaluation.
type EvalEligibility struct {
// job tracks the eligibility at the job level per computed node class.
job map[string]ComputedClassFeasibility
// jobEscaped marks whether constraints have escaped at the job level.
jobEscaped bool
// taskGroups tracks the eligibility at the task group level per computed
// node class.
taskGroups map[string]map[string]ComputedClassFeasibility
// tgEscapedConstraints is a map of task groups to whether constraints have
// escaped.
tgEscapedConstraints map[string]bool
// quotaReached marks that the quota limit has been reached for the given
// quota
quotaReached string
}
// NewEvalEligibility returns an eligibility tracker for the context of an evaluation.
func NewEvalEligibility() *EvalEligibility {
return &EvalEligibility{
job: make(map[string]ComputedClassFeasibility),
taskGroups: make(map[string]map[string]ComputedClassFeasibility),
tgEscapedConstraints: make(map[string]bool),
}
}
// SetJob takes the job being evaluated and calculates the escaped constraints
// at the job and task group level.
func (e *EvalEligibility) SetJob(job *structs.Job) {
// Determine whether the job has escaped constraints.
e.jobEscaped = len(structs.EscapedConstraints(job.Constraints)) != 0
// Determine the escaped constraints per task group.
for _, tg := range job.TaskGroups {
constraints := tg.Constraints
for _, task := range tg.Tasks {
constraints = append(constraints, task.Constraints...)
}
e.tgEscapedConstraints[tg.Name] = len(structs.EscapedConstraints(constraints)) != 0
}
}
// HasEscaped returns whether any of the constraints in the passed job have
// escaped computed node classes.
func (e *EvalEligibility) HasEscaped() bool {
if e.jobEscaped {
return true
}
for _, escaped := range e.tgEscapedConstraints {
if escaped {
return true
}
}
return false
}
// GetClasses returns the tracked classes to their eligibility, across the job
// and task groups.
func (e *EvalEligibility) GetClasses() map[string]bool {
elig := make(map[string]bool)
// Go through the task groups.
for _, classes := range e.taskGroups {
for class, feas := range classes {
switch feas {
case EvalComputedClassEligible:
elig[class] = true
case EvalComputedClassIneligible:
// Only mark as ineligible if it hasn't been marked before. This
// prevents one task group marking a class as ineligible when it
// is eligible on another task group.
if _, ok := elig[class]; !ok {
elig[class] = false
}
}
}
}
// Go through the job.
for class, feas := range e.job {
switch feas {
case EvalComputedClassEligible:
// Only mark as eligible if it hasn't been marked before. This
// prevents the job marking a class as eligible when it is ineligible
// to all the task groups.
if _, ok := elig[class]; !ok {
elig[class] = true
}
case EvalComputedClassIneligible:
elig[class] = false
}
}
return elig
}
// JobStatus returns the eligibility status of the job.
func (e *EvalEligibility) JobStatus(class string) ComputedClassFeasibility {
if e.jobEscaped {
return EvalComputedClassEscaped
}
if status, ok := e.job[class]; ok {
return status
}
return EvalComputedClassUnknown
}
// SetJobEligibility sets the eligibility status of the job for the computed
// node class.
func (e *EvalEligibility) SetJobEligibility(eligible bool, class string) {
if eligible {
e.job[class] = EvalComputedClassEligible
} else {
e.job[class] = EvalComputedClassIneligible
}
}
// TaskGroupStatus returns the eligibility status of the task group.
func (e *EvalEligibility) TaskGroupStatus(tg, class string) ComputedClassFeasibility {
if escaped, ok := e.tgEscapedConstraints[tg]; ok {
if escaped {
return EvalComputedClassEscaped
}
}
if classes, ok := e.taskGroups[tg]; ok {
if status, ok := classes[class]; ok {
return status
}
}
return EvalComputedClassUnknown
}
// SetTaskGroupEligibility sets the eligibility status of the task group for the
// computed node class.
func (e *EvalEligibility) SetTaskGroupEligibility(eligible bool, tg, class string) {
var eligibility ComputedClassFeasibility
if eligible {
eligibility = EvalComputedClassEligible
} else {
eligibility = EvalComputedClassIneligible
}
if classes, ok := e.taskGroups[tg]; ok {
classes[class] = eligibility
} else {
e.taskGroups[tg] = map[string]ComputedClassFeasibility{class: eligibility}
}
}
// SetQuotaLimitReached marks that the quota limit has been reached for the
// given quota
func (e *EvalEligibility) SetQuotaLimitReached(quota string) {
e.quotaReached = quota
}
// QuotaLimitReached returns the quota name if the quota limit has been reached.
func (e *EvalEligibility) QuotaLimitReached() string {
return e.quotaReached
}