make firstfit.go threadsafe and fix its comments

This commit is contained in:
Kouhei Ueno 2014-07-12 15:06:51 +09:00
parent 2a03a4d502
commit 070b4ffe9f

View File

@ -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
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
}