-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reregister node when periodic fingerprint changes node properties #749
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"github.com/hashicorp/nomad/client/fingerprint" | ||
"github.com/hashicorp/nomad/nomad" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/mitchellh/hashstructure" | ||
) | ||
|
||
const ( | ||
|
@@ -53,6 +54,10 @@ const ( | |
// starting and the intial heartbeat. After the intial heartbeat, | ||
// we switch to using the TTL specified by the servers. | ||
initialHeartbeatStagger = 10 * time.Second | ||
|
||
// nodeUpdateRetryIntv is how often the client checks for updates to the | ||
// node attributes or meta map. | ||
nodeUpdateRetryIntv = 30 * time.Second | ||
) | ||
|
||
// DefaultConfig returns the default configuration | ||
|
@@ -604,17 +609,10 @@ func (c *Client) retryIntv(base time.Duration) time.Duration { | |
|
||
// run is a long lived goroutine used to run the client | ||
func (c *Client) run() { | ||
// Register the client | ||
for { | ||
if err := c.registerNode(); err == nil { | ||
break | ||
} | ||
select { | ||
case <-time.After(c.retryIntv(registerRetryIntv)): | ||
case <-c.shutdownCh: | ||
return | ||
} | ||
} | ||
c.retryRegisterNode() | ||
|
||
// Watch for node changes | ||
go c.watchNodeUpdates() | ||
|
||
// Setup the heartbeat timer, for the initial registration | ||
// we want to do this quickly. We want to do it extra quickly | ||
|
@@ -658,6 +656,39 @@ func (c *Client) run() { | |
} | ||
} | ||
|
||
func (c *Client) hasNodeChanged(oldAttrHash uint64, oldMetaHash uint64) (bool, uint64, uint64) { | ||
newAttrHash, err := hashstructure.Hash(c.config.Node.Attributes, nil) | ||
if err != nil { | ||
c.logger.Printf("[DEBUG] client: unable to calculate node attributes hash: %v", err) | ||
} | ||
// Calculate node meta map hash | ||
newMetaHash, err := hashstructure.Hash(c.config.Node.Meta, nil) | ||
if err != nil { | ||
c.logger.Printf("[DEBUG] client: unable to calculate node meta hash: %v", err) | ||
} | ||
if newAttrHash != oldAttrHash || newMetaHash != oldMetaHash { | ||
return true, newAttrHash, newMetaHash | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can remove the else block and just keep the return. Common Go style There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return false, oldAttrHash, oldMetaHash | ||
} | ||
} | ||
|
||
// retryRegisterNode is used to register the node or update the registration and | ||
// retry in case of failure. | ||
func (c *Client) retryRegisterNode() { | ||
// Register the client | ||
for { | ||
if err := c.registerNode(); err == nil { | ||
break | ||
} | ||
select { | ||
case <-time.After(c.retryIntv(registerRetryIntv)): | ||
case <-c.shutdownCh: | ||
return | ||
} | ||
} | ||
} | ||
|
||
// registerNode is used to register the node or update the registration | ||
func (c *Client) registerNode() error { | ||
node := c.Node() | ||
|
@@ -851,6 +882,25 @@ func (c *Client) watchAllocations(updates chan *allocUpdates) { | |
} | ||
} | ||
|
||
// watchNodeUpdates periodically checks for changes to the node attributes or meta map | ||
func (c *Client) watchNodeUpdates() { | ||
c.logger.Printf("[DEBUG] client: periodically checking for node changes at duration %v", nodeUpdateRetryIntv) | ||
var attrHash, metaHash uint64 | ||
var changed bool | ||
for { | ||
select { | ||
case <-time.After(nodeUpdateRetryIntv): | ||
changed, attrHash, metaHash = c.hasNodeChanged(attrHash, metaHash) | ||
if changed { | ||
c.logger.Printf("[DEBUG] client: state changed, updating node.") | ||
c.retryRegisterNode() | ||
} | ||
case <-c.shutdownCh: | ||
return | ||
} | ||
} | ||
} | ||
|
||
// runAllocs is invoked when we get an updated set of allocations | ||
func (c *Client) runAllocs(update *allocUpdates) { | ||
// Get the existing allocs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a comment on what this does/its purpose and its return values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments added.