|
| 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 mysql |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "database/sql" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/uber-go/tally" |
| 24 | + "github.com/uber/submitqueue/platform/metrics" |
| 25 | + "github.com/uber/submitqueue/stovepipe/entity" |
| 26 | + "github.com/uber/submitqueue/stovepipe/extension/storage" |
| 27 | +) |
| 28 | + |
| 29 | +type queueStore struct { |
| 30 | + db *sql.DB |
| 31 | + scope tally.Scope |
| 32 | +} |
| 33 | + |
| 34 | +// NewQueueStore creates a new MySQL-backed QueueStore. |
| 35 | +func NewQueueStore(db *sql.DB, scope tally.Scope) storage.QueueStore { |
| 36 | + return &queueStore{db: db, scope: scope} |
| 37 | +} |
| 38 | + |
| 39 | +// Create persists a new queue row. Returns ErrAlreadyExists if the name already exists. |
| 40 | +func (q *queueStore) Create(ctx context.Context, queue entity.Queue) (retErr error) { |
| 41 | + op := metrics.Begin(q.scope, "create") |
| 42 | + defer func() { op.Complete(retErr) }() |
| 43 | + |
| 44 | + _, err := q.db.ExecContext(ctx, |
| 45 | + `INSERT INTO queue (name, last_green_uri, in_flight_count, latest_request_seq, version) |
| 46 | + VALUES (?, ?, ?, ?, ?)`, |
| 47 | + queue.Name, |
| 48 | + queue.LastGreenURI, |
| 49 | + queue.InFlightCount, |
| 50 | + queue.LatestRequestSeq, |
| 51 | + queue.Version, |
| 52 | + ) |
| 53 | + if err != nil { |
| 54 | + if isDuplicateEntry(err) { |
| 55 | + return fmt.Errorf("queue name=%s: %w", queue.Name, storage.ErrAlreadyExists) |
| 56 | + } |
| 57 | + return fmt.Errorf("failed to insert queue name=%s: %w", queue.Name, err) |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Get retrieves a queue by name. Returns ErrNotFound if the queue is not found. |
| 63 | +func (q *queueStore) Get(ctx context.Context, name string) (ret entity.Queue, retErr error) { |
| 64 | + op := metrics.Begin(q.scope, "get") |
| 65 | + defer func() { op.Complete(retErr) }() |
| 66 | + |
| 67 | + var queue entity.Queue |
| 68 | + err := q.db.QueryRowContext(ctx, |
| 69 | + "SELECT name, last_green_uri, in_flight_count, latest_request_seq, version FROM queue WHERE name = ?", |
| 70 | + name, |
| 71 | + ).Scan( |
| 72 | + &queue.Name, |
| 73 | + &queue.LastGreenURI, |
| 74 | + &queue.InFlightCount, |
| 75 | + &queue.LatestRequestSeq, |
| 76 | + &queue.Version, |
| 77 | + ) |
| 78 | + |
| 79 | + if errors.Is(err, sql.ErrNoRows) { |
| 80 | + return entity.Queue{}, storage.WrapNotFound(err) |
| 81 | + } |
| 82 | + if err != nil { |
| 83 | + return entity.Queue{}, fmt.Errorf("failed to get queue name=%s from the database: %w", name, err) |
| 84 | + } |
| 85 | + |
| 86 | + return queue, nil |
| 87 | +} |
| 88 | + |
| 89 | +// Update persists the mutable fields of queue if the stored version matches oldVersion, |
| 90 | +// writing newVersion. Returns ErrVersionMismatch if the stored version does not match. |
| 91 | +func (q *queueStore) Update(ctx context.Context, queue entity.Queue, oldVersion, newVersion int32) (retErr error) { |
| 92 | + op := metrics.Begin(q.scope, "update") |
| 93 | + defer func() { op.Complete(retErr) }() |
| 94 | + |
| 95 | + result, err := q.db.ExecContext(ctx, |
| 96 | + `UPDATE queue |
| 97 | + SET last_green_uri = ?, in_flight_count = ?, latest_request_seq = ?, version = ? |
| 98 | + WHERE name = ? AND version = ?`, |
| 99 | + queue.LastGreenURI, |
| 100 | + queue.InFlightCount, |
| 101 | + queue.LatestRequestSeq, |
| 102 | + newVersion, |
| 103 | + queue.Name, |
| 104 | + oldVersion, |
| 105 | + ) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf( |
| 108 | + "failed to update queue name=%q oldVersion=%d newVersion=%d: %w", |
| 109 | + queue.Name, oldVersion, newVersion, err, |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + rowsAffected, err := result.RowsAffected() |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf( |
| 116 | + "failed to get rows affected from update for name=%q oldVersion=%d newVersion=%d: %w", |
| 117 | + queue.Name, oldVersion, newVersion, err, |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + if rowsAffected != 1 { |
| 122 | + return fmt.Errorf( |
| 123 | + "version mismatch for queue update: name=%q expected_version=%d: %w", |
| 124 | + queue.Name, oldVersion, storage.ErrVersionMismatch, |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
0 commit comments