From 070b4ffe9f9960b5f06efa68c48b0ef09cdd52dd Mon Sep 17 00:00:00 2001 From: Kouhei Ueno Date: Sat, 12 Jul 2014 15:06:51 +0900 Subject: [PATCH] make firstfit.go threadsafe and fix its comments --- pkg/scheduler/firstfit.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/scheduler/firstfit.go b/pkg/scheduler/firstfit.go index e1e088c5cc4..250fa99faf1 100644 --- a/pkg/scheduler/firstfit.go +++ b/pkg/scheduler/firstfit.go @@ -19,16 +19,17 @@ package scheduler import ( "fmt" "math/rand" + "sync" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" ) -// FirstFitScheduler is a Scheduler interface implementation which uses first fit algorithm. +// FirstFitScheduler is a Scheduler which schedules a Pod on a random machine which matches its requirement. type FirstFitScheduler struct { - podLister PodLister - // TODO: *rand.Rand is *not* threadsafe - random *rand.Rand + podLister PodLister + random *rand.Rand + randomLock sync.Mutex } func MakeFirstFitScheduler(podLister PodLister, random *rand.Rand) Scheduler { @@ -49,7 +50,7 @@ func (s *FirstFitScheduler) containsPort(pod api.Pod, port api.Port) bool { return false } -// Schedule schedules a pod on the first machine which matches its requirement. +// Schedule schedules a pod on a random machine which matches its requirement. func (s *FirstFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (string, error) { machines, err := minionLister.List() if err != nil { @@ -83,5 +84,7 @@ func (s *FirstFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (st if len(machineOptions) == 0 { return "", fmt.Errorf("failed to find fit for %#v", pod) } + s.randomLock.Lock() + defer s.randomLock.Unlock() return machineOptions[s.random.Int()%len(machineOptions)], nil }