-
Notifications
You must be signed in to change notification settings - Fork 99
feat: WorkerPool Auto scaling #219
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
Open
Omer Yahud (omeryahud)
wants to merge
7
commits into
agent-substrate:main
Choose a base branch
from
omeryahud:feat/workerpool-autoscaling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,700
−70
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03fb8ee
feat(api): add WorkerPool autoscaling bounds to WorkerPoolSpec
omeryahud 5b2626c
feat(autoscaler): add pure WorkerPool autoscaling decision logic
omeryahud ee0e2d1
feat(autoscaler): add WorkerPool autoscaler reconciler
omeryahud 72e96a4
feat(ateapi): emit capacity-pressure signal on resume miss
omeryahud bc5b9f0
feat(autoscaler): trigger reactive scale-up on capacity pressure
omeryahud 10c8fdf
docs(demo): add WorkerPool autoscaling demo
omeryahud 62df3d2
fix demo doc
omeryahud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import "sync" | ||
|
|
||
| // poolKey identifies a worker pool by namespace and name. | ||
| type poolKey struct { | ||
| namespace string | ||
| name string | ||
| } | ||
|
|
||
| // CapacityPressureHub fans out capacity-pressure notifications — a pool had no | ||
| // free worker for a resume — to any number of subscribers (the WatchCapacity- | ||
| // Pressure RPC handlers). Publish is called on the resume hot path and must | ||
| // never block: a subscriber whose buffer is full simply misses the event, and | ||
| // the autoscaler's periodic reconcile is the backstop. | ||
| type CapacityPressureHub struct { | ||
| mu sync.Mutex | ||
| nextID int | ||
| subs map[int]chan poolKey | ||
| } | ||
|
|
||
| // NewCapacityPressureHub returns an empty hub. | ||
| func NewCapacityPressureHub() *CapacityPressureHub { | ||
| return &CapacityPressureHub{subs: make(map[int]chan poolKey)} | ||
| } | ||
|
|
||
| // Subscribe registers a subscriber and returns its event channel plus a cancel | ||
| // func that unregisters and closes the channel. cancel is idempotent. The | ||
| // channel is buffered so short bursts aren't dropped, and lossy beyond that by | ||
| // design. | ||
| func (h *CapacityPressureHub) Subscribe() (<-chan poolKey, func()) { | ||
| ch := make(chan poolKey, 64) | ||
|
|
||
| h.mu.Lock() | ||
| id := h.nextID | ||
| h.nextID++ | ||
| h.subs[id] = ch | ||
| h.mu.Unlock() | ||
|
|
||
| var once sync.Once | ||
| cancel := func() { | ||
| once.Do(func() { | ||
| h.mu.Lock() | ||
| defer h.mu.Unlock() | ||
| delete(h.subs, id) | ||
| close(ch) | ||
| }) | ||
| } | ||
| return ch, cancel | ||
| } | ||
|
|
||
| // Publish notifies every subscriber that the named pool had no free worker. It | ||
| // never blocks: a full subscriber buffer drops the event. | ||
| func (h *CapacityPressureHub) Publish(namespace, name string) { | ||
| key := poolKey{namespace: namespace, name: name} | ||
|
|
||
| h.mu.Lock() | ||
| defer h.mu.Unlock() | ||
| for _, ch := range h.subs { | ||
| select { | ||
| case ch <- key: | ||
| default: | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestCapacityPressureHubFanOut(t *testing.T) { | ||
| h := NewCapacityPressureHub() | ||
| a, cancelA := h.Subscribe() | ||
| defer cancelA() | ||
| b, cancelB := h.Subscribe() | ||
| defer cancelB() | ||
|
|
||
| h.Publish("ns", "pool") | ||
|
|
||
| for i, ch := range []<-chan poolKey{a, b} { | ||
| select { | ||
| case got := <-ch: | ||
| if got.namespace != "ns" || got.name != "pool" { | ||
| t.Fatalf("subscriber %d: got %+v, want {ns pool}", i, got) | ||
| } | ||
| case <-time.After(time.Second): | ||
| t.Fatalf("subscriber %d: no event delivered", i) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCapacityPressureHubUnsubscribe(t *testing.T) { | ||
| h := NewCapacityPressureHub() | ||
| ch, cancel := h.Subscribe() | ||
|
|
||
| cancel() | ||
| // Publishing after unsubscribe must not panic, and the channel is closed. | ||
| h.Publish("ns", "pool") | ||
| if _, ok := <-ch; ok { | ||
| t.Fatal("channel should be closed after cancel") | ||
| } | ||
| // cancel is idempotent. | ||
| cancel() | ||
| } | ||
|
|
||
| func TestCapacityPressureHubPublishNeverBlocks(t *testing.T) { | ||
| h := NewCapacityPressureHub() | ||
| _, cancel := h.Subscribe() // a subscriber that never drains | ||
| defer cancel() | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| for i := 0; i < 10_000; i++ { | ||
| h.Publish("ns", "pool") // must drop, not block, once the buffer fills | ||
| } | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("Publish blocked on a full subscriber buffer") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import ( | ||
| "google.golang.org/grpc" | ||
|
|
||
| "github.com/agent-substrate/substrate/pkg/proto/ateapipb" | ||
| ) | ||
|
|
||
| // WatchCapacityPressure streams a CapacityPressureEvent every time a pool has | ||
| // no free worker for a resume, until the client disconnects. | ||
| func (s *Service) WatchCapacityPressure(_ *ateapipb.WatchCapacityPressureRequest, stream grpc.ServerStreamingServer[ateapipb.CapacityPressureEvent]) error { | ||
| events, cancel := s.pressure.Subscribe() | ||
| defer cancel() | ||
|
|
||
| ctx := stream.Context() | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil | ||
| case key := <-events: | ||
| if err := stream.Send(&ateapipb.CapacityPressureEvent{ | ||
| WorkerNamespace: key.namespace, | ||
| WorkerPool: key.name, | ||
| }); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe should be configurable per worker pool?