|
| 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 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 openIssueStore struct { |
| 30 | + db *sql.DB |
| 31 | + scope tally.Scope |
| 32 | + queue string |
| 33 | +} |
| 34 | + |
| 35 | +// NewOpenIssueStore binds currently open issues to queue over the shared database. |
| 36 | +func NewOpenIssueStore(db *sql.DB, scope tally.Scope, queue string) storage.OpenIssueStore { |
| 37 | + return &openIssueStore{db: db, scope: scope, queue: queue} |
| 38 | +} |
| 39 | + |
| 40 | +func (s *openIssueStore) Create(ctx context.Context, issue entity.OpenIssue) (retErr error) { |
| 41 | + op := metrics.Begin(s.scope, "create", metrics.StorageLatencyBuckets) |
| 42 | + defer func() { op.Complete(retErr) }() |
| 43 | + |
| 44 | + if err := s.validateIssue(issue); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + _, err := s.db.ExecContext(ctx, |
| 49 | + `INSERT INTO open_issue (queue, task_id, unit_id, first_seen_build_id) |
| 50 | + VALUES (?, ?, ?, ?)`, |
| 51 | + s.queue, issue.TaskID, issue.UnitID, issue.FirstSeenBuildID) |
| 52 | + if err != nil { |
| 53 | + if isDuplicateEntry(err) { |
| 54 | + return fmt.Errorf("open issue task=%q unit=%q: %w", issue.TaskID, issue.UnitID, storage.ErrAlreadyExists) |
| 55 | + } |
| 56 | + |
| 57 | + return fmt.Errorf("failed to create open issue: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (s *openIssueStore) Get(ctx context.Context, taskID, unitID string) (ret entity.OpenIssue, retErr error) { |
| 64 | + op := metrics.Begin(s.scope, "get", metrics.StorageLatencyBuckets) |
| 65 | + defer func() { op.Complete(retErr) }() |
| 66 | + |
| 67 | + if err := validateBuildIssueKey(s.queue, "", taskID, unitID); err != nil { |
| 68 | + return entity.OpenIssue{}, err |
| 69 | + } |
| 70 | + |
| 71 | + var issue entity.OpenIssue |
| 72 | + err := s.db.QueryRowContext(ctx, |
| 73 | + `SELECT task_id, unit_id, first_seen_build_id |
| 74 | + FROM open_issue WHERE queue = ? AND task_id = ? AND unit_id = ?`, |
| 75 | + s.queue, taskID, unitID, |
| 76 | + ).Scan(&issue.TaskID, &issue.UnitID, &issue.FirstSeenBuildID) |
| 77 | + if errors.Is(err, sql.ErrNoRows) { |
| 78 | + return entity.OpenIssue{}, storage.WrapNotFound(err) |
| 79 | + } |
| 80 | + if err != nil { |
| 81 | + return entity.OpenIssue{}, fmt.Errorf("failed to get open issue: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + return issue, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (s *openIssueStore) Delete(ctx context.Context, issue entity.OpenIssue) (retErr error) { |
| 88 | + op := metrics.Begin(s.scope, "delete", metrics.StorageLatencyBuckets) |
| 89 | + defer func() { op.Complete(retErr) }() |
| 90 | + |
| 91 | + if err := s.validateIssue(issue); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + _, err := s.db.ExecContext(ctx, |
| 96 | + `DELETE FROM open_issue |
| 97 | + WHERE queue = ? AND task_id = ? AND unit_id = ? AND first_seen_build_id = ?`, |
| 98 | + s.queue, issue.TaskID, issue.UnitID, issue.FirstSeenBuildID) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to delete open issue: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (s *openIssueStore) validateIssue(issue entity.OpenIssue) error { |
| 107 | + if err := validateBuildIssueKey(s.queue, issue.FirstSeenBuildID, issue.TaskID, issue.UnitID); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + if issue.FirstSeenBuildID == "" { |
| 111 | + return fmt.Errorf("open issue first-seen build ID must not be empty") |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
0 commit comments