@@ -3,6 +3,7 @@ package calcium
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "sync"
6
7
"time"
7
8
8
9
log "github.com/Sirupsen/logrus"
@@ -61,14 +62,26 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t
61
62
}
62
63
63
64
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 )
69
82
}
70
- go c . reallocContainerWithMemoryPrior ( ch , pod , nodeContainers , cpu , mem )
71
- }
83
+ wg . Wait ( )
84
+ }()
72
85
return ch , nil
73
86
}
74
87
@@ -102,8 +115,9 @@ func (c *calcium) reallocContainerWithMemoryPrior(
102
115
return
103
116
}
104
117
118
+ // 不并发操作了
105
119
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 )
107
121
}
108
122
}
109
123
@@ -221,8 +235,10 @@ func (c *calcium) reallocContainersWithCPUPrior(
221
235
log .Errorf ("[realloc] realloc cpu resource failed %v" , err )
222
236
return
223
237
}
238
+
239
+ // 不并发操作了
224
240
for cpu , nodesCPUResult := range nodesCPUMap {
225
- go c .doReallocContainersWithCPUPrior (ch , pod .Name , nodesCPUResult , nodesInfoMap [cpu ])
241
+ c .doReallocContainersWithCPUPrior (ch , pod .Name , nodesCPUResult , nodesInfoMap [cpu ])
226
242
}
227
243
}
228
244
0 commit comments