How to pass data to `OnActivateAsync ()` to initialize my active state?

I create a reliable, wealthy, service actor.

Question:

Is there a way to pass initialization data while creating an actor ( ActorProxy.Create()) proxy server ? In principle, the equivalent of a constructor for my actor.

Current thoughts:

I can achieve this by executing a proxy creation call with an actor method call responsible for initializing the state.

eg.

//Danger, the following calls are not atomic
ITokenManager tokenActor = ActorProxy.Create<IMyActor>(actorId, "AppName");
//Something could happen here and leave my actor in an unknown state
await tokenActor.InitializeAsync(desiredInitialState);

My concern for this approach:

  • This operation is not atomic. This may leave my actor in an inconsistent state.
  • This initialization method is now available throughout the entire life cycle of the actor, which is undesirable.
+4
source share
3

. -, , , OnActivateAsync? , , .

protected override Task OnActivateAsync()
{
   if (State == null)
   {
       var initialState = await externalSource.GetSomeState();
       // simplified here but map the values properly onto the actual actor state
       this.State = initialState;
       return base.OnActivateAsync();
   }
}

, , , , , - .

 public Task InitializeAsync(State someState)
 {
     if (State.IsActivated)
     {
         // log out here that someone is attempting to reactivate when they shouldn't
         return Task.CompletedTask;
     }

     State = someState;
     State.IsActivated = true;
     return Task.CompletedTask;
 }

, , , - , .

+2

, - , OnActivateAsync() .

+1

Creating a proxy is not equivalent to a constructor. In Service Fabric, the client does not need to know whether the actor has already been created or not, and the life cycle is controlled by the runtime.

So, the actor himself must initialize some default state. It is the task of implementing an actor to prevent other calls before initialization calls and to prevent multiple initialization, if necessary. Since actors are always single-threaded, they can be easily achieved using something like Boolean flags.

0
source

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


All Articles