From b6b2838a9596d6779f8e9170fd7f09d1bb64b87f Mon Sep 17 00:00:00 2001 From: Wei Huang Date: Wed, 7 Aug 2019 10:33:44 -0700 Subject: [PATCH] Fix a racing issue in FakeFilterPlugin Filter() is called simultaneously, so the member of its (fake) implementation cannot be written without lock. The issue can be triggered by: go test k8s.io/kubernetes/pkg/scheduler/core --race --count=50 --- pkg/scheduler/core/generic_scheduler_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/scheduler/core/generic_scheduler_test.go b/pkg/scheduler/core/generic_scheduler_test.go index 916181e5909..9a1cd27bd2c 100644 --- a/pkg/scheduler/core/generic_scheduler_test.go +++ b/pkg/scheduler/core/generic_scheduler_test.go @@ -22,6 +22,7 @@ import ( "reflect" "strconv" "strings" + "sync/atomic" "testing" "time" @@ -142,7 +143,7 @@ var emptyFramework, _ = framework.NewFramework(EmptyPluginRegistry, nil, []sched // FakeFilterPlugin is a test filter plugin used by default scheduler. type FakeFilterPlugin struct { - numFilterCalled int + numFilterCalled int32 failFilter bool } @@ -162,7 +163,7 @@ func (fp *FakeFilterPlugin) reset() { // Filter is a test function that returns an error or nil, depending on the // value of "failFilter". func (fp *FakeFilterPlugin) Filter(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status { - fp.numFilterCalled++ + atomic.AddInt32(&fp.numFilterCalled, 1) if fp.failFilter { return framework.NewStatus(framework.Unschedulable, fmt.Sprintf("injecting failure for pod %v", pod.Name)) @@ -171,7 +172,7 @@ func (fp *FakeFilterPlugin) Filter(pc *framework.PluginContext, pod *v1.Pod, nod return nil } -// NewFilterPlugin is the factory for filtler plugin. +// NewFilterPlugin is the factory for filter plugin. func NewFilterPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) { return filterPlugin, nil }