Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/ateapi/internal/controlapi/crash.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ import (

// maybeCrashActor inspects err returned by an atelet RPC, it crashes
// the actor if the err carries the actorCrashed=true metadata directive.
func maybeCrashActor(ctx context.Context, st store.Interface, atespace, actorID string, err error, wrapMsg string) error {
func maybeCrashActor(ctx context.Context, st store.Interface, atespace, actorName string, err error, wrapMsg string) error {
if err == nil {
return nil
}

if ateerrors.ActorCrashRequested(err) {
slog.ErrorContext(ctx, "Setting Actor to crashed due to error", slog.Any("error", err))
if cerr := crashActor(ctx, st, atespace, actorID); cerr != nil {
if cerr := crashActor(ctx, st, atespace, actorName); cerr != nil {
slog.ErrorContext(ctx, "Failed to crash actor", slog.Any("cerr", cerr))
return cerr
}
return status.Errorf(codes.DataLoss, "actor %s crashed", actorID)
return status.Errorf(codes.DataLoss, "actor %s crashed", actorName)
}
return fmt.Errorf("%s: %w", wrapMsg, err)
}

// crashActor moves the actor to CRASHED state.
func crashActor(ctx context.Context, st store.Interface, atespace, actorID string) error {
actor, err := st.GetActor(ctx, atespace, actorID)
func crashActor(ctx context.Context, st store.Interface, atespace, actorName string) error {
actor, err := st.GetActor(ctx, atespace, actorName)
if err != nil {
return fmt.Errorf("while loading actor to crash: %w", err)
}
Expand All @@ -59,7 +59,7 @@ func crashActor(ctx context.Context, st store.Interface, atespace, actorID strin
// we must preserve the Actor's assigned node VM in order
// to support `ate actor dump` command.
// (https://github.com/agent-substrate/substrate/issues/119)
if err := st.UpdateActor(ctx, actor, actor.GetVersion()); err != nil {
if _, err := st.UpdateActor(ctx, actor, actor.GetMetadata().GetVersion()); err != nil {
return fmt.Errorf("while marking actor crashed: %w", err)
}

Expand Down
39 changes: 19 additions & 20 deletions cmd/ateapi/internal/controlapi/crash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import (

// seedActor stores a running actor with all worker-binding fields populated, so
// tests can assert they are cleared when the actor crashes.
func seedActor(t *testing.T, ctx context.Context, st store.Interface, atespace, id string) {
func seedActor(t *testing.T, ctx context.Context, st store.Interface, atespace, actorName string) {
t.Helper()
if err := st.CreateActor(ctx, &ateapipb.Actor{
ActorId: id,
Atespace: atespace,
if _, err := st.CreateActor(ctx, &ateapipb.Actor{
Metadata: &ateapipb.ResourceMetadata{Name: actorName, Atespace: atespace},
Status: ateapipb.Actor_STATUS_RUNNING,
AteomPodNamespace: "ns",
AteomPodName: "pod",
Expand All @@ -47,11 +46,11 @@ func seedActor(t *testing.T, ctx context.Context, st store.Interface, atespace,
}

// assertCrashed reloads the actor and verifies it is CRASHED.
func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespace, id string) {
func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespace, actorName string) {
t.Helper()
got, err := st.GetActor(ctx, atespace, id)
got, err := st.GetActor(ctx, atespace, actorName)
if err != nil {
t.Fatalf("GetActor(%q, %q) = %v, want nil", atespace, id, err)
t.Fatalf("GetActor(%q, %q) = %v, want nil", atespace, actorName, err)
}
if got.GetStatus() != ateapipb.Actor_STATUS_CRASHED {
t.Errorf("status = %v, want %v", got.GetStatus(), ateapipb.Actor_STATUS_CRASHED)
Expand All @@ -60,8 +59,8 @@ func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespa

func TestCrashActor(t *testing.T) {
const (
atespace = "team-a"
actorID = "actor-1"
atespace = "team-a"
actorName = "actor-1"
)

tests := []struct {
Expand All @@ -77,7 +76,7 @@ func TestCrashActor(t *testing.T) {
if err != nil {
t.Fatalf("crashActor() = %v, want nil", err)
}
assertCrashed(t, ctx, st, atespace, actorID)
assertCrashed(t, ctx, st, atespace, actorName)
},
},
{
Expand All @@ -104,20 +103,20 @@ func TestCrashActor(t *testing.T) {
defer cleanup()

if tt.seed {
seedActor(t, ctx, st, atespace, actorID)
seedActor(t, ctx, st, atespace, actorName)
}

err := crashActor(ctx, st, atespace, actorID)
err := crashActor(ctx, st, atespace, actorName)
tt.check(t, ctx, st, err)
})
}
}

func TestMaybeCrashActor(t *testing.T) {
const (
atespace = "team-a"
actorID = "actor-1"
wrapMsg = "calling atelet"
atespace = "team-a"
actorName = "actor-1"
wrapMsg = "calling atelet"
)

crashErr := ateerrors.NewGRPCError(context.Background(), codes.NotFound, ateerrors.ReasonTerminalFileSystemError, ateerrors.ActorCrashedMetadata(), errors.New("boom"))
Expand Down Expand Up @@ -154,7 +153,7 @@ func TestMaybeCrashActor(t *testing.T) {
if got := status.Code(err); got != codes.DataLoss {
t.Errorf("status code = %v, want %v", got, codes.DataLoss)
}
assertCrashed(t, ctx, st, atespace, actorID)
assertCrashed(t, ctx, st, atespace, actorName)
},
},
{
Expand Down Expand Up @@ -188,7 +187,7 @@ func TestMaybeCrashActor(t *testing.T) {
t.Errorf("maybeCrashActor() error = %q, want prefix %q", err, wrapMsg)
}
// The actor must not have been crashed.
got, gerr := st.GetActor(ctx, atespace, actorID)
got, gerr := st.GetActor(ctx, atespace, actorName)
if gerr != nil {
t.Fatalf("GetActor() = %v, want nil", gerr)
}
Expand All @@ -212,7 +211,7 @@ func TestMaybeCrashActor(t *testing.T) {
t.Errorf("maybeCrashActor() error = %q, want prefix %q", err, wrapMsg)
}
// The actor must not have been crashed.
got, gerr := st.GetActor(ctx, atespace, actorID)
got, gerr := st.GetActor(ctx, atespace, actorName)
if gerr != nil {
t.Fatalf("GetActor() = %v, want nil", gerr)
}
Expand All @@ -230,10 +229,10 @@ func TestMaybeCrashActor(t *testing.T) {
defer cleanup()

if tt.seed {
seedActor(t, ctx, st, atespace, actorID)
seedActor(t, ctx, st, atespace, actorName)
}

err := maybeCrashActor(ctx, st, atespace, actorID, tt.err, wrapMsg)
err := maybeCrashActor(ctx, st, atespace, actorName, tt.err, wrapMsg)
tt.check(t, ctx, st, err)
})
}
Expand Down
20 changes: 8 additions & 12 deletions cmd/ateapi/internal/controlapi/create_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,27 @@ func (s *Service) CreateActor(ctx context.Context, req *ateapipb.CreateActorRequ
return nil, status.Errorf(codes.FailedPrecondition, "Atespace %s not found", req.GetActorRef().GetAtespace())
}

id := req.GetActorRef().GetName()
name := req.GetActorRef().GetName()
actor := &ateapipb.Actor{
ActorId: id,
Version: 1,
Metadata: &ateapipb.ResourceMetadata{
Atespace: req.GetActorRef().GetAtespace(),
Name: name,
},
Status: ateapipb.Actor_STATUS_SUSPENDED,
ActorTemplateNamespace: req.GetActorTemplateNamespace(),
ActorTemplateName: req.GetActorTemplateName(),
WorkerSelector: req.GetWorkerSelector(),
Atespace: req.GetActorRef().GetAtespace(),
}
err = s.persistence.CreateActor(ctx, actor)
stored, err := s.persistence.CreateActor(ctx, actor)
if err != nil {
if errors.Is(err, store.ErrAlreadyExists) {
return nil, status.Errorf(codes.AlreadyExists, "Actor %s already exists", id)
return nil, status.Errorf(codes.AlreadyExists, "Actor %s already exists", name)
}
return nil, fmt.Errorf("while recording actor: %w", err)
}

storedActor, err := s.persistence.GetActor(ctx, req.GetActorRef().GetAtespace(), id)
if err != nil {
return nil, fmt.Errorf("while fetching recorded actor from DB: %w", err)
}

return &ateapipb.CreateActorResponse{
Actor: storedActor,
Actor: stored,
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/ateapi/internal/controlapi/create_atespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ func (s *Service) CreateAtespace(ctx context.Context, req *ateapipb.CreateAtespa
return nil, err
}

atespace := &ateapipb.Atespace{Name: req.GetName()}
if err := s.persistence.CreateAtespace(ctx, atespace); err != nil {
atespace := &ateapipb.Atespace{
Metadata: &ateapipb.ResourceMetadata{Name: req.GetName()},
}
stored, err := s.persistence.CreateAtespace(ctx, atespace)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: storedAtespace, like storedActor in create_actor.go.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or both use stored as I saw in update_actor.go there's just updated.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if err != nil {
if errors.Is(err, store.ErrAlreadyExists) {
return nil, status.Errorf(codes.AlreadyExists, "Atespace %s already exists", req.GetName())
}
return nil, fmt.Errorf("while recording atespace: %w", err)
}

return &ateapipb.CreateAtespaceResponse{Atespace: atespace}, nil
return &ateapipb.CreateAtespaceResponse{Atespace: stored}, nil
}

func validateCreateAtespaceRequest(req *ateapipb.CreateAtespaceRequest) error {
Expand Down
Loading
Loading