2015-08-13 17:05:54 +00:00
|
|
|
package scheduler
|
|
|
|
|
2015-08-13 18:54:59 +00:00
|
|
|
import (
|
2015-10-13 03:15:07 +00:00
|
|
|
"regexp"
|
2015-08-13 18:54:59 +00:00
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2017-02-08 04:31:23 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2015-08-13 18:54:59 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-08-13 17:13:11 +00:00
|
|
|
// Context is used to track contextual information used for placement
|
|
|
|
type Context interface {
|
2015-08-13 18:33:58 +00:00
|
|
|
// State is used to inspect the current global state
|
|
|
|
State() State
|
2015-08-13 18:54:59 +00:00
|
|
|
|
|
|
|
// Plan returns the current plan
|
|
|
|
Plan() *structs.Plan
|
|
|
|
|
|
|
|
// Logger provides a way to log
|
2018-09-15 23:23:13 +00:00
|
|
|
Logger() log.Logger
|
2015-08-14 01:34:04 +00:00
|
|
|
|
|
|
|
// Metrics returns the current metrics
|
|
|
|
Metrics() *structs.AllocMetric
|
|
|
|
|
|
|
|
// Reset is invoked after making a placement
|
|
|
|
Reset()
|
2015-08-16 17:28:58 +00:00
|
|
|
|
2019-12-03 04:22:22 +00:00
|
|
|
// ProposedAllocs returns the proposed allocations for a node which are
|
|
|
|
// the existing allocations, removing evictions, and adding any planned
|
|
|
|
// placements.
|
2015-08-16 17:28:58 +00:00
|
|
|
ProposedAllocs(nodeID string) ([]*structs.Allocation, error)
|
2015-10-13 03:15:07 +00:00
|
|
|
|
|
|
|
// RegexpCache is a cache of regular expressions
|
|
|
|
RegexpCache() map[string]*regexp.Regexp
|
|
|
|
|
2018-07-16 13:47:18 +00:00
|
|
|
// VersionConstraintCache is a cache of version constraints
|
2019-11-13 23:36:15 +00:00
|
|
|
VersionConstraintCache() map[string]VerConstraints
|
|
|
|
|
|
|
|
// SemverConstraintCache is a cache of semver constraints
|
|
|
|
SemverConstraintCache() map[string]VerConstraints
|
2016-01-25 19:45:48 +00:00
|
|
|
|
|
|
|
// Eligibility returns a tracker for node eligibility in the context of the
|
|
|
|
// eval.
|
|
|
|
Eligibility() *EvalEligibility
|
2015-10-13 03:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvalCache is used to cache certain things during an evaluation
|
|
|
|
type EvalCache struct {
|
2019-11-13 23:36:15 +00:00
|
|
|
reCache map[string]*regexp.Regexp
|
|
|
|
versionCache map[string]VerConstraints
|
|
|
|
semverCache map[string]VerConstraints
|
2015-10-13 03:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EvalCache) RegexpCache() map[string]*regexp.Regexp {
|
|
|
|
if e.reCache == nil {
|
|
|
|
e.reCache = make(map[string]*regexp.Regexp)
|
|
|
|
}
|
|
|
|
return e.reCache
|
|
|
|
}
|
2018-07-16 13:47:18 +00:00
|
|
|
|
2019-11-13 23:36:15 +00:00
|
|
|
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)
|
2015-10-13 03:15:07 +00:00
|
|
|
}
|
2019-11-13 23:36:15 +00:00
|
|
|
return e.semverCache
|
2015-08-13 17:13:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 17:05:54 +00:00
|
|
|
// EvalContext is a Context used during an Evaluation
|
|
|
|
type EvalContext struct {
|
2015-10-13 03:15:07 +00:00
|
|
|
EvalCache
|
2016-01-25 19:45:48 +00:00
|
|
|
state State
|
|
|
|
plan *structs.Plan
|
2018-09-15 23:23:13 +00:00
|
|
|
logger log.Logger
|
2016-01-25 19:45:48 +00:00
|
|
|
metrics *structs.AllocMetric
|
|
|
|
eligibility *EvalEligibility
|
2015-08-13 17:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEvalContext constructs a new EvalContext
|
2018-09-15 23:23:13 +00:00
|
|
|
func NewEvalContext(s State, p *structs.Plan, log log.Logger) *EvalContext {
|
2015-08-13 18:54:59 +00:00
|
|
|
ctx := &EvalContext{
|
2015-08-14 01:34:04 +00:00
|
|
|
state: s,
|
|
|
|
plan: p,
|
|
|
|
logger: log,
|
|
|
|
metrics: new(structs.AllocMetric),
|
2015-08-13 18:54:59 +00:00
|
|
|
}
|
2015-08-13 17:05:54 +00:00
|
|
|
return ctx
|
|
|
|
}
|
2015-08-13 18:33:58 +00:00
|
|
|
|
|
|
|
func (e *EvalContext) State() State {
|
|
|
|
return e.state
|
|
|
|
}
|
|
|
|
|
2015-08-13 18:54:59 +00:00
|
|
|
func (e *EvalContext) Plan() *structs.Plan {
|
|
|
|
return e.plan
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
func (e *EvalContext) Logger() log.Logger {
|
2015-08-13 18:54:59 +00:00
|
|
|
return e.logger
|
|
|
|
}
|
|
|
|
|
2015-08-14 01:34:04 +00:00
|
|
|
func (e *EvalContext) Metrics() *structs.AllocMetric {
|
|
|
|
return e.metrics
|
|
|
|
}
|
|
|
|
|
2015-08-13 18:33:58 +00:00
|
|
|
func (e *EvalContext) SetState(s State) {
|
|
|
|
e.state = s
|
|
|
|
}
|
2015-08-14 01:34:04 +00:00
|
|
|
|
|
|
|
func (e *EvalContext) Reset() {
|
|
|
|
e.metrics = new(structs.AllocMetric)
|
|
|
|
}
|
2015-08-16 17:28:58 +00:00
|
|
|
|
|
|
|
func (e *EvalContext) ProposedAllocs(nodeID string) ([]*structs.Allocation, error) {
|
2016-02-20 19:29:15 +00:00
|
|
|
// Get the existing allocations that are non-terminal
|
2017-02-08 04:31:23 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2019-12-03 04:22:22 +00:00
|
|
|
proposed, err := e.state.AllocsByNodeTerminal(ws, nodeID, false)
|
2015-08-16 17:28:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the proposed allocation by first removing allocations
|
|
|
|
// that are planned evictions and adding the new allocations.
|
2015-08-26 00:06:06 +00:00
|
|
|
if update := e.plan.NodeUpdate[nodeID]; len(update) > 0 {
|
2019-12-03 04:22:22 +00:00
|
|
|
proposed = structs.RemoveAllocs(proposed, update)
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 02:26:42 +00:00
|
|
|
// Remove any allocs that are being preempted
|
|
|
|
nodePreemptedAllocs := e.plan.NodePreemptions[nodeID]
|
|
|
|
if len(nodePreemptedAllocs) > 0 {
|
2019-12-03 04:22:22 +00:00
|
|
|
proposed = structs.RemoveAllocs(proposed, nodePreemptedAllocs)
|
2018-10-17 02:26:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 00:23:04 +00:00
|
|
|
// 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
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
2016-03-22 00:23:04 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:28:58 +00:00
|
|
|
return proposed, nil
|
|
|
|
}
|
2016-01-25 19:45:48 +00:00
|
|
|
|
|
|
|
func (e *EvalContext) Eligibility() *EvalEligibility {
|
|
|
|
if e.eligibility == nil {
|
|
|
|
e.eligibility = NewEvalEligibility()
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.eligibility
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
type ComputedClassFeasibility byte
|
2016-01-25 19:45:48 +00:00
|
|
|
|
|
|
|
const (
|
2016-01-27 00:43:42 +00:00
|
|
|
// EvalComputedClassUnknown is the initial state until the eligibility has
|
2016-05-15 16:41:34 +00:00
|
|
|
// been explicitly marked to eligible/ineligible or escaped.
|
2016-01-27 00:43:42 +00:00
|
|
|
EvalComputedClassUnknown ComputedClassFeasibility = iota
|
|
|
|
|
|
|
|
// EvalComputedClassIneligible is used to mark the computed class as
|
|
|
|
// ineligible for the evaluation.
|
2016-01-25 19:45:48 +00:00
|
|
|
EvalComputedClassIneligible
|
2016-01-27 00:43:42 +00:00
|
|
|
|
|
|
|
// EvalComputedClassIneligible is used to mark the computed class as
|
|
|
|
// eligible for the evaluation.
|
2016-01-25 19:45:48 +00:00
|
|
|
EvalComputedClassEligible
|
2016-01-27 00:43:42 +00:00
|
|
|
|
|
|
|
// EvalComputedClassEscaped signals that computed class can not determine
|
|
|
|
// eligibility because a constraint exists that is not captured by computed
|
|
|
|
// node classes.
|
2016-01-25 19:45:48 +00:00
|
|
|
EvalComputedClassEscaped
|
|
|
|
)
|
|
|
|
|
|
|
|
// EvalEligibility tracks eligibility of nodes by computed node class over the
|
|
|
|
// course of an evaluation.
|
|
|
|
type EvalEligibility struct {
|
2016-01-27 00:43:42 +00:00
|
|
|
// job tracks the eligibility at the job level per computed node class.
|
2016-01-30 01:46:44 +00:00
|
|
|
job map[string]ComputedClassFeasibility
|
2016-01-25 19:45:48 +00:00
|
|
|
|
2016-01-27 01:34:41 +00:00
|
|
|
// jobEscaped marks whether constraints have escaped at the job level.
|
|
|
|
jobEscaped bool
|
2016-01-25 19:45:48 +00:00
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
// taskGroups tracks the eligibility at the task group level per computed
|
2016-01-25 19:45:48 +00:00
|
|
|
// node class.
|
2016-01-30 01:46:44 +00:00
|
|
|
taskGroups map[string]map[string]ComputedClassFeasibility
|
2016-01-25 19:45:48 +00:00
|
|
|
|
2016-01-27 01:34:41 +00:00
|
|
|
// tgEscapedConstraints is a map of task groups to whether constraints have
|
|
|
|
// escaped.
|
|
|
|
tgEscapedConstraints map[string]bool
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// quotaReached marks that the quota limit has been reached for the given
|
|
|
|
// quota
|
|
|
|
quotaReached string
|
2016-01-25 19:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEvalEligibility returns an eligibility tracker for the context of an evaluation.
|
|
|
|
func NewEvalEligibility() *EvalEligibility {
|
|
|
|
return &EvalEligibility{
|
2016-01-30 01:46:44 +00:00
|
|
|
job: make(map[string]ComputedClassFeasibility),
|
|
|
|
taskGroups: make(map[string]map[string]ComputedClassFeasibility),
|
2016-01-27 01:34:41 +00:00
|
|
|
tgEscapedConstraints: make(map[string]bool),
|
2016-01-25 19:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-01-27 01:34:41 +00:00
|
|
|
// Determine whether the job has escaped constraints.
|
|
|
|
e.jobEscaped = len(structs.EscapedConstraints(job.Constraints)) != 0
|
2016-01-25 19:45:48 +00:00
|
|
|
|
|
|
|
// 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...)
|
|
|
|
}
|
|
|
|
|
2016-01-27 01:34:41 +00:00
|
|
|
e.tgEscapedConstraints[tg.Name] = len(structs.EscapedConstraints(constraints)) != 0
|
2016-01-25 19:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 01:34:41 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
// 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)
|
2016-01-28 21:43:48 +00:00
|
|
|
|
|
|
|
// Go through the task groups.
|
|
|
|
for _, classes := range e.taskGroups {
|
|
|
|
for class, feas := range classes {
|
|
|
|
switch feas {
|
|
|
|
case EvalComputedClassEligible:
|
2016-01-30 01:46:44 +00:00
|
|
|
elig[class] = true
|
2016-01-28 21:43:48 +00:00
|
|
|
case EvalComputedClassIneligible:
|
2016-02-01 04:56:52 +00:00
|
|
|
// 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
|
|
|
|
}
|
2016-01-28 21:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:08:23 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:46:44 +00:00
|
|
|
return elig
|
2016-01-28 21:43:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 19:45:48 +00:00
|
|
|
// JobStatus returns the eligibility status of the job.
|
2016-01-30 01:46:44 +00:00
|
|
|
func (e *EvalEligibility) JobStatus(class string) ComputedClassFeasibility {
|
2019-06-25 20:26:35 +00:00
|
|
|
if e.jobEscaped {
|
2016-01-25 19:45:48 +00:00
|
|
|
return EvalComputedClassEscaped
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
if status, ok := e.job[class]; ok {
|
2016-01-25 19:45:48 +00:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
return EvalComputedClassUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetJobEligibility sets the eligibility status of the job for the computed
|
|
|
|
// node class.
|
2016-01-30 01:46:44 +00:00
|
|
|
func (e *EvalEligibility) SetJobEligibility(eligible bool, class string) {
|
2016-01-25 19:45:48 +00:00
|
|
|
if eligible {
|
2016-01-27 00:43:42 +00:00
|
|
|
e.job[class] = EvalComputedClassEligible
|
2016-01-25 19:45:48 +00:00
|
|
|
} else {
|
2016-01-27 00:43:42 +00:00
|
|
|
e.job[class] = EvalComputedClassIneligible
|
2016-01-25 19:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskGroupStatus returns the eligibility status of the task group.
|
2016-01-30 01:46:44 +00:00
|
|
|
func (e *EvalEligibility) TaskGroupStatus(tg, class string) ComputedClassFeasibility {
|
2016-01-27 00:43:42 +00:00
|
|
|
if escaped, ok := e.tgEscapedConstraints[tg]; ok {
|
2016-01-27 01:34:41 +00:00
|
|
|
if escaped {
|
2016-01-25 19:45:48 +00:00
|
|
|
return EvalComputedClassEscaped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
if classes, ok := e.taskGroups[tg]; ok {
|
2016-01-25 19:45:48 +00:00
|
|
|
if status, ok := classes[class]; ok {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EvalComputedClassUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTaskGroupEligibility sets the eligibility status of the task group for the
|
|
|
|
// computed node class.
|
2016-01-30 01:46:44 +00:00
|
|
|
func (e *EvalEligibility) SetTaskGroupEligibility(eligible bool, tg, class string) {
|
2016-01-27 00:43:42 +00:00
|
|
|
var eligibility ComputedClassFeasibility
|
2016-01-25 19:45:48 +00:00
|
|
|
if eligible {
|
|
|
|
eligibility = EvalComputedClassEligible
|
|
|
|
} else {
|
|
|
|
eligibility = EvalComputedClassIneligible
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
if classes, ok := e.taskGroups[tg]; ok {
|
2016-01-25 19:45:48 +00:00
|
|
|
classes[class] = eligibility
|
|
|
|
} else {
|
2016-01-30 01:46:44 +00:00
|
|
|
e.taskGroups[tg] = map[string]ComputedClassFeasibility{class: eligibility}
|
2016-01-25 19:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|