我正在运行一个本地 5 节点服务结构集群。
为了更好地控制长时间运行的 Actor ,我想在需要时删除 Actor 。
引用 deleting actors 的文档和 enumerating actors我想出了以下代码
//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>();
do
{
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());
我得到的输出是
0ActorID:1952475710829467062
0ActorID:1952475710829467062
其中0是方法的返回值。
我知道 Actor 已从服务中删除,因为在 foreach 循环中没有打印任何内容,但是如果 Actor 被删除,我对 Actor 方法的第二次调用是如何成功的呢?它是重新创建的吗?请帮助我理解这一点。
最佳答案
[...] how did my second call to the actor method succeed if the actor was deleted? Was it recreated?
是的,它是重新创建的。
你应该阅读 this article ,它提供了 actor 生命周期管理的细节。这是该文章的引述:
When a call comes for an actor and one is not already active, a new actor is created.
这就是你的程序所做的:
通过生成唯一的参与者标识符来定义参与者的实例。
调用已识别的参与者。它不存在,因此激活一个新实例。
删除那个实例。
列举所有 Actor 。什么都没有返回。
再次调用已识别的参与者。它不存在(因为它已被删除),因此激活了一个新实例。
关于c# - 了解服务结构参与者删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40326850/