2018-12-13 06:41:44 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/devicemanager"
|
2019-10-22 13:20:26 +00:00
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
|
2018-12-13 06:41:44 +00:00
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/drivermanager"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// batchFirstFingerprintsTimeout is the maximum amount of time to wait for
|
2018-12-18 03:54:25 +00:00
|
|
|
// initial fingerprinting to complete before sending a batched Node update
|
2019-04-19 13:00:24 +00:00
|
|
|
batchFirstFingerprintsTimeout = 50 * time.Second
|
2018-12-13 06:41:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// batchFirstFingerprints waits for the first fingerprint response from all
|
2018-12-20 05:53:44 +00:00
|
|
|
// plugin managers and sends a single Node update for all fingerprints. It
|
|
|
|
// should only ever be called once
|
2018-12-13 06:41:44 +00:00
|
|
|
func (c *Client) batchFirstFingerprints() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), batchFirstFingerprintsTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ch, err := c.pluginManagers.WaitForFirstFingerprint(ctx)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Warn("failed to batch initial fingerprint updates, switching to incemental updates")
|
|
|
|
goto SEND_BATCH
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for fingerprinting to complete or timeout before processing batches
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
SEND_BATCH:
|
|
|
|
c.configLock.Lock()
|
|
|
|
defer c.configLock.Unlock()
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
newConfig := c.config.Copy()
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// csi updates
|
|
|
|
var csiChanged bool
|
|
|
|
c.batchNodeUpdates.batchCSIUpdates(func(name string, info *structs.CSIInfo) {
|
2022-08-18 23:32:04 +00:00
|
|
|
if c.updateNodeFromCSIControllerLocked(name, info, newConfig.Node) {
|
|
|
|
if newConfig.Node.CSIControllerPlugins[name].UpdateTime.IsZero() {
|
|
|
|
newConfig.Node.CSIControllerPlugins[name].UpdateTime = time.Now()
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
csiChanged = true
|
|
|
|
}
|
2022-08-18 23:32:04 +00:00
|
|
|
if c.updateNodeFromCSINodeLocked(name, info, newConfig.Node) {
|
|
|
|
if newConfig.Node.CSINodePlugins[name].UpdateTime.IsZero() {
|
|
|
|
newConfig.Node.CSINodePlugins[name].UpdateTime = time.Now()
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
csiChanged = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-13 06:41:44 +00:00
|
|
|
// driver node updates
|
|
|
|
var driverChanged bool
|
|
|
|
c.batchNodeUpdates.batchDriverUpdates(func(driver string, info *structs.DriverInfo) {
|
2022-08-18 23:32:04 +00:00
|
|
|
if c.applyNodeUpdatesFromDriver(driver, info, newConfig.Node) {
|
|
|
|
newConfig.Node.Drivers[driver] = info
|
|
|
|
if newConfig.Node.Drivers[driver].UpdateTime.IsZero() {
|
|
|
|
newConfig.Node.Drivers[driver].UpdateTime = time.Now()
|
2018-12-18 03:54:25 +00:00
|
|
|
}
|
2018-12-13 06:41:44 +00:00
|
|
|
driverChanged = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// device node updates
|
|
|
|
var devicesChanged bool
|
|
|
|
c.batchNodeUpdates.batchDevicesUpdates(func(devices []*structs.NodeDeviceResource) {
|
|
|
|
if c.updateNodeFromDevicesLocked(devices) {
|
2022-11-03 21:31:39 +00:00
|
|
|
newConfig.Node.NodeResources.Devices = devices
|
2018-12-13 06:41:44 +00:00
|
|
|
devicesChanged = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-12-18 03:54:25 +00:00
|
|
|
// only update the node if changes occurred
|
2019-10-22 13:20:26 +00:00
|
|
|
if driverChanged || devicesChanged || csiChanged {
|
2022-08-18 23:32:04 +00:00
|
|
|
c.config = newConfig
|
|
|
|
c.updateNode()
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
2018-12-20 05:53:44 +00:00
|
|
|
|
|
|
|
close(c.fpInitialized)
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
// updateNodeFromCSI receives a CSIInfo struct for the plugin and updates the
|
|
|
|
// node accordingly
|
|
|
|
func (c *Client) updateNodeFromCSI(name string, info *structs.CSIInfo) {
|
|
|
|
c.configLock.Lock()
|
|
|
|
defer c.configLock.Unlock()
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
newConfig := c.config.Copy()
|
|
|
|
|
2019-10-22 13:20:26 +00:00
|
|
|
changed := false
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
if c.updateNodeFromCSIControllerLocked(name, info, newConfig.Node) {
|
|
|
|
if newConfig.Node.CSIControllerPlugins[name].UpdateTime.IsZero() {
|
|
|
|
newConfig.Node.CSIControllerPlugins[name].UpdateTime = time.Now()
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
if c.updateNodeFromCSINodeLocked(name, info, newConfig.Node) {
|
|
|
|
if newConfig.Node.CSINodePlugins[name].UpdateTime.IsZero() {
|
|
|
|
newConfig.Node.CSINodePlugins[name].UpdateTime = time.Now()
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
2022-08-18 23:32:04 +00:00
|
|
|
c.config = newConfig
|
|
|
|
c.updateNode()
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateNodeFromCSIControllerLocked makes the changes to the node from a csi
|
|
|
|
// update but does not send the update to the server. c.configLock must be held
|
|
|
|
// before calling this func.
|
|
|
|
//
|
|
|
|
// It is safe to call for all CSI Updates, but will only perform changes when
|
|
|
|
// a ControllerInfo field is present.
|
2022-08-18 23:32:04 +00:00
|
|
|
func (c *Client) updateNodeFromCSIControllerLocked(name string, info *structs.CSIInfo, node *structs.Node) bool {
|
2019-10-22 13:20:26 +00:00
|
|
|
var changed bool
|
|
|
|
if info.ControllerInfo == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
i := info.Copy()
|
|
|
|
i.NodeInfo = nil
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
oldController, hadController := node.CSIControllerPlugins[name]
|
2019-10-22 13:20:26 +00:00
|
|
|
if !hadController {
|
|
|
|
// If the controller info has not yet been set, do that here
|
|
|
|
changed = true
|
2022-08-18 23:32:04 +00:00
|
|
|
node.CSIControllerPlugins[name] = i
|
2019-10-22 13:20:26 +00:00
|
|
|
} else {
|
|
|
|
// The controller info has already been set, fix it up
|
|
|
|
if !oldController.Equal(i) {
|
2022-08-18 23:32:04 +00:00
|
|
|
node.CSIControllerPlugins[name] = i
|
2019-10-22 13:20:26 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If health state has changed, trigger node event
|
|
|
|
if oldController.Healthy != i.Healthy || oldController.HealthDescription != i.HealthDescription {
|
|
|
|
changed = true
|
|
|
|
if i.HealthDescription != "" {
|
2020-03-31 14:02:16 +00:00
|
|
|
event := structs.NewNodeEvent().
|
|
|
|
SetSubsystem("CSI").
|
|
|
|
SetMessage(i.HealthDescription).
|
|
|
|
AddDetail("plugin", name).
|
|
|
|
AddDetail("type", "controller")
|
2019-10-22 13:20:26 +00:00
|
|
|
c.triggerNodeEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateNodeFromCSINodeLocked makes the changes to the node from a csi
|
|
|
|
// update but does not send the update to the server. c.configLock must be hel
|
|
|
|
// before calling this func.
|
|
|
|
//
|
|
|
|
// It is safe to call for all CSI Updates, but will only perform changes when
|
|
|
|
// a NodeInfo field is present.
|
2022-08-18 23:32:04 +00:00
|
|
|
func (c *Client) updateNodeFromCSINodeLocked(name string, info *structs.CSIInfo, node *structs.Node) bool {
|
2019-10-22 13:20:26 +00:00
|
|
|
var changed bool
|
|
|
|
if info.NodeInfo == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
i := info.Copy()
|
|
|
|
i.ControllerInfo = nil
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
oldNode, hadNode := node.CSINodePlugins[name]
|
2019-10-22 13:20:26 +00:00
|
|
|
if !hadNode {
|
|
|
|
// If the Node info has not yet been set, do that here
|
|
|
|
changed = true
|
2022-08-18 23:32:04 +00:00
|
|
|
node.CSINodePlugins[name] = i
|
2019-10-22 13:20:26 +00:00
|
|
|
} else {
|
|
|
|
// The node info has already been set, fix it up
|
|
|
|
if !oldNode.Equal(info) {
|
2022-08-18 23:32:04 +00:00
|
|
|
node.CSINodePlugins[name] = i
|
2019-10-22 13:20:26 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If health state has changed, trigger node event
|
|
|
|
if oldNode.Healthy != i.Healthy || oldNode.HealthDescription != i.HealthDescription {
|
|
|
|
changed = true
|
|
|
|
if i.HealthDescription != "" {
|
2020-03-31 14:02:16 +00:00
|
|
|
event := structs.NewNodeEvent().
|
|
|
|
SetSubsystem("CSI").
|
|
|
|
SetMessage(i.HealthDescription).
|
|
|
|
AddDetail("plugin", name).
|
|
|
|
AddDetail("type", "node")
|
2019-10-22 13:20:26 +00:00
|
|
|
c.triggerNodeEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2018-12-13 06:41:44 +00:00
|
|
|
// updateNodeFromDriver receives a DriverInfo struct for the driver and updates
|
|
|
|
// the node accordingly
|
|
|
|
func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) {
|
|
|
|
c.configLock.Lock()
|
|
|
|
defer c.configLock.Unlock()
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
newConfig := c.config.Copy()
|
|
|
|
|
|
|
|
if c.applyNodeUpdatesFromDriver(name, info, newConfig.Node) {
|
|
|
|
newConfig.Node.Drivers[name] = info
|
|
|
|
if newConfig.Node.Drivers[name].UpdateTime.IsZero() {
|
|
|
|
newConfig.Node.Drivers[name].UpdateTime = time.Now()
|
2018-12-18 03:54:25 +00:00
|
|
|
}
|
2022-08-18 23:32:04 +00:00
|
|
|
|
|
|
|
c.config = newConfig
|
|
|
|
c.updateNode()
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
// applyNodeUpdatesFromDriver applies changes to the passed in node. true is
|
|
|
|
// returned if the node has changed.
|
|
|
|
func (c *Client) applyNodeUpdatesFromDriver(name string, info *structs.DriverInfo, node *structs.Node) bool {
|
2018-12-13 06:41:44 +00:00
|
|
|
var hasChanged bool
|
|
|
|
|
2022-08-18 23:32:04 +00:00
|
|
|
hadDriver := node.Drivers[name] != nil
|
2018-12-13 06:41:44 +00:00
|
|
|
if !hadDriver {
|
|
|
|
// If the driver info has not yet been set, do that here
|
|
|
|
hasChanged = true
|
|
|
|
for attrName, newVal := range info.Attributes {
|
2022-08-18 23:32:04 +00:00
|
|
|
node.Attributes[attrName] = newVal
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-18 23:32:04 +00:00
|
|
|
oldVal := node.Drivers[name]
|
2018-12-13 06:41:44 +00:00
|
|
|
// The driver info has already been set, fix it up
|
|
|
|
if oldVal.Detected != info.Detected {
|
|
|
|
hasChanged = true
|
|
|
|
}
|
|
|
|
|
2018-12-18 03:54:25 +00:00
|
|
|
// If health state has change, trigger node event
|
2018-12-13 06:41:44 +00:00
|
|
|
if oldVal.Healthy != info.Healthy || oldVal.HealthDescription != info.HealthDescription {
|
|
|
|
hasChanged = true
|
|
|
|
if info.HealthDescription != "" {
|
2020-03-31 14:02:16 +00:00
|
|
|
event := structs.NewNodeEvent().
|
|
|
|
SetSubsystem("Driver").
|
|
|
|
SetMessage(info.HealthDescription).
|
|
|
|
AddDetail("driver", name)
|
2018-12-13 06:41:44 +00:00
|
|
|
c.triggerNodeEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for attrName, newVal := range info.Attributes {
|
2022-08-18 23:32:04 +00:00
|
|
|
oldVal := node.Drivers[name].Attributes[attrName]
|
2018-12-13 06:41:44 +00:00
|
|
|
if oldVal == newVal {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hasChanged = true
|
|
|
|
if newVal == "" {
|
2022-08-18 23:32:04 +00:00
|
|
|
delete(node.Attributes, attrName)
|
2018-12-13 06:41:44 +00:00
|
|
|
} else {
|
2022-08-18 23:32:04 +00:00
|
|
|
node.Attributes[attrName] = newVal
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// COMPAT Remove in Nomad 0.10
|
|
|
|
// We maintain the driver enabled attribute until all drivers expose
|
|
|
|
// their attributes as DriverInfo
|
|
|
|
driverName := fmt.Sprintf("driver.%s", name)
|
|
|
|
if info.Detected {
|
2022-08-18 23:32:04 +00:00
|
|
|
node.Attributes[driverName] = "1"
|
2018-12-13 06:41:44 +00:00
|
|
|
} else {
|
2022-08-18 23:32:04 +00:00
|
|
|
delete(node.Attributes, driverName)
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hasChanged
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) updateNodeFromDevices(devices []*structs.NodeDeviceResource) {
|
|
|
|
c.configLock.Lock()
|
|
|
|
defer c.configLock.Unlock()
|
|
|
|
|
|
|
|
// Not updating node.Resources: the field is deprecated and includes
|
|
|
|
// dispatched task resources and not appropriate for expressing
|
|
|
|
// node available device resources
|
|
|
|
if c.updateNodeFromDevicesLocked(devices) {
|
2022-08-18 23:32:04 +00:00
|
|
|
c.updateNode()
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateNodeFromDevicesLocked updates the node with the results of devices,
|
|
|
|
// but does send the update to the server. c.configLock must be held before
|
|
|
|
// calling this func
|
|
|
|
func (c *Client) updateNodeFromDevicesLocked(devices []*structs.NodeDeviceResource) bool {
|
|
|
|
if !structs.DevicesEquals(c.config.Node.NodeResources.Devices, devices) {
|
|
|
|
c.logger.Debug("new devices detected", "devices", len(devices))
|
2022-08-18 23:32:04 +00:00
|
|
|
newConfig := c.config.Copy()
|
|
|
|
newConfig.Node.NodeResources.Devices = devices
|
|
|
|
c.config = newConfig
|
2018-12-13 06:41:44 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// batchNodeUpdates allows for batching multiple Node updates from fingerprinting.
|
|
|
|
// Once ready, the batches can be flushed and toggled to stop batching and forward
|
|
|
|
// all updates to a configured callback to be performed incrementally
|
|
|
|
type batchNodeUpdates struct {
|
|
|
|
// access to driver fields must hold driversMu lock
|
|
|
|
drivers map[string]*structs.DriverInfo
|
|
|
|
driversBatched bool
|
|
|
|
driverCB drivermanager.UpdateNodeDriverInfoFn
|
|
|
|
driversMu sync.Mutex
|
|
|
|
|
|
|
|
// access to devices fields must hold devicesMu lock
|
|
|
|
devices []*structs.NodeDeviceResource
|
|
|
|
devicesBatched bool
|
|
|
|
devicesCB devicemanager.UpdateNodeDevicesFn
|
|
|
|
devicesMu sync.Mutex
|
2019-10-22 13:20:26 +00:00
|
|
|
|
|
|
|
// access to csi fields must hold csiMu lock
|
|
|
|
csiNodePlugins map[string]*structs.CSIInfo
|
|
|
|
csiControllerPlugins map[string]*structs.CSIInfo
|
|
|
|
csiBatched bool
|
|
|
|
csiCB csimanager.UpdateNodeCSIInfoFunc
|
|
|
|
csiMu sync.Mutex
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBatchNodeUpdates(
|
|
|
|
driverCB drivermanager.UpdateNodeDriverInfoFn,
|
2019-10-22 13:20:26 +00:00
|
|
|
devicesCB devicemanager.UpdateNodeDevicesFn,
|
|
|
|
csiCB csimanager.UpdateNodeCSIInfoFunc) *batchNodeUpdates {
|
2018-12-13 06:41:44 +00:00
|
|
|
|
|
|
|
return &batchNodeUpdates{
|
2019-10-22 13:20:26 +00:00
|
|
|
drivers: make(map[string]*structs.DriverInfo),
|
|
|
|
driverCB: driverCB,
|
|
|
|
devices: []*structs.NodeDeviceResource{},
|
|
|
|
devicesCB: devicesCB,
|
|
|
|
csiNodePlugins: make(map[string]*structs.CSIInfo),
|
|
|
|
csiControllerPlugins: make(map[string]*structs.CSIInfo),
|
|
|
|
csiCB: csiCB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateNodeFromCSI implements csimanager.UpdateNodeCSIInfoFunc and is used in
|
2020-02-21 19:48:16 +00:00
|
|
|
// the csi manager to send csi fingerprints to the server.
|
2019-10-22 13:20:26 +00:00
|
|
|
func (b *batchNodeUpdates) updateNodeFromCSI(plugin string, info *structs.CSIInfo) {
|
|
|
|
b.csiMu.Lock()
|
|
|
|
defer b.csiMu.Unlock()
|
|
|
|
if b.csiBatched {
|
|
|
|
b.csiCB(plugin, info)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:48:16 +00:00
|
|
|
// Only one of these is expected to be set, but a future implementation that
|
|
|
|
// explicitly models monolith plugins with a single fingerprinter may set both
|
|
|
|
if info.ControllerInfo != nil {
|
|
|
|
b.csiControllerPlugins[plugin] = info
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.NodeInfo != nil {
|
|
|
|
b.csiNodePlugins[plugin] = info
|
|
|
|
}
|
2019-10-22 13:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// batchCSIUpdates sends all of the batched CSI updates by calling f for each
|
|
|
|
// plugin batched
|
|
|
|
func (b *batchNodeUpdates) batchCSIUpdates(f csimanager.UpdateNodeCSIInfoFunc) error {
|
|
|
|
b.csiMu.Lock()
|
|
|
|
defer b.csiMu.Unlock()
|
|
|
|
if b.csiBatched {
|
|
|
|
return fmt.Errorf("csi updates already batched")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.csiBatched = true
|
|
|
|
for plugin, info := range b.csiNodePlugins {
|
|
|
|
f(plugin, info)
|
|
|
|
}
|
|
|
|
for plugin, info := range b.csiControllerPlugins {
|
|
|
|
f(plugin, info)
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
2019-10-22 13:20:26 +00:00
|
|
|
return nil
|
2018-12-13 06:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateNodeFromDriver implements drivermanager.UpdateNodeDriverInfoFn and is
|
|
|
|
// used in the driver manager to send driver fingerprints to
|
|
|
|
func (b *batchNodeUpdates) updateNodeFromDriver(driver string, info *structs.DriverInfo) {
|
|
|
|
b.driversMu.Lock()
|
|
|
|
defer b.driversMu.Unlock()
|
|
|
|
if b.driversBatched {
|
|
|
|
b.driverCB(driver, info)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.drivers[driver] = info
|
|
|
|
}
|
|
|
|
|
|
|
|
// batchDriverUpdates sends all of the batched driver node updates by calling f
|
|
|
|
// for each driver batched
|
|
|
|
func (b *batchNodeUpdates) batchDriverUpdates(f drivermanager.UpdateNodeDriverInfoFn) error {
|
|
|
|
b.driversMu.Lock()
|
|
|
|
defer b.driversMu.Unlock()
|
|
|
|
if b.driversBatched {
|
|
|
|
return fmt.Errorf("driver updates already batched")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.driversBatched = true
|
|
|
|
for driver, info := range b.drivers {
|
|
|
|
f(driver, info)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateNodeFromDevices implements devicemanager.UpdateNodeDevicesFn and is
|
2018-12-18 03:54:25 +00:00
|
|
|
// used in the device manager to send device fingerprints to
|
2018-12-13 06:41:44 +00:00
|
|
|
func (b *batchNodeUpdates) updateNodeFromDevices(devices []*structs.NodeDeviceResource) {
|
|
|
|
b.devicesMu.Lock()
|
|
|
|
defer b.devicesMu.Unlock()
|
|
|
|
if b.devicesBatched {
|
|
|
|
b.devicesCB(devices)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.devices = devices
|
|
|
|
}
|
|
|
|
|
|
|
|
// batchDevicesUpdates sends the batched device node updates by calling f with
|
|
|
|
// the devices
|
|
|
|
func (b *batchNodeUpdates) batchDevicesUpdates(f devicemanager.UpdateNodeDevicesFn) error {
|
|
|
|
b.devicesMu.Lock()
|
|
|
|
defer b.devicesMu.Unlock()
|
|
|
|
if b.devicesBatched {
|
|
|
|
return fmt.Errorf("devices updates already batched")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.devicesBatched = true
|
|
|
|
f(b.devices)
|
|
|
|
return nil
|
|
|
|
}
|