Skip to content

Commit

Permalink
address comments from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Feb 7, 2022
1 parent 438b099 commit 66f943c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions scheduler/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func testContext(t testing.TB) (*state.StateStore, *EvalContext) {
state := state.TestStateStore(t)
plan := &structs.Plan{
EvalID: uuid.Generate(),
NodeUpdate: make(map[string][]*structs.Allocation),
NodeAllocation: make(map[string][]*structs.Allocation),
NodePreemptions: make(map[string][]*structs.Allocation),
Expand Down
2 changes: 1 addition & 1 deletion scheduler/feasible.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (iter *StaticIterator) SetNodes(nodes []*structs.Node) {
// is applied in-place
func NewRandomIterator(ctx Context, nodes []*structs.Node) *StaticIterator {
// shuffle with the Fisher-Yates algorithm
shuffleNodes(ctx.Plan().EvalID, nodes)
shuffleNodes(ctx.Plan(), nodes)

// Create a static iterator
return NewStaticIterator(ctx, nodes)
Expand Down
2 changes: 1 addition & 1 deletion scheduler/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type GenericStack struct {

func (s *GenericStack) SetNodes(baseNodes []*structs.Node) {
// Shuffle base nodes
shuffleNodes(s.ctx.Plan().EvalID, baseNodes)
shuffleNodes(s.ctx.Plan(), baseNodes)

// Update the set of base nodes
s.source.SetNodes(baseNodes)
Expand Down
15 changes: 13 additions & 2 deletions scheduler/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,20 @@ func taintedNodes(state State, allocs []*structs.Allocation) (map[string]*struct
// algorithm. We seed the random source with the eval ID (which is
// random) to aid in postmortem debuggging of specific evaluations and
// state snapshots.
func shuffleNodes(evalID string, nodes []*structs.Node) {
func shuffleNodes(plan *structs.Plan, nodes []*structs.Node) {

// use the last 4 bytes because those are the random bits
// if we have sortable IDs
buf := []byte(plan.EvalID)
seed := int64(binary.BigEndian.Uint32(buf[len(buf)-4:]))

// include the plan progress so that we don't retry with
// the exact same shuffle
seed = seed |
int64(len(plan.NodeAllocation)) |
int64(len(plan.NodeUpdate)) |
int64(len(plan.DeploymentUpdates))

seed, _ := binary.Varint([]byte(evalID))
r := rand.New(rand.NewSource(seed))

n := len(nodes)
Expand Down
4 changes: 3 additions & 1 deletion scheduler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ func TestShuffleNodes(t *testing.T) {
}
orig := make([]*structs.Node, len(nodes))
copy(orig, nodes)
shuffleNodes(uuid.Generate(), nodes)
eval := mock.Eval() // will have random EvalID
plan := eval.MakePlan(mock.Job())
shuffleNodes(plan, nodes)
require.False(t, reflect.DeepEqual(nodes, orig))
}

Expand Down

0 comments on commit 66f943c

Please sign in to comment.