|
| 1 | +package mysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/go-sql-driver/mysql" |
| 11 | + |
| 12 | + "github.com/uber/submitqueue/entity" |
| 13 | + "github.com/uber/submitqueue/extension/storage" |
| 14 | +) |
| 15 | + |
| 16 | +type batchStore struct { |
| 17 | + db *sql.DB |
| 18 | +} |
| 19 | + |
| 20 | +// NewBatchStore creates a new MySQL-backed BatchStore. |
| 21 | +func NewBatchStore(db *sql.DB) storage.BatchStore { |
| 22 | + return &batchStore{db: db} |
| 23 | +} |
| 24 | + |
| 25 | +// Get retrieves a batch by ID. Returns ErrNotFound if the batch is not found. |
| 26 | +func (s *batchStore) Get(ctx context.Context, id string) (entity.Batch, error) { |
| 27 | + var batch entity.Batch |
| 28 | + var containsJSON []byte |
| 29 | + var dependenciesJSON []byte |
| 30 | + |
| 31 | + err := s.db.QueryRowContext(ctx, |
| 32 | + "SELECT id, queue, contains, dependencies, state, version FROM batch WHERE id = ?", |
| 33 | + id, |
| 34 | + ).Scan(&batch.ID, &batch.Queue, &containsJSON, &dependenciesJSON, &batch.State, &batch.Version) |
| 35 | + |
| 36 | + if errors.Is(err, sql.ErrNoRows) { |
| 37 | + return entity.Batch{}, storage.WrapNotFound(err) |
| 38 | + } |
| 39 | + if err != nil { |
| 40 | + return entity.Batch{}, fmt.Errorf("failed to get batch entity id=%s from the database: %w", id, err) |
| 41 | + } |
| 42 | + |
| 43 | + if err := json.Unmarshal(containsJSON, &batch.Contains); err != nil { |
| 44 | + return entity.Batch{}, fmt.Errorf("failed to unmarshal contains for batch entity id=%s from the database: %w", id, err) |
| 45 | + } |
| 46 | + |
| 47 | + if err := json.Unmarshal(dependenciesJSON, &batch.Dependencies); err != nil { |
| 48 | + return entity.Batch{}, fmt.Errorf("failed to unmarshal dependencies for batch entity id=%s from the database: %w", id, err) |
| 49 | + } |
| 50 | + |
| 51 | + return batch, nil |
| 52 | +} |
| 53 | + |
| 54 | +// Create creates a new batch. The batch must have a unique ID already assigned. Returns ErrAlreadyExists if the batch ID already exists. |
| 55 | +func (s *batchStore) Create(ctx context.Context, batch entity.Batch) error { |
| 56 | + containsJSON, err := json.Marshal(batch.Contains) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to marshal contains=%v id=%s for Create batch entity: %w", batch.Contains, batch.ID, err) |
| 59 | + } |
| 60 | + |
| 61 | + dependenciesJSON, err := json.Marshal(batch.Dependencies) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to marshal dependencies=%v id=%s for Create batch entity: %w", batch.Dependencies, batch.ID, err) |
| 64 | + } |
| 65 | + |
| 66 | + _, err = s.db.ExecContext(ctx, |
| 67 | + "INSERT INTO batch (id, queue, contains, dependencies, state, version) VALUES (?, ?, ?, ?, ?, ?)", |
| 68 | + batch.ID, batch.Queue, containsJSON, dependenciesJSON, batch.State, batch.Version, |
| 69 | + ) |
| 70 | + if err != nil { |
| 71 | + var mysqlErr *mysql.MySQLError |
| 72 | + if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { |
| 73 | + return fmt.Errorf("batch entity id=%s: %w", batch.ID, storage.ErrAlreadyExists) |
| 74 | + } |
| 75 | + return fmt.Errorf("failed to insert batch entity id=%s: %w", batch.ID, err) |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// UpdateState updates the state of a batch if the current version matches the expected version. If versions do not match, returns ErrVersionMismatch. |
| 82 | +// The implementation increments the version by 1 atomically with the state update. |
| 83 | +func (s *batchStore) UpdateState(ctx context.Context, id string, version int32, newState entity.BatchState) error { |
| 84 | + result, err := s.db.ExecContext(ctx, |
| 85 | + "UPDATE batch SET state = ?, version = version + 1 WHERE id = ? AND version = ?", |
| 86 | + newState, id, version, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf( |
| 90 | + "failed to update batch state for id=%q version=%d newState=%v: %w", |
| 91 | + id, version, newState, err, |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + rowsAffected, err := result.RowsAffected() |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf( |
| 98 | + "failed to get rows affected from update for id=%q version=%d newState=%v: %w", |
| 99 | + id, version, newState, err, |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + if rowsAffected != 1 { |
| 104 | + return fmt.Errorf( |
| 105 | + "version mismatch for batch update: id=%q expected_version=%d newState=%v: %w", |
| 106 | + id, version, newState, storage.ErrVersionMismatch, |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
0 commit comments