-
Notifications
You must be signed in to change notification settings - Fork 4
Convert build process to saga framework #842
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
Open
phinze
wants to merge
2
commits into
main
Choose a base branch
from
phinze/mir-441-build-process-saga
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,169 @@ | ||
| package saga | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "miren.dev/runtime/pkg/entity/testutils" | ||
| ) | ||
|
|
||
| // storageFactory builds a fresh, empty Storage for one test. It registers any | ||
| // teardown via t.Cleanup so callers get a clean backend per subtest. | ||
| type storageFactory struct { | ||
| name string | ||
| make func(t *testing.T) Storage | ||
| } | ||
|
|
||
| // allStorageBackends returns every production Storage implementation behind a | ||
| // uniform factory. The whole point of this suite is that every backend must | ||
| // satisfy the same contract: the bug in MIR-441 lived in two of these three | ||
| // (EntityStorage and EACStorage both used create-if-absent semantics and | ||
| // silently dropped every save after the first), while MemoryStorage was | ||
| // correct, so memory-backed unit tests stayed green while production froze | ||
| // every saga at its initial pending state. Running the same scenarios against | ||
| // all backends is what closes that gap. | ||
| // | ||
| // This suite tests a different question than pkg/entity's Store conformance | ||
| // suite, and the two are complementary. The entity suite proves MockStore | ||
| // behaves like the real EtcdStore on the underlying write primitives. This | ||
| // suite proves saga storage *uses* those primitives correctly (the MIR-441 bug | ||
| // was a create-only call where an upsert was needed, which no amount of | ||
| // store-level testing would catch). Because the entity suite vouches for the | ||
| // mock, the EntityStorage backend here is mock-backed on purpose and we do not | ||
| // add a real-etcd backend; the EACStorage backend additionally exercises the | ||
| // EntityAccessClient RPC path, which the entity suite does not reach. | ||
| func allStorageBackends() []storageFactory { | ||
| return []storageFactory{ | ||
| { | ||
| name: "MemoryStorage", | ||
| make: func(t *testing.T) Storage { | ||
| return NewMemoryStorage() | ||
| }, | ||
| }, | ||
| { | ||
| name: "EntityStorage", | ||
| make: func(t *testing.T) Storage { | ||
| inmem, cleanup := testutils.NewInMemEntityServer(t) | ||
| t.Cleanup(cleanup) | ||
| return NewEntityStorage(inmem.Store, testutils.TestLogger(t)) | ||
| }, | ||
| }, | ||
| { | ||
| name: "EACStorage", | ||
| make: func(t *testing.T) Storage { | ||
| inmem, cleanup := testutils.NewInMemEntityServer(t) | ||
| t.Cleanup(cleanup) | ||
| return NewEACStorage(inmem.EAC, testutils.TestLogger(t)) | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // TestStorageConformance_SaveUpdatesExistingExecution is the direct regression | ||
| // for the EnsureEntity/Ensure create-if-absent bug. A saga is saved repeatedly | ||
| // as it progresses (pending -> running -> completed, accumulating action | ||
| // results). Every save after the first must overwrite the stored state. The | ||
| // buggy backends dropped these updates, so Get returned a pending execution | ||
| // with no recorded actions even though the saga had completed. | ||
| func TestStorageConformance_SaveUpdatesExistingExecution(t *testing.T) { | ||
| for _, backend := range allStorageBackends() { | ||
| t.Run(backend.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| storage := backend.make(t) | ||
|
|
||
| exec := &Execution{ | ||
| ID: "build-from-prepared-abc123", | ||
| DefinitionName: "build-from-tar", | ||
| Status: StatusPending, | ||
| InitialInputs: map[string]any{"app_name": "demo"}, | ||
| ExecutedActions: map[string]*ActionResult{}, | ||
| ExecutionOrder: []string{}, | ||
| } | ||
|
|
||
| // First save: creates the entity. | ||
| require.NoError(t, storage.Save(ctx, exec)) | ||
|
|
||
| // Progress the saga and save again. Under the old create-if-absent | ||
| // behavior this save was a silent no-op. | ||
| exec.Status = StatusRunning | ||
| exec.ExecutedActions["receive-tar"] = &ActionResult{ | ||
| Output: []byte(`{"source_dir":"/tmp/build"}`), | ||
| ExecutedAt: time.Unix(0, 0), | ||
| } | ||
| exec.ExecutionOrder = []string{"receive-tar"} | ||
| require.NoError(t, storage.Save(ctx, exec)) | ||
|
|
||
| // Complete the saga and save a final time. | ||
| exec.Status = StatusCompleted | ||
| exec.ExecutedActions["create-version"] = &ActionResult{ | ||
| Output: []byte(`{"version_name":"demo-v1"}`), | ||
| ExecutedAt: time.Unix(0, 0), | ||
| } | ||
| exec.ExecutionOrder = []string{"receive-tar", "create-version"} | ||
| require.NoError(t, storage.Save(ctx, exec)) | ||
|
|
||
| got, err := storage.Get(ctx, exec.ID) | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, StatusCompleted, got.Status, | ||
| "final status must persist; a stuck pending status means saves after the first were dropped") | ||
| assert.Len(t, got.ExecutedActions, 2, | ||
| "executed actions recorded across saves must all persist") | ||
| assert.Equal(t, []string{"receive-tar", "create-version"}, got.ExecutionOrder) | ||
|
|
||
| if action, ok := got.ExecutedActions["create-version"]; assert.True(t, ok, "create-version action must persist") { | ||
| assert.JSONEq(t, `{"version_name":"demo-v1"}`, string(action.Output)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestStorageConformance_CompletedExecutionLeavesIncompleteList verifies that a | ||
| // saga which has reached a terminal status no longer shows up in | ||
| // ListIncomplete. This is the operational consequence of the same bug: when the | ||
| // completed save was dropped, the execution stayed pending in storage forever, | ||
| // so every process restart re-ran an already-finished saga during recovery. | ||
| func TestStorageConformance_CompletedExecutionLeavesIncompleteList(t *testing.T) { | ||
| for _, backend := range allStorageBackends() { | ||
| t.Run(backend.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| storage := backend.make(t) | ||
|
|
||
| exec := &Execution{ | ||
| ID: "saga-incomplete-check", | ||
| DefinitionName: "build-from-tar", | ||
| Status: StatusPending, | ||
| InitialInputs: map[string]any{}, | ||
| ExecutedActions: map[string]*ActionResult{}, | ||
| ExecutionOrder: []string{}, | ||
| } | ||
| require.NoError(t, storage.Save(ctx, exec)) | ||
|
|
||
| incomplete, err := storage.ListIncomplete(ctx) | ||
| require.NoError(t, err) | ||
| assert.True(t, containsExecution(incomplete, exec.ID), | ||
| "a pending saga must appear in ListIncomplete") | ||
|
|
||
| exec.Status = StatusCompleted | ||
| require.NoError(t, storage.Save(ctx, exec)) | ||
|
|
||
| incomplete, err = storage.ListIncomplete(ctx) | ||
| require.NoError(t, err) | ||
| assert.False(t, containsExecution(incomplete, exec.ID), | ||
| "a completed saga must NOT appear in ListIncomplete; if it does, recovery will re-run finished work") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func containsExecution(execs []*Execution, id string) bool { | ||
| for _, e := range execs { | ||
| if e != nil && e.ID == id { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We picked this up in another PR already I'm pretty sure.