|
| 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 buildanalysis accumulates issue observations during and after builds. |
| 16 | +// Build status is read from storage; status polling remains separate. |
| 17 | +package buildanalysis |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/uber-go/tally" |
| 25 | + entityqueue "github.com/uber/submitqueue/platform/base/messagequeue" |
| 26 | + "github.com/uber/submitqueue/platform/consumer" |
| 27 | + "github.com/uber/submitqueue/platform/metrics" |
| 28 | + "github.com/uber/submitqueue/stovepipe/core/loader" |
| 29 | + stovepipemq "github.com/uber/submitqueue/stovepipe/core/messagequeue" |
| 30 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 31 | + "github.com/uber/submitqueue/stovepipe/extension/buildissue" |
| 32 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 33 | + "go.uber.org/zap" |
| 34 | +) |
| 35 | + |
| 36 | +// PollDelayMs is the delay in milliseconds after a successful nonterminal pass. |
| 37 | +// Two minutes limits provider traffic during long-running builds. |
| 38 | +const PollDelayMs int64 = 120000 |
| 39 | + |
| 40 | +// Controller accumulates observations while builds run and after completion. |
| 41 | +// Terminal analysis must finish persistence before its delivery completes. |
| 42 | +type Controller struct { |
| 43 | + logger *zap.SugaredLogger |
| 44 | + metricsScope tally.Scope |
| 45 | + stores storage.Factory |
| 46 | + detectorFactory buildissue.DetectorFactory |
| 47 | + topicKey consumer.TopicKey |
| 48 | + consumerGroup string |
| 49 | +} |
| 50 | + |
| 51 | +func NewController( |
| 52 | + logger *zap.SugaredLogger, |
| 53 | + scope tally.Scope, |
| 54 | + stores storage.Factory, |
| 55 | + detectorFactory buildissue.DetectorFactory, |
| 56 | + topicKey consumer.TopicKey, |
| 57 | + consumerGroup string, |
| 58 | +) *Controller { |
| 59 | + return &Controller{ |
| 60 | + logger: logger.Named("buildanalysis_controller"), |
| 61 | + metricsScope: scope.SubScope("buildanalysis_controller"), |
| 62 | + stores: stores, |
| 63 | + detectorFactory: detectorFactory, |
| 64 | + topicKey: topicKey, |
| 65 | + consumerGroup: consumerGroup, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Process holds after a successful nonterminal pass. Errors are returned for |
| 70 | +// normal queue retry or DLQ handling; partial writes are safe to retry. |
| 71 | +func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) (retErr error) { |
| 72 | + op := metrics.Begin(c.metricsScope, "process", metrics.LongLatencyBuckets, metrics.TagsFromContext(ctx)...) |
| 73 | + defer func() { op.Complete(retErr) }() |
| 74 | + |
| 75 | + msg := delivery.Message() |
| 76 | + analysis := &stovepipemq.BuildAnalysis{} |
| 77 | + if err := stovepipemq.Unmarshal(msg.Payload, analysis); err != nil { |
| 78 | + return fmt.Errorf("failed to deserialize build analysis: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + if err := entityqueue.ValidatePayloadQueue(msg, analysis.GetQueueName()); err != nil { |
| 82 | + return fmt.Errorf("invalid message identity: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + if analysis.GetId() == "" { |
| 86 | + return fmt.Errorf("build analysis requires a build ID") |
| 87 | + } |
| 88 | + |
| 89 | + store, err := c.stores.For(storage.Config{QueueName: analysis.GetQueueName()}) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("failed to resolve storage for queue %q: %w", analysis.GetQueueName(), err) |
| 92 | + } |
| 93 | + |
| 94 | + build, err := loader.ByID(ctx, analysis.GetId(), store.GetBuildStore().Get, "build") |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + request, err := loader.ByID(ctx, build.RequestID, store.GetRequestStore().Get, "request") |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + if request.Queue != analysis.GetQueueName() { |
| 105 | + return fmt.Errorf("request queue %q does not match analysis queue %q", request.Queue, analysis.GetQueueName()) |
| 106 | + } |
| 107 | + |
| 108 | + // The Build may finish between the initial Build read and the Request read. |
| 109 | + // Recheck its status so stopping here does not skip final analysis. |
| 110 | + if !build.Status.IsTerminal() && request.State.IsTerminal() { |
| 111 | + build, err = loader.ByID(ctx, analysis.GetId(), store.GetBuildStore().Get, "build") |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + if !build.Status.IsTerminal() { |
| 117 | + c.logger.Warnw("stopping analysis for a terminal request with nonterminal build", |
| 118 | + "queue", request.Queue, "build_id", build.ID, "request_id", request.ID, "request_state", string(request.State)) |
| 119 | + return nil |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + detector, err := c.detectorFactory.For(buildissue.DetectorConfig{QueueName: analysis.GetQueueName()}) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("failed to resolve issue detector for queue %q: %w", analysis.GetQueueName(), err) |
| 126 | + } |
| 127 | + |
| 128 | + issues, err := detector.Detect(ctx, entity.BuildID{ID: build.ID}) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("failed to detect issues for build %q: %w", build.ID, err) |
| 131 | + } |
| 132 | + |
| 133 | + issueStore := store.GetBuildIssueStore() |
| 134 | + for _, issue := range issues { |
| 135 | + if err := issueStore.Create(ctx, build.ID, issue); err != nil && !errors.Is(err, storage.ErrAlreadyExists) { |
| 136 | + return fmt.Errorf("failed to persist build issue build=%q task=%q unit=%q: %w", build.ID, issue.TaskID, issue.UnitID, err) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // A nonterminal status read before detection requires another pass, |
| 141 | + // even if the build finishes while detection is running. |
| 142 | + if !build.Status.IsTerminal() { |
| 143 | + delivery.Hold(PollDelayMs) |
| 144 | + } |
| 145 | + |
| 146 | + c.logger.Debugw("build analysis pass complete", |
| 147 | + "queue", analysis.GetQueueName(), |
| 148 | + "build_id", build.ID, |
| 149 | + "status", string(build.Status), |
| 150 | + "observed_issue_count", len(issues), |
| 151 | + ) |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func (c *Controller) Name() string { return "buildanalysis" } |
| 157 | + |
| 158 | +func (c *Controller) TopicKey() consumer.TopicKey { return c.topicKey } |
| 159 | + |
| 160 | +func (c *Controller) ConsumerGroup() string { return c.consumerGroup } |
| 161 | + |
| 162 | +var _ consumer.Controller = (*Controller)(nil) |
0 commit comments