scheduler: pull node shuffle into util

This commit is contained in:
Armon Dadgar 2015-09-07 11:23:38 -07:00
parent ad681be59c
commit f1a93b0aa7
3 changed files with 28 additions and 6 deletions

View file

@ -2,7 +2,6 @@ package scheduler
import (
"fmt"
"math/rand"
"reflect"
"strings"
@ -68,11 +67,7 @@ func (iter *StaticIterator) Reset() {
// is applied in-place
func NewRandomIterator(ctx Context, nodes []*structs.Node) *StaticIterator {
// shuffle with the Fisher-Yates algorithm
n := len(nodes)
for i := n - 1; i > 0; i-- {
j := rand.Intn(i + 1)
nodes[i], nodes[j] = nodes[j], nodes[i]
}
shuffleNodes(nodes)
// Create a static iterator
return NewStaticIterator(ctx, nodes)

View file

@ -2,6 +2,7 @@ package scheduler
import (
"fmt"
"math/rand"
"github.com/hashicorp/nomad/nomad/structs"
)
@ -195,3 +196,12 @@ func taintedNodes(state State, allocs []*structs.Allocation) (map[string]bool, e
}
return out, nil
}
// shuffleNodes randomizes the slice order with the Fisher-Yates algorithm
func shuffleNodes(nodes []*structs.Node) {
n := len(nodes)
for i := n - 1; i > 0; i-- {
j := rand.Intn(i + 1)
nodes[i], nodes[j] = nodes[j], nodes[i]
}
}

View file

@ -3,6 +3,7 @@ package scheduler
import (
"fmt"
"os"
"reflect"
"testing"
"github.com/hashicorp/nomad/nomad/mock"
@ -210,3 +211,19 @@ func TestTaintedNodes(t *testing.T) {
t.Fatalf("Bad: %v", tainted)
}
}
func TestShuffleNodes(t *testing.T) {
nodes := []*structs.Node{
mock.Node(),
mock.Node(),
mock.Node(),
mock.Node(),
mock.Node(),
}
orig := make([]*structs.Node, len(nodes))
copy(orig, nodes)
shuffleNodes(nodes)
if reflect.DeepEqual(nodes, orig) {
t.Fatalf("shoudl not match")
}
}