Repository navigation
ateapi/scheduling: index cached workers by node for O(1) RequiredNodes lookup - #3002
Conversation
43fef66 to
eba3b54
Compare
|
Hi M.Besher Massri (@besher-massri), could you please rebase the PR ? |
eba3b54 to
2a98744
Compare
rebased |
2a98744 to
2848301
Compare
| var workers []*ateapi.Worker | ||
| for i, node := range constraints.RequiredNodes { | ||
| if slices.Contains(constraints.RequiredNodes[:i], node) { | ||
| continue | ||
| } | ||
| nodeWorkers, err := nodeLookup.WorkersByNode(node) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| workers = append(workers, nodeWorkers...) | ||
| } | ||
| return workers, nil |
There was a problem hiding this comment.
schedulingConstraints is the only thing that sets RequiredNodes, and it's always one node. Have you considered dropping the multi-node loop and dedup, and just falling back to Workers() when there's more than one?
There was a problem hiding this comment.
Good point, changed as requested.
|
|
||
| mu sync.RWMutex | ||
| workers map[string]*ateapi.Worker | ||
| byNode map[string][]*ateapi.Worker |
There was a problem hiding this comment.
Is there a profile where this scan shows up in ResumeActor latency? ~16µs seems small next to the store write and atelet call, so I want to be sure a second index that every mutation path has to keep in sync is worth it.
There was a problem hiding this comment.
I rebased after #4000 , and used its benchmark at 10,000
On the new benchmarks from #4000 at 10,000 workers (-count=5 medians), Workers() takes 261 µs / 81.9 KB and BenchmarkSchedule/pinned takes 378 µs (~638 µs / 82 KB combined per pinned resume), whereas WorkersByNode + BenchmarkSchedule/pinned stays flat at ~6.8 µs / 352 B (~93x faster, ~234x less allocation).
| // workerNodeLookup is an optional fast path implemented by caches that index | ||
| // workers by node name. | ||
| type workerNodeLookup interface { | ||
| WorkersByNode(node string) ([]*ateapi.Worker, error) | ||
| } |
There was a problem hiding this comment.
Since workercache is the only WorkerSource, have you considered putting WorkersByNode on the interface instead of type-asserting? That way a wrapper around the cache can't silently lose the fast path.
There was a problem hiding this comment.
Moved WorkersByNode directly onto WorkerSource and removed workerNodeLookup and the type assertion.
|
Synced with Julian Gutierrez Oschmann (@juli4n) offline—deprioritizing this for now given upcoming scheduler changes that would likely supersede this optimization. Updating labels. |
cc: M.Besher Massri (@besher-massri) Sunny (@sunnylovestiramisu) |
| for _, w := range workers { | ||
| newMap[workerKey(w)] = w | ||
| } | ||
| newByNode := make(map[string][]*ateapi.Worker) |
There was a problem hiding this comment.
🤖 nit 🟢 – The relist path replaces the index wholesale, but no test covers it. A case where a relist drops a worker, or adds one the watch missed, and WorkersByNode reflects it would guard the third mutation path alongside the watch and Forget cases in TestCache_WorkersByNode.
There was a problem hiding this comment.
Added TestCache_WorkersByNode_Relist.
| if len(constraints.RequiredNodes) > 0 && !slices.Contains(constraints.RequiredNodes, "") { | ||
| if nodeLookup, ok := s.source.(workerNodeLookup); ok { | ||
| if len(constraints.RequiredNodes) == 1 { | ||
| return nodeLookup.WorkersByNode(constraints.RequiredNodes[0]) | ||
| } | ||
| var workers []*ateapi.Worker | ||
| for i, node := range constraints.RequiredNodes { | ||
| if slices.Contains(constraints.RequiredNodes[:i], node) { | ||
| continue | ||
| } | ||
| nodeWorkers, err := nodeLookup.WorkersByNode(node) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| workers = append(workers, nodeWorkers...) | ||
| } | ||
| return workers, nil | ||
| } | ||
| } | ||
| return s.source.Workers() |
There was a problem hiding this comment.
🤖 nit 🟢 – Early returns would flatten the nesting and make the fallback cases easier to read. Behavior is unchanged.
| if len(constraints.RequiredNodes) > 0 && !slices.Contains(constraints.RequiredNodes, "") { | |
| if nodeLookup, ok := s.source.(workerNodeLookup); ok { | |
| if len(constraints.RequiredNodes) == 1 { | |
| return nodeLookup.WorkersByNode(constraints.RequiredNodes[0]) | |
| } | |
| var workers []*ateapi.Worker | |
| for i, node := range constraints.RequiredNodes { | |
| if slices.Contains(constraints.RequiredNodes[:i], node) { | |
| continue | |
| } | |
| nodeWorkers, err := nodeLookup.WorkersByNode(node) | |
| if err != nil { | |
| return nil, err | |
| } | |
| workers = append(workers, nodeWorkers...) | |
| } | |
| return workers, nil | |
| } | |
| } | |
| return s.source.Workers() | |
| if len(constraints.RequiredNodes) == 0 || slices.Contains(constraints.RequiredNodes, "") { | |
| return s.source.Workers() | |
| } | |
| nodeLookup, ok := s.source.(workerNodeLookup) | |
| if !ok { | |
| return s.source.Workers() | |
| } | |
| if len(constraints.RequiredNodes) == 1 { | |
| return nodeLookup.WorkersByNode(constraints.RequiredNodes[0]) | |
| } | |
| var workers []*ateapi.Worker | |
| for i, node := range constraints.RequiredNodes { | |
| if slices.Contains(constraints.RequiredNodes[:i], node) { | |
| continue | |
| } | |
| nodeWorkers, err := nodeLookup.WorkersByNode(node) | |
| if err != nil { | |
| return nil, err | |
| } | |
| workers = append(workers, nodeWorkers...) | |
| } | |
| return workers, nil |
There was a problem hiding this comment.
Changed.
…s lookup Every suspended actor with a retained local snapshot sets AssignedNode and passes a single RequiredNodes entry into scheduling.Schedule on ResumeActor. Without a node index, Schedule calls s.source.Workers(), copying the full worker slice and scanning every worker in O(W) time on each node-pinned resume. Index cached workers by node name in workercache.Cache via WorkersByNode(node), add WorkersByNode to scheduling.WorkerSource, and query it from scheduling.Schedule when RequiredNodes names a single non-empty node. Multi-node, empty-node, and unconstrained requests fall back to Workers. Signed-off-by: Besher Massri <[email protected]>
87975c4 to
7de180b
Compare
Fixes #3781
An actor suspended with a local snapshot keeps AssignedNode (#2883), and ResumeActor passes it to scheduling.Schedule as RequiredNodes (#2881). Schedule calls Workers(), which copies every cached worker into a new slice and then scans all of them to find the few on that node.
workercache.Cache keeps a byNode index, updated on relist, watch events, and Forget. WorkersByNode(node) is on scheduling.WorkerSource and returns the []*ateapi.Worker on one node. Schedule uses it when RequiredNodes names a single non-empty node, and falls back to Workers() otherwise.
4 workers per node, median of -count=5:
At 10,000 workers, BenchmarkSchedule/pinned drops from 378 µs to 6.7 µs (~56x faster) and the cache lookup drops from 261 µs / 81.9 KB (Workers) to 115 ns / 32 B (WorkersByNode), or ~638 µs / 82 KB to ~6.8 µs / 352 B combined (~93x faster, ~234x less allocation).
go test -run='^$' -bench='^BenchmarkWorkers' -benchmem -count=5 ./cmd/ateapi/internal/workercache/go test -run='^$' -bench='^BenchmarkSchedule/pinned' -benchmem -count=5 ./cmd/ateapi/internal/scheduling/AI use: AI-assisted, I reviewed and tested the changes.