-
Notifications
You must be signed in to change notification settings - Fork 5
feat(speculation): standard composed speculator #451
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
behinddwalls
wants to merge
1
commit into
preetam/speculation-allocator
from
preetam/speculation-standard
Open
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions
31
submitqueue/extension/speculation/speculator/standard/BUILD.bazel
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,31 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["standard.go"], | ||
| importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/speculator/standard", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//submitqueue/entity:go_default_library", | ||
| "//submitqueue/extension/speculation/allocator:go_default_library", | ||
| "//submitqueue/extension/speculation/generator:go_default_library", | ||
| "//submitqueue/extension/speculation/speculator:go_default_library", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "go_default_test", | ||
| srcs = ["standard_test.go"], | ||
| embed = [":go_default_library"], | ||
| deps = [ | ||
| "//submitqueue/entity:go_default_library", | ||
| "//submitqueue/extension/speculation/allocator/mock:go_default_library", | ||
| "//submitqueue/extension/speculation/allocator/sticky:go_default_library", | ||
| "//submitqueue/extension/speculation/generator/bestfirst:go_default_library", | ||
| "//submitqueue/extension/speculation/generator/mock:go_default_library", | ||
| "@com_github_stretchr_testify//assert:go_default_library", | ||
| "@com_github_stretchr_testify//require:go_default_library", | ||
| "@org_uber_go_mock//gomock:go_default_library", | ||
| "@org_uber_go_zap//zaptest:go_default_library", | ||
| ], | ||
| ) |
52 changes: 52 additions & 0 deletions
52
submitqueue/extension/speculation/speculator/standard/standard.go
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,52 @@ | ||
| // Copyright (c) 2025 Uber Technologies, Inc. | ||
| // | ||
| // 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 standard provides the standard speculator.Speculator: it composes a | ||
| // Generator and an Allocator so a queue's candidate scoring and its budget | ||
| // rationing policy can vary independently without changing the Speculator. The | ||
| // Speculator itself decides nothing — it opens the Generator's candidate stream | ||
| // and hands it to the Allocator; all of its behavior comes from those two parts. | ||
| package standard | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/uber/submitqueue/submitqueue/entity" | ||
| "github.com/uber/submitqueue/submitqueue/extension/speculation/allocator" | ||
| "github.com/uber/submitqueue/submitqueue/extension/speculation/generator" | ||
| "github.com/uber/submitqueue/submitqueue/extension/speculation/speculator" | ||
| ) | ||
|
|
||
| // spec is a speculator.Speculator that opens the generator's candidate stream | ||
| // and hands it to the allocator to spend the build budget. | ||
| type spec struct { | ||
| gen generator.Generator | ||
| alloc allocator.Allocator | ||
| } | ||
|
|
||
| // New returns the standard speculator.Speculator, composing a Generator and an | ||
| // Allocator. | ||
| func New(gen generator.Generator, alloc allocator.Allocator) speculator.Speculator { | ||
| return spec{gen: gen, alloc: alloc} | ||
| } | ||
|
|
||
| // Speculate opens the generator over the queue's batches and path sets, then | ||
| // lets the allocator spend the budget over the resulting candidate iterator. | ||
| func (s spec) Speculate(ctx context.Context, batches []entity.Batch, pathSets []entity.SpeculationPathSet) ([]entity.Speculation, error) { | ||
| iter, err := s.gen.Open(ctx, batches, pathSets) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return s.alloc.Allocate(ctx, pathSets, iter) | ||
| } |
108 changes: 108 additions & 0 deletions
108
submitqueue/extension/speculation/speculator/standard/standard_test.go
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,108 @@ | ||
| // Copyright (c) 2025 Uber Technologies, Inc. | ||
| // | ||
| // 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 standard | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/mock/gomock" | ||
| "go.uber.org/zap/zaptest" | ||
|
|
||
| "github.com/uber/submitqueue/submitqueue/entity" | ||
| allocatormock "github.com/uber/submitqueue/submitqueue/extension/speculation/allocator/mock" | ||
| "github.com/uber/submitqueue/submitqueue/extension/speculation/allocator/sticky" | ||
| "github.com/uber/submitqueue/submitqueue/extension/speculation/generator/bestfirst" | ||
| generatormock "github.com/uber/submitqueue/submitqueue/extension/speculation/generator/mock" | ||
| ) | ||
|
|
||
| // betFor returns the bet a path carries for a given dependency batch. | ||
| func betFor(p entity.SpeculationPath, dep string) entity.DependencyBetType { | ||
| for _, b := range p.Bets { | ||
| if b.Batch == dep { | ||
| return b.Bet | ||
| } | ||
| } | ||
| return entity.BetUnknown | ||
| } | ||
|
|
||
| // constScorer is a minimal scorer.Scorer that scores every batch identically. | ||
| type constScorer struct{ v float64 } | ||
|
|
||
| func (c constScorer) Score(context.Context, entity.Batch) (float64, error) { return c.v, nil } | ||
|
|
||
| func TestComposed_EndToEnd_NaivePair(t *testing.T) { | ||
| batches := []entity.Batch{ | ||
| {ID: "q/1", State: entity.BatchStateSpeculating}, | ||
| {ID: "q/2", State: entity.BatchStateSpeculating, Dependencies: []string{"q/1"}}, | ||
| } | ||
|
|
||
| // bestfirst generator + sticky allocator with a 2-build budget. | ||
| spec := New(bestfirst.New(2, constScorer{0.9}), sticky.New(zaptest.NewLogger(t).Sugar(), 2)) | ||
| got, err := spec.Speculate(context.Background(), batches, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| // Two free budget slots -> two builds. Everything proposed is a build. | ||
| require.Len(t, got, 2) | ||
| var q2 *entity.Speculation | ||
| for i := range got { | ||
| assert.Equal(t, entity.PathActionBuild, got[i].Action) | ||
| if got[i].Path.Head == "q/2" { | ||
| q2 = &got[i] | ||
| } | ||
| } | ||
| // q/2's funded path is the optimistic all-included guess on q/1. | ||
| require.NotNil(t, q2) | ||
| assert.Equal(t, entity.BetIncluded, betFor(q2.Path, "q/1")) | ||
| } | ||
|
|
||
| func TestComposed_WiresGeneratorIntoAllocator(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| batches := []entity.Batch{{ID: "q/1", State: entity.BatchStateSpeculating}} | ||
| pathSets := []entity.SpeculationPathSet{{BatchID: "q/1"}} | ||
| want := []entity.Speculation{{ | ||
| Path: entity.SpeculationPath{Head: "q/1"}, | ||
| Action: entity.PathActionBuild, | ||
| }} | ||
|
|
||
| iter := generatormock.NewMockPathIterator(ctrl) | ||
| gen := generatormock.NewMockGenerator(ctrl) | ||
| alloc := allocatormock.NewMockAllocator(ctrl) | ||
|
|
||
| gen.EXPECT().Open(gomock.Any(), batches, pathSets).Return(iter, nil) | ||
| alloc.EXPECT().Allocate(gomock.Any(), pathSets, iter).Return(want, nil) | ||
|
|
||
| got, err := New(gen, alloc).Speculate(context.Background(), batches, pathSets) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, want, got) | ||
| } | ||
|
|
||
| func TestComposed_PropagatesGeneratorError(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| errOpen := errors.New("open boom") | ||
| gen := generatormock.NewMockGenerator(ctrl) | ||
| alloc := allocatormock.NewMockAllocator(ctrl) | ||
|
|
||
| gen.EXPECT().Open(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errOpen) | ||
| // Allocate must not be called when Open fails (no alloc.EXPECT()). | ||
|
|
||
| _, err := New(gen, alloc).Speculate(context.Background(), nil, nil) | ||
| require.ErrorIs(t, err, errOpen) | ||
| } |
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.
move to "standard"'s README
The description should say not how it is composed, but what it does (what behavior it implements wrt prioritizing paths and working with build budget).