Skip to content

Commit 25ccb1f

Browse files
author
ZhengW22
committed
Change var names and add modify map struct.
Signed-off-by: ZhengW22 <[email protected]>
1 parent 2680d5a commit 25ccb1f

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

pkg/scheduler/nodes.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
"github.com/Project-HAMi/HAMi/pkg/device/nvidia"
3737
)
3838

39-
var vendorUUIDMap = map[string][]string{
39+
var vendorNoUseAnnoKeyMap = map[string][]string{
4040
nvidia.GPUNoUseUUID: {nvidia.NvidiaGPUDevice},
4141
cambricon.MLUNoUseUUID: {cambricon.CambriconMLUDevice},
4242
hygon.DCUNoUseUUID: {hygon.HygonDCUDevice},
@@ -94,35 +94,40 @@ func (m *nodeManager) addNode(nodeID string, nodeInfo *device.NodeInfo) {
9494
m.nodes[nodeID].Devices = rmDeviceByNodeAnnotation(m.nodes[nodeID])
9595
}
9696

97-
func rmDeviceByNodeAnnotation(nodeInfo *device.NodeInfo) []device.DeviceInfo {
98-
disableGPUUUIDVendorMap := make(map[string][]string)
97+
func rmDeviceByNodeAnnotation(nodeInfo *util.NodeInfo) []util.DeviceInfo {
98+
vendorWithDisableGPUUUIDMap := make(map[string]map[string]bool)
9999
if nodeInfo.Node != nil && nodeInfo.Node.Annotations != nil {
100-
for annoKey, vendor := range vendorUUIDMap {
101-
klog.V(5).Infof("Current annokey is %s, and vendor is %v", annoKey, vendor)
100+
for annoKey, vendors := range vendorNoUseAnnoKeyMap {
101+
klog.V(5).Infof("Current annokey is %s, and vendor is %v", annoKey, vendors)
102102
if value, ok := nodeInfo.Node.Annotations[annoKey]; ok {
103103
disableGPUUUIDList := strings.Split(value, ",")
104104
klog.V(5).Infof("Disable gpu uuid list is: %v", disableGPUUUIDList)
105105
for _, disableGPUUUID := range disableGPUUUIDList {
106106
if id := strings.TrimSpace(disableGPUUUID); id != "" {
107-
disableGPUUUIDVendorMap[id] = vendor
107+
for _, vendor := range vendors {
108+
if vendorWithDisableGPUUUIDMap[vendor] == nil {
109+
newVendorMap := make(map[string]bool)
110+
newVendorMap[disableGPUUUID] = true
111+
vendorWithDisableGPUUUIDMap[vendor] = newVendorMap
112+
} else {
113+
vendorWithDisableGPUUUIDMap[vendor][disableGPUUUID] = true
114+
}
115+
}
108116
}
109117
}
110118
}
111119
}
112120
}
113-
if len(disableGPUUUIDVendorMap) == 0 {
121+
if len(vendorWithDisableGPUUUIDMap) == 0 {
114122
return nodeInfo.Devices
115123
}
116124
tmp := make([]device.DeviceInfo, 0, len(nodeInfo.Devices))
117125
for _, d := range nodeInfo.Devices {
118126
removeFlag := false
119-
if vendorList, ok := disableGPUUUIDVendorMap[d.ID]; ok {
120-
for _, vendor := range vendorList {
121-
if vendor == d.DeviceVendor {
122-
klog.V(5).Infof("Disable gpu uuid is : %s", d.ID)
123-
removeFlag = true
124-
break
125-
}
127+
if disableGPUUUIDMap, ok := vendorWithDisableGPUUUIDMap[d.DeviceVendor]; ok {
128+
if ok := disableGPUUUIDMap[d.ID]; ok {
129+
klog.V(5).Infof("Disable gpu uuid is : %s", d.ID)
130+
removeFlag = true
126131
}
127132
}
128133
if !removeFlag {

pkg/scheduler/nodes_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package scheduler
1919
import (
2020
"fmt"
2121
"reflect"
22+
"strings"
2223
"testing"
2324

2425
"github.com/Project-HAMi/HAMi/pkg/device"
@@ -30,6 +31,7 @@ import (
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132

3233
"github.com/Project-HAMi/HAMi/pkg/device"
34+
"github.com/Project-HAMi/HAMi/pkg/device/metax"
3335
"github.com/Project-HAMi/HAMi/pkg/device/nvidia"
3436
"github.com/Project-HAMi/HAMi/pkg/util"
3537
)
@@ -332,7 +334,7 @@ func Test_rmNodeDevices(t *testing.T) {
332334

333335
func Test_rmDeviceByNodeAnnotation(t *testing.T) {
334336
id1 := "60151478-4709-4242-a8c1-a944252d194b"
335-
id2 := "60151478-4709-4242-a8c1-a944252d194d"
337+
id2 := "33c00a52-72ab-4b61-a7ce-43107588835b"
336338
type args struct {
337339
nodeInfo *device.NodeInfo
338340
}
@@ -342,7 +344,7 @@ func Test_rmDeviceByNodeAnnotation(t *testing.T) {
342344
want []device.DeviceInfo
343345
}{
344346
{
345-
name: "Test remove device",
347+
name: "Test remove one device",
346348
args: args{
347349
nodeInfo: &util.NodeInfo{
348350
Node: &corev1.Node{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{nvidia.GPUNoUseUUID: id1}}},
@@ -351,6 +353,26 @@ func Test_rmDeviceByNodeAnnotation(t *testing.T) {
351353
},
352354
want: []device.DeviceInfo{},
353355
},
356+
{
357+
name: "Test remove two devices",
358+
args: args{
359+
nodeInfo: &util.NodeInfo{
360+
Node: &corev1.Node{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{nvidia.GPUNoUseUUID: strings.Join([]string{id1, id2}, ",")}}},
361+
Devices: []util.DeviceInfo{{DeviceVendor: nvidia.NvidiaGPUDevice, ID: id1}, {DeviceVendor: nvidia.NvidiaGPUDevice, ID: id2}},
362+
},
363+
},
364+
want: []util.DeviceInfo{},
365+
},
366+
{
367+
name: "Test remove one device and keep one device",
368+
args: args{
369+
nodeInfo: &util.NodeInfo{
370+
Node: &corev1.Node{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{nvidia.GPUNoUseUUID: strings.Join([]string{id2}, ",")}}},
371+
Devices: []util.DeviceInfo{{DeviceVendor: nvidia.NvidiaGPUDevice, ID: id1}, {DeviceVendor: nvidia.NvidiaGPUDevice, ID: id2}},
372+
},
373+
},
374+
want: []util.DeviceInfo{{DeviceVendor: nvidia.NvidiaGPUDevice, ID: id1}},
375+
},
354376
{
355377
name: "Test no removing device, case1",
356378
args: args{
@@ -371,6 +393,26 @@ func Test_rmDeviceByNodeAnnotation(t *testing.T) {
371393
},
372394
want: []util.DeviceInfo{{DeviceVendor: nvidia.NvidiaGPUDevice, ID: id1}},
373395
},
396+
{
397+
name: "Test removing metax device, case1",
398+
args: args{
399+
nodeInfo: &util.NodeInfo{
400+
Node: &corev1.Node{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{metax.MetaxNoUseUUID: id1}}},
401+
Devices: []util.DeviceInfo{{DeviceVendor: metax.MetaxGPUDevice, ID: id1}},
402+
},
403+
},
404+
want: []util.DeviceInfo{},
405+
},
406+
{
407+
name: "Test removing metax device, case2",
408+
args: args{
409+
nodeInfo: &util.NodeInfo{
410+
Node: &corev1.Node{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{metax.MetaxNoUseUUID: id1}}},
411+
Devices: []util.DeviceInfo{{DeviceVendor: metax.MetaxSGPUDevice, ID: id1}},
412+
},
413+
},
414+
want: []util.DeviceInfo{},
415+
},
374416
}
375417
for _, tt := range tests {
376418
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)