Merge pull request #117199 from pohly/scheduler-perf-race-fix

scheduler_perf: fix race condition
This commit is contained in:
Kubernetes Prow Robot 2023-04-11 21:18:05 -07:00 committed by GitHub
commit 69b59b9d42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -831,12 +831,19 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [
var collectors []testDataCollector var collectors []testDataCollector
var collectorCtx context.Context var collectorCtx context.Context
var collectorCancel func() var collectorCancel func()
var collectorWG sync.WaitGroup
if concreteOp.CollectMetrics { if concreteOp.CollectMetrics {
collectorCtx, collectorCancel = context.WithCancel(ctx) collectorCtx, collectorCancel = context.WithCancel(ctx)
defer collectorCancel() defer collectorCancel()
collectors = getTestDataCollectors(podInformer, fmt.Sprintf("%s/%s", b.Name(), namespace), namespace, tc.MetricsCollectorConfig) collectors = getTestDataCollectors(podInformer, fmt.Sprintf("%s/%s", b.Name(), namespace), namespace, tc.MetricsCollectorConfig)
for _, collector := range collectors { for _, collector := range collectors {
go collector.run(collectorCtx) // Need loop-local variable for function below.
collector := collector
collectorWG.Add(1)
go func() {
defer collectorWG.Done()
collector.run(collectorCtx)
}()
} }
} }
if err := createPods(ctx, b, namespace, concreteOp, client); err != nil { if err := createPods(ctx, b, namespace, concreteOp, client); err != nil {
@ -860,6 +867,7 @@ func runWorkload(ctx context.Context, b *testing.B, tc *testCase, w *workload) [
// same time, so if we're here, it means that all pods have been // same time, so if we're here, it means that all pods have been
// scheduled. // scheduled.
collectorCancel() collectorCancel()
collectorWG.Wait()
mu.Lock() mu.Lock()
for _, collector := range collectors { for _, collector := range collectors {
dataItems = append(dataItems, collector.collect()...) dataItems = append(dataItems, collector.collect()...)