|
| 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 dlq |
| 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 | + sqmq "github.com/uber/submitqueue/submitqueue/core/messagequeue" |
| 27 | + "github.com/uber/submitqueue/submitqueue/entity" |
| 28 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 29 | + corebatch "github.com/uber/submitqueue/submitqueue/orchestrator/core/batch" |
| 30 | + orchstorage "github.com/uber/submitqueue/submitqueue/orchestrator/extension/storage" |
| 31 | + "go.uber.org/zap" |
| 32 | +) |
| 33 | + |
| 34 | +// dependencyAnalysisController is the DLQ reconciler for the dependency-analysis |
| 35 | +// topic. |
| 36 | +// |
| 37 | +// It does not share the generic batch reconciler because the batch on this |
| 38 | +// message need not own its requests. The batch stage mints a batch per |
| 39 | +// delivery, so one request can have several batches, and dependency analysis is |
| 40 | +// where one of them is chosen. The generic reconciler fails every member |
| 41 | +// request, so a dead-lettered duplicate would write Error over a request whose |
| 42 | +// chosen batch is still live, and that batch would go on to land it. |
| 43 | +// |
| 44 | +// So the batch's state decides: |
| 45 | +// |
| 46 | +// - Creating, with a request already carried by another batch past Creating: |
| 47 | +// a duplicate. Nothing is touched; it stays inert in Creating, as the |
| 48 | +// primary controller leaves it. |
| 49 | +// - Creating otherwise, Created, or Failed: this batch owns its requests and |
| 50 | +// nothing downstream will advance it, so it is failed and fanned out. |
| 51 | +// - Any later state: a downstream stage took the batch over after this message |
| 52 | +// was written, and owns its outcome. |
| 53 | +type dependencyAnalysisController struct { |
| 54 | + logger *zap.SugaredLogger |
| 55 | + metricsScope tally.Scope |
| 56 | + stores orchstorage.Factory |
| 57 | + registry consumer.TopicRegistry |
| 58 | + topicKey consumer.TopicKey |
| 59 | + consumerGroup string |
| 60 | +} |
| 61 | + |
| 62 | +// Verify dependencyAnalysisController implements consumer.Controller at compile time. |
| 63 | +var _ consumer.Controller = (*dependencyAnalysisController)(nil) |
| 64 | + |
| 65 | +// NewDLQDependencyAnalysisController builds the DLQ controller for the |
| 66 | +// dependency-analysis topic. topicKey must be the DLQ topic key (typically |
| 67 | +// TopicKey(primary)). |
| 68 | +func NewDLQDependencyAnalysisController( |
| 69 | + logger *zap.SugaredLogger, |
| 70 | + scope tally.Scope, |
| 71 | + stores orchstorage.Factory, |
| 72 | + registry consumer.TopicRegistry, |
| 73 | + topicKey consumer.TopicKey, |
| 74 | + consumerGroup string, |
| 75 | +) consumer.Controller { |
| 76 | + name := string(topicKey) + "_controller" |
| 77 | + return &dependencyAnalysisController{ |
| 78 | + logger: logger.Named(name), |
| 79 | + metricsScope: scope.SubScope(name), |
| 80 | + stores: stores, |
| 81 | + registry: registry, |
| 82 | + topicKey: topicKey, |
| 83 | + consumerGroup: consumerGroup, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Process reconciles a single dead-lettered dependency-analysis message. |
| 88 | +func (c *dependencyAnalysisController) Process(ctx context.Context, delivery consumer.Delivery) error { |
| 89 | + const opName = "process" |
| 90 | + |
| 91 | + msg := delivery.Message() |
| 92 | + |
| 93 | + bid, err := sqmq.UnmarshalBatchID(primaryTopicKey(c.topicKey), msg.Payload) |
| 94 | + if err != nil { |
| 95 | + metrics.NamedCounter(c.metricsScope, opName, "deserialize_errors", 1) |
| 96 | + return fmt.Errorf("failed to decode batch id from dlq payload: %w", err) |
| 97 | + } |
| 98 | + if err := entityqueue.ValidatePayloadQueue(msg, bid.Queue); err != nil { |
| 99 | + metrics.NamedCounter(c.metricsScope, opName, "queue_identity_errors", 1) |
| 100 | + return nil |
| 101 | + } |
| 102 | + if bid.ID == "" { |
| 103 | + metrics.NamedCounter(c.metricsScope, opName, "empty_id_errors", 1) |
| 104 | + return fmt.Errorf("dlq payload decoded to empty batch id") |
| 105 | + } |
| 106 | + |
| 107 | + store, err := c.stores.For(orchstorage.Config{QueueName: bid.Queue}) |
| 108 | + if err != nil { |
| 109 | + metrics.NamedCounter(c.metricsScope, opName, "storage_resolve_errors", 1) |
| 110 | + // Non-retryable: a missing or unresolvable queue is a malformed message. |
| 111 | + return fmt.Errorf("failed to resolve storage for queue %q: %w", bid.Queue, err) |
| 112 | + } |
| 113 | + |
| 114 | + lastError, failureMeta := failureContext(delivery) |
| 115 | + dmeta := delivery.Metadata() |
| 116 | + c.logger.Warnw("dlq message received", |
| 117 | + "batch_id", bid.ID, |
| 118 | + "attempt", delivery.Attempt(), |
| 119 | + "dlq_original_topic", dmeta["dlq.original_topic"], |
| 120 | + "dlq_failure_count", dmeta["dlq.failure_count"], |
| 121 | + "dlq_last_error", dmeta["dlq.last_error"], |
| 122 | + ) |
| 123 | + |
| 124 | + batch, err := store.GetBatchStore().Get(ctx, bid.ID) |
| 125 | + if err != nil { |
| 126 | + if errors.Is(err, storage.ErrNotFound) { |
| 127 | + c.logger.Warnw("dlq reconcile: batch not found, skipping", "batch_id", bid.ID) |
| 128 | + return nil |
| 129 | + } |
| 130 | + metrics.NamedCounter(c.metricsScope, opName, "storage_errors", 1) |
| 131 | + return fmt.Errorf("failed to get batch %s: %w", bid.ID, err) |
| 132 | + } |
| 133 | + |
| 134 | + switch batch.State { |
| 135 | + case entity.BatchStateCreating: |
| 136 | + carrier, err := c.findOtherBatchCarryingRequests(ctx, store, batch) |
| 137 | + if err != nil { |
| 138 | + metrics.NamedCounter(c.metricsScope, opName, "batch_lookup_errors", 1) |
| 139 | + return err |
| 140 | + } |
| 141 | + if carrier != "" { |
| 142 | + metrics.NamedCounter(c.metricsScope, opName, "skipped_duplicate", 1) |
| 143 | + c.logger.Infow("dlq reconcile: batch is a duplicate, its requests are carried by another", |
| 144 | + "batch_id", batch.ID, |
| 145 | + "carried_by", carrier, |
| 146 | + ) |
| 147 | + return nil |
| 148 | + } |
| 149 | + case entity.BatchStateCreated, entity.BatchStateFailed: |
| 150 | + default: |
| 151 | + metrics.NamedCounter(c.metricsScope, opName, "skipped_owned_downstream", 1) |
| 152 | + c.logger.Infow("dlq reconcile: batch has moved past dependency analysis, leaving it to its current stage", |
| 153 | + "batch_id", batch.ID, |
| 154 | + "state", string(batch.State), |
| 155 | + ) |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + if _, err := failBatch(ctx, store, c.registry, c.logger, batch.ID, lastError, failureMeta); err != nil { |
| 160 | + metrics.NamedCounter(c.metricsScope, opName, "reconcile_errors", 1) |
| 161 | + return err |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +// findOtherBatchCarryingRequests returns a batch other than this one that got |
| 167 | +// past Creating with one of this batch's requests, or the empty string. It is |
| 168 | +// the same test the primary controller uses to abandon a duplicate. |
| 169 | +func (c *dependencyAnalysisController) findOtherBatchCarryingRequests(ctx context.Context, store orchstorage.Storage, batch entity.Batch) (string, error) { |
| 170 | + for _, requestID := range batch.Contains { |
| 171 | + batches, _, err := corebatch.FindByRequestID(ctx, store, requestID) |
| 172 | + if err != nil { |
| 173 | + return "", err |
| 174 | + } |
| 175 | + for _, other := range batches { |
| 176 | + if other.ID != batch.ID && other.State != entity.BatchStateCreating { |
| 177 | + return other.ID, nil |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return "", nil |
| 182 | +} |
| 183 | + |
| 184 | +// Name returns the controller name for logging and metrics. |
| 185 | +func (c *dependencyAnalysisController) Name() string { |
| 186 | + return string(c.topicKey) |
| 187 | +} |
| 188 | + |
| 189 | +// TopicKey returns the topic key this controller subscribes to. |
| 190 | +func (c *dependencyAnalysisController) TopicKey() consumer.TopicKey { |
| 191 | + return c.topicKey |
| 192 | +} |
| 193 | + |
| 194 | +// ConsumerGroup returns the consumer group for offset tracking. |
| 195 | +func (c *dependencyAnalysisController) ConsumerGroup() string { |
| 196 | + return c.consumerGroup |
| 197 | +} |
0 commit comments