Skip to content

Commit

Permalink
Merge pull request #143 from chaunceyjiang/mutex
Browse files Browse the repository at this point in the history
reduce race condition. replace mutex to RWmutex
  • Loading branch information
archlitchi authored Feb 4, 2024
2 parents b312f94 + e8a0353 commit c1d8616
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pkg/scheduler/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type NodeUsage struct {

type nodeManager struct {
nodes map[string]*NodeInfo
mutex sync.Mutex
mutex sync.RWMutex
}

func (m *nodeManager) init() {
Expand Down Expand Up @@ -101,14 +101,16 @@ func (m *nodeManager) rmNodeDevice(nodeID string, nodeInfo *NodeInfo) {
}

func (m *nodeManager) GetNode(nodeID string) (*NodeInfo, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.mutex.RLock()
defer m.mutex.RUnlock()
if n, ok := m.nodes[nodeID]; ok {
return n, nil
}
return &NodeInfo{}, fmt.Errorf("node %v not found", nodeID)
}

func (m *nodeManager) ListNodes() (map[string]*NodeInfo, error) {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.nodes, nil
}
4 changes: 3 additions & 1 deletion pkg/scheduler/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type podInfo struct {

type podManager struct {
pods map[k8stypes.UID]*podInfo
mutex sync.Mutex
mutex sync.RWMutex
}

func (m *podManager) init() {
Expand Down Expand Up @@ -65,6 +65,8 @@ func (m *podManager) delPod(pod *corev1.Pod) {
}

func (m *podManager) GetScheduledPods() (map[k8stypes.UID]*podInfo, error) {
m.mutex.RLock()
defer m.mutex.RUnlock()
klog.Infof("Getting all scheduled pods with %d nums", len(m.pods))
return m.pods, nil
}
2 changes: 1 addition & 1 deletion pkg/scheduler/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func fitInCertainDevice(node *NodeUsage, request util.ContainerDeviceRequest, an
originReq := k.Nums
prevnuma := -1
klog.InfoS("Allocating device for container request", "pod", klog.KObj(pod), "card request", k)
tmpDevs := []util.ContainerDevice{}
var tmpDevs []util.ContainerDevice
for i := len(node.Devices) - 1; i >= 0; i-- {
klog.InfoS("scoring pod", "pod", klog.KObj(pod), "Memreq", k.Memreq, "MemPercentagereq", k.MemPercentagereq, "Coresreq", k.Coresreq, "Nums", k.Nums, "device index", i, "device", node.Devices[i].Id)
found, numa := checkType(annos, *node.Devices[i], k)
Expand Down

0 comments on commit c1d8616

Please sign in to comment.