Cleanup DynamicConstraintIterator

This commit is contained in:
Alex Dadgar 2015-10-26 14:01:32 -07:00
parent fd9c2baf02
commit a9135b92b2
1 changed files with 10 additions and 22 deletions

View File

@ -187,10 +187,6 @@ func (iter *DynamicConstraintIterator) SetJob(job *structs.Job) {
} }
func (iter *DynamicConstraintIterator) hasDistinctHostsConstraint(constraints []*structs.Constraint) bool { func (iter *DynamicConstraintIterator) hasDistinctHostsConstraint(constraints []*structs.Constraint) bool {
if constraints == nil {
return false
}
for _, con := range constraints { for _, con := range constraints {
if con.Operand == structs.ConstraintDistinctHosts { if con.Operand == structs.ConstraintDistinctHosts {
return true return true
@ -200,16 +196,6 @@ func (iter *DynamicConstraintIterator) hasDistinctHostsConstraint(constraints []
} }
func (iter *DynamicConstraintIterator) Next() *structs.Node { func (iter *DynamicConstraintIterator) Next() *structs.Node {
if iter.job == nil {
iter.ctx.Logger().Printf("[ERR] scheduler.dynamic-constraint: job not set")
return nil
}
if iter.tg == nil {
iter.ctx.Logger().Printf("[ERR] scheduler.dynamic-constraint: task group not set")
return nil
}
for { for {
// Get the next option from the source // Get the next option from the source
option := iter.source.Next() option := iter.source.Next()
@ -219,7 +205,7 @@ func (iter *DynamicConstraintIterator) Next() *structs.Node {
return option return option
} }
if !iter.satisfiesDistinctHosts(option, iter.jobDistinctHosts) { if !iter.satisfiesDistinctHosts(option) {
iter.ctx.Metrics().FilterNode(option, structs.ConstraintDistinctHosts) iter.ctx.Metrics().FilterNode(option, structs.ConstraintDistinctHosts)
continue continue
} }
@ -230,7 +216,12 @@ func (iter *DynamicConstraintIterator) Next() *structs.Node {
// satisfiesDistinctHosts checks if the node satisfies a distinct_hosts // satisfiesDistinctHosts checks if the node satisfies a distinct_hosts
// constraint either specified at the job level or the TaskGroup level. // constraint either specified at the job level or the TaskGroup level.
func (iter *DynamicConstraintIterator) satisfiesDistinctHosts(option *structs.Node, job bool) bool { func (iter *DynamicConstraintIterator) satisfiesDistinctHosts(option *structs.Node) bool {
// Check if there is no constraint set.
if !(iter.jobDistinctHosts || iter.tgDistinctHosts) {
return true
}
// Get the proposed allocations // Get the proposed allocations
proposed, err := iter.ctx.ProposedAllocs(option.ID) proposed, err := iter.ctx.ProposedAllocs(option.ID)
if err != nil { if err != nil {
@ -241,15 +232,12 @@ func (iter *DynamicConstraintIterator) satisfiesDistinctHosts(option *structs.No
// Skip the node if the task group has already been allocated on it. // Skip the node if the task group has already been allocated on it.
for _, alloc := range proposed { for _, alloc := range proposed {
jobCollision := alloc.JobID == iter.job.ID
taskCollision := alloc.TaskGroup == iter.tg.Name
// If the job has a distinct_hosts constraint we only need an alloc // If the job has a distinct_hosts constraint we only need an alloc
// collision on the JobID but if the constraint is on the TaskGroup then // collision on the JobID but if the constraint is on the TaskGroup then
// we need both a job and TaskGroup collision. // we need both a job and TaskGroup collision.
jobInvalid := job && jobCollision jobCollision := alloc.JobID == iter.job.ID
tgInvalid := !job && jobCollision && taskCollision taskCollision := alloc.TaskGroup == iter.tg.Name
if jobInvalid || tgInvalid { if iter.jobDistinctHosts && jobCollision || jobCollision && taskCollision {
return false return false
} }
} }