Skip to content

Commit af1f005

Browse files
juli4nHavenXia
authored andcommitted
Add ResourceMetadata to both actor and atespace resources.
Replace actor_id and atespace name with `metadata` field which holds common fields. This commit only changes the ate-apiserver to support this field, but intentionally skips upstream / downstream systems (such as kubectl-ate, atenet, atelet, etc) to keep the scope manageable. This will come in subsequent PRs.
1 parent d858ef8 commit af1f005

23 files changed

Lines changed: 930 additions & 692 deletions

File tree

cmd/ateapi/internal/controlapi/crash.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ import (
2828

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

3636
if ateerrors.ActorCrashRequested(err) {
3737
slog.ErrorContext(ctx, "Setting Actor to crashed due to error", slog.Any("error", err))
38-
if cerr := crashActor(ctx, st, atespace, actorID); cerr != nil {
38+
if cerr := crashActor(ctx, st, atespace, actorName); cerr != nil {
3939
slog.ErrorContext(ctx, "Failed to crash actor", slog.Any("cerr", cerr))
4040
return cerr
4141
}
42-
return status.Errorf(codes.DataLoss, "actor %s crashed", actorID)
42+
return status.Errorf(codes.DataLoss, "actor %s crashed", actorName)
4343
}
4444
return fmt.Errorf("%s: %w", wrapMsg, err)
4545
}
4646

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

cmd/ateapi/internal/controlapi/crash_test.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ import (
3030

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

4948
// assertCrashed reloads the actor and verifies it is CRASHED.
50-
func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespace, id string) {
49+
func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespace, actorName string) {
5150
t.Helper()
52-
got, err := st.GetActor(ctx, atespace, id)
51+
got, err := st.GetActor(ctx, atespace, actorName)
5352
if err != nil {
54-
t.Fatalf("GetActor(%q, %q) = %v, want nil", atespace, id, err)
53+
t.Fatalf("GetActor(%q, %q) = %v, want nil", atespace, actorName, err)
5554
}
5655
if got.GetStatus() != ateapipb.Actor_STATUS_CRASHED {
5756
t.Errorf("status = %v, want %v", got.GetStatus(), ateapipb.Actor_STATUS_CRASHED)
@@ -60,8 +59,8 @@ func assertCrashed(t *testing.T, ctx context.Context, st store.Interface, atespa
6059

6160
func TestCrashActor(t *testing.T) {
6261
const (
63-
atespace = "team-a"
64-
actorID = "actor-1"
62+
atespace = "team-a"
63+
actorName = "actor-1"
6564
)
6665

6766
tests := []struct {
@@ -77,7 +76,7 @@ func TestCrashActor(t *testing.T) {
7776
if err != nil {
7877
t.Fatalf("crashActor() = %v, want nil", err)
7978
}
80-
assertCrashed(t, ctx, st, atespace, actorID)
79+
assertCrashed(t, ctx, st, atespace, actorName)
8180
},
8281
},
8382
{
@@ -104,20 +103,20 @@ func TestCrashActor(t *testing.T) {
104103
defer cleanup()
105104

106105
if tt.seed {
107-
seedActor(t, ctx, st, atespace, actorID)
106+
seedActor(t, ctx, st, atespace, actorName)
108107
}
109108

110-
err := crashActor(ctx, st, atespace, actorID)
109+
err := crashActor(ctx, st, atespace, actorName)
111110
tt.check(t, ctx, st, err)
112111
})
113112
}
114113
}
115114

116115
func TestMaybeCrashActor(t *testing.T) {
117116
const (
118-
atespace = "team-a"
119-
actorID = "actor-1"
120-
wrapMsg = "calling atelet"
117+
atespace = "team-a"
118+
actorName = "actor-1"
119+
wrapMsg = "calling atelet"
121120
)
122121

123122
crashErr := ateerrors.NewGRPCError(context.Background(), codes.NotFound, ateerrors.ReasonTerminalFileSystemError, ateerrors.ActorCrashedMetadata(), errors.New("boom"))
@@ -154,7 +153,7 @@ func TestMaybeCrashActor(t *testing.T) {
154153
if got := status.Code(err); got != codes.DataLoss {
155154
t.Errorf("status code = %v, want %v", got, codes.DataLoss)
156155
}
157-
assertCrashed(t, ctx, st, atespace, actorID)
156+
assertCrashed(t, ctx, st, atespace, actorName)
158157
},
159158
},
160159
{
@@ -188,7 +187,7 @@ func TestMaybeCrashActor(t *testing.T) {
188187
t.Errorf("maybeCrashActor() error = %q, want prefix %q", err, wrapMsg)
189188
}
190189
// The actor must not have been crashed.
191-
got, gerr := st.GetActor(ctx, atespace, actorID)
190+
got, gerr := st.GetActor(ctx, atespace, actorName)
192191
if gerr != nil {
193192
t.Fatalf("GetActor() = %v, want nil", gerr)
194193
}
@@ -212,7 +211,7 @@ func TestMaybeCrashActor(t *testing.T) {
212211
t.Errorf("maybeCrashActor() error = %q, want prefix %q", err, wrapMsg)
213212
}
214213
// The actor must not have been crashed.
215-
got, gerr := st.GetActor(ctx, atespace, actorID)
214+
got, gerr := st.GetActor(ctx, atespace, actorName)
216215
if gerr != nil {
217216
t.Fatalf("GetActor() = %v, want nil", gerr)
218217
}
@@ -230,10 +229,10 @@ func TestMaybeCrashActor(t *testing.T) {
230229
defer cleanup()
231230

232231
if tt.seed {
233-
seedActor(t, ctx, st, atespace, actorID)
232+
seedActor(t, ctx, st, atespace, actorName)
234233
}
235234

236-
err := maybeCrashActor(ctx, st, atespace, actorID, tt.err, wrapMsg)
235+
err := maybeCrashActor(ctx, st, atespace, actorName, tt.err, wrapMsg)
237236
tt.check(t, ctx, st, err)
238237
})
239238
}

cmd/ateapi/internal/controlapi/create_actor.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,27 @@ func (s *Service) CreateActor(ctx context.Context, req *ateapipb.CreateActorRequ
5050
return nil, status.Errorf(codes.FailedPrecondition, "Atespace %s not found", req.GetActorRef().GetAtespace())
5151
}
5252

53-
id := req.GetActorRef().GetName()
53+
name := req.GetActorRef().GetName()
5454
actor := &ateapipb.Actor{
55-
ActorId: id,
56-
Version: 1,
55+
Metadata: &ateapipb.ResourceMetadata{
56+
Atespace: req.GetActorRef().GetAtespace(),
57+
Name: name,
58+
},
5759
Status: ateapipb.Actor_STATUS_SUSPENDED,
5860
ActorTemplateNamespace: req.GetActorTemplateNamespace(),
5961
ActorTemplateName: req.GetActorTemplateName(),
6062
WorkerSelector: req.GetWorkerSelector(),
61-
Atespace: req.GetActorRef().GetAtespace(),
6263
}
63-
err = s.persistence.CreateActor(ctx, actor)
64+
stored, err := s.persistence.CreateActor(ctx, actor)
6465
if err != nil {
6566
if errors.Is(err, store.ErrAlreadyExists) {
66-
return nil, status.Errorf(codes.AlreadyExists, "Actor %s already exists", id)
67+
return nil, status.Errorf(codes.AlreadyExists, "Actor %s already exists", name)
6768
}
6869
return nil, fmt.Errorf("while recording actor: %w", err)
6970
}
7071

71-
storedActor, err := s.persistence.GetActor(ctx, req.GetActorRef().GetAtespace(), id)
72-
if err != nil {
73-
return nil, fmt.Errorf("while fetching recorded actor from DB: %w", err)
74-
}
75-
7672
return &ateapipb.CreateActorResponse{
77-
Actor: storedActor,
73+
Actor: stored,
7874
}, nil
7975
}
8076

cmd/ateapi/internal/controlapi/create_atespace.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ func (s *Service) CreateAtespace(ctx context.Context, req *ateapipb.CreateAtespa
3131
return nil, err
3232
}
3333

34-
atespace := &ateapipb.Atespace{Name: req.GetName()}
35-
if err := s.persistence.CreateAtespace(ctx, atespace); err != nil {
34+
atespace := &ateapipb.Atespace{
35+
Metadata: &ateapipb.ResourceMetadata{Name: req.GetName()},
36+
}
37+
stored, err := s.persistence.CreateAtespace(ctx, atespace)
38+
if err != nil {
3639
if errors.Is(err, store.ErrAlreadyExists) {
3740
return nil, status.Errorf(codes.AlreadyExists, "Atespace %s already exists", req.GetName())
3841
}
3942
return nil, fmt.Errorf("while recording atespace: %w", err)
4043
}
4144

42-
return &ateapipb.CreateAtespaceResponse{Atespace: atespace}, nil
45+
return &ateapipb.CreateAtespaceResponse{Atespace: stored}, nil
4346
}
4447

4548
func validateCreateAtespaceRequest(req *ateapipb.CreateAtespaceRequest) error {

0 commit comments

Comments
 (0)