Skip to content

ateapi/scheduling: index cached workers by node for O(1) RequiredNodes lookup - #3002

Open
M.Besher Massri (besher-massri) wants to merge 1 commit into
agent-substrate:mainfrom
besher-massri:prod/slim-u5b-scheduling-soft-affinity
Open

M.Besher Massri (besher-massri) wants to merge 1 commit into
agent-substrate:mainfrom
besher-massri:prod/slim-u5b-scheduling-soft-affinity

Conversation

@besher-massri

@besher-massri M.Besher Massri (besher-massri) commented Oct 20, 2026 •

Copy link
Copy Markdown

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:

Benchmark n=100 n=1,000 n=10,000
BenchmarkWorkers 2,775 ns / 896 B 29,092 ns / 8,192 B 260,566 ns / 81,920 B
BenchmarkWorkersByNode 98.8 ns / 32 B 118.6 ns / 32 B 114.6 ns / 32 B
BenchmarkSchedule/pinned (before) 12,022 ns / 320 B 44,570 ns / 320 B 377,854 ns / 320 B
BenchmarkSchedule/pinned (after) 6,738 ns / 320 B 7,326 ns / 320 B 6,721 ns / 320 B

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.

  • Tests pass
  • Docs updated

@besher-massri
M.Besher Massri (besher-massri) force-pushed the prod/slim-u5b-scheduling-soft-affinity branch 4 times, most recently from 43fef66 to eba3b54 Compare October 21, 2026 09:05
@bowei Bowei Du (bowei) added area/api-machinery area/scheduling kind/feature An enhancement / feature request or implementation labels Oct 27, 2026
@maxsmythe Max Smythe (maxsmythe) added this to the v1.1 milestone Nov 11, 2026
@shrutiyam-glitch

Copy link
Copy Markdown
Collaborator

Hi M.Besher Massri (@besher-massri), could you please rebase the PR ?
This closes the issue #3781

cc: Zoe Zhao (@zoez7) Chenyi Wang (@chw120)

@besher-massri
M.Besher Massri (besher-massri) force-pushed the prod/slim-u5b-scheduling-soft-affinity branch from eba3b54 to 2a98744 Compare November 17, 2026 13:43
@besher-massri

Copy link
Copy Markdown
Author

Hi M.Besher Massri (@besher-massri), could you please rebase the PR ?

rebased

Comment on lines +176 to +187
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, changed as requested.


mu sync.RWMutex
workers map[string]*ateapi.Worker
byNode map[string][]*ateapi.Worker

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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).

Comment on lines +94 to +98
// workerNodeLookup is an optional fast path implemented by caches that index
// workers by node name.
type workerNodeLookup interface {
WorkersByNode(node string) ([]*ateapi.Worker, error)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Moved WorkersByNode directly onto WorkerSource and removed workerNodeLookup and the type assertion.

@chw120 Chenyi Wang (chw120) added the kind/performance Anything that is intended to improve performance label Nov 20, 2026
@chw120

Copy link
Copy Markdown
Collaborator

Synced with Julian Gutierrez Oschmann (@juli4n) offline—deprioritizing this for now given upcoming scheduler changes that would likely supersede this optimization. Updating labels.

@chw120 Chenyi Wang (chw120) removed this from the v1.1 milestone Nov 20, 2026
@chw120

Copy link
Copy Markdown
Collaborator

Synced with Julian Gutierrez Oschmann (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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added TestCache_WorkersByNode_Relist.

Comment on lines +171 to +190
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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 nit 🟢 – Early returns would flatten the nesting and make the fallback cases easier to read. Behavior is unchanged.

Suggested change
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api-machinery area/scheduling kind/feature An enhancement / feature request or implementation kind/performance Anything that is intended to improve performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ateapi: index workercache by node to speed up pinned actor resumes

7 participants