How can I ensure grain consistency in orleans?

In erlang, you can transfer the initial state to an actor when it spawns. Thus, you do not need to deal with init messages that return to its original state or messages requiring initialization messages that were received earlier. In orleans, when the grain assumption always exists, you cannot use constructors. Is there a way to pass the initial state to the grains, while avoiding any init method that breaks consistency if it should be called before any other method?

When I say: "Take the actor to this initial state," I mean, in the context of orleans, the call init method of special activation of the grain twice. This is similar to a rewrite state. Maybe you need this message king, which is reset, but if you do not need it, this is a trap, a potential source of errors.

I am looking for some type of constructor, something like spawn(module, function, [initial state])from erlang. My first attempt was to look for any GetGrain overload with the following signature:GrainFactory.GetGrain<IGrain>(id, initialState);

+4
source share
1 answer

As @svick shows, OnActivateAsyncis the best approach to load the initial state for grain.

 public class ExampleGrain : Orleans.Grain, IExampleGrain
 {

   public override Task OnActivateAsync()
   {
        // set initial state for grain
        return base.OnActivateAsync();
    }

 ...

, ( ). Persistence, Orleans, , (, ),

public class ExampleGrainState : GrainState
{
    public bool Initialised { get; set; }
}

[StorageProvider(ProviderName = "Storage")]
public class QuadKeyGrain : Orleans.Grain<ExampleGrainState>, IExampleGrain
{
    public override async Task OnActivateAsync()
    {
        if (!this.State.Initialised)
        {
            // do initialisation 
            this.State.Initialised = true;
            await this.WriteStateAsync();
        }
        await base.OnActivateAsync();
    }

. :

http://dotnet.imtqy.com/orleans/Tutorials/Declarative-Persistence.html

+3

Source: https://habr.com/ru/post/1617473/


All Articles