fix allDevices map data race

This commit is contained in:
huyinhou 2022-12-29 18:27:08 +08:00
parent 997cefc9da
commit b9987eeb6c
3 changed files with 9 additions and 3 deletions

View File

@ -646,7 +646,7 @@ func (m *ManagerImpl) devicesToAllocate(podUID, contName, resource string, requi
func (m *ManagerImpl) filterByAffinity(podUID, contName, resource string, available sets.String) (sets.String, sets.String, sets.String) { func (m *ManagerImpl) filterByAffinity(podUID, contName, resource string, available sets.String) (sets.String, sets.String, sets.String) {
// If alignment information is not available, just pass the available list back. // If alignment information is not available, just pass the available list back.
hint := m.topologyAffinityStore.GetAffinity(podUID, contName) hint := m.topologyAffinityStore.GetAffinity(podUID, contName)
if !m.deviceHasTopologyAlignment(resource) || hint.NUMANodeAffinity == nil { if !m.deviceHasTopologyAlignmentLocked(resource) || hint.NUMANodeAffinity == nil {
return sets.NewString(), sets.NewString(), available return sets.NewString(), sets.NewString(), available
} }

View File

@ -136,7 +136,7 @@ func (m *ManagerImpl) GetPodTopologyHints(pod *v1.Pod) map[string][]topologymana
return deviceHints return deviceHints
} }
func (m *ManagerImpl) deviceHasTopologyAlignment(resource string) bool { func (m *ManagerImpl) deviceHasTopologyAlignmentLocked(resource string) bool {
// If any device has Topology NUMANodes available, we assume they care about alignment. // If any device has Topology NUMANodes available, we assume they care about alignment.
for _, device := range m.allDevices[resource] { for _, device := range m.allDevices[resource] {
if device.Topology != nil && len(device.Topology.Nodes) > 0 { if device.Topology != nil && len(device.Topology.Nodes) > 0 {
@ -146,6 +146,12 @@ func (m *ManagerImpl) deviceHasTopologyAlignment(resource string) bool {
return false return false
} }
func (m *ManagerImpl) deviceHasTopologyAlignment(resource string) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.deviceHasTopologyAlignmentLocked(resource)
}
func (m *ManagerImpl) getAvailableDevices(resource string) sets.String { func (m *ManagerImpl) getAvailableDevices(resource string) sets.String {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()

View File

@ -456,7 +456,7 @@ func TestTopologyAlignedAllocation(t *testing.T) {
} }
alignment := make(map[int]int) alignment := make(map[int]int)
if m.deviceHasTopologyAlignment(tc.resource) { if m.deviceHasTopologyAlignmentLocked(tc.resource) {
for d := range allocated { for d := range allocated {
if m.allDevices[tc.resource][d].Topology != nil { if m.allDevices[tc.resource][d].Topology != nil {
alignment[int(m.allDevices[tc.resource][d].Topology.Nodes[0].ID)]++ alignment[int(m.allDevices[tc.resource][d].Topology.Nodes[0].ID)]++