c# - 了解服务结构参与者删除

标签 c# actor azure-service-fabric

我正在运行一个本地 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.

这就是你的程序所做的:

  1. 通过生成唯一的参与者标识符来定义参与者的实例。

  2. 调用已识别的参与者。它不存在,因此激活一个新实例。

  3. 删除那个实例。

  4. 列举所有 Actor 。什么都没有返回。

  5. 再次调用已识别的参与者。它不存在(因为它已被删除),因此激活了一个新实例。

关于c# - 了解服务结构参与者删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40326850/

相关文章:

c# - 将 XML 文件加载到数据表中(而不是从数据库中)

scala - 使用 Scala Actors 来创建像流水线一样的东西

ruby - 如何设计和构建使用Actor的程序

scala - 不同的 Scala Actor 实现概述

azure-service-fabric - "HTTP Error 503. The service is unavailable"与 Service Fabric 上的 WebListener 共享端口

azure-service-fabric - 服务结构需要时间来部署 RollingForwardPending 需要时间

具有选择不同列的 c# mysql 查询

c# - 日期时间转换为无效格式

c# - 为 Localhost c# 使用 Oledb 连接字符串

azure - 如何在 Service Fabric 应用程序中添加 .NET Core 类库引用