-
Notifications
You must be signed in to change notification settings - Fork 58
feat: implement durable entities #126
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
Open
qmuntal
wants to merge
13
commits into
microsoft:main
Choose a base branch
from
qmuntal:feature/durable-entities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2082235
feat: implement durable entities
qmuntal 4eec0d6
fix: address golangci-lint issues
qmuntal b579846
fix: address code review feedback
qmuntal 170d244
refactor: wire-compatible entity message protocol
qmuntal d66156e
fix: address remaining review feedback
qmuntal e6f7212
fix: concurrent entity execution with metadata-based correlation
qmuntal 15021ea
fix: address second round of review feedback
qmuntal 9ed7e78
ci: retrigger CI
qmuntal b99091b
fix: normalize entity IDs, consume events on empty batches, case-inse…
qmuntal b864241
preserve visible time and ids
qmuntal a1bb8a4
fix: restore internal entity validation
qmuntal c749480
fix: respect explicit entity state updates
qmuntal 4edd2cc
fix: address entity review findings
qmuntal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/microsoft/durabletask-go/internal/helpers" | ||
| "github.com/microsoft/durabletask-go/internal/protos" | ||
| "google.golang.org/protobuf/types/known/timestamppb" | ||
| "google.golang.org/protobuf/types/known/wrapperspb" | ||
| ) | ||
|
|
||
| // EntityID uniquely identifies an entity by its name and key. | ||
| type EntityID struct { | ||
| Name string | ||
| Key string | ||
| } | ||
|
|
||
| // NewEntityID creates a new EntityID with the specified name and key. | ||
| func NewEntityID(name string, key string) EntityID { | ||
| if err := helpers.ValidateEntityName(name); err != nil { | ||
| panic(err) | ||
| } | ||
| return EntityID{Name: strings.ToLower(name), Key: key} | ||
| } | ||
|
|
||
| // String returns the entity instance ID in the format "@<name>@<key>". | ||
| func (e EntityID) String() string { | ||
| return fmt.Sprintf("@%s@%s", strings.ToLower(e.Name), e.Key) | ||
| } | ||
|
|
||
| // EntityIDFromString parses an entity instance ID string in the format "@<name>@<key>". | ||
| func EntityIDFromString(s string) (EntityID, error) { | ||
| name, key, err := helpers.ParseEntityInstanceID(s) | ||
| if err != nil { | ||
| return EntityID{}, err | ||
| } | ||
| return EntityID{Name: name, Key: key}, nil | ||
| } | ||
|
|
||
| // EntityMetadata contains metadata about an entity instance. | ||
| type EntityMetadata struct { | ||
| InstanceID EntityID | ||
| LastModifiedTime time.Time | ||
| BacklogQueueSize int32 | ||
| LockedBy string | ||
| SerializedState string | ||
| } | ||
|
|
||
| // SignalEntityOptions is a functional option type for signaling an entity. | ||
| type SignalEntityOptions func(*protos.SignalEntityRequest) error | ||
|
|
||
| // WithSignalInput configures the input for an entity signal. | ||
| func WithSignalInput(input any) SignalEntityOptions { | ||
| return func(req *protos.SignalEntityRequest) error { | ||
| bytes, err := json.Marshal(input) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req.Input = wrapperspb.String(string(bytes)) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithRawSignalInput configures a raw string input for an entity signal. | ||
| func WithRawSignalInput(input string) SignalEntityOptions { | ||
| return func(req *protos.SignalEntityRequest) error { | ||
| req.Input = wrapperspb.String(input) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithSignalScheduledTime configures a scheduled time for the entity signal. | ||
| func WithSignalScheduledTime(t time.Time) SignalEntityOptions { | ||
| return func(req *protos.SignalEntityRequest) error { | ||
| req.ScheduledTime = timestamppb.New(t) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // EntityQuery defines filter criteria for querying entities. | ||
| type EntityQuery struct { | ||
| // InstanceIDStartsWith filters entities whose instance ID starts with this prefix. | ||
| InstanceIDStartsWith string | ||
| // LastModifiedFrom filters entities modified on or after this time. | ||
| LastModifiedFrom time.Time | ||
| // LastModifiedTo filters entities modified before this time. | ||
| LastModifiedTo time.Time | ||
| // IncludeState whether to include entity state in the results. | ||
| IncludeState bool | ||
| // IncludeTransient whether to include transient (stateless) entities. | ||
| IncludeTransient bool | ||
| // PageSize limits the number of entities returned per page. | ||
| PageSize int32 | ||
| // ContinuationToken for fetching the next page of results. | ||
| ContinuationToken string | ||
| } | ||
|
|
||
| // EntityQueryResults contains the results of an entity query. | ||
| type EntityQueryResults struct { | ||
| Entities []*EntityMetadata | ||
| ContinuationToken string | ||
| } | ||
|
|
||
| // CleanEntityStorageRequest contains options for cleaning entity storage. | ||
| type CleanEntityStorageRequest struct { | ||
| // ContinuationToken for resuming a previous cleanup operation. | ||
| ContinuationToken string | ||
| // RemoveEmptyEntities removes entities with no state and no locks. | ||
| RemoveEmptyEntities bool | ||
| // ReleaseOrphanedLocks releases locks held by non-running orchestrations. | ||
| ReleaseOrphanedLocks bool | ||
| } | ||
|
|
||
| // CleanEntityStorageResult contains the results of a cleanup operation. | ||
| type CleanEntityStorageResult struct { | ||
| // EmptyEntitiesRemoved is the number of empty entities removed. | ||
| EmptyEntitiesRemoved int32 | ||
| // OrphanedLocksReleased is the number of orphaned locks released. | ||
| OrphanedLocksReleased int32 | ||
| // ContinuationToken for resuming cleanup. Empty if complete. | ||
| ContinuationToken string | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_API_EntityID_String(t *testing.T) { | ||
| id := NewEntityID("Counter", "myCounter") | ||
| assert.Equal(t, "@counter@myCounter", id.String()) | ||
| } | ||
|
|
||
| func Test_API_EntityIDFromString(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want EntityID | ||
| wantErr bool | ||
| }{ | ||
| {name: "valid", input: "@counter@key1", want: EntityID{Name: "counter", Key: "key1"}}, | ||
| {name: "empty key", input: "@entity@", want: EntityID{Name: "entity", Key: ""}}, | ||
| {name: "invalid empty name", input: "@@key1", wantErr: true}, | ||
| {name: "invalid no prefix", input: "no-at-sign", wantErr: true}, | ||
| {name: "invalid no second @", input: "@onlyone", wantErr: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := EntityIDFromString(tt.input) | ||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_API_NewEntityID_InvalidNamePanics(t *testing.T) { | ||
| assert.Panics(t, func() { NewEntityID("", "key") }) | ||
| assert.Panics(t, func() { NewEntityID("bad@name", "key") }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/durabletask-go/internal/protos" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_API_WithInstanceID_RejectsEntityFormat(t *testing.T) { | ||
| req := &protos.CreateInstanceRequest{} | ||
|
|
||
| err := WithInstanceID(InstanceID("@counter@key"))(req) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func Test_API_WithInstanceID_AllowsNormalValue(t *testing.T) { | ||
| req := &protos.CreateInstanceRequest{} | ||
|
|
||
| err := WithInstanceID(InstanceID("my-instance"))(req) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "my-instance", req.InstanceId) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.