2015-08-13 17:05:54 +00:00
|
|
|
package scheduler
|
|
|
|
|
2015-08-13 18:54:59 +00:00
|
|
|
import (
|
|
|
|
"log"
|
2015-10-13 03:15:07 +00:00
|
|
|
"regexp"
|
2015-08-13 18:54:59 +00:00
|
|
|
|
2015-10-13 03:15:07 +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
|
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
|
|
|
|
|
|
|
// 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)
|
2015-10-13 03:15:07 +00:00
|
|
|
|
|
|
|
// RegexpCache is a cache of regular expressions
|
|
|
|
RegexpCache() map[string]*regexp.Regexp
|
|
|
|
|
|
|
|
// ConstraintCache is a cache of version constraints
|
|
|
|
ConstraintCache() map[string]version.Constraints
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-10-13 03:15:07 +00:00
|
|
|
EvalCache
|
2015-08-14 01:34:04 +00:00
|
|
|
state State
|
|
|
|
plan *structs.Plan
|
|
|
|
logger *log.Logger
|
|
|
|
metrics *structs.AllocMetric
|
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{
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EvalContext) Logger() *log.Logger {
|
|
|
|
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) {
|
|
|
|
// 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)
|
|
|
|
|
2015-08-16 17:28:58 +00:00
|
|
|
// 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)
|
2015-08-16 17:28:58 +00:00
|
|
|
}
|
|
|
|
proposed = append(proposed, e.plan.NodeAllocation[nodeID]...)
|
|
|
|
|
|
|
|
// Ensure the return is not nil
|
|
|
|
if proposed == nil {
|
|
|
|
proposed = make([]*structs.Allocation, 0)
|
|
|
|
}
|
|
|
|
return proposed, nil
|
|
|
|
}
|