Skip to content

Commit

Permalink
Rename Dynamic -> ProposedAllocConstraintIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Oct 26, 2015
1 parent 302c989 commit 2ab5790
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
31 changes: 16 additions & 15 deletions scheduler/feasible.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ func (iter *DriverIterator) hasDrivers(option *structs.Node) bool {
return true
}

// DynamicConstraintIterator is a FeasibleIterator which returns nodes that
// ProposedAllocConstraintIterator is a FeasibleIterator which returns nodes that
// match constraints that are not static such as Node attributes but are
// effected by alloc placements. Examples are distinct_hosts and tenancy constraints.
// This is used to filter on job and task group constraints.
type DynamicConstraintIterator struct {
// 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
Expand All @@ -166,27 +167,27 @@ type DynamicConstraintIterator struct {
jobDistinctHosts bool
}

// NewDynamicConstraintIterator creates a DynamicConstraintIterator from a
// source.
func NewDynamicConstraintIterator(ctx Context, source FeasibleIterator) *DynamicConstraintIterator {
iter := &DynamicConstraintIterator{
// NewProposedAllocConstraintIterator creates a ProposedAllocConstraintIterator
// from a source.
func NewProposedAllocConstraintIterator(ctx Context, source FeasibleIterator) *ProposedAllocConstraintIterator {
iter := &ProposedAllocConstraintIterator{
ctx: ctx,
source: source,
}
return iter
}

func (iter *DynamicConstraintIterator) SetTaskGroup(tg *structs.TaskGroup) {
func (iter *ProposedAllocConstraintIterator) SetTaskGroup(tg *structs.TaskGroup) {
iter.tg = tg
iter.tgDistinctHosts = iter.hasDistinctHostsConstraint(tg.Constraints)
}

func (iter *DynamicConstraintIterator) SetJob(job *structs.Job) {
func (iter *ProposedAllocConstraintIterator) SetJob(job *structs.Job) {
iter.job = job
iter.jobDistinctHosts = iter.hasDistinctHostsConstraint(job.Constraints)
}

func (iter *DynamicConstraintIterator) hasDistinctHostsConstraint(constraints []*structs.Constraint) bool {
func (iter *ProposedAllocConstraintIterator) hasDistinctHostsConstraint(constraints []*structs.Constraint) bool {
for _, con := range constraints {
if con.Operand == structs.ConstraintDistinctHosts {
return true
Expand All @@ -195,13 +196,13 @@ func (iter *DynamicConstraintIterator) hasDistinctHostsConstraint(constraints []
return false
}

func (iter *DynamicConstraintIterator) Next() *structs.Node {
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) {
if option == nil || !(iter.jobDistinctHosts || iter.tgDistinctHosts) {
return option
}

Expand All @@ -216,7 +217,7 @@ func (iter *DynamicConstraintIterator) Next() *structs.Node {

// satisfiesDistinctHosts checks if the node satisfies a distinct_hosts
// constraint either specified at the job level or the TaskGroup level.
func (iter *DynamicConstraintIterator) satisfiesDistinctHosts(option *structs.Node) bool {
func (iter *ProposedAllocConstraintIterator) satisfiesDistinctHosts(option *structs.Node) bool {
// Check if there is no constraint set.
if !(iter.jobDistinctHosts || iter.tgDistinctHosts) {
return true
Expand Down Expand Up @@ -245,7 +246,7 @@ func (iter *DynamicConstraintIterator) satisfiesDistinctHosts(option *structs.No
return true
}

func (iter *DynamicConstraintIterator) Reset() {
func (iter *ProposedAllocConstraintIterator) Reset() {
iter.source.Reset()
}

Expand Down
40 changes: 20 additions & 20 deletions scheduler/feasible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func TestCheckRegexpConstraint(t *testing.T) {
}
}

func TestDynamicConstraint_JobDistinctHosts(t *testing.T) {
func TestProposedAllocConstraint_JobDistinctHosts(t *testing.T) {
_, ctx := testContext(t)
nodes := []*structs.Node{
mock.Node(),
Expand All @@ -402,11 +402,11 @@ func TestDynamicConstraint_JobDistinctHosts(t *testing.T) {
TaskGroups: []*structs.TaskGroup{tg1, tg2},
}

dynamic := NewDynamicConstraintIterator(ctx, static)
dynamic.SetTaskGroup(tg1)
dynamic.SetJob(job)
propsed := NewProposedAllocConstraintIterator(ctx, static)
propsed.SetTaskGroup(tg1)
propsed.SetJob(job)

out := collectFeasible(dynamic)
out := collectFeasible(propsed)
if len(out) != 4 {
t.Fatalf("Bad: %#v", out)
}
Expand All @@ -420,7 +420,7 @@ func TestDynamicConstraint_JobDistinctHosts(t *testing.T) {
}
}

func TestDynamicConstraint_JobDistinctHosts_Infeasible(t *testing.T) {
func TestProposedAllocConstraint_JobDistinctHosts_Infeasible(t *testing.T) {
_, ctx := testContext(t)
nodes := []*structs.Node{
mock.Node(),
Expand Down Expand Up @@ -466,17 +466,17 @@ func TestDynamicConstraint_JobDistinctHosts_Infeasible(t *testing.T) {
},
}

dynamic := NewDynamicConstraintIterator(ctx, static)
dynamic.SetTaskGroup(tg1)
dynamic.SetJob(job)
propsed := NewProposedAllocConstraintIterator(ctx, static)
propsed.SetTaskGroup(tg1)
propsed.SetJob(job)

out := collectFeasible(dynamic)
out := collectFeasible(propsed)
if len(out) != 0 {
t.Fatalf("Bad: %#v", out)
}
}

func TestDynamicConstraint_JobDistinctHosts_InfeasibleCount(t *testing.T) {
func TestProposedAllocConstraint_JobDistinctHosts_InfeasibleCount(t *testing.T) {
_, ctx := testContext(t)
nodes := []*structs.Node{
mock.Node(),
Expand All @@ -495,18 +495,18 @@ func TestDynamicConstraint_JobDistinctHosts_InfeasibleCount(t *testing.T) {
TaskGroups: []*structs.TaskGroup{tg1, tg2, tg3},
}

dynamic := NewDynamicConstraintIterator(ctx, static)
dynamic.SetTaskGroup(tg1)
dynamic.SetJob(job)
propsed := NewProposedAllocConstraintIterator(ctx, static)
propsed.SetTaskGroup(tg1)
propsed.SetJob(job)

// It should not be able to place 3 tasks with only two nodes.
out := collectFeasible(dynamic)
out := collectFeasible(propsed)
if len(out) != 2 {
t.Fatalf("Bad: %#v", out)
}
}

func TestDynamicConstraint_TaskGroupDistinctHosts(t *testing.T) {
func TestProposedAllocConstraint_TaskGroupDistinctHosts(t *testing.T) {
_, ctx := testContext(t)
nodes := []*structs.Node{
mock.Node(),
Expand Down Expand Up @@ -540,11 +540,11 @@ func TestDynamicConstraint_TaskGroupDistinctHosts(t *testing.T) {
},
}

dynamic := NewDynamicConstraintIterator(ctx, static)
dynamic.SetTaskGroup(taskGroup)
dynamic.SetJob(&structs.Job{ID: "foo"})
propsed := NewProposedAllocConstraintIterator(ctx, static)
propsed.SetTaskGroup(taskGroup)
propsed.SetJob(&structs.Job{ID: "foo"})

out := collectFeasible(dynamic)
out := collectFeasible(propsed)
if len(out) != 1 {
t.Fatalf("Bad: %#v", out)
}
Expand Down
31 changes: 16 additions & 15 deletions scheduler/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ type Stack interface {
// GenericStack is the Stack used for the Generic scheduler. It is
// designed to make better placement decisions at the cost of performance.
type GenericStack struct {
batch bool
ctx Context
source *StaticIterator
jobConstraint *ConstraintIterator
taskGroupDrivers *DriverIterator
taskGroupConstraint *ConstraintIterator
dynamicConstraint *DynamicConstraintIterator
binPack *BinPackIterator
jobAntiAff *JobAntiAffinityIterator
limit *LimitIterator
maxScore *MaxScoreIterator
batch bool
ctx Context
source *StaticIterator
jobConstraint *ConstraintIterator
taskGroupDrivers *DriverIterator
taskGroupConstraint *ConstraintIterator
proposedAllocConstraint *ProposedAllocConstraintIterator
binPack *BinPackIterator
jobAntiAff *JobAntiAffinityIterator
limit *LimitIterator
maxScore *MaxScoreIterator
}

// NewGenericStack constructs a stack used for selecting service placements
Expand All @@ -70,10 +70,11 @@ func NewGenericStack(batch bool, ctx Context) *GenericStack {
// Filter on task group constraints second
s.taskGroupConstraint = NewConstraintIterator(ctx, s.taskGroupDrivers, nil)

s.dynamicConstraint = NewDynamicConstraintIterator(ctx, s.taskGroupConstraint)
// Filter on constraints that are affected by propsed allocations.
s.proposedAllocConstraint = NewProposedAllocConstraintIterator(ctx, s.taskGroupConstraint)

// Upgrade from feasible to rank iterator
rankSource := NewFeasibleRankIterator(ctx, s.dynamicConstraint)
rankSource := NewFeasibleRankIterator(ctx, s.proposedAllocConstraint)

// Apply the bin packing, this depends on the resources needed
// by a particular task group. Only enable eviction for the service
Expand Down Expand Up @@ -122,7 +123,7 @@ func (s *GenericStack) SetNodes(baseNodes []*structs.Node) {

func (s *GenericStack) SetJob(job *structs.Job) {
s.jobConstraint.SetConstraints(job.Constraints)
s.dynamicConstraint.SetJob(job)
s.proposedAllocConstraint.SetJob(job)
s.binPack.SetPriority(job.Priority)
s.jobAntiAff.SetJob(job.ID)
}
Expand All @@ -139,7 +140,7 @@ func (s *GenericStack) Select(tg *structs.TaskGroup) (*RankedNode, *structs.Reso
// Update the parameters of iterators
s.taskGroupDrivers.SetDrivers(tgConstr.drivers)
s.taskGroupConstraint.SetConstraints(tgConstr.constraints)
s.dynamicConstraint.SetTaskGroup(tg)
s.proposedAllocConstraint.SetTaskGroup(tg)
s.binPack.SetTasks(tg.Tasks)

// Find the node with the max score
Expand Down

0 comments on commit 2ab5790

Please sign in to comment.