From 7b127311a4115994dda17204af89eff2683550e7 Mon Sep 17 00:00:00 2001 From: Abdullah Gharaibeh Date: Wed, 8 May 2019 22:19:21 -0400 Subject: [PATCH] Make thread-safe the prebind callback of stateful plugin in scheduler framework. --- .../framework/plugins/examples/stateful/stateful.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/scheduler/framework/plugins/examples/stateful/stateful.go b/pkg/scheduler/framework/plugins/examples/stateful/stateful.go index e42022a48bb..30b2104a473 100644 --- a/pkg/scheduler/framework/plugins/examples/stateful/stateful.go +++ b/pkg/scheduler/framework/plugins/examples/stateful/stateful.go @@ -18,6 +18,7 @@ package stateful import ( "fmt" + "sync" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -31,6 +32,7 @@ import ( type MultipointExample struct { mpState map[int]string numRuns int + mu sync.RWMutex } var _ = framework.ReservePlugin(&MultipointExample{}) @@ -46,12 +48,16 @@ func (mp *MultipointExample) Name() string { // Reserve is the functions invoked by the framework at "reserve" extension point. func (mp *MultipointExample) Reserve(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status { + // Reserve is not called concurrently, and so we don't need to lock. mp.numRuns++ return nil } // Prebind is the functions invoked by the framework at "prebind" extension point. func (mp *MultipointExample) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status { + // Prebind could be called concurrently for different pods. + mp.mu.Lock() + defer mp.mu.Unlock() mp.numRuns++ if pod == nil { return framework.NewStatus(framework.Error, "pod must not be nil")