From 48842ed4224718214cee790bc6e1068b0f156415 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Fri, 29 Dec 2023 20:01:19 -0600 Subject: [PATCH 1/3] Clarified the actor state operations performed and what subsequent activities must be performed by the user for persistance. Previous wording was confusing. signed-off-by: Whit Waldo --- actors/csharp/sdk/service/SmokeDetectorActor.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/actors/csharp/sdk/service/SmokeDetectorActor.cs b/actors/csharp/sdk/service/SmokeDetectorActor.cs index 99374da48..d9a2d8ff0 100644 --- a/actors/csharp/sdk/service/SmokeDetectorActor.cs +++ b/actors/csharp/sdk/service/SmokeDetectorActor.cs @@ -36,7 +36,7 @@ protected override Task OnActivateAsync() /// protected override Task OnDeactivateAsync() { - // Provides Opportunity to perform optional cleanup. + // Provides opportunity to perform optional cleanup. Console.WriteLine($"Deactivating actor id: {Id}"); return Task.CompletedTask; } @@ -47,9 +47,12 @@ protected override Task OnDeactivateAsync() /// the user-defined MyData which will be stored into state store as "device_data" state public async Task SetDataAsync(SmartDeviceData data) { - // Data is saved to configured state store *implicitly* after each method execution by Actor's runtime. - // Data can also be saved *explicitly* by calling this.StateManager.SaveStateAsync(); - // State to be saved must be DataContract serializable. + // All data operations performed on the actor are maintained against a per-actor cache and are not persisted + // to the actor state store until you've called `this.StateManager.SaveStateAsync();` Per the actor lifetime documentation, + // "If an actor is not used for a period of time, the Dapr actor runtime garbage-collects the in-memory object" meaning that + // if you haven't persisted your changes to the underlying Dapr state store, your changes will be lost. + + // Note also that all saved state must be DataContract serializable. await StateManager.SetStateAsync( deviceDataKey, data); From cea991610f8bb4b366af3fa31ccbab1e01307e71 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Sat, 30 Dec 2023 14:11:08 -0600 Subject: [PATCH 2/3] Updated comment to reflect call out on Discord by Pundil Signed-off-by: Whit Waldo --- actors/csharp/sdk/service/SmokeDetectorActor.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/actors/csharp/sdk/service/SmokeDetectorActor.cs b/actors/csharp/sdk/service/SmokeDetectorActor.cs index d9a2d8ff0..c88b4abf8 100644 --- a/actors/csharp/sdk/service/SmokeDetectorActor.cs +++ b/actors/csharp/sdk/service/SmokeDetectorActor.cs @@ -47,10 +47,10 @@ protected override Task OnDeactivateAsync() /// the user-defined MyData which will be stored into state store as "device_data" state public async Task SetDataAsync(SmartDeviceData data) { - // All data operations performed on the actor are maintained against a per-actor cache and are not persisted - // to the actor state store until you've called `this.StateManager.SaveStateAsync();` Per the actor lifetime documentation, - // "If an actor is not used for a period of time, the Dapr actor runtime garbage-collects the in-memory object" meaning that - // if you haven't persisted your changes to the underlying Dapr state store, your changes will be lost. + // This set state action can happen along other state changing operations in each actor method and those changes will be maintained + // in a local cache to be committed as a single transaction to the backing store when the method has completed. As such, there is + // no need to (and in fact makes your code less transactional) call `this.StateManager.SaveStateAsync()` as it will be automatically + // invoked by the actor runtime following the conclusion of this method as part of the internal `OnPostActorMethodAsyncInternal` method. // Note also that all saved state must be DataContract serializable. await StateManager.SetStateAsync( From 0f93b1d4e77cd6bdbcc3f121ec6e8cfe80a26642 Mon Sep 17 00:00:00 2001 From: Whit Waldo Date: Sat, 30 Dec 2023 14:11:08 -0600 Subject: [PATCH 3/3] Updated comment to reflect call out on Discord by Pundil Signed-off-by: Whit Waldo --- actors/csharp/sdk/service/SmokeDetectorActor.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/actors/csharp/sdk/service/SmokeDetectorActor.cs b/actors/csharp/sdk/service/SmokeDetectorActor.cs index d9a2d8ff0..c88b4abf8 100644 --- a/actors/csharp/sdk/service/SmokeDetectorActor.cs +++ b/actors/csharp/sdk/service/SmokeDetectorActor.cs @@ -47,10 +47,10 @@ protected override Task OnDeactivateAsync() /// the user-defined MyData which will be stored into state store as "device_data" state public async Task SetDataAsync(SmartDeviceData data) { - // All data operations performed on the actor are maintained against a per-actor cache and are not persisted - // to the actor state store until you've called `this.StateManager.SaveStateAsync();` Per the actor lifetime documentation, - // "If an actor is not used for a period of time, the Dapr actor runtime garbage-collects the in-memory object" meaning that - // if you haven't persisted your changes to the underlying Dapr state store, your changes will be lost. + // This set state action can happen along other state changing operations in each actor method and those changes will be maintained + // in a local cache to be committed as a single transaction to the backing store when the method has completed. As such, there is + // no need to (and in fact makes your code less transactional) call `this.StateManager.SaveStateAsync()` as it will be automatically + // invoked by the actor runtime following the conclusion of this method as part of the internal `OnPostActorMethodAsyncInternal` method. // Note also that all saved state must be DataContract serializable. await StateManager.SetStateAsync(