|
| 1 | +// Copyright (c) 2026 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 tango |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/uber/submitqueue/submitqueue/core/changeset" |
| 24 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 25 | + "github.com/uber/submitqueue/submitqueue/extension/conflict" |
| 26 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 27 | +) |
| 28 | + |
| 29 | +// ErrNoSignature means an in-flight batch has no recorded signature. Tango is |
| 30 | +// never asked for it: a signature computed later could differ from the one the |
| 31 | +// batch was admitted with. |
| 32 | +var ErrNoSignature = errors.New("no target signature recorded for in-flight batch") |
| 33 | + |
| 34 | +// Params are the dependencies of one queue's Analyzer. |
| 35 | +type Params struct { |
| 36 | + // Signatures persists signatures. It must be bound to the queue the |
| 37 | + // analyzer serves. |
| 38 | + Signatures SignatureStore |
| 39 | + |
| 40 | + // Cache holds recently used signatures and may be shared across queues. |
| 41 | + Cache *SignatureCache |
| 42 | + |
| 43 | + // Client returns the targets a set of changes affects. |
| 44 | + Client ChangedTargetsClient |
| 45 | + |
| 46 | + // BaseRevision returns the full commit SHA of the target branch tip that a |
| 47 | + // new batch of the named queue is analyzed against. |
| 48 | + BaseRevision func(ctx context.Context, queue string) (string, error) |
| 49 | + |
| 50 | + // Changes resolves a batch into the changes it contains. |
| 51 | + Changes changeset.Resolver |
| 52 | + |
| 53 | + // Now returns the current time. It defaults to time.Now. |
| 54 | + Now func() time.Time |
| 55 | +} |
| 56 | + |
| 57 | +type analyzer struct { |
| 58 | + Params |
| 59 | +} |
| 60 | + |
| 61 | +// New returns a conflict.Analyzer that reports an in-flight batch as a |
| 62 | +// ConflictTypeTargetOverlap conflict when its signature shares a target ID with |
| 63 | +// the analyzed batch's. It fails closed: any signature it cannot obtain fails |
| 64 | +// the analysis. |
| 65 | +func New(params Params) conflict.Analyzer { |
| 66 | + if params.Now == nil { |
| 67 | + params.Now = time.Now |
| 68 | + } |
| 69 | + return &analyzer{Params: params} |
| 70 | +} |
| 71 | + |
| 72 | +// Analyze signs batch, which is the only step that can call Tango, and compares |
| 73 | +// it with the recorded signature of each in-flight batch. The new batch is |
| 74 | +// signed even when nothing is in flight, so later batches find its record. |
| 75 | +func (a *analyzer) Analyze(ctx context.Context, batch entity.Batch, inFlight []entity.Batch) ([]entity.Conflict, error) { |
| 76 | + mine, err := a.signNewBatch(ctx, batch) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to sign batch %s: %w", batch.ID, err) |
| 79 | + } |
| 80 | + |
| 81 | + // TODO(tango): read the in-flight signatures concurrently with a bound. |
| 82 | + var conflicts []entity.Conflict |
| 83 | + for _, other := range inFlight { |
| 84 | + theirs, found, err := a.recordedSignature(ctx, other.Queue, other.ID) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("failed to read the signature of in-flight batch %s: %w", other.ID, err) |
| 87 | + } |
| 88 | + if !found { |
| 89 | + return nil, fmt.Errorf("in-flight batch %s: %w", other.ID, ErrNoSignature) |
| 90 | + } |
| 91 | + if sharesTarget(mine.Targets, theirs.Targets) { |
| 92 | + conflicts = append(conflicts, entity.Conflict{BatchID: other.ID, Type: entity.ConflictTypeTargetOverlap}) |
| 93 | + } |
| 94 | + } |
| 95 | + return conflicts, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (a *analyzer) recordedSignature(ctx context.Context, queue, batchID string) (TargetSignature, bool, error) { |
| 99 | + if signature, found := a.Cache.get(queue, batchID); found { |
| 100 | + return signature, true, nil |
| 101 | + } |
| 102 | + signature, found, err := a.Signatures.Get(ctx, batchID) |
| 103 | + if err != nil || !found { |
| 104 | + return TargetSignature{}, false, err |
| 105 | + } |
| 106 | + a.Cache.put(signature) |
| 107 | + return signature, true, nil |
| 108 | +} |
| 109 | + |
| 110 | +// signNewBatch returns the batch's own record if an earlier attempt created |
| 111 | +// one; otherwise it creates the signature first, so racing attempts agree. |
| 112 | +func (a *analyzer) signNewBatch(ctx context.Context, batch entity.Batch) (TargetSignature, error) { |
| 113 | + signature, found, err := a.recordedSignature(ctx, batch.Queue, batch.ID) |
| 114 | + if err != nil || found { |
| 115 | + return signature, err |
| 116 | + } |
| 117 | + |
| 118 | + changes, err := a.Changes.ChangesForBatch(ctx, batch) |
| 119 | + if err != nil { |
| 120 | + return TargetSignature{}, fmt.Errorf("failed to resolve the changes: %w", err) |
| 121 | + } |
| 122 | + baseRevision, err := a.BaseRevision(ctx, batch.Queue) |
| 123 | + if err != nil { |
| 124 | + return TargetSignature{}, fmt.Errorf("failed to resolve the base revision of queue %s: %w", batch.Queue, err) |
| 125 | + } |
| 126 | + affected, err := a.Client.ChangedTargets(ctx, changedTargetsRequestFor(baseRevision, changes)) |
| 127 | + if err != nil { |
| 128 | + return TargetSignature{}, fmt.Errorf("failed to get the changed targets: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + // TODO(tango): record the labels in the target dictionary before creating the signature. |
| 132 | + targets, _ := toSignatureTargets(affected) |
| 133 | + signature = TargetSignature{ |
| 134 | + Queue: batch.Queue, |
| 135 | + BatchID: batch.ID, |
| 136 | + BaseRevision: baseRevision, |
| 137 | + Targets: targets, |
| 138 | + CreatedAt: a.Now().UnixMilli(), |
| 139 | + } |
| 140 | + |
| 141 | + if err := a.Signatures.Create(ctx, signature); err != nil { |
| 142 | + if !errors.Is(err, storage.ErrAlreadyExists) { |
| 143 | + return TargetSignature{}, fmt.Errorf("failed to create the signature: %w", err) |
| 144 | + } |
| 145 | + signature, found, err = a.Signatures.Get(ctx, batch.ID) |
| 146 | + if err != nil { |
| 147 | + return TargetSignature{}, fmt.Errorf("failed to read the existing signature: %w", err) |
| 148 | + } |
| 149 | + if !found { |
| 150 | + return TargetSignature{}, fmt.Errorf("signature reported as existing but not found") |
| 151 | + } |
| 152 | + } |
| 153 | + a.Cache.put(signature) |
| 154 | + return signature, nil |
| 155 | +} |
0 commit comments