Skip to content

Commit c67be95

Browse files
author
zhangye
committed
Merge branch 'realloc' into 'master'
fix realloc bug See merge request !132
2 parents 9949beb + 6f19265 commit c67be95

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

cluster/calcium/realloc.go

+25-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package calcium
33
import (
44
"context"
55
"fmt"
6+
"sync"
67
"time"
78

89
log "github.com/Sirupsen/logrus"
@@ -61,14 +62,26 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t
6162
}
6263

6364
ch := make(chan *types.ReallocResourceMessage)
64-
for pod, nodeContainers := range containersInfo {
65-
if pod.Scheduler == CPU_PRIOR {
66-
nodeCPUContainersInfo := cpuContainersInfo[pod]
67-
go c.reallocContainersWithCPUPrior(ch, pod, nodeCPUContainersInfo, cpu, mem)
68-
continue
65+
go func() {
66+
defer close(ch)
67+
wg := sync.WaitGroup{}
68+
wg.Add(len(containersInfo))
69+
for pod, nodeContainers := range containersInfo {
70+
if pod.Scheduler == CPU_PRIOR {
71+
nodeCPUContainersInfo := cpuContainersInfo[pod]
72+
go func(pod *types.Pod, nodeCPUContainersInfo CPUNodeContainers) {
73+
defer wg.Done()
74+
c.reallocContainersWithCPUPrior(ch, pod, nodeCPUContainersInfo, cpu, mem)
75+
}(pod, nodeCPUContainersInfo)
76+
continue
77+
}
78+
go func(pod *types.Pod, nodeContainers NodeContainers) {
79+
defer wg.Done()
80+
c.reallocContainerWithMemoryPrior(ch, pod, nodeContainers, cpu, mem)
81+
}(pod, nodeContainers)
6982
}
70-
go c.reallocContainerWithMemoryPrior(ch, pod, nodeContainers, cpu, mem)
71-
}
83+
wg.Wait()
84+
}()
7285
return ch, nil
7386
}
7487

@@ -102,8 +115,9 @@ func (c *calcium) reallocContainerWithMemoryPrior(
102115
return
103116
}
104117

118+
// 不并发操作了
105119
for node, containers := range nodeContainers {
106-
go c.doUpdateContainerWithMemoryPrior(ch, pod.Name, node, containers, cpu, memory)
120+
c.doUpdateContainerWithMemoryPrior(ch, pod.Name, node, containers, cpu, memory)
107121
}
108122
}
109123

@@ -221,8 +235,10 @@ func (c *calcium) reallocContainersWithCPUPrior(
221235
log.Errorf("[realloc] realloc cpu resource failed %v", err)
222236
return
223237
}
238+
239+
// 不并发操作了
224240
for cpu, nodesCPUResult := range nodesCPUMap {
225-
go c.doReallocContainersWithCPUPrior(ch, pod.Name, nodesCPUResult, nodesInfoMap[cpu])
241+
c.doReallocContainersWithCPUPrior(ch, pod.Name, nodesCPUResult, nodesInfoMap[cpu])
226242
}
227243
}
228244

0 commit comments

Comments
 (0)