|
| 1 | +// Copyright (c) 2025 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 reconciles dead-lettered merge requests back to the client. |
| 16 | +// |
| 17 | +// Runway's inbound merge topics dead-letter a message after the primary |
| 18 | +// controller returns a non-retryable error or exhausts retries on a retryable |
| 19 | +// one. Runway is stateless and the sole responder on the client's correlation |
| 20 | +// id, so a dead-lettered request that produced no signal would leave the client |
| 21 | +// (SubmitQueue) waiting forever. Expected outcomes — conflicts and invalid |
| 22 | +// requests — are already published as FAILED results by the primary controllers; |
| 23 | +// this reconciler is the backstop for everything else (unexpected faults, retry |
| 24 | +// exhaustion). |
| 25 | +// |
| 26 | +// On each delivery from a `{topic}_dlq` topic it decodes the same MergeRequest |
| 27 | +// payload the primary controller consumes (the queue preserves the bytes |
| 28 | +// verbatim), and republishes a FAILED MergeResult — echoing the correlation id |
| 29 | +// — to the corresponding signal topic. Unlike the stovepipe/orchestrator DLQ |
| 30 | +// reconcilers it writes no entity state (Runway has none); the signal is the |
| 31 | +// resolution. A payload that cannot be decoded carries no correlation id to |
| 32 | +// resolve, so it is logged and acked (dropped). |
| 33 | +// |
| 34 | +// Wire this controller on a dedicated consumer built with |
| 35 | +// errs.AlwaysRetryableProcessor so a transient publish failure retries forever |
| 36 | +// rather than dead-lettering again (the DLQ topic has no DLQ of its own). |
| 37 | +package dlq |
| 38 | + |
| 39 | +import ( |
| 40 | + "context" |
| 41 | + "fmt" |
| 42 | + |
| 43 | + "github.com/uber-go/tally" |
| 44 | + runwaymq "github.com/uber/submitqueue/api/runway/messagequeue" |
| 45 | + runwaypb "github.com/uber/submitqueue/api/runway/messagequeue/protopb" |
| 46 | + entityqueue "github.com/uber/submitqueue/platform/base/messagequeue" |
| 47 | + "github.com/uber/submitqueue/platform/consumer" |
| 48 | + "github.com/uber/submitqueue/platform/metrics" |
| 49 | + "go.uber.org/zap" |
| 50 | +) |
| 51 | + |
| 52 | +// topicSuffix is appended to a primary topic key to derive its DLQ topic key. |
| 53 | +// It matches DefaultSubscriptionConfig's DLQ TopicSuffix so a registered DLQ |
| 54 | +// subscription's topic name matches the controller's TopicKey(). |
| 55 | +const topicSuffix = "_dlq" |
| 56 | + |
| 57 | +// TopicKey returns the DLQ topic key for a primary merge topic. Exported so the |
| 58 | +// wiring layer builds matching pairs without duplicating the suffix literal. |
| 59 | +func TopicKey(main consumer.TopicKey) consumer.TopicKey { |
| 60 | + return consumer.TopicKey(string(main) + topicSuffix) |
| 61 | +} |
| 62 | + |
| 63 | +// Verify Controller implements consumer.Controller at compile time. |
| 64 | +var _ consumer.Controller = (*Controller)(nil) |
| 65 | + |
| 66 | +// Controller consumes a merge topic's dead-letter queue and republishes a |
| 67 | +// terminal FAILED result to the corresponding signal topic. |
| 68 | +type Controller struct { |
| 69 | + logger *zap.SugaredLogger |
| 70 | + metricsScope tally.Scope |
| 71 | + registry consumer.TopicRegistry |
| 72 | + topicKey consumer.TopicKey |
| 73 | + signalTopicKey consumer.TopicKey |
| 74 | + consumerGroup string |
| 75 | +} |
| 76 | + |
| 77 | +// Params are the parameters for creating a new DLQ reconciler. |
| 78 | +type Params struct { |
| 79 | + // TopicKey is the dead-letter topic this controller consumes |
| 80 | + // (typically dlq.TopicKey(<primary topic>)). |
| 81 | + TopicKey consumer.TopicKey |
| 82 | + // SignalTopicKey is the signal topic the FAILED result is published to. |
| 83 | + SignalTopicKey consumer.TopicKey |
| 84 | + ConsumerGroup string |
| 85 | + |
| 86 | + Registry consumer.TopicRegistry |
| 87 | + |
| 88 | + Scope tally.Scope |
| 89 | + Logger *zap.SugaredLogger |
| 90 | +} |
| 91 | + |
| 92 | +// NewController creates a DLQ reconciler for a merge topic's dead-letter queue. |
| 93 | +func NewController(p Params) *Controller { |
| 94 | + return &Controller{ |
| 95 | + logger: p.Logger.Named("merge_dlq_controller"), |
| 96 | + metricsScope: p.Scope.SubScope("merge_dlq_controller"), |
| 97 | + registry: p.Registry, |
| 98 | + topicKey: p.TopicKey, |
| 99 | + signalTopicKey: p.SignalTopicKey, |
| 100 | + consumerGroup: p.ConsumerGroup, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Process decodes the dead-lettered merge request and republishes a FAILED |
| 105 | +// result to the signal topic so the client's correlation id resolves. Returns |
| 106 | +// nil to ack; an error to nack (retried indefinitely under |
| 107 | +// AlwaysRetryableProcessor). |
| 108 | +func (c *Controller) Process(ctx context.Context, delivery consumer.Delivery) error { |
| 109 | + const opName = "process" |
| 110 | + |
| 111 | + msg := delivery.Message() |
| 112 | + meta := delivery.Metadata() |
| 113 | + |
| 114 | + request := &runwaymq.MergeRequest{} |
| 115 | + if err := runwaymq.Unmarshal(msg.Payload, request); err != nil { |
| 116 | + // No correlation id is recoverable, so there is nothing to resolve for |
| 117 | + // the client. Log and ack (drop) rather than retry forever. |
| 118 | + metrics.NamedCounter(c.metricsScope, opName, "undecodable", 1) |
| 119 | + c.logger.Errorw("dlq reconcile: undecodable merge request, dropping", |
| 120 | + "err", err, |
| 121 | + "original_topic", meta["dlq.original_topic"], |
| 122 | + ) |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + reason := meta["dlq.last_error"] |
| 127 | + if reason == "" { |
| 128 | + reason = "runway failed to process the merge request" |
| 129 | + } |
| 130 | + |
| 131 | + c.logger.Warnw("dlq reconcile: publishing terminal failure", |
| 132 | + "id", request.GetId(), |
| 133 | + "queue_name", request.GetQueueName(), |
| 134 | + "original_topic", meta["dlq.original_topic"], |
| 135 | + "failure_count", meta["dlq.failure_count"], |
| 136 | + "last_error", reason, |
| 137 | + ) |
| 138 | + |
| 139 | + result := &runwaymq.MergeResult{ |
| 140 | + Id: request.GetId(), |
| 141 | + Outcome: runwaypb.Outcome_FAILED, |
| 142 | + Reason: fmt.Sprintf("dead-lettered: %s", reason), |
| 143 | + } |
| 144 | + |
| 145 | + if err := c.publish(ctx, result, msg.PartitionKey); err != nil { |
| 146 | + metrics.NamedCounter(c.metricsScope, opName, "publish_errors", 1) |
| 147 | + return fmt.Errorf("failed to publish dlq failure result for %s: %w", request.GetId(), err) |
| 148 | + } |
| 149 | + |
| 150 | + metrics.NamedCounter(c.metricsScope, opName, "reconciled", 1) |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +// publish serializes a MergeResult and publishes it to the signal topic. |
| 155 | +func (c *Controller) publish(ctx context.Context, result *runwaymq.MergeResult, partitionKey string) error { |
| 156 | + payload, err := runwaymq.Marshal(result) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("failed to serialize merge result: %w", err) |
| 159 | + } |
| 160 | + |
| 161 | + msg := entityqueue.NewMessage(result.GetId(), payload, partitionKey, nil) |
| 162 | + |
| 163 | + q, ok := c.registry.Queue(c.signalTopicKey) |
| 164 | + if !ok { |
| 165 | + return fmt.Errorf("no queue registered for topic key %s", c.signalTopicKey) |
| 166 | + } |
| 167 | + |
| 168 | + topicName, ok := c.registry.TopicName(c.signalTopicKey) |
| 169 | + if !ok { |
| 170 | + return fmt.Errorf("no topic name registered for topic key %s", c.signalTopicKey) |
| 171 | + } |
| 172 | + |
| 173 | + if err := q.Publisher().Publish(ctx, topicName, msg); err != nil { |
| 174 | + return fmt.Errorf("failed to publish message: %w", err) |
| 175 | + } |
| 176 | + |
| 177 | + return nil |
| 178 | +} |
| 179 | + |
| 180 | +// Name returns the controller name for logging and metrics. |
| 181 | +func (c *Controller) Name() string { |
| 182 | + return string(c.topicKey) |
| 183 | +} |
| 184 | + |
| 185 | +// TopicKey returns the dead-letter topic key this controller subscribes to. |
| 186 | +func (c *Controller) TopicKey() consumer.TopicKey { |
| 187 | + return c.topicKey |
| 188 | +} |
| 189 | + |
| 190 | +// ConsumerGroup returns the consumer group for offset tracking. |
| 191 | +func (c *Controller) ConsumerGroup() string { |
| 192 | + return c.consumerGroup |
| 193 | +} |
0 commit comments