|
| 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 issuetrack applies build issues to operational open issues and lifecycle history. |
| 16 | +// All deliveries for one queue must share its partition and consumer group. |
| 17 | +package issuetrack |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "sort" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/uber-go/tally" |
| 27 | + "github.com/uber/submitqueue/platform/base/id" |
| 28 | + "github.com/uber/submitqueue/platform/consumer" |
| 29 | + "github.com/uber/submitqueue/platform/metrics" |
| 30 | + "github.com/uber/submitqueue/stovepipe/core/loader" |
| 31 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 32 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 33 | + "go.uber.org/zap" |
| 34 | +) |
| 35 | + |
| 36 | +// Each batch applies at most this many distinct issue keys. |
| 37 | +const _issueBatchSize = 100 |
| 38 | + |
| 39 | +type issueKey struct{ task, unit string } |
| 40 | + |
| 41 | +// Controller is the queue-serialized writer of open issues and issue transitions. |
| 42 | +// Accepted pending work is replayed before a newer source can change issue state. |
| 43 | +type Controller struct { |
| 44 | + logger *zap.SugaredLogger |
| 45 | + metricsScope tally.Scope |
| 46 | + stores storage.Factory |
| 47 | + topicKey consumer.TopicKey |
| 48 | + consumerGroup string |
| 49 | + nowMs func() int64 |
| 50 | +} |
| 51 | + |
| 52 | +func NewController( |
| 53 | + logger *zap.SugaredLogger, |
| 54 | + scope tally.Scope, |
| 55 | + stores storage.Factory, |
| 56 | + topicKey consumer.TopicKey, |
| 57 | + consumerGroup string, |
| 58 | +) *Controller { |
| 59 | + return &Controller{ |
| 60 | + logger: logger.Named("issuetrack_controller"), |
| 61 | + metricsScope: scope.SubScope("issuetrack_controller"), |
| 62 | + stores: stores, |
| 63 | + topicKey: topicKey, |
| 64 | + consumerGroup: consumerGroup, |
| 65 | + nowMs: func() int64 { return time.Now().UnixMilli() }, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (c *Controller) reconcileBatch(ctx context.Context, store storage.Storage, queue string, progress entity.IssueTrackingState) (checkpoint entity.IssueTrackingState, retErr error) { |
| 70 | + op := metrics.Begin(c.metricsScope, "batch", metrics.LongLatencyBuckets, metrics.TagsFromContext(ctx)...) |
| 71 | + defer func() { op.Complete(retErr) }() |
| 72 | + |
| 73 | + build, request, err := loadBuildAndRequest(ctx, store, queue, progress.BuildID) |
| 74 | + if err != nil { |
| 75 | + return entity.IssueTrackingState{}, err |
| 76 | + } |
| 77 | + if request.ID != progress.RequestID || !progress.IsPending() { |
| 78 | + return entity.IssueTrackingState{}, fmt.Errorf("issue batch requires pending work for its source request") |
| 79 | + } |
| 80 | + finalizing := progress.Phase == entity.IssueTrackingPhaseFinalizing |
| 81 | + if finalizing && (!build.Status.IsTerminal() || request.TerminalBuildID != build.ID) { |
| 82 | + return entity.IssueTrackingState{}, fmt.Errorf("final issue reconciliation requires the request's terminal build") |
| 83 | + } |
| 84 | + |
| 85 | + buildIssues, err := store.GetBuildIssueStore().List(ctx, build.ID, storage.BuildIssueRange{ |
| 86 | + After: storage.BuildIssueCursor{TaskID: progress.AfterTaskID, UnitID: progress.AfterUnitID}, |
| 87 | + Limit: _issueBatchSize, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return entity.IssueTrackingState{}, fmt.Errorf("list build issues: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + var openIssues []entity.OpenIssue |
| 94 | + // A canceled build may not have run the validation that would find an existing issue. |
| 95 | + // Keep those issues open even when the canceled build does not report them. |
| 96 | + if finalizing && build.Status != entity.BuildStatusCancelled { |
| 97 | + openIssues, err = store.GetOpenIssueStore().List(ctx, storage.OpenIssueRange{ |
| 98 | + After: storage.OpenIssueCursor{TaskID: progress.AfterTaskID, UnitID: progress.AfterUnitID}, |
| 99 | + Limit: _issueBatchSize, |
| 100 | + }) |
| 101 | + if err != nil { |
| 102 | + return entity.IssueTrackingState{}, fmt.Errorf("list open issues: %w", err) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Combine both bounded reads by task/unit identity, then process the first batch in key order. |
| 107 | + buildByKey := make(map[issueKey]entity.BuildIssue, len(buildIssues)) |
| 108 | + openByKey := make(map[issueKey]entity.OpenIssue, len(openIssues)) |
| 109 | + keys := make([]issueKey, 0, len(buildIssues)+len(openIssues)) |
| 110 | + for _, issue := range buildIssues { |
| 111 | + key := issueKey{issue.TaskID, issue.UnitID} |
| 112 | + buildByKey[key] = issue |
| 113 | + keys = append(keys, key) |
| 114 | + } |
| 115 | + for _, issue := range openIssues { |
| 116 | + key := issueKey{issue.TaskID, issue.UnitID} |
| 117 | + openByKey[key] = issue |
| 118 | + if _, reported := buildByKey[key]; !reported { |
| 119 | + keys = append(keys, key) |
| 120 | + } |
| 121 | + } |
| 122 | + sort.Slice(keys, func(i, j int) bool { |
| 123 | + if keys[i].task != keys[j].task { |
| 124 | + return keys[i].task < keys[j].task |
| 125 | + } |
| 126 | + return keys[i].unit < keys[j].unit |
| 127 | + }) |
| 128 | + |
| 129 | + // A full read may hide more keys. An oversized combined set also needs another batch. |
| 130 | + complete := len(buildIssues) < _issueBatchSize && len(openIssues) < _issueBatchSize && len(keys) <= _issueBatchSize |
| 131 | + if len(keys) > _issueBatchSize { |
| 132 | + keys = keys[:_issueBatchSize] |
| 133 | + } |
| 134 | + |
| 135 | + next := progress |
| 136 | + for _, key := range keys { |
| 137 | + if err := ctx.Err(); err != nil { |
| 138 | + return entity.IssueTrackingState{}, err |
| 139 | + } |
| 140 | + |
| 141 | + if issue, reported := buildByKey[key]; reported { |
| 142 | + if err := c.openBuildIssue(ctx, store, build.ID, issue); err != nil { |
| 143 | + return entity.IssueTrackingState{}, err |
| 144 | + } |
| 145 | + } else { |
| 146 | + if err := c.closeIssueIfAbsent(ctx, store, build.ID, openByKey[key]); err != nil { |
| 147 | + return entity.IssueTrackingState{}, err |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + next.AfterTaskID, next.AfterUnitID = key.task, key.unit |
| 152 | + } |
| 153 | + |
| 154 | + if complete { |
| 155 | + next.Phase = entity.IssueTrackingPhaseRefreshed |
| 156 | + if finalizing { |
| 157 | + next.Phase = entity.IssueTrackingPhaseFinalized |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + newVersion := progress.Version + 1 |
| 162 | + if err := store.GetIssueTrackingStore().Update(ctx, next, progress.Version, newVersion); err != nil { |
| 163 | + return entity.IssueTrackingState{}, fmt.Errorf("checkpoint issue reconciliation: %w", err) |
| 164 | + } |
| 165 | + next.Version = newVersion |
| 166 | + return next, nil |
| 167 | +} |
| 168 | + |
| 169 | +func (c *Controller) openBuildIssue(ctx context.Context, store storage.Storage, buildID string, issue entity.BuildIssue) error { |
| 170 | + openStore := store.GetOpenIssueStore() |
| 171 | + openIssue, err := openStore.Get(ctx, issue.TaskID, issue.UnitID) |
| 172 | + if err != nil && !errors.Is(err, storage.ErrNotFound) { |
| 173 | + return fmt.Errorf("read open issue: %w", err) |
| 174 | + } |
| 175 | + |
| 176 | + if errors.Is(err, storage.ErrNotFound) { |
| 177 | + openIssue = entity.OpenIssue{ |
| 178 | + TaskID: issue.TaskID, |
| 179 | + UnitID: issue.UnitID, |
| 180 | + FirstSeenBuildID: buildID, |
| 181 | + } |
| 182 | + err = openStore.Create(ctx, openIssue) |
| 183 | + if errors.Is(err, storage.ErrAlreadyExists) { |
| 184 | + openIssue, err = openStore.Get(ctx, issue.TaskID, issue.UnitID) |
| 185 | + } |
| 186 | + if err != nil { |
| 187 | + return fmt.Errorf("open build issue: %w", err) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + if openIssue.FirstSeenBuildID == buildID { |
| 192 | + return c.recordIssueEvent(ctx, store, buildID, entity.IssueEventOpened, issue.TaskID, issue.UnitID) |
| 193 | + } |
| 194 | + return nil |
| 195 | +} |
| 196 | + |
| 197 | +func (c *Controller) closeIssueIfAbsent(ctx context.Context, store storage.Storage, buildID string, issue entity.OpenIssue) error { |
| 198 | + _, err := store.GetBuildIssueStore().Get(ctx, buildID, issue.TaskID, issue.UnitID) |
| 199 | + if err == nil { |
| 200 | + return nil |
| 201 | + } |
| 202 | + if !errors.Is(err, storage.ErrNotFound) { |
| 203 | + return fmt.Errorf("check build issue: %w", err) |
| 204 | + } |
| 205 | + |
| 206 | + // Record closure before deletion so a failed history write leaves the issue retryable. |
| 207 | + if err := c.recordIssueEvent(ctx, store, buildID, entity.IssueEventResolved, issue.TaskID, issue.UnitID); err != nil { |
| 208 | + return err |
| 209 | + } |
| 210 | + if err := store.GetOpenIssueStore().Delete(ctx, issue); err != nil { |
| 211 | + return fmt.Errorf("close open issue: %w", err) |
| 212 | + } |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func (c *Controller) recordIssueEvent(ctx context.Context, store storage.Storage, buildID string, event entity.IssueEvent, taskID, unitID string) error { |
| 217 | + log := entity.IssueLog{ |
| 218 | + TaskID: taskID, |
| 219 | + UnitID: unitID, |
| 220 | + BuildID: buildID, |
| 221 | + Event: event, |
| 222 | + TimestampMs: c.nowMs(), |
| 223 | + } |
| 224 | + if err := store.GetIssueLogStore().Create(ctx, log); err != nil && !errors.Is(err, storage.ErrAlreadyExists) { |
| 225 | + return fmt.Errorf("record issue transition: %w", err) |
| 226 | + } |
| 227 | + return nil |
| 228 | +} |
| 229 | + |
| 230 | +func loadBuildAndRequest(ctx context.Context, store storage.Storage, queue, buildID string) (entity.Build, entity.Request, error) { |
| 231 | + build, err := loader.ByID(ctx, buildID, store.GetBuildStore().Get, "build") |
| 232 | + if err != nil { |
| 233 | + return entity.Build{}, entity.Request{}, err |
| 234 | + } |
| 235 | + |
| 236 | + request, err := loader.ByID(ctx, build.RequestID, store.GetRequestStore().Get, "request") |
| 237 | + if err != nil { |
| 238 | + return entity.Build{}, entity.Request{}, err |
| 239 | + } |
| 240 | + |
| 241 | + if request.Queue != queue { |
| 242 | + return entity.Build{}, entity.Request{}, fmt.Errorf("issue tracking request belongs to another queue") |
| 243 | + } |
| 244 | + if err := id.Validate(request.ID); err != nil { |
| 245 | + return entity.Build{}, entity.Request{}, fmt.Errorf("invalid issue tracking request ID: %w", err) |
| 246 | + } |
| 247 | + |
| 248 | + return build, request, nil |
| 249 | +} |
| 250 | + |
| 251 | +func (c *Controller) Name() string { return "issuetrack" } |
| 252 | +func (c *Controller) TopicKey() consumer.TopicKey { return c.topicKey } |
| 253 | +func (c *Controller) ConsumerGroup() string { return c.consumerGroup } |
0 commit comments