|
| 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 queue history or summaries. |
| 31 | +var ErrInconsistentHistory = errors.New("inconsistent queue history") |
| 32 | + |
| 33 | +// Materializer retains committed state changes and updates their 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 | +func NewMaterializer(scope tally.Scope) Materializer { |
| 42 | + return &materializer{scope: scope.SubScope("queue_log_materializer")} |
| 43 | +} |
| 44 | + |
| 45 | +func NewQueueStateLog(queue entity.Queue) entity.QueueLog { |
| 46 | + return entity.QueueLog{ID: entity.QueueLogID(queue.EnablementVersion), Queue: queue.Name, |
| 47 | + State: queue.Enablement, QueueStateVersion: queue.EnablementVersion, |
| 48 | + EffectiveFromCommitURI: queue.EffectiveFromCommitURI, TimestampMs: queue.EnablementChangedAtMs} |
| 49 | +} |
| 50 | + |
| 51 | +func (m *materializer) PersistLog(ctx context.Context, stores storage.Storage, log entity.QueueLog) (retErr error) { |
| 52 | + op := metrics.Begin(m.scope, "persist_log", metrics.StorageLatencyBuckets, metrics.TagsFromContext(ctx)...) |
| 53 | + defer func() { op.Complete(retErr) }() |
| 54 | + if err := ctx.Err(); err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if err := log.Validate(); err != nil { |
| 58 | + return fmt.Errorf("queue %q: invalid log %q at state version %d: %w: %v", log.Queue, log.ID, log.QueueStateVersion, ErrInconsistentHistory, err) |
| 59 | + } |
| 60 | + logs := stores.GetQueueLogStore() |
| 61 | + if err := logs.Create(ctx, log); err != nil { |
| 62 | + if !errors.Is(err, storage.ErrAlreadyExists) { |
| 63 | + return fmt.Errorf("retain queue log: %w", err) |
| 64 | + } |
| 65 | + retained, err := logs.Get(ctx, log.ID) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("reconcile queue log: %w", err) |
| 68 | + } |
| 69 | + if retained != log { |
| 70 | + return fmt.Errorf("queue %q: log %q at state version %d conflicts with retained log %q for queue %q at state version %d: %w", log.Queue, log.ID, log.QueueStateVersion, retained.ID, retained.Queue, retained.QueueStateVersion, ErrInconsistentHistory) |
| 71 | + } |
| 72 | + log = retained |
| 73 | + } |
| 74 | + return materializeQueueSummary(ctx, stores.GetQueueSummaryStore(), log) |
| 75 | +} |
| 76 | + |
| 77 | +func materializeQueueSummary(ctx context.Context, store storage.QueueSummaryStore, log entity.QueueLog) error { |
| 78 | + for { |
| 79 | + if err := ctx.Err(); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + current, err := store.Get(ctx) |
| 83 | + if storage.IsNotFound(err) { |
| 84 | + initial := entity.QueueSummary{Queue: log.Queue, State: log.State, |
| 85 | + QueueStateVersion: log.QueueStateVersion, EffectiveFromCommitURI: log.EffectiveFromCommitURI, |
| 86 | + StateTimestampMs: log.TimestampMs, Version: 1} |
| 87 | + if err := store.Create(ctx, initial); errors.Is(err, storage.ErrAlreadyExists) { |
| 88 | + continue |
| 89 | + } else { |
| 90 | + return err |
| 91 | + } |
| 92 | + } |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + if current.Queue != log.Queue { |
| 97 | + return fmt.Errorf("queue %q log %q: summary belongs to queue %q at projection version %d: %w", log.Queue, log.ID, current.Queue, current.Version, ErrInconsistentHistory) |
| 98 | + } |
| 99 | + if err := current.Validate(); err != nil { |
| 100 | + return fmt.Errorf("queue %q: invalid summary at projection version %d, state version %d: %w: %v", log.Queue, current.Version, current.QueueStateVersion, ErrInconsistentHistory, err) |
| 101 | + } |
| 102 | + if log.QueueStateVersion < current.QueueStateVersion { |
| 103 | + return nil |
| 104 | + } |
| 105 | + if log.QueueStateVersion == current.QueueStateVersion { |
| 106 | + if current.Snapshot() != log.Snapshot() { |
| 107 | + return fmt.Errorf("queue %q: log %q conflicts with summary state version %d (projection version %d): %w", log.Queue, log.ID, current.QueueStateVersion, current.Version, ErrInconsistentHistory) |
| 108 | + } |
| 109 | + return nil |
| 110 | + } |
| 111 | + if current.Version == math.MaxInt64 { |
| 112 | + return fmt.Errorf("queue %q log %q: summary projection version %d exhausted: %w", log.Queue, log.ID, current.Version, ErrInconsistentHistory) |
| 113 | + } |
| 114 | + updated := current |
| 115 | + updated.State, updated.QueueStateVersion = log.State, log.QueueStateVersion |
| 116 | + updated.EffectiveFromCommitURI, updated.StateTimestampMs = log.EffectiveFromCommitURI, log.TimestampMs |
| 117 | + newVersion := current.Version + 1 |
| 118 | + if err := store.Update(ctx, updated, current.Version, newVersion); errors.Is(err, storage.ErrVersionMismatch) { |
| 119 | + continue |
| 120 | + } else if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + return nil |
| 124 | + } |
| 125 | +} |
0 commit comments