Files
substrate/cmd/ateapi/internal/controlapi/workflow.go
T
Troy Chiu 07da819da1 controlapi: refactor workflows to typed ensure-steps (#797)
Fixes #554 

Each workflow becomes a plain method orchestrating typed, idempotent
ensureX steps, e.g.:

```go
func (w *ActorWorkflow) ResumeActor(ctx, ref, boot) (*Actor, bool, error) {
    // acquire actor lock, metrics defer — unchanged
    actor, tmpl, snapSrc, err := w.loadActorForResume(ctx, ref, boot)
    if actor.Status == RUNNING { return actor, false, nil }        // explicit no-op
    actor, err         = w.ensureVolumesCreated(ctx, actor, tmpl)
    actor, worker, err = w.ensureWorkerAssigned(ctx, actor, tmpl)
    err                = w.ensureVolumesAttached(ctx, actor, worker, tmpl)
    tele, err          = w.ensureAteletRestored(ctx, actor, worker, tmpl, snapSrc)
    actor, err         = w.finalizeRunning(ctx, ref)
    return actor, true, nil
}
```
Each `ensureX` 
- early-returns if its postcondition already holds, which is derived
from persisted state only, replacing `IsComplete`
- validates the state machine edge internally, replacing
`CheckPrerequisite`
- takes explicit typed inputs and returns what it produced.
- persists what it changed before returning, returning the stored copy
so the optimistic lock version threads forward. No shared
`*ResumeState`.

There is one behavioral change: worker ownership/eligibility validation
for a re-entered resume now runs before volumes are attached (it used to
run in `CallAteletRestoreStep`'s prerequisite, after attach), so a
resume that is about to crash on a reassigned/ineligible worker no
longer attaches volumes to it first.

- [X] Tests pass
- [ ] Appropriate changes to documentation are included in the PR
2026-08-10 14:19:43 -04:00

130 lines
4.9 KiB
Go

// 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"
"errors"
"fmt"
"github.com/agent-substrate/substrate/cmd/ateapi/internal/scheduling"
"github.com/agent-substrate/substrate/cmd/ateapi/internal/store"
"github.com/agent-substrate/substrate/cmd/ateapi/internal/workercache"
"github.com/agent-substrate/substrate/internal/resources"
listersv1alpha1 "github.com/agent-substrate/substrate/pkg/client/listers/api/v1alpha1"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
grpcCodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/client-go/kubernetes"
storagev1listers "k8s.io/client-go/listers/storage/v1"
)
// stepSpan opens the per-step trace span ("step.<name>" on the controlapi
// tracer) and returns the step context plus a finish func the step defers:
// it records a non-nil error on the span and wraps it with the step name.
//
// Workflow steps follow the ensure pattern: each step derives whether its
// work is already done from persisted state alone (calling markSkipped when
// so), validates the state-machine edge it is about to take, and persists
// what it changed before returning — so a re-entered workflow fast-forwards
// to wherever the previous attempt stopped.
func stepSpan(ctx context.Context, name string) (context.Context, func(error) error) {
ctx, span := otel.Tracer("controlapi").Start(ctx, "step."+name)
return ctx, func(err error) error {
defer span.End()
if err == nil {
return nil
}
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return fmt.Errorf("workflow failed at step %s: %w", name, err)
}
}
// markSkipped annotates the current step's span when its postcondition
// already holds, so a re-entered workflow's trace shows which steps
// fast-forwarded and where real work restarted.
func markSkipped(ctx context.Context, reason string) {
trace.SpanFromContext(ctx).SetAttributes(
attribute.Bool("step.skipped", true),
attribute.String("step.skip_reason", reason),
)
}
// ActorWorkflow handles the workflows for actor's resume / suspend operations.
type ActorWorkflow struct {
store store.Interface
workerCache *workercache.Cache
scheduler scheduling.Scheduler
dialer *AteletDialer
actorTemplateLister listersv1alpha1.ActorTemplateLister
workerPoolLister listersv1alpha1.WorkerPoolLister
sandboxConfigLister listersv1alpha1.SandboxConfigLister
storageClassLister storagev1listers.StorageClassLister
kubeClient kubernetes.Interface
secretCache *envSecretCache
instruments *Instruments
egressGatewayAddress string
pluginRegistry VolumePluginRegistry
}
// NewActorWorkflow creates a new ActorWorkflow. instruments may be nil.
func NewActorWorkflow(
store store.Interface,
workerCache *workercache.Cache,
dialer *AteletDialer,
actorTemplateLister listersv1alpha1.ActorTemplateLister,
workerPoolLister listersv1alpha1.WorkerPoolLister,
sandboxConfigLister listersv1alpha1.SandboxConfigLister,
storageClassLister storagev1listers.StorageClassLister,
kubeClient kubernetes.Interface,
instruments *Instruments,
egressGatewayAddress string,
pluginRegistry VolumePluginRegistry,
) *ActorWorkflow {
return &ActorWorkflow{
store: store,
workerCache: workerCache,
scheduler: scheduling.New(workerCache, scheduling.WithMeter(otel.Meter("ateapi"))),
dialer: dialer,
actorTemplateLister: actorTemplateLister,
workerPoolLister: workerPoolLister,
sandboxConfigLister: sandboxConfigLister,
storageClassLister: storageClassLister,
kubeClient: kubeClient,
secretCache: newEnvSecretCache(envSecretCacheTTL),
instruments: instruments,
egressGatewayAddress: egressGatewayAddress,
pluginRegistry: pluginRegistry,
}
}
func (w *ActorWorkflow) acquireActorLock(ctx context.Context, actorRef resources.ActorRef) (context.Context, *store.Lock, error) {
lockKey := "lock:actor:" + actorRef.Atespace + ":" + actorRef.Name
lock, err := w.store.AcquireLock(ctx, lockKey)
if err != nil {
if errors.Is(err, store.ErrLockConflict) {
return nil, nil, status.Error(grpcCodes.Aborted, "another operation is in progress for this actor")
}
return nil, nil, fmt.Errorf("while acquiring lock: %w", err)
}
return lock.Context(), lock, nil
}