Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scheduler: seed random shuffle of nodes with eval ID #12008

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/12008.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
scheduler: Seed node shuffling with the evaluation ID to make the order reproducible
```
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
3 changes: 2 additions & 1 deletion scheduler/feasible.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ 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(nodes)
idx, _ := ctx.State().LatestIndex()
shuffleNodes(ctx.Plan(), idx, nodes)

// Create a static iterator
return NewStaticIterator(ctx, nodes)
Expand Down
3 changes: 3 additions & 0 deletions scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type State interface {

// CSIVolumeByID fetch CSI volumes, containing controller jobs
CSIVolumesByNodeID(memdb.WatchSet, string, string) (memdb.ResultIterator, error)

// LatestIndex returns the greatest index value for all indexes.
LatestIndex() (uint64, error)
}

// Planner interface is used to submit a task allocation plan.
Expand Down
3 changes: 2 additions & 1 deletion scheduler/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ type GenericStack struct {

func (s *GenericStack) SetNodes(baseNodes []*structs.Node) {
// Shuffle base nodes
shuffleNodes(baseNodes)
idx, _ := s.ctx.State().LatestIndex()
Copy link
Member

@schmichael schmichael Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't believe how many words I'm writing about we need a nonce 😅, however I think there are some subtle pros and cons to this approach:

tl;dr This allows writing reproducible tests against deterministic state stores which is the main improvement we're looking for! :shipit:

Pros:

  1. Subsequent scheduling attempts of the same eval will be shuffled differently! This accomplishes that goal perfectly. 🎉
  2. Test cases with deterministic state store updates will make shuffle nodes deterministically.

Cons:

  1. When trying to reproduce scheduling behavior from a user's snapshot, the LatestIndex() will be the snapshot's index. The snapshot was likely taken long (in Raft index terms (pun not intended)) after when the scheduling attempt we wish to reproduce was made. Obviously in cases like this the entire state snapshot is likely divergent from the case we wish to inspect, so we can only reduce non-determinism, not remove it. I'm unsure how much value there is in reducing non-determinism in this case.
  2. Workers only ever enforce the snapshots are >= a Raft index (Eval.WaitIndex initially, the RefreshIndex on subsequent attempts). Whether the index is == (reproducible shuffle!) or > (oh no) is entirely dependent on races between the Raft/fsm and scheduling subsystems. Even if the indexes are only off-by-1 the nodes will be shuffled completely differently.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the primary use case for this is to make it so that we can have deterministic tests, maybe the right way to deal with this is to just have the index used for the shuffle get logged somewhere. That way we can feed that index to the shuffle function as part of tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that or the retry # option.

Although I don't think there's a rush as this should work for those tests where we either bypass raft and manually insert objects into the state store or we load a snapshot into an otherwise inactive server and perform some scheduling attempts.

shuffleNodes(s.ctx.Plan(), idx, baseNodes)

// Update the set of base nodes
s.source.SetNodes(baseNodes)
Expand Down
21 changes: 18 additions & 3 deletions scheduler/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scheduler

import (
"encoding/binary"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -376,11 +377,25 @@ func taintedNodes(state State, allocs []*structs.Allocation) (map[string]*struct
return out, nil
}

// shuffleNodes randomizes the slice order with the Fisher-Yates algorithm
func shuffleNodes(nodes []*structs.Node) {
// shuffleNodes randomizes the slice order with the Fisher-Yates
// algorithm. We seed the random source with the eval ID (which is
// random) to aid in postmortem debugging of specific evaluations and
// state snapshots.
tgross marked this conversation as resolved.
Show resolved Hide resolved
func shuffleNodes(plan *structs.Plan, index uint64, nodes []*structs.Node) {

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

// for retried plans the index is the plan result's RefreshIndex
// so that we don't retry with the exact same shuffle
seed ^= index
r := rand.New(rand.NewSource(int64(seed >> 2)))

n := len(nodes)
for i := n - 1; i > 0; i-- {
j := rand.Intn(i + 1)
j := r.Intn(i + 1)
nodes[i], nodes[j] = nodes[j], nodes[i]
}
}
Expand Down
11 changes: 10 additions & 1 deletion scheduler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,17 @@ func TestShuffleNodes(t *testing.T) {
}
orig := make([]*structs.Node, len(nodes))
copy(orig, nodes)
shuffleNodes(nodes)
eval := mock.Eval() // will have random EvalID
plan := eval.MakePlan(mock.Job())
shuffleNodes(plan, 1000, nodes)
require.False(t, reflect.DeepEqual(nodes, orig))

nodes2 := make([]*structs.Node, len(nodes))
copy(nodes2, orig)
shuffleNodes(plan, 1000, nodes2)

require.True(t, reflect.DeepEqual(nodes, nodes2))

}

func TestTaskUpdatedAffinity(t *testing.T) {
Expand Down