2015-08-14 00:48:26 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2015-08-14 04:46:33 +00:00
|
|
|
"time"
|
2015-08-14 00:48:26 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-08-16 17:37:11 +00:00
|
|
|
const (
|
|
|
|
// serviceJobAntiAffinityPenalty is the penalty applied
|
|
|
|
// to the score for placing an alloc on a node that
|
|
|
|
// already has an alloc for this job.
|
|
|
|
serviceJobAntiAffinityPenalty = 10.0
|
|
|
|
|
|
|
|
// batchJobAntiAffinityPenalty is the same as the
|
|
|
|
// serviceJobAntiAffinityPenalty but for batch type jobs.
|
|
|
|
batchJobAntiAffinityPenalty = 5.0
|
|
|
|
)
|
|
|
|
|
2015-08-14 01:44:27 +00:00
|
|
|
// Stack is a chained collection of iterators. The stack is used to
|
|
|
|
// make placement decisions. Different schedulers may customize the
|
|
|
|
// stack they use to vary the way placements are made.
|
2015-08-14 00:48:26 +00:00
|
|
|
type Stack interface {
|
2015-09-07 18:30:13 +00:00
|
|
|
// SetNodes is used to set the base set of potential nodes
|
|
|
|
SetNodes([]*structs.Node)
|
|
|
|
|
2015-08-14 00:48:26 +00:00
|
|
|
// SetTaskGroup is used to set the job for selection
|
|
|
|
SetJob(job *structs.Job)
|
|
|
|
|
|
|
|
// Select is used to select a node for the task group
|
2015-08-14 01:44:27 +00:00
|
|
|
Select(tg *structs.TaskGroup) (*RankedNode, *structs.Resources)
|
2015-08-14 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 05:35:48 +00:00
|
|
|
// GenericStack is the Stack used for the Generic scheduler. It is
|
2015-08-14 01:44:27 +00:00
|
|
|
// designed to make better placement decisions at the cost of performance.
|
2015-08-14 05:35:48 +00:00
|
|
|
type GenericStack struct {
|
2016-01-26 18:07:33 +00:00
|
|
|
batch bool
|
|
|
|
ctx Context
|
|
|
|
source *StaticIterator
|
|
|
|
|
|
|
|
wrappedChecks *FeasibilityWrapper
|
|
|
|
jobConstraint *ConstraintChecker
|
|
|
|
taskGroupDrivers *DriverChecker
|
|
|
|
taskGroupConstraint *ConstraintChecker
|
|
|
|
|
2015-10-26 21:12:54 +00:00
|
|
|
proposedAllocConstraint *ProposedAllocConstraintIterator
|
|
|
|
binPack *BinPackIterator
|
|
|
|
jobAntiAff *JobAntiAffinityIterator
|
|
|
|
limit *LimitIterator
|
|
|
|
maxScore *MaxScoreIterator
|
2015-08-14 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 05:35:48 +00:00
|
|
|
// NewGenericStack constructs a stack used for selecting service placements
|
2015-10-17 00:05:23 +00:00
|
|
|
func NewGenericStack(batch bool, ctx Context) *GenericStack {
|
2015-08-14 00:48:26 +00:00
|
|
|
// Create a new stack
|
2015-09-11 19:03:41 +00:00
|
|
|
s := &GenericStack{
|
2015-08-14 05:35:48 +00:00
|
|
|
batch: batch,
|
|
|
|
ctx: ctx,
|
2015-08-14 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the source iterator. We randomize the order we visit nodes
|
|
|
|
// to reduce collisions between schedulers and to do a basic load
|
|
|
|
// balancing across eligible nodes.
|
2015-10-17 00:05:23 +00:00
|
|
|
s.source = NewRandomIterator(ctx, nil)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
|
|
|
// Attach the job constraints. The job is filled in later.
|
2016-01-26 18:07:33 +00:00
|
|
|
s.jobConstraint = NewConstraintChecker(ctx, nil)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
|
|
|
// Filter on task group drivers first as they are faster
|
2016-01-26 18:07:33 +00:00
|
|
|
s.taskGroupDrivers = NewDriverChecker(ctx, nil)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
|
|
|
// Filter on task group constraints second
|
2016-01-26 18:07:33 +00:00
|
|
|
s.taskGroupConstraint = NewConstraintChecker(ctx, nil)
|
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
// Create the feasibility wrapper which wraps all feasibility checks in
|
|
|
|
// which feasibility checking can be skipped if the computed node class has
|
|
|
|
// previously been marked as eligible or ineligible. Generally this will be
|
|
|
|
// checks that only needs to examine the single node to determine feasibility.
|
2016-01-26 18:07:33 +00:00
|
|
|
jobs := []FeasibilityChecker{s.jobConstraint}
|
|
|
|
tgs := []FeasibilityChecker{s.taskGroupDrivers, s.taskGroupConstraint}
|
|
|
|
s.wrappedChecks = NewFeasibilityWrapper(ctx, s.source, jobs, tgs)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
2015-10-26 21:12:54 +00:00
|
|
|
// Filter on constraints that are affected by propsed allocations.
|
2016-01-26 18:07:33 +00:00
|
|
|
s.proposedAllocConstraint = NewProposedAllocConstraintIterator(ctx, s.wrappedChecks)
|
2015-10-22 22:09:03 +00:00
|
|
|
|
2015-08-14 00:48:26 +00:00
|
|
|
// Upgrade from feasible to rank iterator
|
2015-10-26 21:12:54 +00:00
|
|
|
rankSource := NewFeasibleRankIterator(ctx, s.proposedAllocConstraint)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
2015-08-14 05:35:48 +00:00
|
|
|
// Apply the bin packing, this depends on the resources needed
|
|
|
|
// by a particular task group. Only enable eviction for the service
|
|
|
|
// scheduler as that logic is expensive.
|
|
|
|
evict := !batch
|
2015-09-13 21:31:32 +00:00
|
|
|
s.binPack = NewBinPackIterator(ctx, rankSource, evict, 0)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
2015-08-16 17:37:11 +00:00
|
|
|
// Apply the job anti-affinity iterator. This is to avoid placing
|
|
|
|
// multiple allocations on the same node for this job. The penalty
|
|
|
|
// is less for batch jobs as it matters less.
|
|
|
|
penalty := serviceJobAntiAffinityPenalty
|
|
|
|
if batch {
|
|
|
|
penalty = batchJobAntiAffinityPenalty
|
|
|
|
}
|
2015-09-11 19:03:41 +00:00
|
|
|
s.jobAntiAff = NewJobAntiAffinityIterator(ctx, s.binPack, penalty, "")
|
2015-08-16 17:37:11 +00:00
|
|
|
|
2015-08-14 00:48:26 +00:00
|
|
|
// Apply a limit function. This is to avoid scanning *every* possible node.
|
2015-09-23 05:20:07 +00:00
|
|
|
s.limit = NewLimitIterator(ctx, s.jobAntiAff, 2)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
|
|
|
// Select the node with the maximum score for placement
|
2015-09-11 19:03:41 +00:00
|
|
|
s.maxScore = NewMaxScoreIterator(ctx, s.limit)
|
|
|
|
return s
|
2015-08-14 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 18:30:13 +00:00
|
|
|
func (s *GenericStack) SetNodes(baseNodes []*structs.Node) {
|
|
|
|
// Shuffle base nodes
|
|
|
|
shuffleNodes(baseNodes)
|
|
|
|
|
|
|
|
// Update the set of base nodes
|
|
|
|
s.source.SetNodes(baseNodes)
|
2015-09-11 19:03:41 +00:00
|
|
|
|
|
|
|
// Apply a limit function. This is to avoid scanning *every* possible node.
|
|
|
|
// For batch jobs we only need to evaluate 2 options and depend on the
|
2015-10-14 23:43:06 +00:00
|
|
|
// power of two choices. For services jobs we need to visit "enough".
|
2015-09-11 19:03:41 +00:00
|
|
|
// Using a log of the total number of nodes is a good restriction, with
|
|
|
|
// at least 2 as the floor
|
|
|
|
limit := 2
|
|
|
|
if n := len(baseNodes); !s.batch && n > 0 {
|
|
|
|
logLimit := int(math.Ceil(math.Log2(float64(n))))
|
|
|
|
if logLimit > limit {
|
|
|
|
limit = logLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.limit.SetLimit(limit)
|
2015-09-07 18:30:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 05:35:48 +00:00
|
|
|
func (s *GenericStack) SetJob(job *structs.Job) {
|
2015-08-14 01:44:27 +00:00
|
|
|
s.jobConstraint.SetConstraints(job.Constraints)
|
2015-10-26 21:12:54 +00:00
|
|
|
s.proposedAllocConstraint.SetJob(job)
|
2015-08-14 01:44:27 +00:00
|
|
|
s.binPack.SetPriority(job.Priority)
|
2015-08-16 17:37:11 +00:00
|
|
|
s.jobAntiAff.SetJob(job.ID)
|
2016-01-26 18:07:33 +00:00
|
|
|
s.ctx.Eligibility().SetJob(job)
|
2015-08-14 00:48:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 05:35:48 +00:00
|
|
|
func (s *GenericStack) Select(tg *structs.TaskGroup) (*RankedNode, *structs.Resources) {
|
2015-08-14 01:44:27 +00:00
|
|
|
// Reset the max selector and context
|
|
|
|
s.maxScore.Reset()
|
|
|
|
s.ctx.Reset()
|
2015-08-14 04:46:33 +00:00
|
|
|
start := time.Now()
|
2015-08-14 01:44:27 +00:00
|
|
|
|
2015-10-16 21:00:51 +00:00
|
|
|
// Get the task groups constraints.
|
|
|
|
tgConstr := taskGroupConstraints(tg)
|
2015-08-14 00:48:26 +00:00
|
|
|
|
|
|
|
// Update the parameters of iterators
|
2015-10-16 21:00:51 +00:00
|
|
|
s.taskGroupDrivers.SetDrivers(tgConstr.drivers)
|
|
|
|
s.taskGroupConstraint.SetConstraints(tgConstr.constraints)
|
2015-10-26 21:12:54 +00:00
|
|
|
s.proposedAllocConstraint.SetTaskGroup(tg)
|
2015-09-13 21:31:32 +00:00
|
|
|
s.binPack.SetTasks(tg.Tasks)
|
2015-08-14 01:36:13 +00:00
|
|
|
|
2015-08-14 04:46:33 +00:00
|
|
|
// Find the node with the max score
|
|
|
|
option := s.maxScore.Next()
|
|
|
|
|
2015-09-13 22:20:50 +00:00
|
|
|
// Ensure that the task resources were specified
|
|
|
|
if option != nil && len(option.TaskResources) != len(tg.Tasks) {
|
|
|
|
for _, task := range tg.Tasks {
|
|
|
|
option.SetTaskResources(task, task.Resources)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:46:33 +00:00
|
|
|
// Store the compute time
|
|
|
|
s.ctx.Metrics().AllocationTime = time.Since(start)
|
2015-10-16 21:00:51 +00:00
|
|
|
return option, tgConstr.size
|
2015-08-14 00:48:26 +00:00
|
|
|
}
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// SystemStack is the Stack used for the System scheduler. It is designed to
|
|
|
|
// attempt to make placements on all nodes.
|
|
|
|
type SystemStack struct {
|
|
|
|
ctx Context
|
|
|
|
source *StaticIterator
|
2016-01-26 18:07:33 +00:00
|
|
|
wrappedChecks *FeasibilityWrapper
|
|
|
|
jobConstraint *ConstraintChecker
|
|
|
|
taskGroupDrivers *DriverChecker
|
|
|
|
taskGroupConstraint *ConstraintChecker
|
2015-10-14 23:43:06 +00:00
|
|
|
binPack *BinPackIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSystemStack constructs a stack used for selecting service placements
|
2015-10-17 00:05:23 +00:00
|
|
|
func NewSystemStack(ctx Context) *SystemStack {
|
2015-10-14 23:43:06 +00:00
|
|
|
// Create a new stack
|
|
|
|
s := &SystemStack{ctx: ctx}
|
|
|
|
|
|
|
|
// Create the source iterator. We visit nodes in a linear order because we
|
|
|
|
// have to evaluate on all nodes.
|
2015-10-17 00:05:23 +00:00
|
|
|
s.source = NewStaticIterator(ctx, nil)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// Attach the job constraints. The job is filled in later.
|
2016-01-26 18:07:33 +00:00
|
|
|
s.jobConstraint = NewConstraintChecker(ctx, nil)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// Filter on task group drivers first as they are faster
|
2016-01-26 18:07:33 +00:00
|
|
|
s.taskGroupDrivers = NewDriverChecker(ctx, nil)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// Filter on task group constraints second
|
2016-01-26 18:07:33 +00:00
|
|
|
s.taskGroupConstraint = NewConstraintChecker(ctx, nil)
|
|
|
|
|
2016-01-27 00:43:42 +00:00
|
|
|
// Create the feasibility wrapper which wraps all feasibility checks in
|
|
|
|
// which feasibility checking can be skipped if the computed node class has
|
|
|
|
// previously been marked as eligible or ineligible. Generally this will be
|
|
|
|
// checks that only needs to examine the single node to determine feasibility.
|
2016-01-26 18:07:33 +00:00
|
|
|
jobs := []FeasibilityChecker{s.jobConstraint}
|
|
|
|
tgs := []FeasibilityChecker{s.taskGroupDrivers, s.taskGroupConstraint}
|
|
|
|
s.wrappedChecks = NewFeasibilityWrapper(ctx, s.source, jobs, tgs)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// Upgrade from feasible to rank iterator
|
2016-01-26 18:07:33 +00:00
|
|
|
rankSource := NewFeasibleRankIterator(ctx, s.wrappedChecks)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// Apply the bin packing, this depends on the resources needed
|
|
|
|
// by a particular task group. Enable eviction as system jobs are high
|
|
|
|
// priority.
|
|
|
|
s.binPack = NewBinPackIterator(ctx, rankSource, true, 0)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SystemStack) SetNodes(baseNodes []*structs.Node) {
|
|
|
|
// Update the set of base nodes
|
|
|
|
s.source.SetNodes(baseNodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SystemStack) SetJob(job *structs.Job) {
|
|
|
|
s.jobConstraint.SetConstraints(job.Constraints)
|
|
|
|
s.binPack.SetPriority(job.Priority)
|
2016-01-26 18:07:33 +00:00
|
|
|
s.ctx.Eligibility().SetJob(job)
|
2015-10-14 23:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SystemStack) Select(tg *structs.TaskGroup) (*RankedNode, *structs.Resources) {
|
|
|
|
// Reset the binpack selector and context
|
|
|
|
s.binPack.Reset()
|
|
|
|
s.ctx.Reset()
|
|
|
|
start := time.Now()
|
|
|
|
|
2015-10-16 21:00:51 +00:00
|
|
|
// Get the task groups constraints.
|
|
|
|
tgConstr := taskGroupConstraints(tg)
|
2015-10-14 23:43:06 +00:00
|
|
|
|
|
|
|
// Update the parameters of iterators
|
2015-10-16 21:00:51 +00:00
|
|
|
s.taskGroupDrivers.SetDrivers(tgConstr.drivers)
|
|
|
|
s.taskGroupConstraint.SetConstraints(tgConstr.constraints)
|
2015-10-14 23:43:06 +00:00
|
|
|
s.binPack.SetTasks(tg.Tasks)
|
|
|
|
|
|
|
|
// Get the next option that satisfies the constraints.
|
|
|
|
option := s.binPack.Next()
|
|
|
|
|
|
|
|
// Ensure that the task resources were specified
|
|
|
|
if option != nil && len(option.TaskResources) != len(tg.Tasks) {
|
|
|
|
for _, task := range tg.Tasks {
|
|
|
|
option.SetTaskResources(task, task.Resources)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the compute time
|
|
|
|
s.ctx.Metrics().AllocationTime = time.Since(start)
|
2015-10-16 21:00:51 +00:00
|
|
|
return option, tgConstr.size
|
2015-10-14 23:43:06 +00:00
|
|
|
}
|