Skip to content

Commit

Permalink
core: do not memoize constraint strings
Browse files Browse the repository at this point in the history
This PR causes Nomad to no longer memoize the String value of
a Constraint. The private memoized variable may or may not be
initialized at any given time, which means a reflect.DeepEqual
comparison between two jobs (e.g. during Plan) may return incorrect
results.

Fixes #10836
  • Loading branch information
shoenig committed Jul 14, 2021
1 parent 7538eed commit 07c57ff
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8245,17 +8245,17 @@ type Constraint struct {
LTarget string // Left-hand target
RTarget string // Right-hand target
Operand string // Constraint operand (<=, <, =, !=, >, >=), contains, near
str string // Memoized string
}

// Equals checks if two constraints are equal
// Equals checks if two constraints are equal.
func (c *Constraint) Equals(o *Constraint) bool {
return c == o ||
c.LTarget == o.LTarget &&
c.RTarget == o.RTarget &&
c.Operand == o.Operand
}

// Equal is like Equals but with one less s.
func (c *Constraint) Equal(o *Constraint) bool {
return c.Equals(o)
}
Expand All @@ -8264,17 +8264,15 @@ func (c *Constraint) Copy() *Constraint {
if c == nil {
return nil
}
nc := new(Constraint)
*nc = *c
return nc
return &Constraint{
LTarget: c.LTarget,
RTarget: c.RTarget,
Operand: c.Operand,
}
}

func (c *Constraint) String() string {
if c.str != "" {
return c.str
}
c.str = fmt.Sprintf("%s %s %s", c.LTarget, c.Operand, c.RTarget)
return c.str
return fmt.Sprintf("%s %s %s", c.LTarget, c.Operand, c.RTarget)
}

func (c *Constraint) Validate() error {
Expand Down

0 comments on commit 07c57ff

Please sign in to comment.