-
Notifications
You must be signed in to change notification settings - Fork 138
Add CheckPrerequisite to WorkflowStep to validate state machine edges #374
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
1795b42
4104a7d
c968955
6d976cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/agent-substrate/substrate/cmd/ateapi/internal/store/storetest" | ||
| "github.com/agent-substrate/substrate/pkg/proto/ateapipb" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // TestPauseActorWorkflow exercises the pause workflow end-to-end against | ||
| // seeded actor statuses, covering both the rejected and the idempotent-success | ||
| // paths. The atelet dialer is nil, so any step that unexpectedly reaches it | ||
| // panics. | ||
| func TestPauseActorWorkflow(t *testing.T) { | ||
|
zoez7 marked this conversation as resolved.
Outdated
|
||
| tests := []struct { | ||
| name string | ||
| seedStatus ateapipb.Actor_Status | ||
| // wantErr true means PauseActor must fail with FailedPrecondition. | ||
| wantErr bool | ||
| // wantStatus is the stored status after the call. | ||
| wantStatus ateapipb.Actor_Status | ||
| }{ | ||
| { | ||
| // Pausing a SUSPENDED actor is rejected by MarkPausingStep's | ||
| // CheckPrerequisite and the actor's status is left untouched. | ||
| name: "not running rejected", | ||
| seedStatus: ateapipb.Actor_STATUS_SUSPENDED, | ||
| wantErr: true, | ||
| wantStatus: ateapipb.Actor_STATUS_SUSPENDED, | ||
| }, | ||
| { | ||
| // Pausing a PAUSED actor succeeds idempotently via IsComplete | ||
| // fast-forward without calling atelet. | ||
| name: "already paused succeeds", | ||
| seedStatus: ateapipb.Actor_STATUS_PAUSED, | ||
| wantStatus: ateapipb.Actor_STATUS_PAUSED, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| st, cleanup := storetest.SetupTestStore(t) | ||
| defer cleanup() | ||
| w := newTestActorWorkflow(t, st, "ns", "tmpl1") | ||
|
|
||
| seedWorkflowActor(t, ctx, st, "team-a", "id1", "ns", "tmpl1", tc.seedStatus) | ||
|
|
||
| actor, err := w.PauseActor(ctx, "team-a", "id1") | ||
| if tc.wantErr { | ||
| if got := status.Code(err); got != codes.FailedPrecondition { | ||
| t.Fatalf("status.Code(err) = %v, want %v (err: %v)", got, codes.FailedPrecondition, err) | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Fatalf("PauseActor failed: %v", err) | ||
| } | ||
| if actor.GetStatus() != tc.wantStatus { | ||
| t.Errorf("returned status = %v, want %v", actor.GetStatus(), tc.wantStatus) | ||
| } | ||
| } | ||
|
|
||
| got, err := st.GetActor(ctx, "team-a", "id1") | ||
| if err != nil { | ||
| t.Fatalf("GetActor failed: %v", err) | ||
| } | ||
| if got.GetStatus() != tc.wantStatus { | ||
| t.Errorf("stored status = %v, want %v", got.GetStatus(), tc.wantStatus) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestPauseSteps_CheckPrerequisite verifies each pause step's CheckPrerequisite | ||
| // against every actor status: nil for the step's allowed statuses, | ||
| // FailedPrecondition for all others. | ||
| func TestPauseSteps_CheckPrerequisite(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| step WorkflowStep[*PauseInput, *PauseState] | ||
| // allowed lists the statuses CheckPrerequisite accepts; nil means | ||
| // every status is accepted. | ||
| allowed map[ateapipb.Actor_Status]bool | ||
| }{ | ||
| { | ||
| // Loading has no prerequisite: it is allowed from every status. | ||
| name: "LoadActorForPauseStep", | ||
| step: &LoadActorForPauseStep{}, | ||
| allowed: nil, | ||
| }, | ||
| { | ||
| // Pausing is allowed only from RUNNING. | ||
| name: "MarkPausingStep", | ||
| step: &MarkPausingStep{}, | ||
| allowed: map[ateapipb.Actor_Status]bool{ | ||
| ateapipb.Actor_STATUS_RUNNING: true, | ||
| }, | ||
| }, | ||
| { | ||
| // The checkpoint call is allowed only from PAUSING (PAUSED is | ||
| // fast-forwarded by IsComplete). | ||
| name: "CallAteletPauseStep", | ||
| step: &CallAteletPauseStep{}, | ||
| allowed: map[ateapipb.Actor_Status]bool{ | ||
| ateapipb.Actor_STATUS_PAUSING: true, | ||
| }, | ||
| }, | ||
| { | ||
| // Finalizing is allowed only from PAUSING: a persisted PAUSED | ||
| // actor always has its worker pod fields cleared and is | ||
| // fast-forwarded by IsComplete. | ||
| name: "FinalizePausedStep", | ||
| step: &FinalizePausedStep{}, | ||
| allowed: map[ateapipb.Actor_Status]bool{ | ||
| ateapipb.Actor_STATUS_PAUSING: true, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| for _, st := range allActorStatuses { | ||
| err := tc.step.CheckPrerequisite(ctx, &PauseInput{ActorName: "id1"}, &PauseState{Actor: &ateapipb.Actor{Status: st}}) | ||
| assertPrerequisiteResult(t, st, err, tc.allowed == nil || tc.allowed[st]) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,9 @@ func (s *LoadActorForResumeStep) IsComplete(ctx context.Context, input *ResumeIn | |
| // Always run this step to get the latest state from the DB | ||
| return false, nil | ||
| } | ||
| func (s *LoadActorForResumeStep) CheckPrerequisite(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| return nil | ||
| } | ||
| func (s *LoadActorForResumeStep) Execute(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| actor, err := s.store.GetActor(ctx, input.Atespace, input.ActorName) | ||
| if err != nil { | ||
|
|
@@ -113,8 +116,22 @@ type AssignWorkerStep struct { | |
| func (s *AssignWorkerStep) Name() string { return "AssignWorker" } | ||
|
|
||
| func (s *AssignWorkerStep) IsComplete(ctx context.Context, input *ResumeInput, state *ResumeState) (bool, error) { | ||
| // Only RUNNING is past this step. RESUMING intentionally re-runs because | ||
| // a retry must be able to release a stale worker whose pool became | ||
| // ineligible and pick a fresh one. | ||
| return state.Actor.GetStatus() == ateapipb.Actor_STATUS_RUNNING, nil | ||
| } | ||
| func (s *AssignWorkerStep) CheckPrerequisite(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| // The resume edge exists from SUSPENDED and PAUSED. | ||
| // RESUMING is allowed for retrying this step. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because of line 168 below.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please clarify what is special at line 168? Is it for cases it was an error in resuming process and we are OK to try resuming one more time? Same case might happening with Pausing and the CheckPrerequisite for CallAteletPauseStep does not accept pausing state.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the logic here and stopped allowing resuming state to reenter the AssignWorkerStep. Because setting the state to Resuming is the last step of AssignWorkerStep, if the actor is already in resuming, we know it has completed all the steps and don't need to retry. On line 168, we would check which worker is assigned and if the worker is no longer eligible, we will pick another one, but this is for cleaning up and doesn't have to be retried during this operation. |
||
| switch state.Actor.GetStatus() { | ||
| case ateapipb.Actor_STATUS_SUSPENDED, ateapipb.Actor_STATUS_PAUSED, ateapipb.Actor_STATUS_RESUMING: | ||
| return nil | ||
| default: | ||
| return status.Errorf(codes.FailedPrecondition, "AssignWorkerStep prerequisite not met for Actor: %s (got: %v, want %s, %s or %s)", input.ActorName, state.Actor.GetStatus(), ateapipb.Actor_STATUS_SUSPENDED, ateapipb.Actor_STATUS_PAUSED, ateapipb.Actor_STATUS_RESUMING) | ||
| } | ||
| } | ||
|
|
||
| func (s *AssignWorkerStep) Execute(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| workers, err := s.workerCache.Workers() | ||
| if err != nil { | ||
|
|
@@ -254,6 +271,12 @@ func (s *CallAteletRestoreStep) Name() string { return "CallAteletRestore" } | |
| func (s *CallAteletRestoreStep) IsComplete(ctx context.Context, input *ResumeInput, state *ResumeState) (bool, error) { | ||
| return state.Actor.GetStatus() == ateapipb.Actor_STATUS_RUNNING, nil | ||
| } | ||
| func (s *CallAteletRestoreStep) CheckPrerequisite(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added more checks in CheckPrerequisite to make sure that, if we retry ResumeActor, the Worker assignment and eligibility are still correct for the actor. |
||
| if state.Actor.GetStatus() != ateapipb.Actor_STATUS_RESUMING { | ||
| return status.Errorf(codes.FailedPrecondition, "CallAteletRestoreStep prerequisite not met for Actor: %s (got: %v, want %s)", input.ActorName, state.Actor.GetStatus(), ateapipb.Actor_STATUS_RESUMING) | ||
| } | ||
| return nil | ||
| } | ||
| func (s *CallAteletRestoreStep) Execute(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| ateletConn, err := s.dialer.DialForWorker(state.Actor.GetAteomPodNamespace(), state.Actor.GetAteomPodName()) | ||
| if err != nil { | ||
|
|
@@ -358,6 +381,12 @@ func (s *FinalizeRunningStep) Name() string { return "FinalizeRunning" } | |
| func (s *FinalizeRunningStep) IsComplete(ctx context.Context, input *ResumeInput, state *ResumeState) (bool, error) { | ||
| return state.Actor.GetStatus() == ateapipb.Actor_STATUS_RUNNING, nil | ||
| } | ||
| func (s *FinalizeRunningStep) CheckPrerequisite(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| if state.Actor.GetStatus() != ateapipb.Actor_STATUS_RESUMING { | ||
| return status.Errorf(codes.FailedPrecondition, "FinalizeRunningStep prerequisite not met for Actor: %s (got: %v, want %s)", input.ActorName, state.Actor.GetStatus(), ateapipb.Actor_STATUS_RESUMING) | ||
| } | ||
| return nil | ||
| } | ||
| func (s *FinalizeRunningStep) Execute(ctx context.Context, input *ResumeInput, state *ResumeState) error { | ||
| latestActor, err := s.store.GetActor(ctx, input.Atespace, input.ActorName) | ||
| if err != nil { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
this is a pre-requisite too, however function in current implementation does not support transition to crashActor.
Might be rename the "CheckPrerequisite" to different name? might be allow transition to crash? or might be from the beginning we were not supposed to be in this state, that actor does not have pod or namespace?
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 should crash the actor here. We can only get here if MarkPausing has succeeded, not having the ateom pod or namespace should crash the actor. I changed the implementation.