You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// If the version is not greater than the last processed version, do not process the state change.
if (Version <= _lastProcessedVersion)
{
return;
}
InternalOnStateChangedAsync().ContinueWith(task =>
{
if (task.Exception != null)
{
Logger.LogError(task.Exception, "InternalOnStateChangedAsync operation failed");
}
}, TaskContinuationOptions.OnlyOnFaulted);
_lastProcessedVersion = Version;
}
private async Task InternalOnStateChangedAsync()
{
await HandleStateChangedAsync();
if (StateDispatcher != null)
{
var snapshot = _copier!.Copy(State);
var singleStateWrapper = new StateWrapper<TState>(this.GetGrainId(), snapshot, Version);
singleStateWrapper.PublishedTimestampUtc = DateTime.UtcNow;
await StateDispatcher.PublishSingleAsync(this.GetGrainId(), singleStateWrapper);
var batchStateWrapper = new StateWrapper<TState>(this.GetGrainId(), snapshot, Version);
batchStateWrapper.PublishedTimestampUtc = DateTime.UtcNow;
await StateDispatcher.PublishAsync(this.GetGrainId(), batchStateWrapper);
}
}`
When I changed the code to pass the Version as a parameter to the method, the phenomenon disappeared.
`
protected sealed override void OnStateChanged()
{
// If the GAgent is not activated, do not process the state change.
if (!_isActivated)
{
return;
}
// If the version is not greater than the last processed version, do not process the state change.
if (Version <= _lastProcessedVersion)
{
return;
}
InternalOnStateChangedAsync(Version).ContinueWith(task =>
{
if (task.Exception != null)
{
Logger.LogError(task.Exception, "InternalOnStateChangedAsync operation failed");
}
}, TaskContinuationOptions.OnlyOnFaulted);
_lastProcessedVersion = Version;
}
private async Task InternalOnStateChangedAsync(int version)
{
await HandleStateChangedAsync();
if (StateDispatcher != null)
{
var snapshot = _copier!.Copy(State);
var singleStateWrapper = new StateWrapper<TState>(this.GetGrainId(), snapshot, version);
singleStateWrapper.PublishedTimestampUtc = DateTime.UtcNow;
await StateDispatcher.PublishSingleAsync(this.GetGrainId(), singleStateWrapper);
var batchStateWrapper = new StateWrapper<TState>(this.GetGrainId(), snapshot, version);
batchStateWrapper.PublishedTimestampUtc = DateTime.UtcNow;
await StateDispatcher.PublishAsync(this.GetGrainId(), batchStateWrapper);
}
}`
So does ContinueWith really execute synchronously within the grain?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
https://github.com/aevatarAI/aevatar-station/blob/dev/framework/src/Aevatar.Core/GAgentBase.cs
This is the current base implementation of our project, which inherits from JournaledGrain and overrides the OnStateChanged method. Then I noticed in the relevant documentation at https://learn.microsoft.com/en-us/dotnet/orleans/grains/external-tasks-and-grainsthat using the ContinueWith method would also execute synchronously. However, from the received log prints, I observed duplicate occurrences of the Version.
`
protected sealed override void OnStateChanged()
{
// If the GAgent is not activated, do not process the state change.
if (!_isActivated)
{
return;
}
When I changed the code to pass the Version as a parameter to the method, the phenomenon disappeared.
`
protected sealed override void OnStateChanged()
{
// If the GAgent is not activated, do not process the state change.
if (!_isActivated)
{
return;
}
Beta Was this translation helpful? Give feedback.
All reactions