Skip to content

Commit

Permalink
Check for changes to affinity and constraints
Browse files Browse the repository at this point in the history
Adds checks for affinity and constraint changes when determining if we
should update inplace.
  • Loading branch information
drewbailey committed Nov 14, 2019
1 parent ed111c3 commit 3ab5ba5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scheduler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ func tasksUpdated(jobA, jobB *structs.Job, taskGroup string) bool {
a := jobA.LookupTaskGroup(taskGroup)
b := jobB.LookupTaskGroup(taskGroup)

// Check Job level Affinities and Constraints
if !reflect.DeepEqual(jobA.Affinities, jobB.Affinities) {
return true
}

if !reflect.DeepEqual(jobA.Constraints, jobB.Constraints) {
return true
}

// If the number of tasks do not match, clearly there is an update
if len(a.Tasks) != len(b.Tasks) {
return true
Expand All @@ -356,6 +365,16 @@ func tasksUpdated(jobA, jobB *structs.Job, taskGroup string) bool {
return true
}

// Check that the task group affinities haven't changed
if !reflect.DeepEqual(a.Affinities, b.Affinities) {
return true
}

// Check that the task group constraints haven't changed
if !reflect.DeepEqual(a.Constraints, b.Constraints) {
return true
}

// Check each task
for _, at := range a.Tasks {
bt := b.LookupTask(at.Name)
Expand Down Expand Up @@ -384,6 +403,14 @@ func tasksUpdated(jobA, jobB *structs.Job, taskGroup string) bool {
return true
}

if !reflect.DeepEqual(at.Affinities, bt.Affinities) {
return true
}

if !reflect.DeepEqual(at.Constraints, bt.Constraints) {
return true
}

// Check the metadata
if !reflect.DeepEqual(
jobA.CombinedTaskMeta(taskGroup, at.Name),
Expand Down
83 changes: 83 additions & 0 deletions scheduler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,89 @@ func TestShuffleNodes(t *testing.T) {
require.False(t, reflect.DeepEqual(nodes, orig))
}

func TestTaskUpdatedAffinity(t *testing.T) {
j1 := mock.Job()
j2 := mock.Job()
name := j1.TaskGroups[0].Name

require.False(t, tasksUpdated(j1, j2, name))

// TaskGroup Affinity
j2.TaskGroups[0].Affinities = []*structs.Affinity{
{
LTarget: "",
RTarget: "",
Operand: "",
Weight: 100,
},
}
require.True(t, tasksUpdated(j1, j2, name))

// TaskGroup Task Affinity
j3 := mock.Job()
j3.TaskGroups[0].Tasks[0].Affinities = []*structs.Affinity{
{
LTarget: "",
RTarget: "",
Operand: "",
Weight: 100,
},
}

require.True(t, tasksUpdated(j1, j3, name))

j4 := mock.Job()
j4.TaskGroups[0].Tasks[0].Affinities = []*structs.Affinity{
{
LTarget: "",
RTarget: "",
Operand: "",
Weight: 100,
},
}

require.True(t, tasksUpdated(j1, j4, name))
}

func TestTaskUpdated_Constraint(t *testing.T) {
j1 := mock.Job()
j2 := mock.Job()
name := j1.TaskGroups[0].Name
require.False(t, tasksUpdated(j1, j2, name))

// TaskGroup Constraint
j2.TaskGroups[0].Constraints = []*structs.Constraint{
{
LTarget: "kernel",
RTarget: "linux",
Operand: "=",
},
}

// TaskGroup Task Constraint
j3 := mock.Job()
j3.TaskGroups[0].Tasks[0].Constraints = []*structs.Constraint{
{
LTarget: "kernel",
RTarget: "linux",
Operand: "=",
},
}

require.True(t, tasksUpdated(j1, j3, name))

j4 := mock.Job()
j4.TaskGroups[0].Tasks[0].Constraints = []*structs.Constraint{
{
LTarget: "kernel",
RTarget: "linux",
Operand: "=",
},
}

require.True(t, tasksUpdated(j1, j4, name))
}

func TestTasksUpdated(t *testing.T) {
j1 := mock.Job()
j2 := mock.Job()
Expand Down

0 comments on commit 3ab5ba5

Please sign in to comment.