-
Notifications
You must be signed in to change notification settings - Fork 298
atepg(cleanup): update the in-place metadata setter migration #1377
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 all commits
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 |
|---|---|---|
|
|
@@ -188,34 +188,13 @@ type querier interface { | |
| Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) | ||
| } | ||
|
|
||
| // TODO: EOL this in favor of setCreateMetadata | ||
| func newCreateMetadata(atespace, name string) *ateapipb.ResourceMetadata { | ||
| now := timestamppb.Now() | ||
| return &ateapipb.ResourceMetadata{ | ||
| Atespace: atespace, | ||
| Name: name, | ||
| Uid: uuid.NewString(), | ||
| Version: 1, | ||
| CreateTime: now, | ||
| UpdateTime: now, | ||
| } | ||
| } | ||
|
|
||
| func setCreateMetadata(metadata *ateapipb.ResourceMetadata) { | ||
| metadata.Uid = uuid.NewString() | ||
| metadata.Version = 1 | ||
| metadata.CreateTime = timestamppb.Now() | ||
| metadata.UpdateTime = metadata.CreateTime | ||
| } | ||
|
|
||
| // TODO: EOL this in favor of setUpdateMetadata | ||
| func newUpdateMetadata(current *ateapipb.ResourceMetadata) *ateapipb.ResourceMetadata { | ||
| metadata := proto.Clone(current).(*ateapipb.ResourceMetadata) | ||
| metadata.Version++ | ||
| metadata.UpdateTime = timestamppb.Now() | ||
| return metadata | ||
| } | ||
|
|
||
| // validateProtoMetadataMatchesColumns verifies that the metadata in the database | ||
| // matches the metadata in the proto. | ||
| func validateProtoMetadataMatchesColumns(resource string, metadata *ateapipb.ResourceMetadata, uid string, version int64) error { | ||
|
|
@@ -271,8 +250,14 @@ func pgErrConstraint(err error) string { | |
| func (p *Persistence) CreateAtespace(ctx context.Context, atespace *ateapipb.Atespace) (*ateapipb.Atespace, error) { | ||
| name := atespace.GetMetadata().GetName() | ||
|
|
||
| dbAtespace := proto.Clone(atespace).(*ateapipb.Atespace) | ||
| dbAtespace.Metadata = newCreateMetadata("", name) | ||
| // The atespace is mutated in place: callers pass a dedicated object. | ||
| dbAtespace := atespace | ||
| if dbAtespace.Metadata == nil { | ||
|
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. Under what conditions would we end up here with nil metadata? If that is the case, we don't know the name of the atespace to create, we certainly can't proceed. This should have been validated already? I see similar patterns elsewhere -- we should make them all consistent. |
||
| dbAtespace.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
| // Atespaces are global-scoped, so the atespace is always empty. | ||
| dbAtespace.Metadata.Atespace = "" | ||
|
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. This should have been validated already, right? If this is set and we get this far, there's a problem. Doing this here implies that this is acceptable, and I think it's impossible? |
||
| setCreateMetadata(dbAtespace.Metadata) | ||
|
|
||
| protoBytes, err := proto.Marshal(dbAtespace) | ||
| if err != nil { | ||
|
|
@@ -383,7 +368,8 @@ func (p *Persistence) DeleteAtespace(ctx context.Context, name string) (*ateapip | |
|
|
||
| func (p *Persistence) CreateActorTemplate(ctx context.Context, template *ateapipb.ActorTemplate) (*ateapipb.ActorTemplate, error) { | ||
| atespace, name := template.GetMetadata().GetAtespace(), template.GetMetadata().GetName() | ||
| dbTemplate := proto.Clone(template).(*ateapipb.ActorTemplate) | ||
| // The template is mutated in place: callers pass a dedicated object. | ||
| dbTemplate := template | ||
| if dbTemplate.Metadata == nil { | ||
|
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. we really CAN'T be in here without metadata because we don't have a name! |
||
| dbTemplate.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
|
|
@@ -588,11 +574,12 @@ func (p *Persistence) CreateActor(ctx context.Context, actor *ateapipb.Actor) (* | |
| atespace := actor.GetMetadata().GetAtespace() | ||
| name := actor.GetMetadata().GetName() | ||
|
|
||
| // TODO: doing a full clone here is wasteful - the caller already has to | ||
| // make modifications to the actor before passing it in, so we can safely | ||
| // mutate it in place. This breaks some of the contract tests, so we can | ||
| // fix it later. | ||
| dbActor := proto.Clone(actor).(*ateapipb.Actor) | ||
| // The actor is mutated in place: the caller already builds a dedicated | ||
| // object to pass in, so a defensive clone is wasted work. | ||
| dbActor := actor | ||
| if dbActor.Metadata == nil { | ||
|
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. Same as elsewhere - we really CAN'T be in here without metadata because we don't have a name! |
||
| dbActor.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
| setCreateMetadata(dbActor.Metadata) | ||
|
|
||
| protoBytes, err := proto.Marshal(dbActor) | ||
|
|
@@ -845,8 +832,16 @@ func (p *Persistence) listActorsGlobal(ctx context.Context, pageSize int32, page | |
| // --- Actor egress policies --- | ||
|
|
||
| func (p *Persistence) CreateEgressPolicy(ctx context.Context, actorRef resources.ActorRef, policy *ateapipb.EgressPolicy) (*ateapipb.EgressPolicy, error) { | ||
| dbPolicy := proto.Clone(policy).(*ateapipb.EgressPolicy) | ||
| dbPolicy.Metadata = newCreateMetadata(actorRef.Atespace, "default") | ||
| // The policy is mutated in place: callers pass a dedicated object. | ||
| dbPolicy := policy | ||
| if dbPolicy.Metadata == nil { | ||
| dbPolicy.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
| // The policy is a singleton nested under its actor: its identity comes | ||
| // from the actor, not from caller-supplied metadata. | ||
| dbPolicy.Metadata.Atespace = actorRef.Atespace | ||
| dbPolicy.Metadata.Name = "default" | ||
| setCreateMetadata(dbPolicy.Metadata) | ||
| protoBytes, err := proto.Marshal(dbPolicy) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("marshaling egress policy: %w", err) | ||
|
|
@@ -960,7 +955,10 @@ func (p *Persistence) CreateActorSnapshot(ctx context.Context, snapshot *ateapip | |
| atespace := snapshot.GetMetadata().GetAtespace() | ||
| name := snapshot.GetMetadata().GetName() | ||
| dbSnapshot := proto.Clone(snapshot).(*ateapipb.ActorSnapshot) | ||
| dbSnapshot.Metadata = newCreateMetadata(atespace, name) | ||
| if dbSnapshot.Metadata == nil { | ||
| dbSnapshot.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
| setCreateMetadata(dbSnapshot.Metadata) | ||
|
|
||
| protoBytes, err := proto.Marshal(dbSnapshot) | ||
| if err != nil { | ||
|
|
@@ -1129,7 +1127,10 @@ func (p *Persistence) CreateActorSnapshotTag(ctx context.Context, snapshotRef re | |
| tagAtespace := tag.GetMetadata().GetAtespace() | ||
| tagName := tag.GetMetadata().GetName() | ||
| dbTag := proto.Clone(tag).(*ateapipb.ActorSnapshotTag) | ||
| dbTag.Metadata = newCreateMetadata(tagAtespace, tagName) | ||
| if dbTag.Metadata == nil { | ||
| dbTag.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
| setCreateMetadata(dbTag.Metadata) | ||
| dbTag.Snapshot = &ateapipb.ObjectRef{Atespace: snapshotAtespace, Name: snapshotName} | ||
| protoBytes, err := proto.Marshal(dbTag) | ||
| if err != nil { | ||
|
|
@@ -1238,9 +1239,12 @@ func (p *Persistence) UpdateActorSnapshotTag(ctx context.Context, tagRef resourc | |
| if err := validateUpdateActorSnapshotTagMutation(tagBeforeMutation, dbTag); err != nil { | ||
| return nil, fmt.Errorf("%w: %w", store.ErrImmutableField, err) | ||
| } | ||
| // Stored metadata is authoritative; discard any metadata edits made by the | ||
| // closure and derive the next revision from the state this attempt read. | ||
| dbTag.Metadata = newUpdateMetadata(tagBeforeMutation.GetMetadata()) | ||
| // Stored server-assigned metadata is authoritative; the next revision is | ||
| // derived from the state this attempt read. | ||
| if dbTag.Metadata == nil { | ||
| dbTag.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
| setUpdateMetadata(dbTag.Metadata, tagBeforeMutation.GetMetadata()) | ||
|
|
||
| updatedBytes, err := proto.Marshal(dbTag) | ||
| if err != nil { | ||
|
|
@@ -1285,7 +1289,8 @@ func (p *Persistence) DeleteActorSnapshotTag(ctx context.Context, tagRef resourc | |
| // --- Workers --- | ||
|
|
||
| func (p *Persistence) CreateWorker(ctx context.Context, worker *ateapipb.Worker) (*ateapipb.Worker, error) { | ||
| dbWorker := proto.Clone(worker).(*ateapipb.Worker) | ||
| // The worker is mutated in place: callers pass a dedicated object. | ||
| dbWorker := worker | ||
| if dbWorker.Metadata == nil { | ||
| dbWorker.Metadata = &ateapipb.ResourceMetadata{} | ||
| } | ||
|
|
||
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.
Why not just get rid of
dbAtespaceand useatespace?