Skip to content

Commit

Permalink
api: use the same initial time for all drain properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Nov 14, 2019
1 parent 1024103 commit 527290c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 11 additions & 8 deletions nomad/node_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,10 @@ func (n *Node) UpdateDrain(args *structs.NodeUpdateDrainRequest,
return fmt.Errorf("node not found")
}

now := time.Now().UTC()

// Update the timestamp of when the node status was updated
args.UpdatedAt = time.Now().Unix()
args.UpdatedAt = now.Unix()

// COMPAT: Remove in 0.9. Attempt to upgrade the request if it is of the old
// format.
Expand All @@ -536,18 +538,19 @@ func (n *Node) UpdateDrain(args *structs.NodeUpdateDrainRequest,
}
}

// Mark start time for the drain
// Setup drain strategy
if args.DrainStrategy != nil {
if node.DrainStrategy == nil || node.DrainStrategy.StartedAt.IsZero() {
args.DrainStrategy.StartedAt = time.Now().UTC()
// Mark start time for the drain
if node.DrainStrategy == nil {
args.DrainStrategy.StartedAt = now
} else {
args.DrainStrategy.StartedAt = node.DrainStrategy.StartedAt
}
}

// Mark the deadline time
if args.DrainStrategy != nil && args.DrainStrategy.Deadline.Nanoseconds() > 0 {
args.DrainStrategy.ForceDeadline = time.Now().Add(args.DrainStrategy.Deadline).UTC()
// Mark the deadline time
if args.DrainStrategy.Deadline.Nanoseconds() > 0 {
args.DrainStrategy.ForceDeadline = now.Add(args.DrainStrategy.Deadline)
}
}

// Construct the node event
Expand Down
2 changes: 1 addition & 1 deletion nomad/node_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ func TestClientEndpoint_UpdateDrain(t *testing.T) {

drainStartedAt := out.DrainStrategy.StartedAt
// StartedAt should be close to the time the drain started
require.WithinDuration(beforeUpdate, out.DrainStrategy.StartedAt, 1*time.Second)
require.WithinDuration(beforeUpdate, drainStartedAt, 1*time.Second)

// StartedAt shouldn't change if a new request comes while still draining
require.Nil(msgpackrpc.CallWithCodec(codec, "Node.UpdateDrain", dereg, &resp2))
Expand Down

0 comments on commit 527290c

Please sign in to comment.