Skip to content
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

client: update driver info on new driver fingerprint #4984

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,6 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
if !hadDriver {
// If the driver info has not yet been set, do that here
hasChanged = true
c.config.Node.Drivers[name] = info
for attrName, newVal := range info.Attributes {
c.config.Node.Attributes[attrName] = newVal
}
Expand All @@ -1163,11 +1162,11 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
// The driver info has already been set, fix it up
if oldVal.Detected != info.Detected {
hasChanged = true
c.config.Node.Drivers[name].Detected = info.Detected
}

if oldVal.Healthy != info.Healthy || oldVal.HealthDescription != info.HealthDescription {
hasChanged = true

if info.HealthDescription != "" {
event := &structs.NodeEvent{
Subsystem: "Driver",
Expand All @@ -1186,6 +1185,7 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
}

hasChanged = true

if newVal == "" {
delete(c.config.Node.Attributes, attrName)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug in this code where node attributes are never removed if the key was removed from driver info. Fixing this requires comparing previous driver info against new one and removing old keys from node attributes. This seems somewhat complex and I suspect some of the attribute changes will be deprecated and removed by 0.10; so not sure if I should fix it here.

} else {
Expand All @@ -1205,6 +1205,7 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
}

if hasChanged {
c.config.Node.Drivers[name] = info
c.config.Node.Drivers[name].UpdateTime = time.Now()
c.updateNodeLocked()
}
Expand Down
77 changes: 77 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,80 @@ func TestClient_computeAllocatedDeviceStats(t *testing.T) {

assert.EqualValues(t, expected, result)
}

func TestClient_updateNodeFromDriverUpdatesAll(t *testing.T) {
t.Parallel()
client, cleanup := TestClient(t, nil)
defer cleanup()

// initial update
{
info := &structs.DriverInfo{
Detected: true,
Healthy: false,
HealthDescription: "not healthy at start",
Attributes: map[string]string{
"node.mock.testattr1": "val1",
},
}
n := client.updateNodeFromDriver("mock", info)

updatedInfo := *n.Drivers["mock"]
// compare without update time
updatedInfo.UpdateTime = info.UpdateTime
assert.EqualValues(t, updatedInfo, *info)

// check node attributes
assert.Equal(t, "val1", n.Attributes["node.mock.testattr1"])
}

// initial update
{
info := &structs.DriverInfo{
Detected: true,
Healthy: true,
HealthDescription: "healthy",
Attributes: map[string]string{
"node.mock.testattr1": "val2",
},
}
n := client.updateNodeFromDriver("mock", info)

updatedInfo := *n.Drivers["mock"]
// compare without update time
updatedInfo.UpdateTime = info.UpdateTime
assert.EqualValues(t, updatedInfo, *info)

// check node attributes are updated
assert.Equal(t, "val2", n.Attributes["node.mock.testattr1"])

// update once more with the same info, updateTime shouldn't change
un := client.updateNodeFromDriver("mock", info)
assert.EqualValues(t, n, un)
}

// update once more to unhealthy because why not
{
info := &structs.DriverInfo{
Detected: true,
Healthy: false,
HealthDescription: "lost track",
Attributes: map[string]string{
"node.mock.testattr1": "",
},
}
n := client.updateNodeFromDriver("mock", info)

updatedInfo := *n.Drivers["mock"]
// compare without update time
updatedInfo.UpdateTime = info.UpdateTime
assert.EqualValues(t, updatedInfo, *info)

// check node attributes are updated
assert.Equal(t, "", n.Attributes["node.mock.testattr1"])

// update once more with the same info, updateTime shouldn't change
un := client.updateNodeFromDriver("mock", info)
assert.EqualValues(t, n, un)
}
}