我正在运行本地5节点服务结构群集.
为了更好地控制长时间运行的角色,我想在需要时删除这些角色.
参考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());
我得到的输出是
0演员ID:1952475710829467062
0演员ID: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,它提供了参与者生命周期管理的详细信息.这是该文章的引文:
When a call comes for an actor and one is not already active, a new actor is created.
这是您的程序执行的操作:
>通过生成唯一的参与者标识符来定义参与者的实例.
>调用已识别的演员.它不存在,因此将激活一个新实例.
>删除该实例.
>列举所有演员.什么也没退.
>再次调用已识别的演员.它不存在(因为它已被删除),因此激活了一个新实例.