dotnet / orleans

Cloud Native application framework for .NET
https://docs.microsoft.com/dotnet/orleans
MIT License
10.1k stars 2.03k forks source link

Duplicate state created #9093

Open jerviscui opened 3 months ago

jerviscui commented 3 months ago

IGrainState creates the state through IActivatorProvider. IGrainStorage.ReadStateAsync uses Activator.CreateInstance() to create the state once again when there is no data in the stage. And even if it is created again, it should be created using the IActivatorProvider to keep the logic consistent?

jerviscui commented 3 months ago

IGrainStorage.ReadStateAsync:

T state = readRecords != null ? (T) readRecords.Item1 : default;
string etag = readRecords != null ? readRecords.Item2 : null;
bool recordExists = readRecords != null;
if(state == null)
{
    logger.LogInformation(
        (int)RelationalStorageProviderCodes.RelationalProviderNoStateFound,
        "Null grain state read (default will be instantiated): ServiceId={ServiceId} ProviderName={Name} GrainType={BaseGrainType} GrainId={GrainId} ETag={ETag}.",
        serviceId,
        name,
        baseGrainType,
        grainId,
        grainState.ETag);
    state = Activator.CreateInstance<T>();
}

grainState.State = state;