raghav710 raghav710 - 1 year ago 412
C# Question

Understanding service fabric actor deletion

I am running a local 5 node service fabric cluster.

In order to have finer control over long running actors, I want to delete the actors when required.

Referring to the documentation for deleting actors and enumerating actors I came up with the following code

//create actor ID, get instance and call a method on the actor
ActorId id = ActorId.CreateRandom();
IActor1 myActor = ActorProxy.Create<IActor1>(id, new Uri(URI));
Console.WriteLine(myActor.GetCountAsync().GetAwaiter().GetResult() + "ActorID:" + id.GetPartitionKey());
IActorService myActorServiceProxy = ActorServiceProxy.Create(new Uri(URI), id);

//delete actor from Actor service
myActorServiceProxy.DeleteActorAsync(id, new CancellationToken()).GetAwaiter().GetResult();

//enumerate actors
IActorService actorServiceProxy = ActorServiceProxy.Create(new Uri(URI), id.GetPartitionKey());

ContinuationToken continuationToken = null;
List<ActorInformation> activeActors = new List<ActorInformation>();

PagedResult<ActorInformation> page = actorServiceProxy
.GetActorsAsync(continuationToken, new CancellationToken()).GetAwaiter().GetResult();

activeActors.AddRange(page.Items.Where(x => x.IsActive));

continuationToken = page.ContinuationToken;
while (continuationToken != null);
foreach(ActorInformation info in activeActors)
Console.WriteLine("INFO:" + info.ActorId + ":" + info.IsActive);

//call the method again
Console.WriteLine(myActor.GetCountAsync().GetAwaiter().GetResult() + "ActorID:" + id.GetLongId());

And the output that I get is


where 0 is the return value of the method.

I understand that the actor has been deleted from the service as nothing was printed in the foreach loop, but how did my second call to the actor method succeed if the actor was deleted? Was it recreated? Please do help me out in understanding that.

Answer Source

[...] how did my second call to the actor method succeed if the actor was deleted? Was it recreated?

Yes, it was re-created.

You should read this article, it provides details of actor life cycle management. Here's a quote from that article:

When a call comes for an actor and one is not already active, a new actor is created.

This is what your program does:

  1. Define an instance of an actor by generating a unique actor identifier.

  2. Invoke the identified actor. It does not exist, so a new instance is activated.

  3. Delete that instance.

  4. Enumerate all actors. Nothing is returned.

  5. Invoke the identified actor again. It does not exist (since it was deleted), so a new instance is activated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download