-
Notifications
You must be signed in to change notification settings - Fork 6
feat(speculation): generator contract and bestfirst impl #446
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-generator-rfc
from
preetam/speculation-generator
+2,071
−62
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
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 |
|---|---|---|
| @@ -1,64 +1,17 @@ | ||
| # Scorer | ||
| # scorer | ||
|
|
||
| Vendor-agnostic interface for computing success probability scores for code changes. | ||
| A `Scorer` returns the probability that a batch's build succeeds — a number between 0.0 and 1.0. It is handed the batch identity and resolves the batch's changes itself through an injected `changeset.Resolver`, so callers pass an `entity.Batch` and nothing more. | ||
|
|
||
| ## Interface | ||
| Callers may score every batch a queue is waiting on, so implementations should be cheap. A speculation run scores each batch at most once, but it does not carry results across runs; anything expensive belongs behind the implementation's own cache. | ||
|
|
||
| ### Scorer | ||
|
|
||
| Computes a success probability for a given change. | ||
|
|
||
| ```go | ||
| type Scorer interface { | ||
| Score(ctx context.Context, change entity.Change) (float64, error) | ||
| } | ||
| ``` | ||
|
|
||
| - **change**: A `entity.Change` identifying the code change to score. | ||
| - **Score**: Returns a probability between 0.0 and 1.0 indicating the likelihood of a successful land. Returns an error if scoring fails. | ||
| Like the other extensions, a `Scorer` is selected **per queue** by the wiring layer through the `Config` (queue name) and `Factory` interface. | ||
|
|
||
| ## Implementations | ||
|
|
||
| ### Heuristic | ||
|
|
||
| Scores a change by extracting a numeric value via a `ValueFunc` and matching it against ordered buckets. Each bucket maps a `[Min, Max]` range to a probability. | ||
|
|
||
| ```go | ||
| s := heuristic.New( | ||
| []heuristic.Bucket{ | ||
| {Min: 0, Max: 5, Score: 0.95}, | ||
| {Min: 6, Max: 20, Score: 0.75}, | ||
| {Min: 21, Max: 100, Score: 0.5}, | ||
| }, | ||
| func(ctx context.Context, change entity.Change) (int, error) { | ||
| // resolve the change into a numeric metric | ||
| return filesChanged, nil | ||
| }, | ||
| ) | ||
|
|
||
| score, err := s.Score(ctx, change) | ||
| ``` | ||
|
|
||
| ### Composite | ||
|
|
||
| Combines multiple named scorers into a single score using a reduce function. The reduce function receives a `map[string]float64` mapping scorer names to their scores, enabling domain-aware aggregation. | ||
|
|
||
| Built-in reduce functions: `Min`, `Max`, `Avg`. | ||
|
|
||
| ```go | ||
| s := composite.New( | ||
| map[string]scorer.Scorer{ | ||
| "files": fileScorer, | ||
| "deps": depScorer, | ||
| }, | ||
| composite.Min, | ||
| ) | ||
| **`heuristic`** scores a batch by extracting one number from its changes and matching that against ordered buckets, each mapping a `[Min, Max]` range to a probability. The extraction is a caller-supplied `ValueFunc` over the resolved `entity.BatchChanges`, so the same bucketing works for files touched, lines changed, or any other metric. | ||
|
|
||
| score, err := s.Score(ctx, change) | ||
| ``` | ||
| **`composite`** runs several named scorers and reduces their scores to one. The reduce function receives the scores keyed by scorer name, so it can weigh sources differently rather than treating them as interchangeable; `Min`, `Max`, and `Avg` are provided. | ||
|
|
||
| ## Implementing a Backend | ||
| ## Adding a backend | ||
|
|
||
| 1. Create `extension/scorer/{backend}/` directory | ||
| 2. Implement the `Scorer` interface | ||
| 3. Accept `entity.Change` and resolve it into whatever data the implementation needs | ||
| Create a package under `scorer/<backend>/` whose `New(...)` returns a `scorer.Scorer`, injecting whatever it needs at construction — a `changeset.Resolver` to reach the batch's changes, a metrics scope, any client. Do not add a `Config` or `Factory` implementation here; per-queue routing and the factory adapter live in the wiring layer. |
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,9 @@ | ||
| load("@rules_go//go:def.bzl", "go_library") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["generator.go"], | ||
| importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/generator", | ||
| visibility = ["//visibility:public"], | ||
| deps = ["//submitqueue/entity:go_default_library"], | ||
| ) |
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,9 @@ | ||
| # generator | ||
|
|
||
| The `generator` package is a piece the `standard` `Speculator` is built from: a `Generator` produces the queue's candidate paths as one ordered stream across all heads. It is **not** controller-facing — the speculate controller only knows the `Speculator` contract, and a different `Speculator` need not split its work this way. So there is no `Config` or `Factory` here; a `Generator` is chosen when the `standard` `Speculator` is constructed. | ||
|
|
||
| `Open` starts the stream over the queue's live batches and returns a `PathIterator`. Beyond any ranking work required up front, the generator computes only the candidates the caller pulls. A cancelled or expired context ends the stream with its error. | ||
|
|
||
| Candidates never repeat and never contradict a known fact. Beyond that, the order is the `Generator`'s own: it yields candidates in whatever ranking it implements, and each carries the score it ranked by — higher first, on a scale the generator defines. Consumers take the iterator in the order given and do not interpret the score. Scores mean something only within the run and are never stored. | ||
|
|
||
| The generator offers every path in the space, including paths whose builds already ran. Suppressing finished paths is the `Allocator`'s job, since that is the piece reconciling candidates against the stored path sets. |
30 changes: 30 additions & 0 deletions
30
submitqueue/extension/speculation/generator/bestfirst/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,30 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = [ | ||
| "bestfirst.go", | ||
| "head.go", | ||
| "iterator.go", | ||
| ], | ||
| importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/generator/bestfirst", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//submitqueue/entity:go_default_library", | ||
| "//submitqueue/extension/scorer:go_default_library", | ||
| "//submitqueue/extension/speculation/generator:go_default_library", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "go_default_test", | ||
| srcs = ["bestfirst_test.go"], | ||
| embed = [":go_default_library"], | ||
| deps = [ | ||
| "//submitqueue/entity:go_default_library", | ||
| "//submitqueue/extension/scorer:go_default_library", | ||
| "//submitqueue/extension/speculation/generator:go_default_library", | ||
| "@com_github_stretchr_testify//assert:go_default_library", | ||
| "@com_github_stretchr_testify//require:go_default_library", | ||
| ], | ||
| ) |
15 changes: 15 additions & 0 deletions
15
submitqueue/extension/speculation/generator/bestfirst/README.md
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,15 @@ | ||
| # bestfirst | ||
|
|
||
| `bestfirst` implements `generator.Generator` by ranking candidate paths by the probability that all their dependency assumptions hold. It returns one path per pull across all speculating heads without enumerating every combination up front. | ||
|
|
||
| The [best-first speculation path generation RFC](../../../../../doc/rfc/submitqueue/speculation-generator-best-first.md) defines the terminology, algorithm, correctness argument, worked example, and alternatives considered. This README records only the package's operational behavior. | ||
|
|
||
| ## Behavior | ||
|
|
||
| - `Open` scores each unique unresolved dependency once, fixes assumptions for resolved dependencies, calculates each head's best score, and seeds one shared heap with every eligible head's best-path candidate. | ||
| - `Next` removes the highest-ranked candidate, adds at most two child candidates, constructs that candidate's complete path, and returns it. Pulling long enough returns every path exactly once in non-increasing score order. | ||
| - Ranking scores are sums of log probabilities, avoiding underflow while preserving probability order. They are meaningful only within the run that produced them. | ||
| - Exact ties prefer fewer flips, then compare taken flip indexes, then head ID. | ||
| - A missing dependency uses a success probability of 0.95 because it cannot be scored from the input snapshot. | ||
|
|
||
| The behavior is covered by `bestfirst_test.go`. |
Oops, something went wrong.
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.
Should not document implementation internals on what data the scorer should manipulate, as long as the interface is not expected to implement this.