-
Notifications
You must be signed in to change notification settings - Fork 0
feat(storage) Adds batch store interface and mysql implementation #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| ) | ||
|
|
||
| // BatchStore is an interface that defines methods for managing batches in the database. | ||
| type BatchStore interface { | ||
| // Get retrieves a batch by ID. Returns ErrNotFound if the batch is not found. | ||
| Get(ctx context.Context, id string) (entity.Batch, error) | ||
|
|
||
| // Create creates a new batch. The batch must have a unique ID already assigned. | ||
| // Returns ErrAlreadyExists if a batch with the same ID already exists. | ||
| Create(ctx context.Context, batch entity.Batch) error | ||
|
|
||
| // UpdateState updates the state of a batch if the current version matches the expected version. If versions do not match, returns ErrVersionMismatch. | ||
| // The implementation should increment the version by 1 atomically with the state update. | ||
| UpdateState(ctx context.Context, id string, version int32, newState entity.BatchState) error | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| "github.com/uber/submitqueue/extension/storage" | ||
| ) | ||
|
|
||
| type batchStore struct { | ||
| db *sql.DB | ||
| } | ||
|
|
||
| // NewBatchStore creates a new MySQL-backed BatchStore. | ||
| func NewBatchStore(db *sql.DB) storage.BatchStore { | ||
| return &batchStore{db: db} | ||
| } | ||
|
|
||
| // Get retrieves a batch by ID. Returns ErrNotFound if the batch is not found. | ||
| func (s *batchStore) Get(ctx context.Context, id string) (entity.Batch, error) { | ||
| var batch entity.Batch | ||
| var containsJSON []byte | ||
| var dependenciesJSON []byte | ||
|
|
||
| err := s.db.QueryRowContext(ctx, | ||
| "SELECT id, queue, contains, dependencies, state, version FROM batch WHERE id = ?", | ||
| id, | ||
| ).Scan(&batch.ID, &batch.Queue, &containsJSON, &dependenciesJSON, &batch.State, &batch.Version) | ||
|
|
||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return entity.Batch{}, storage.WrapNotFound(err) | ||
| } | ||
| if err != nil { | ||
| return entity.Batch{}, fmt.Errorf("failed to get batch entity id=%s from the database: %w", id, err) | ||
| } | ||
|
|
||
| if err := json.Unmarshal(containsJSON, &batch.Contains); err != nil { | ||
| return entity.Batch{}, fmt.Errorf("failed to unmarshal contains for batch entity id=%s from the database: %w", id, err) | ||
| } | ||
|
|
||
| if err := json.Unmarshal(dependenciesJSON, &batch.Dependencies); err != nil { | ||
| return entity.Batch{}, fmt.Errorf("failed to unmarshal dependencies for batch entity id=%s from the database: %w", id, err) | ||
| } | ||
|
|
||
| return batch, nil | ||
| } | ||
|
|
||
| // Create creates a new batch. The batch must have a unique ID already assigned. Returns ErrAlreadyExists if the batch ID already exists. | ||
| func (s *batchStore) Create(ctx context.Context, batch entity.Batch) error { | ||
| containsJSON, err := json.Marshal(batch.Contains) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal contains=%v id=%s for Create batch entity: %w", batch.Contains, batch.ID, err) | ||
| } | ||
|
|
||
| dependenciesJSON, err := json.Marshal(batch.Dependencies) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal dependencies=%v id=%s for Create batch entity: %w", batch.Dependencies, batch.ID, err) | ||
| } | ||
|
|
||
| _, err = s.db.ExecContext(ctx, | ||
| "INSERT INTO batch (id, queue, contains, dependencies, state, version) VALUES (?, ?, ?, ?, ?, ?)", | ||
| batch.ID, batch.Queue, containsJSON, dependenciesJSON, batch.State, batch.Version, | ||
| ) | ||
| if err != nil { | ||
| var mysqlErr *mysql.MySQLError | ||
| if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { | ||
| return fmt.Errorf("batch entity id=%s: %w", batch.ID, storage.ErrAlreadyExists) | ||
| } | ||
| return fmt.Errorf("failed to insert batch entity id=%s: %w", batch.ID, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // UpdateState updates the state of a batch if the current version matches the expected version. If versions do not match, returns ErrVersionMismatch. | ||
| // The implementation increments the version by 1 atomically with the state update. | ||
| func (s *batchStore) UpdateState(ctx context.Context, id string, version int32, newState entity.BatchState) error { | ||
| result, err := s.db.ExecContext(ctx, | ||
| "UPDATE batch SET state = ?, version = version + 1 WHERE id = ? AND version = ?", | ||
| newState, id, version, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf( | ||
| "failed to update batch state for id=%q version=%d newState=%v: %w", | ||
| id, version, newState, err, | ||
| ) | ||
| } | ||
|
|
||
| rowsAffected, err := result.RowsAffected() | ||
| if err != nil { | ||
| return fmt.Errorf( | ||
| "failed to get rows affected from update for id=%q version=%d newState=%v: %w", | ||
| id, version, newState, err, | ||
| ) | ||
| } | ||
|
|
||
| if rowsAffected != 1 { | ||
| return fmt.Errorf( | ||
| "version mismatch for batch update: id=%q expected_version=%d newState=%v: %w", | ||
| id, version, newState, storage.ErrVersionMismatch, | ||
| ) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.