|
| 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 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +package mysql |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "database/sql" |
| 18 | + "encoding/json" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 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 | +) |
| 27 | + |
| 28 | +type buildAnalysisStore struct { |
| 29 | + db *sql.DB |
| 30 | + scope tally.Scope |
| 31 | + queue string |
| 32 | +} |
| 33 | + |
| 34 | +// NewBuildAnalysisStore creates a queue-scoped MySQL report snapshot store. |
| 35 | +func NewBuildAnalysisStore(db *sql.DB, scope tally.Scope, queue string) storage.BuildAnalysisStore { |
| 36 | + return &buildAnalysisStore{db: db, scope: scope, queue: queue} |
| 37 | +} |
| 38 | + |
| 39 | +func (s *buildAnalysisStore) Create(ctx context.Context, analysis entity.BuildAnalysis) (retErr error) { |
| 40 | + op := metrics.Begin(s.scope, "create", metrics.StorageLatencyBuckets) |
| 41 | + defer func() { op.Complete(retErr) }() |
| 42 | + |
| 43 | + if analysis.BuildID == "" { |
| 44 | + return fmt.Errorf("build analysis requires a nonempty build ID") |
| 45 | + } |
| 46 | + reportsJSON, err := json.Marshal(analysis.Reports) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("marshal build analysis reports: %w", err) |
| 49 | + } |
| 50 | + _, err = s.db.ExecContext(ctx, |
| 51 | + `INSERT INTO build_analysis (queue, build_id, reports, observed_at_ms, version) |
| 52 | + VALUES (?, ?, ?, ?, ?)`, |
| 53 | + s.queue, analysis.BuildID, reportsJSON, analysis.ObservedAtMs, analysis.Version, |
| 54 | + ) |
| 55 | + if err != nil { |
| 56 | + if isDuplicateEntry(err) { |
| 57 | + return fmt.Errorf("build analysis %q: %w", analysis.BuildID, storage.ErrAlreadyExists) |
| 58 | + } |
| 59 | + return fmt.Errorf("insert build analysis %q: %w", analysis.BuildID, err) |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (s *buildAnalysisStore) Get(ctx context.Context, buildID string) (ret entity.BuildAnalysis, retErr error) { |
| 65 | + op := metrics.Begin(s.scope, "get", metrics.StorageLatencyBuckets) |
| 66 | + defer func() { op.Complete(retErr) }() |
| 67 | + |
| 68 | + var analysis entity.BuildAnalysis |
| 69 | + var reportsJSON []byte |
| 70 | + err := s.db.QueryRowContext(ctx, |
| 71 | + `SELECT build_id, reports, observed_at_ms, version |
| 72 | + FROM build_analysis WHERE queue = ? AND build_id = ?`, |
| 73 | + s.queue, buildID, |
| 74 | + ).Scan(&analysis.BuildID, &reportsJSON, &analysis.ObservedAtMs, &analysis.Version) |
| 75 | + if errors.Is(err, sql.ErrNoRows) { |
| 76 | + return entity.BuildAnalysis{}, storage.WrapNotFound(err) |
| 77 | + } |
| 78 | + if err != nil { |
| 79 | + return entity.BuildAnalysis{}, fmt.Errorf("get build analysis %q: %w", buildID, err) |
| 80 | + } |
| 81 | + if err := json.Unmarshal(reportsJSON, &analysis.Reports); err != nil { |
| 82 | + return entity.BuildAnalysis{}, fmt.Errorf("decode build analysis reports for %q: %w", buildID, err) |
| 83 | + } |
| 84 | + return analysis, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (s *buildAnalysisStore) Update(ctx context.Context, analysis entity.BuildAnalysis, oldVersion, newVersion int32) (retErr error) { |
| 88 | + op := metrics.Begin(s.scope, "update", metrics.StorageLatencyBuckets) |
| 89 | + defer func() { op.Complete(retErr) }() |
| 90 | + |
| 91 | + if analysis.BuildID == "" { |
| 92 | + return fmt.Errorf("build analysis requires a nonempty build ID") |
| 93 | + } |
| 94 | + reportsJSON, err := json.Marshal(analysis.Reports) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("marshal build analysis reports: %w", err) |
| 97 | + } |
| 98 | + result, err := s.db.ExecContext(ctx, |
| 99 | + `UPDATE build_analysis SET reports = ?, observed_at_ms = ?, version = ? |
| 100 | + WHERE queue = ? AND build_id = ? AND version = ?`, |
| 101 | + reportsJSON, analysis.ObservedAtMs, newVersion, s.queue, analysis.BuildID, oldVersion, |
| 102 | + ) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("update build analysis %q: %w", analysis.BuildID, err) |
| 105 | + } |
| 106 | + rowsAffected, err := result.RowsAffected() |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("get rows affected for build analysis %q: %w", analysis.BuildID, err) |
| 109 | + } |
| 110 | + if rowsAffected != 1 { |
| 111 | + return fmt.Errorf("build analysis %q expected version %d: %w", analysis.BuildID, oldVersion, storage.ErrVersionMismatch) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
0 commit comments