Skip to content

Commit

Permalink
Merge pull request #10904 from hashicorp/b-no-affinity-intern
Browse files Browse the repository at this point in the history
core: remove internalization of affinity strings
  • Loading branch information
shoenig authored and Mahmood Ali committed Jul 29, 2021
1 parent c13b770 commit aee471d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .changelog/10897.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core: Fixed a bug where affinity memoization may cause planning problems
```
12 changes: 0 additions & 12 deletions nomad/structs/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,12 @@ func TestJobDiff(t *testing.T) {
RTarget: "foo",
Operand: "foo",
Weight: 20,
str: "foo",
},
{
LTarget: "bar",
RTarget: "bar",
Operand: "bar",
Weight: 20,
str: "bar",
},
},
},
Expand All @@ -779,14 +777,12 @@ func TestJobDiff(t *testing.T) {
RTarget: "foo",
Operand: "foo",
Weight: 20,
str: "foo",
},
{
LTarget: "baz",
RTarget: "baz",
Operand: "baz",
Weight: 20,
str: "baz",
},
},
},
Expand Down Expand Up @@ -1558,14 +1554,12 @@ func TestTaskGroupDiff(t *testing.T) {
RTarget: "foo",
Operand: "foo",
Weight: 20,
str: "foo",
},
{
LTarget: "bar",
RTarget: "bar",
Operand: "bar",
Weight: 20,
str: "bar",
},
},
},
Expand All @@ -1576,14 +1570,12 @@ func TestTaskGroupDiff(t *testing.T) {
RTarget: "foo",
Operand: "foo",
Weight: 20,
str: "foo",
},
{
LTarget: "baz",
RTarget: "baz",
Operand: "baz",
Weight: 20,
str: "baz",
},
},
},
Expand Down Expand Up @@ -4040,14 +4032,12 @@ func TestTaskDiff(t *testing.T) {
RTarget: "foo",
Operand: "foo",
Weight: 20,
str: "foo",
},
{
LTarget: "bar",
RTarget: "bar",
Operand: "bar",
Weight: 20,
str: "bar",
},
},
},
Expand All @@ -4058,14 +4048,12 @@ func TestTaskDiff(t *testing.T) {
RTarget: "foo",
Operand: "foo",
Weight: 20,
str: "foo",
},
{
LTarget: "baz",
RTarget: "baz",
Operand: "baz",
Weight: 20,
str: "baz",
},
},
},
Expand Down
18 changes: 8 additions & 10 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8226,10 +8226,9 @@ type Affinity struct {
RTarget string // Right-hand target
Operand string // Affinity operand (<=, <, =, !=, >, >=), set_contains_all, set_contains_any
Weight int8 // Weight applied to nodes that match the affinity. Can be negative
str string // Memoized string
}

// Equal checks if two affinities are equal
// Equals checks if two affinities are equal.
func (a *Affinity) Equals(o *Affinity) bool {
return a == o ||
a.LTarget == o.LTarget &&
Expand All @@ -8246,17 +8245,16 @@ func (a *Affinity) Copy() *Affinity {
if a == nil {
return nil
}
na := new(Affinity)
*na = *a
return na
return &Affinity{
LTarget: a.LTarget,
RTarget: a.RTarget,
Operand: a.Operand,
Weight: a.Weight,
}
}

func (a *Affinity) String() string {
if a.str != "" {
return a.str
}
a.str = fmt.Sprintf("%s %s %s %v", a.LTarget, a.Operand, a.RTarget, a.Weight)
return a.str
return fmt.Sprintf("%s %s %s %v", a.LTarget, a.Operand, a.RTarget, a.Weight)
}

func (a *Affinity) Validate() error {
Expand Down
18 changes: 12 additions & 6 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ func TestJob_SpecChanged(t *testing.T) {
Original: &Job{Constraints: []*Constraint{{"A", "B", "="}}},
New: &Job{Constraints: []*Constraint{{"A", "B", "="}}},
},
{
Name: "With Affinities",
Changed: false,
Original: &Job{Affinities: []*Affinity{{"A", "B", "=", 1}}},
New: &Job{Affinities: []*Affinity{{"A", "B", "=", 1}}},
},
}

for _, c := range cases {
Expand Down Expand Up @@ -814,14 +820,14 @@ func TestJob_PartEqual(t *testing.T) {
}))

as := &Affinities{
&Affinity{"left0", "right0", "=", 0, ""},
&Affinity{"left1", "right1", "=", 0, ""},
&Affinity{"left2", "right2", "=", 0, ""},
&Affinity{"left0", "right0", "=", 0},
&Affinity{"left1", "right1", "=", 0},
&Affinity{"left2", "right2", "=", 0},
}
require.True(t, as.Equals(&Affinities{
&Affinity{"left0", "right0", "=", 0, ""},
&Affinity{"left2", "right2", "=", 0, ""},
&Affinity{"left1", "right1", "=", 0, ""},
&Affinity{"left0", "right0", "=", 0},
&Affinity{"left2", "right2", "=", 0},
&Affinity{"left1", "right1", "=", 0},
}))
}

Expand Down

0 comments on commit aee471d

Please sign in to comment.