|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package speculate |
| 16 | + |
| 17 | +import "github.com/uber/submitqueue/submitqueue/entity" |
| 18 | + |
| 19 | +// bypassablePath returns a passed path once passed builds cover every possible |
| 20 | +// outcome of the head's unsettled dependencies. Settled dependencies stay |
| 21 | +// pinned to reality through assumptionBroken. |
| 22 | +// |
| 23 | +// A path's combination is read positionally — the i-th assumption belongs to |
| 24 | +// the head's i-th dependency — which isWellFormed's order check is what |
| 25 | +// licenses: two paths with the same assumptions in different orders are |
| 26 | +// different stacks, not the same outcome. |
| 27 | +func bypassablePath(head entity.Batch, set entity.SpeculationPathSet, snap snapshot) (entity.SpeculationPathEntry, bool) { |
| 28 | + unsettled := unsettledDependencyIndices(head, snap) |
| 29 | + if len(unsettled) == 0 { |
| 30 | + return entity.SpeculationPathEntry{}, false |
| 31 | + } |
| 32 | + |
| 33 | + required := 1 |
| 34 | + for range unsettled { |
| 35 | + if required > len(set.Paths)/2 { |
| 36 | + return entity.SpeculationPathEntry{}, false |
| 37 | + } |
| 38 | + required *= 2 |
| 39 | + } |
| 40 | + |
| 41 | + seen := make(map[string]struct{}, required) |
| 42 | + var winner entity.SpeculationPathEntry |
| 43 | + for _, entry := range set.Paths { |
| 44 | + if entry.Status != entity.SpeculationPathStatusPassed || |
| 45 | + assumptionBroken(entry.Path, snap) || |
| 46 | + !isWellFormed(entry.Path, head) { |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + signature := make([]byte, len(unsettled)) |
| 51 | + for i, depIndex := range unsettled { |
| 52 | + if entry.Path.Dependencies[depIndex].Assumption == entity.DependencyAssumptionFails { |
| 53 | + signature[i] = 'f' |
| 54 | + } else { |
| 55 | + signature[i] = 's' |
| 56 | + } |
| 57 | + } |
| 58 | + key := string(signature) |
| 59 | + if _, exists := seen[key]; exists { |
| 60 | + continue |
| 61 | + } |
| 62 | + seen[key] = struct{}{} |
| 63 | + if len(seen) == 1 { |
| 64 | + winner = entry |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return winner, len(seen) == required |
| 69 | +} |
| 70 | + |
| 71 | +func unsettledDependencyIndices(head entity.Batch, snap snapshot) []int { |
| 72 | + var indices []int |
| 73 | + for i, depID := range head.Dependencies { |
| 74 | + if !snap.batchState(depID).IsTerminal() { |
| 75 | + indices = append(indices, i) |
| 76 | + } |
| 77 | + } |
| 78 | + return indices |
| 79 | +} |
0 commit comments