|
| 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 |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/uber-go/tally" |
| 23 | + entityqueue "github.com/uber/submitqueue/platform/base/messagequeue" |
| 24 | + "github.com/uber/submitqueue/platform/consumer" |
| 25 | + "github.com/uber/submitqueue/platform/metrics" |
| 26 | + stovepipemq "github.com/uber/submitqueue/stovepipe/core/messagequeue" |
| 27 | + "github.com/uber/submitqueue/stovepipe/core/requestlog" |
| 28 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 29 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 30 | + "go.uber.org/zap" |
| 31 | +) |
| 32 | + |
| 33 | +const _dlqOpName = "buildanalysis_dlq" |
| 34 | + |
| 35 | +// DLQController records incomplete analysis without rerunning detection or |
| 36 | +// changing the validation outcome. |
| 37 | +type DLQController struct { |
| 38 | + logger *zap.SugaredLogger |
| 39 | + metricsScope tally.Scope |
| 40 | + stores storage.Factory |
| 41 | + materializer requestlog.Materializer |
| 42 | + topicKey consumer.TopicKey |
| 43 | + consumerGroup string |
| 44 | +} |
| 45 | + |
| 46 | +// NewDLQController creates a controller for incomplete build analysis. |
| 47 | +func NewDLQController( |
| 48 | + logger *zap.SugaredLogger, |
| 49 | + scope tally.Scope, |
| 50 | + stores storage.Factory, |
| 51 | + materializer requestlog.Materializer, |
| 52 | + topicKey consumer.TopicKey, |
| 53 | + consumerGroup string, |
| 54 | +) *DLQController { |
| 55 | + name := string(topicKey) + "_controller" |
| 56 | + return &DLQController{ |
| 57 | + logger: logger.Named(name), |
| 58 | + metricsScope: scope.SubScope(name), |
| 59 | + stores: stores, |
| 60 | + materializer: materializer, |
| 61 | + topicKey: topicKey, |
| 62 | + consumerGroup: consumerGroup, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Process retains incomplete-analysis history and acknowledges unusable messages. |
| 67 | +// State-read and history-write failures are returned for DLQ recovery retry. |
| 68 | +func (c *DLQController) Process(ctx context.Context, delivery consumer.Delivery) error { |
| 69 | + msg := delivery.Message() |
| 70 | + analysis := &stovepipemq.BuildAnalysis{} |
| 71 | + if err := stovepipemq.Unmarshal(msg.Payload, analysis); err != nil { |
| 72 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "deserialize_errors", 1, metrics.TagsFromContext(ctx)...) |
| 73 | + c.logger.Errorw("discarding malformed analysis dlq message", |
| 74 | + "message_id", msg.ID, |
| 75 | + "error", err, |
| 76 | + ) |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + if err := entityqueue.ValidatePayloadQueue(msg, analysis.GetQueueName()); err != nil { |
| 81 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "queue_identity_errors", 1, metrics.TagsFromContext(ctx)...) |
| 82 | + c.logger.Errorw("discarding analysis dlq message with invalid queue identity", |
| 83 | + "message_id", msg.ID, |
| 84 | + "build_id", analysis.GetId(), |
| 85 | + "error", err, |
| 86 | + ) |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + if analysis.GetId() == "" { |
| 91 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "empty_id_errors", 1, metrics.TagsFromContext(ctx)...) |
| 92 | + c.logger.Errorw("discarding analysis dlq message with empty build id", |
| 93 | + "message_id", msg.ID, |
| 94 | + "queue", analysis.GetQueueName(), |
| 95 | + ) |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + store, err := c.stores.For(storage.Config{QueueName: analysis.GetQueueName()}) |
| 100 | + if err != nil { |
| 101 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "storage_resolve_errors", 1, metrics.TagsFromContext(ctx)...) |
| 102 | + c.logger.Errorw("discarding analysis dlq message for unresolvable queue", |
| 103 | + "message_id", msg.ID, |
| 104 | + "build_id", analysis.GetId(), |
| 105 | + "queue", analysis.GetQueueName(), |
| 106 | + "error", err, |
| 107 | + ) |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + build, err := store.GetBuildStore().Get(ctx, analysis.GetId()) |
| 112 | + if err != nil { |
| 113 | + if errors.Is(err, storage.ErrNotFound) { |
| 114 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "build_not_found", 1, metrics.TagsFromContext(ctx)...) |
| 115 | + c.logger.Errorw("discarding analysis dlq message for missing build", |
| 116 | + "message_id", msg.ID, "build_id", analysis.GetId(), "queue", analysis.GetQueueName()) |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + return fmt.Errorf("failed to load build %s for analysis dlq: %w", analysis.GetId(), err) |
| 121 | + } |
| 122 | + |
| 123 | + if build.RequestID == "" { |
| 124 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "empty_request_id", 1, metrics.TagsFromContext(ctx)...) |
| 125 | + c.logger.Errorw("discarding analysis dlq message for build without a request", |
| 126 | + "message_id", msg.ID, "build_id", analysis.GetId()) |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + request, err := store.GetRequestStore().Get(ctx, build.RequestID) |
| 131 | + if err != nil { |
| 132 | + if errors.Is(err, storage.ErrNotFound) { |
| 133 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "request_not_found", 1, metrics.TagsFromContext(ctx)...) |
| 134 | + c.logger.Errorw("discarding analysis dlq message for missing request", |
| 135 | + "message_id", msg.ID, "build_id", analysis.GetId(), "request_id", build.RequestID) |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + return fmt.Errorf("failed to load request %s for analysis dlq: %w", build.RequestID, err) |
| 140 | + } |
| 141 | + |
| 142 | + if request.Queue != analysis.GetQueueName() { |
| 143 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "queue_identity_errors", 1, metrics.TagsFromContext(ctx)...) |
| 144 | + c.logger.Errorw("discarding analysis dlq message for request in another queue", |
| 145 | + "message_id", msg.ID, "request_id", request.ID, "queue", request.Queue, |
| 146 | + "message_queue", analysis.GetQueueName()) |
| 147 | + return nil |
| 148 | + } |
| 149 | + |
| 150 | + log := requestlog.NewRequestEventLog(request, entity.RequestEventBuildAnalysisAbandoned, analysis.GetId(), |
| 151 | + map[string]string{requestlog.MetadataKeyBuildID: analysis.GetId()}) |
| 152 | + if err := c.materializer.PersistLog(ctx, store, log); err != nil { |
| 153 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "history_errors", 1, metrics.TagsFromContext(ctx)...) |
| 154 | + return fmt.Errorf("failed to retain incomplete build analysis for request %s: %w", request.ID, err) |
| 155 | + } |
| 156 | + |
| 157 | + originalFailure, hasFailure := delivery.Failure() |
| 158 | + metrics.NamedCounter(c.metricsScope, _dlqOpName, "analysis_abandoned", 1, metrics.TagsFromContext(ctx)...) |
| 159 | + fields := []any{ |
| 160 | + "message_id", msg.ID, |
| 161 | + "request_id", request.ID, |
| 162 | + "build_id", analysis.GetId(), |
| 163 | + "queue", request.Queue, |
| 164 | + "request_state", request.State, |
| 165 | + "history_event", entity.RequestEventBuildAnalysisAbandoned, |
| 166 | + "attempt", delivery.Attempt(), |
| 167 | + } |
| 168 | + if hasFailure { |
| 169 | + fields = append(fields, |
| 170 | + "failure", originalFailure.Message, |
| 171 | + "failure_subjects", originalFailure.Subjects, |
| 172 | + "failure_detail", originalFailure.Detail, |
| 173 | + ) |
| 174 | + } |
| 175 | + c.logger.Errorw("build analysis could not complete; issue observations may be incomplete", fields...) |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +func (c *DLQController) Name() string { return string(c.topicKey) } |
| 180 | + |
| 181 | +func (c *DLQController) TopicKey() consumer.TopicKey { return c.topicKey } |
| 182 | + |
| 183 | +func (c *DLQController) ConsumerGroup() string { return c.consumerGroup } |
| 184 | + |
| 185 | +var _ consumer.Controller = (*DLQController)(nil) |
0 commit comments