open-nomad/scheduler/context.go

270 lines
7.6 KiB
Go
Raw Normal View History

2015-08-13 17:05:54 +00:00
package scheduler
2015-08-13 18:54:59 +00:00
import (
"log"
"regexp"
2015-08-13 18:54:59 +00:00
"github.com/hashicorp/go-version"
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
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 is 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
// ConstraintCache is a cache of version constraints
ConstraintCache() map[string]version.Constraints
2016-01-25 19:45:48 +00:00
// 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
constraintCache map[string]version.Constraints
}
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) ConstraintCache() map[string]version.Constraints {
if e.constraintCache == nil {
e.constraintCache = make(map[string]version.Constraints)
}
return e.constraintCache
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 {
EvalCache
2016-01-25 19:45:48 +00:00
state State
plan *structs.Plan
logger *log.Logger
metrics *structs.AllocMetric
eligibility *EvalEligibility
2015-08-13 17:05:54 +00:00
}
// NewEvalContext constructs a new EvalContext
2015-08-13 18:54:59 +00:00
func NewEvalContext(s State, p *structs.Plan, log *log.Logger) *EvalContext {
ctx := &EvalContext{
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
}
func (e *EvalContext) Logger() *log.Logger {
return e.logger
}
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
}
func (e *EvalContext) Reset() {
e.metrics = new(structs.AllocMetric)
}
func (e *EvalContext) ProposedAllocs(nodeID string) ([]*structs.Allocation, error) {
// Get the existing allocations
existingAlloc, err := e.state.AllocsByNode(nodeID)
if err != nil {
return nil, err
}
2015-08-23 01:30:49 +00:00
// Filter on alloc state
existingAlloc = structs.FilterTerminalAllocs(existingAlloc)
// Determine the proposed allocation by first removing allocations
// that are planned evictions and adding the new allocations.
proposed := existingAlloc
2015-08-26 00:06:06 +00:00
if update := e.plan.NodeUpdate[nodeID]; len(update) > 0 {
proposed = structs.RemoveAllocs(existingAlloc, update)
}
proposed = append(proposed, e.plan.NodeAllocation[nodeID]...)
// Ensure the return is not nil
if proposed == nil {
proposed = make([]*structs.Allocation, 0)
}
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
// been explicitely marked to eligible/ineligible or escaped.
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.
job map[uint64]ComputedClassFeasibility
2016-01-25 19:45:48 +00:00
2016-01-27 00:43:42 +00:00
// jobEscapedConstraints tracks escaped constraints at the job level.
jobEscapedConstraints []*structs.Constraint
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-27 00:43:42 +00:00
taskGroups map[string]map[uint64]ComputedClassFeasibility
2016-01-25 19:45:48 +00:00
2016-01-27 00:43:42 +00:00
// tgEscapedConstraints is a map of task groups to a set of constraints that
2016-01-25 19:45:48 +00:00
// have escaped.
2016-01-27 00:43:42 +00:00
tgEscapedConstraints map[string][]*structs.Constraint
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-27 00:43:42 +00:00
job: make(map[uint64]ComputedClassFeasibility),
taskGroups: make(map[string]map[uint64]ComputedClassFeasibility),
tgEscapedConstraints: make(map[string][]*structs.Constraint),
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) {
// Determine the escaped constraints for the job.
2016-01-27 00:43:42 +00:00
e.jobEscapedConstraints = structs.EscapedConstraints(job.Constraints)
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 00:43:42 +00:00
e.tgEscapedConstraints[tg.Name] = structs.EscapedConstraints(constraints)
2016-01-25 19:45:48 +00:00
}
}
// JobStatus returns the eligibility status of the job.
2016-01-27 00:43:42 +00:00
func (e *EvalEligibility) JobStatus(class uint64) ComputedClassFeasibility {
// COMPAT: Computed node class was introduced in 0.3. Clients running < 0.3
// will not have a computed class. The safest value to return is the escaped
// case, since it disables any optimization.
if len(e.jobEscapedConstraints) != 0 || class == 0 {
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.
func (e *EvalEligibility) SetJobEligibility(eligible bool, class uint64) {
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-27 00:43:42 +00:00
func (e *EvalEligibility) TaskGroupStatus(tg string, class uint64) ComputedClassFeasibility {
// COMPAT: Computed node class was introduced in 0.3. Clients running < 0.3
// will not have a computed class. The safest value to return is the escaped
// case, since it disables any optimization.
if class == 0 {
return EvalComputedClassEscaped
}
if escaped, ok := e.tgEscapedConstraints[tg]; ok {
2016-01-25 19:45:48 +00:00
if len(escaped) != 0 {
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.
func (e *EvalEligibility) SetTaskGroupEligibility(eligible bool, tg string, class uint64) {
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-27 00:43:42 +00:00
e.taskGroups[tg] = map[uint64]ComputedClassFeasibility{class: eligibility}
2016-01-25 19:45:48 +00:00
}
}