client: avoid registering node twice right away

I noticed that `watchNodeUpdates()` almost immediately after
`registerAndHeartbeat()` calls `retryRegisterNode()`, well after 5
seconds.

This call is unnecessary and made debugging a bit harder.  So here, we
ensure that we only re-register node for new node events, not for
initial registration.
This commit is contained in:
Mahmood Ali 2019-04-19 09:12:50 -04:00
parent f82ea8824f
commit 6bdc9860b7
2 changed files with 14 additions and 8 deletions

View File

@ -1468,13 +1468,7 @@ func (c *Client) watchNodeEvents() {
// batchEvents stores events that have yet to be published
var batchEvents []*structs.NodeEvent
// Create and drain the timer
timer := time.NewTimer(0)
timer.Stop()
select {
case <-timer.C:
default:
}
timer := stoppedTimer()
defer timer.Stop()
for {
@ -1930,7 +1924,8 @@ func (c *Client) updateNodeLocked() {
// it will update the client node copy and re-register the node.
func (c *Client) watchNodeUpdates() {
var hasChanged bool
timer := time.NewTimer(c.retryIntv(nodeUpdateRetryIntv))
timer := stoppedTimer()
defer timer.Stop()
for {

View File

@ -3,6 +3,7 @@ package client
import (
"fmt"
"math/rand"
"time"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -63,3 +64,13 @@ func shuffleStrings(list []string) {
list[i], list[j] = list[j], list[i]
}
}
// stoppedTimer returns a timer that's stopped and wouldn't fire until
// it's reset
func stoppedTimer() *time.Timer {
timer := time.NewTimer(0)
if !timer.Stop() {
<-timer.C
}
return timer
}