-
Notifications
You must be signed in to change notification settings - Fork 374
Implementation of UnloadStateAsync #1750
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e49f831
Implementation of UnloadStateAsyc
olitomlinson 7f20644
Merge branch 'master' into unload-async
yaron2 c13b508
Merge branch 'master' into unload-async
WhitWaldo f4ca8de
Merge branch 'master' into unload-async
olitomlinson 0f79ec1
Merge branch 'master' into unload-async
WhitWaldo 905c376
Merge branch 'master' into unload-async
WhitWaldo 75100d3
Merge branch 'master' into unload-async
WhitWaldo 94be0e8
Merge branch 'master' into unload-async
WhitWaldo 02bece8
Update UnloadStateOptions.cs
olitomlinson 9099b71
Merge branch 'master' into unload-async
WhitWaldo 4e654ee
Merge branch 'master' into unload-async
WhitWaldo 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
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,14 @@ | ||
| // Options for UnloadStateAsync operation | ||
|
olitomlinson marked this conversation as resolved.
Outdated
|
||
| namespace Dapr.Actors.Runtime | ||
| { | ||
| /// <summary> | ||
| /// Options for the UnloadStateAsync operation on ActorStateManager. | ||
| /// </summary> | ||
| public class UnloadStateOptions | ||
| { | ||
| /// <summary> | ||
| /// If true, allows unloading state even if it is modified and not yet persisted. | ||
| /// </summary> | ||
| public bool AllowUnloadingWhenStateModified { get; set; } = false; | ||
| } | ||
| } | ||
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,85 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2023 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Dapr.Actors.Runtime; | ||
| using Dapr.Actors.Communication; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Dapr.Actors.Test | ||
| { | ||
| public class ActorStateManagerUnloadStateTest | ||
| { | ||
| [Fact] | ||
| public async Task UnloadState_RemovesFromMemoryButNotStore() | ||
| { | ||
| var interactor = new Moq.Mock<TestDaprInteractor>(); | ||
| // Simulate state existence only after SaveStateAsync | ||
| bool stateSaved = false; | ||
| interactor.Setup(d => d.GetStateAsync( | ||
| Moq.It.IsAny<string>(), | ||
| Moq.It.IsAny<string>(), | ||
| Moq.It.Is<string>(key => key == "big-data"), | ||
| Moq.It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(() => | ||
| stateSaved | ||
| ? new Dapr.Actors.Communication.ActorStateResponse<string>("\"payload\"", null) | ||
| : new Dapr.Actors.Communication.ActorStateResponse<string>("", null)); | ||
| var host = ActorHost.CreateForTest<TestActor>(); | ||
| host.StateProvider = new DaprStateProvider(interactor.Object, new System.Text.Json.JsonSerializerOptions()); | ||
| var mngr = new ActorStateManager(new TestActor(host)); | ||
| var token = TestContext.Current.CancellationToken; | ||
|
|
||
| // Add and save state | ||
| await mngr.AddStateAsync("big-data", "payload", token); | ||
| await mngr.SaveStateAsync(token); | ||
| stateSaved = true; | ||
| Assert.Equal("payload", await mngr.GetStateAsync<string>("big-data", token)); | ||
|
|
||
| // Unload from memory | ||
| await mngr.UnloadStateAsync("big-data", cancellationToken: token); | ||
|
|
||
| // Should reload from store | ||
| interactor.Setup(d => d.GetStateAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(new Dapr.Actors.Communication.ActorStateResponse<string>("\"payload\"", null)); | ||
| Assert.Equal("payload", await mngr.GetStateAsync<string>("big-data", token)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task UnloadState_ThrowsIfModifiedUnlessAllowed() | ||
| { | ||
| var interactor = new Moq.Mock<TestDaprInteractor>(); | ||
| // Default: state does not exist | ||
| interactor.Setup(d => d.GetStateAsync( | ||
| Moq.It.IsAny<string>(), | ||
| Moq.It.IsAny<string>(), | ||
| Moq.It.IsAny<string>(), | ||
| Moq.It.IsAny<CancellationToken>())) | ||
| .ReturnsAsync(new Dapr.Actors.Communication.ActorStateResponse<string>("", null)); | ||
| var host = ActorHost.CreateForTest<TestActor>(); | ||
| host.StateProvider = new DaprStateProvider(interactor.Object, new System.Text.Json.JsonSerializerOptions()); | ||
| var mngr = new ActorStateManager(new TestActor(host)); | ||
| var token = TestContext.Current.CancellationToken; | ||
|
|
||
| await mngr.AddStateAsync("key", "value", token); | ||
| // Not yet saved, so is modified | ||
| await Assert.ThrowsAsync<InvalidOperationException>(() => mngr.UnloadStateAsync("key", cancellationToken: token)); | ||
|
|
||
| // Should not throw if allowed | ||
| await mngr.UnloadStateAsync("key", new UnloadStateOptions { AllowUnloadingWhenStateModified = true }, token); | ||
| } | ||
| } | ||
| } |
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.
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.
consider adding as a default interface method with a
NotImplementedException/ virtual behavior.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?
Uh oh!
There was an error while loading. Please reload this page.
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.
In this scenario, adding a default interface method to throw
NotSupportedExceptionisn't a meaningful inclusion because we already provide the concrete type implementing this interface ourselves, and this will ship with same-day support.Especially as this project doesn't support dependency injection, I would not expect that developers have implemented their own types on this interface, modified the library to use them instead, update the package and see this new method on the interface and call it in their applications just to see a
TypeLoadExceptionbecause they didn't implement the method themselves - I just don't think that's likely. I wouldn't have expected them to wrap every actor invocation in a try/catch block either, so this would just shift throwing aTypeLoadExceptionto anNotSupportedException- not a meaningful change here.