-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "distinctHost" constraint #321
Changes from all commits
3ae9340
9572878
7127f6e
9f259c2
a52bdd9
db90849
bd10a6b
ec78455
cccef7a
302c989
2ab5790
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
job "foo" { | ||
constraint { | ||
distinct_hosts = "true" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,106 @@ func (iter *DriverIterator) hasDrivers(option *structs.Node) bool { | |
return true | ||
} | ||
|
||
// ProposedAllocConstraintIterator is a FeasibleIterator which returns nodes that | ||
// match constraints that are not static such as Node attributes but are | ||
// effected by proposed alloc placements. Examples are distinct_hosts and | ||
// tenancy constraints. This is used to filter on job and task group | ||
// constraints. | ||
type ProposedAllocConstraintIterator struct { | ||
ctx Context | ||
source FeasibleIterator | ||
tg *structs.TaskGroup | ||
job *structs.Job | ||
|
||
// Store whether the Job or TaskGroup has a distinct_hosts constraints so | ||
// they don't have to be calculated every time Next() is called. | ||
tgDistinctHosts bool | ||
jobDistinctHosts bool | ||
} | ||
|
||
// NewProposedAllocConstraintIterator creates a ProposedAllocConstraintIterator | ||
// from a source. | ||
func NewProposedAllocConstraintIterator(ctx Context, source FeasibleIterator) *ProposedAllocConstraintIterator { | ||
iter := &ProposedAllocConstraintIterator{ | ||
ctx: ctx, | ||
source: source, | ||
} | ||
return iter | ||
} | ||
|
||
func (iter *ProposedAllocConstraintIterator) SetTaskGroup(tg *structs.TaskGroup) { | ||
iter.tg = tg | ||
iter.tgDistinctHosts = iter.hasDistinctHostsConstraint(tg.Constraints) | ||
} | ||
|
||
func (iter *ProposedAllocConstraintIterator) SetJob(job *structs.Job) { | ||
iter.job = job | ||
iter.jobDistinctHosts = iter.hasDistinctHostsConstraint(job.Constraints) | ||
} | ||
|
||
func (iter *ProposedAllocConstraintIterator) hasDistinctHostsConstraint(constraints []*structs.Constraint) bool { | ||
for _, con := range constraints { | ||
if con.Operand == structs.ConstraintDistinctHosts { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (iter *ProposedAllocConstraintIterator) Next() *structs.Node { | ||
for { | ||
// Get the next option from the source | ||
option := iter.source.Next() | ||
|
||
// Hot-path if the option is nil or there are no distinct_hosts constraints. | ||
if option == nil || !(iter.jobDistinctHosts || iter.tgDistinctHosts) { | ||
return option | ||
} | ||
|
||
if !iter.satisfiesDistinctHosts(option) { | ||
iter.ctx.Metrics().FilterNode(option, structs.ConstraintDistinctHosts) | ||
continue | ||
} | ||
|
||
return option | ||
} | ||
} | ||
|
||
// satisfiesDistinctHosts checks if the node satisfies a distinct_hosts | ||
// constraint either specified at the job level or the TaskGroup level. | ||
func (iter *ProposedAllocConstraintIterator) satisfiesDistinctHosts(option *structs.Node) bool { | ||
// Check if there is no constraint set. | ||
if !(iter.jobDistinctHosts || iter.tgDistinctHosts) { | ||
return true | ||
} | ||
|
||
// Get the proposed allocations | ||
proposed, err := iter.ctx.ProposedAllocs(option.ID) | ||
if err != nil { | ||
iter.ctx.Logger().Printf( | ||
"[ERR] scheduler.dynamic-constraint: failed to get proposed allocations: %v", err) | ||
return false | ||
} | ||
|
||
// Skip the node if the task group has already been allocated on it. | ||
for _, alloc := range proposed { | ||
// 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 | ||
// we need both a job and TaskGroup collision. | ||
jobCollision := alloc.JobID == iter.job.ID | ||
taskCollision := alloc.TaskGroup == iter.tg.Name | ||
if iter.jobDistinctHosts && jobCollision || jobCollision && taskCollision { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (iter *ProposedAllocConstraintIterator) Reset() { | ||
iter.source.Reset() | ||
} | ||
|
||
// ConstraintIterator is a FeasibleIterator which returns nodes | ||
// that match a given set of constraints. This is used to filter | ||
// on job, task group, and task constraints. | ||
|
@@ -257,16 +357,24 @@ func resolveConstraintTarget(target string, node *structs.Node) (interface{}, bo | |
|
||
// checkConstraint checks if a constraint is satisfied | ||
func checkConstraint(ctx Context, operand string, lVal, rVal interface{}) bool { | ||
// Check for constraints not handled by this iterator. | ||
switch operand { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not merge this with the next one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we add more constraints that are not handled here I want it to be easily distinguishable which iterator is handling which constraints. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
case structs.ConstraintDistinctHosts: | ||
return true | ||
default: | ||
break | ||
} | ||
|
||
switch operand { | ||
case "=", "==", "is": | ||
return reflect.DeepEqual(lVal, rVal) | ||
case "!=", "not": | ||
return !reflect.DeepEqual(lVal, rVal) | ||
case "<", "<=", ">", ">=": | ||
return checkLexicalOrder(operand, lVal, rVal) | ||
case "version": | ||
case structs.ConstraintVersion: | ||
return checkVersionConstraint(ctx, lVal, rVal) | ||
case "regexp": | ||
case structs.ConstraintRegex: | ||
return checkRegexpConstraint(ctx, lVal, rVal) | ||
default: | ||
return false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Investigate caching this