refactor: make merge semantics explicit in OnHostConfigUpdated/OnClusterUpdated

Start from existing entry and apply this-source's fields, rather than
starting from incoming and restoring the other-source's fields. The new
pattern makes it immediately clear which fields each push type WRITES,
without having to reason about which fields are being "restored".

OnHostUpdated keeps the merged := *incoming + if ok guard because the
worker push also populates HostModel/HostType from attrs; config-sourced
values must only win when a prior config push has already set them.

All 37 backend unit tests pass.
This commit is contained in:
Rongsong Shen 2026-04-17 06:48:25 +00:00
parent 893812e7aa
commit ac7ac99ced

View file

@ -38,52 +38,32 @@ func (h *DefaultHandler) OnHostUpdated(incoming *models.HostInfo) {
store.Hosts.Put(merged.HostName, merged) store.Hosts.Put(merged.HostName, merged)
} }
// OnHostConfigUpdated merges config-sourced fields into an existing host entry. // OnHostConfigUpdated applies config-sourced fields onto the existing host entry.
// Worker-sourced fields (HostStatus, CPUFactor, NumCPUs, MaxMem, MaxSwap, // All other fields (worker-sourced) are preserved from the prior entry.
// MaxTmp, NumDisk, NumRun, NumSSusp, NumUSusp, NumDone) are preserved from // NumCores, NumThreads, Topology, SchedStop have no current write path in
// the prior entry if one exists. // rawWorkerData and remain zero until a future push source populates them
// NumCores, NumThreads, Topology, SchedStop are preserved here but currently // (analogous to the DisptWindow deferral).
// have no write path in rawWorkerData — they remain zero until a future worker
// push source populates them (analogous to the DisptWindow deferral).
func (h *DefaultHandler) OnHostConfigUpdated(incoming *models.HostInfo) { func (h *DefaultHandler) OnHostConfigUpdated(incoming *models.HostInfo) {
merged := *incoming existing, _ := store.Hosts.Get(incoming.HostName)
if existing, ok := store.Hosts.Get(merged.HostName); ok { existing.HostName = incoming.HostName
merged.HostStatus = existing.HostStatus existing.HostModel = incoming.HostModel
merged.CPUFactor = existing.CPUFactor existing.HostType = incoming.HostType
merged.NumCPUs = existing.NumCPUs existing.MaxSlot = incoming.MaxSlot
merged.NumCores = existing.NumCores existing.DisptWindow = incoming.DisptWindow
merged.NumThreads = existing.NumThreads store.Hosts.Put(existing.HostName, existing)
merged.MaxMem = existing.MaxMem
merged.MaxSwap = existing.MaxSwap
merged.MaxTmp = existing.MaxTmp
merged.NumDisk = existing.NumDisk
merged.NumRun = existing.NumRun
merged.NumSSusp = existing.NumSSusp
merged.NumUSusp = existing.NumUSusp
merged.NumDone = existing.NumDone
merged.Topology = existing.Topology
merged.SchedStop = existing.SchedStop
}
store.Hosts.Put(merged.HostName, merged)
} }
// OnClusterUpdated merges config-sourced fields into an existing cluster entry. // OnClusterUpdated applies config-sourced fields onto the existing cluster entry.
// Built-in fields (Version, Status, ControllerName, NumServers, Resources, // All other fields (built-in/worker-sourced: Version, Status, ControllerName,
// HostTypes, HostModels, ClusterAdmins) are preserved from the prior entry // NumServers, Resources, HostTypes, HostModels, ClusterAdmins) are preserved
// if one exists. // from the prior entry.
func (h *DefaultHandler) OnClusterUpdated(incoming *models.ClusterInfo) { func (h *DefaultHandler) OnClusterUpdated(incoming *models.ClusterInfo) {
merged := *incoming existing, _ := store.Clusters.Get(incoming.ClusterName)
if existing, ok := store.Clusters.Get(merged.ClusterName); ok { existing.ClusterName = incoming.ClusterName
merged.Version = existing.Version existing.HeartbeatInterval = incoming.HeartbeatInterval
merged.Status = existing.Status existing.Vcpus = incoming.Vcpus
merged.ControllerName = existing.ControllerName existing.InactiveHeartbeatCount = incoming.InactiveHeartbeatCount
merged.NumServers = existing.NumServers store.Clusters.Put(existing.ClusterName, existing)
merged.Resources = existing.Resources
merged.HostTypes = existing.HostTypes
merged.HostModels = existing.HostModels
merged.ClusterAdmins = existing.ClusterAdmins
}
store.Clusters.Put(merged.ClusterName, merged)
} }
func (h *DefaultHandler) OnResourceUpdated(res *models.ResourceDefinitionInfo) { func (h *DefaultHandler) OnResourceUpdated(res *models.ResourceDefinitionInfo) {