|
| 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 controller |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/uber-go/tally" |
| 23 | + "github.com/uber/submitqueue/platform/metrics" |
| 24 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 25 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 26 | + "go.uber.org/zap" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + defaultListPageSize = 50 |
| 31 | + maxListPageSize = 200 |
| 32 | +) |
| 33 | + |
| 34 | +// ListController handles queue-scoped acceptance-time listing. |
| 35 | +type ListController interface { |
| 36 | + List(ctx context.Context, req entity.ListRequest) (entity.ListResult, error) |
| 37 | +} |
| 38 | + |
| 39 | +type listController struct { |
| 40 | + logger *zap.SugaredLogger |
| 41 | + metricsScope tally.Scope |
| 42 | + stores storage.Factory |
| 43 | + configuredQueues map[string]struct{} |
| 44 | + now func() time.Time |
| 45 | +} |
| 46 | + |
| 47 | +// NewListController creates a controller restricted to the supplied configured queues. |
| 48 | +func NewListController(logger *zap.SugaredLogger, scope tally.Scope, stores storage.Factory, configuredQueues []string) ListController { |
| 49 | + queues := make(map[string]struct{}, len(configuredQueues)) |
| 50 | + for _, queue := range configuredQueues { |
| 51 | + queues[queue] = struct{}{} |
| 52 | + } |
| 53 | + return &listController{ |
| 54 | + logger: logger, metricsScope: scope.SubScope("list_controller"), stores: stores, |
| 55 | + configuredQueues: queues, now: time.Now, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// List returns current summaries in descending acceptance-time order within a half-open time window. |
| 60 | +// Omitted bounds resolve to [0, server now) on the first page and to the token's window on continuations. |
| 61 | +func (c *listController) List(ctx context.Context, req entity.ListRequest) (result entity.ListResult, retErr error) { |
| 62 | + op := metrics.Begin(c.metricsScope, "list", metrics.StorageLatencyBuckets, metrics.TagsFromContext(ctx)...) |
| 63 | + defer func() { op.Complete(retErr) }() |
| 64 | + |
| 65 | + if req.Queue == "" { |
| 66 | + return entity.ListResult{}, fmt.Errorf("List requires a queue: %w", ErrInvalidRequest) |
| 67 | + } |
| 68 | + if _, ok := c.configuredQueues[req.Queue]; !ok { |
| 69 | + return entity.ListResult{}, fmt.Errorf("List queue %q is not configured: %w", req.Queue, ErrInvalidRequest) |
| 70 | + } |
| 71 | + query, err := c.resolveListRange(req) |
| 72 | + if err != nil { |
| 73 | + return entity.ListResult{}, err |
| 74 | + } |
| 75 | + stores, err := c.stores.For(storage.Config{QueueName: req.Queue}) |
| 76 | + if err != nil { |
| 77 | + return entity.ListResult{}, fmt.Errorf("List failed to resolve storage for queue %q: %w", req.Queue, err) |
| 78 | + } |
| 79 | + mappings, err := stores.GetRequestAcceptanceStore().List(ctx, query) |
| 80 | + if err != nil { |
| 81 | + return entity.ListResult{}, fmt.Errorf("List failed to read acceptance mappings for queue %q: %w", req.Queue, err) |
| 82 | + } |
| 83 | + pageSize := query.Limit - 1 |
| 84 | + visible := mappings[:min(len(mappings), pageSize)] |
| 85 | + result.Requests = make([]entity.RequestSummary, 0, len(visible)) |
| 86 | + if len(visible) > 0 { |
| 87 | + summaries := stores.GetRequestSummaryStore() |
| 88 | + for _, mapping := range visible { |
| 89 | + summary, err := readListSummary(ctx, summaries, req.Queue, mapping) |
| 90 | + if err != nil { |
| 91 | + return entity.ListResult{}, err |
| 92 | + } |
| 93 | + result.Requests = append(result.Requests, summary) |
| 94 | + } |
| 95 | + } |
| 96 | + if len(mappings) > pageSize { |
| 97 | + last := visible[len(visible)-1] |
| 98 | + nextToken, err := encodeListPageToken(listPageToken{ |
| 99 | + Version: listPageTokenVersion, Queue: req.Queue, |
| 100 | + AcceptedAtOrAfterMs: query.AcceptedAtOrAfterMs, AcceptedBeforeMs: query.AcceptedBeforeMs, |
| 101 | + LastAcceptedAtMs: last.AcceptedAtMs, LastRequestID: last.RequestID, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return entity.ListResult{}, fmt.Errorf("List failed to encode continuation: %w", err) |
| 105 | + } |
| 106 | + result.NextPageToken = nextToken |
| 107 | + } |
| 108 | + c.logger.Debugw("queue requests listed", "queue", req.Queue, "request_count", len(result.Requests), "has_next_page", result.NextPageToken != "") |
| 109 | + return result, nil |
| 110 | +} |
| 111 | + |
| 112 | +func readListSummary(ctx context.Context, summaries storage.RequestSummaryStore, queue string, mapping entity.RequestAcceptance) (entity.RequestSummary, error) { |
| 113 | + if mapping.Queue != queue || mapping.AcceptedAtMs <= 0 || mapping.RequestID == "" { |
| 114 | + return entity.RequestSummary{}, &ListConsistencyError{ |
| 115 | + Queue: queue, RequestID: mapping.RequestID, Reason: "invalid acceptance mapping", |
| 116 | + } |
| 117 | + } |
| 118 | + summary, err := summaries.Get(ctx, mapping.RequestID) |
| 119 | + if err != nil { |
| 120 | + if storage.IsNotFound(err) { |
| 121 | + return entity.RequestSummary{}, &ListConsistencyError{ |
| 122 | + Queue: queue, RequestID: mapping.RequestID, Reason: "acceptance mapping has no summary", Err: err, |
| 123 | + } |
| 124 | + } |
| 125 | + return entity.RequestSummary{}, fmt.Errorf("List failed to read summary queue=%q request_id=%q: %w", queue, mapping.RequestID, err) |
| 126 | + } |
| 127 | + if summary.Queue != queue || summary.RequestID != mapping.RequestID || summary.AcceptedAtMs != mapping.AcceptedAtMs { |
| 128 | + return entity.RequestSummary{}, &ListConsistencyError{ |
| 129 | + Queue: queue, RequestID: mapping.RequestID, Reason: "acceptance mapping disagrees with summary", |
| 130 | + } |
| 131 | + } |
| 132 | + return summary, nil |
| 133 | +} |
0 commit comments