|
| 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 queuelog retains queue history and projects its current status summary. |
| 16 | +package queuelog |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "math" |
| 23 | + |
| 24 | + "github.com/uber-go/tally" |
| 25 | + "github.com/uber/submitqueue/platform/metrics" |
| 26 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 27 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 28 | +) |
| 29 | + |
| 30 | +// ErrInconsistentHistory identifies missing or conflicting committed occurrences or projections. |
| 31 | +var ErrInconsistentHistory = errors.New("inconsistent queue history") |
| 32 | + |
| 33 | +// Materializer retains committed queue occurrences and advances their derived summary. |
| 34 | +type Materializer interface { |
| 35 | + // PersistLog idempotently retains an occurrence, then projects its state. |
| 36 | + PersistLog(context.Context, storage.Storage, entity.QueueLog) error |
| 37 | +} |
| 38 | + |
| 39 | +type materializer struct{ scope tally.Scope } |
| 40 | + |
| 41 | +// NewMaterializer creates a queue history materializer with bounded outcome metrics. |
| 42 | +func NewMaterializer(scope tally.Scope) Materializer { |
| 43 | + return &materializer{scope: scope.SubScope("queue_log_materializer")} |
| 44 | +} |
| 45 | + |
| 46 | +// NewQueueStateLog represents the committed enablement occurrence in an internal queue snapshot. |
| 47 | +func NewQueueStateLog(queue entity.Queue) entity.QueueLog { |
| 48 | + return entity.QueueLog{ID: entity.QueueLogID(queue.EnablementVersion), Queue: queue.Name, |
| 49 | + State: queue.Enablement, QueueStateVersion: queue.EnablementVersion, |
| 50 | + EffectiveFromCommitURI: queue.EffectiveFromCommitURI, TimestampMs: queue.EnablementChangedAtMs} |
| 51 | +} |
| 52 | + |
| 53 | +func (m *materializer) PersistLog(ctx context.Context, stores storage.Storage, log entity.QueueLog) (retErr error) { |
| 54 | + op := metrics.Begin(m.scope, "persist_log", metrics.StorageLatencyBuckets, metrics.TagsFromContext(ctx)...) |
| 55 | + defer func() { op.Complete(retErr) }() |
| 56 | + if err := ctx.Err(); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if err := log.Validate(); err != nil { |
| 60 | + return fmt.Errorf("%w: %v", ErrInconsistentHistory, err) |
| 61 | + } |
| 62 | + logs := stores.GetQueueLogStore() |
| 63 | + if err := logs.Create(ctx, log); err != nil { |
| 64 | + if !errors.Is(err, storage.ErrAlreadyExists) { |
| 65 | + return fmt.Errorf("retain queue log: %w", err) |
| 66 | + } |
| 67 | + retained, err := logs.Get(ctx, log.ID) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("reconcile queue log: %w", err) |
| 70 | + } |
| 71 | + if retained != log { |
| 72 | + return ErrInconsistentHistory |
| 73 | + } |
| 74 | + log = retained |
| 75 | + } |
| 76 | + return materializeQueueSummary(ctx, stores.GetQueueSummaryStore(), log) |
| 77 | +} |
| 78 | + |
| 79 | +func materializeQueueSummary(ctx context.Context, store storage.QueueSummaryStore, log entity.QueueLog) error { |
| 80 | + for { |
| 81 | + if err := ctx.Err(); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + current, err := store.Get(ctx) |
| 85 | + if storage.IsNotFound(err) { |
| 86 | + initial := entity.QueueSummary{Queue: log.Queue, State: log.State, |
| 87 | + QueueStateVersion: log.QueueStateVersion, EffectiveFromCommitURI: log.EffectiveFromCommitURI, |
| 88 | + StateTimestampMs: log.TimestampMs, Version: 1} |
| 89 | + if err := store.Create(ctx, initial); errors.Is(err, storage.ErrAlreadyExists) { |
| 90 | + continue |
| 91 | + } else { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + if current.Queue != log.Queue { |
| 99 | + return ErrInconsistentHistory |
| 100 | + } |
| 101 | + if err := current.Validate(); err != nil { |
| 102 | + return fmt.Errorf("%w: %v", ErrInconsistentHistory, err) |
| 103 | + } |
| 104 | + if log.QueueStateVersion < current.QueueStateVersion { |
| 105 | + return nil |
| 106 | + } |
| 107 | + if log.QueueStateVersion == current.QueueStateVersion { |
| 108 | + if current.Snapshot() != log.Snapshot() { |
| 109 | + return ErrInconsistentHistory |
| 110 | + } |
| 111 | + return nil |
| 112 | + } |
| 113 | + if current.Version == math.MaxInt64 { |
| 114 | + return ErrInconsistentHistory |
| 115 | + } |
| 116 | + updated := current |
| 117 | + updated.State, updated.QueueStateVersion = log.State, log.QueueStateVersion |
| 118 | + updated.EffectiveFromCommitURI, updated.StateTimestampMs = log.EffectiveFromCommitURI, log.TimestampMs |
| 119 | + newVersion := current.Version + 1 |
| 120 | + if err := store.Update(ctx, updated, current.Version, newVersion); errors.Is(err, storage.ErrVersionMismatch) { |
| 121 | + continue |
| 122 | + } else if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + return nil |
| 126 | + } |
| 127 | +} |
0 commit comments