gpt4 book ai didi

azure - 无法确认正在创建任何 Actor

转载 作者:行者123 更新时间:2023-12-03 02:49:41 25 4
gpt4 key购买 nike

在 Service Fabric 中,我尝试调用 ActorService 并获取所有参与者的列表。我没有收到任何错误,但没有返回任何 Actor 。它始终为零。

这就是我添加 Actor 的方式:

ActorProxy.Create<IUserActor>(
new ActorId(uniqueName),
"fabric:/ECommerce/UserActorService");

这就是我尝试获取所有 Actor 列表的方法:

var proxy = ActorServiceProxy.Create(new Uri("fabric:/ECommerce/UserActorService"), 0);

ContinuationToken continuationToken = null;
CancellationToken cancellationToken = new CancellationTokenSource().Token;
List<ActorInformation> activeActors = new List<ActorInformation>();

do
{
var proxy = GetUserActorServiceProxy();
PagedResult<ActorInformation> page = await proxy.GetActorsAsync(continuationToken, cancellationToken);

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

continuationToken = page.ContinuationToken;
}
while (continuationToken != null);

但无论我添加了多少用户,页面对象始终具有零个项目。我缺少什么?

最佳答案

ActorServiceProxy.Create(Uri, int, string) 中的第二个参数 int是分区键(您可以了解有关 actor 分区的更多信息 here )。

此处的问题是您的代码仅检查一个分区 (partitionKey = 0)。

所以解决方案非常简单 - 您必须迭代服务的所有分区。这是一个answer使用代码示例获取分区并迭代它们。

更新2019.07.01

<小时/>

我第一次没有发现这一点,但您没有返回任何 Actor 的原因是因为您没有创建任何 Actor - 您正在创建代理!

造成这种困惑的原因是 Service Fabric actor 是虚拟的,即从用户的角度来看,actor 始终存在,但在现实生活中,Service Fabric 管理 actor 对象的生命周期,自动持久保存并根据需要恢复其状态。

这里引用 documentation :

An actor is automatically activated (causing an actor object to be constructed) the first time a message is sent to its actor ID. After some period of time, the actor object is garbage collected. In the future, using the actor ID again, causes a new actor object to be constructed. An actor's state outlives the object's lifetime when stored in the state manager.

在您的示例中,您从未向 Actor 发送任何消息!

这是我在新创建的 Actor 项目的 Program.cs 中编写的代码示例:

// Please don't forget to replace "fabric:/Application16/Actor1ActorService" with your actor service name.

ActorRuntime.RegisterActorAsync<Actor1> (
(context, actorType) =>
new ActorService(context, actorType)).GetAwaiter().GetResult();

var actor = ActorProxy.Create<IActor1>(
ActorId.CreateRandom(),
new Uri("fabric:/Application16/Actor1ActorService"));

_ = actor.GetCountAsync(default).GetAwaiter().GetResult();

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

var serviceName = new Uri("fabric:/Application16/Actor1ActorService");
using (var client = new FabricClient())
{
var partitions = client.QueryManager.GetPartitionListAsync(serviceName).GetAwaiter().GetResult();;

foreach (var partition in partitions)
{
var pi = (Int64RangePartitionInformation) partition.PartitionInformation;
var proxy = ActorServiceProxy.Create(new Uri("fabric:/Application16/Actor1ActorService"), pi.LowKey);
var page = proxy.GetActorsAsync(continuationToken, default).GetAwaiter().GetResult();

activeActors.AddRange(page.Items);

continuationToken = page.ContinuationToken;
}
}

Thread.Sleep(Timeout.Infinite);

特别注意这一行:

_ = actor.GetCountAsync(default).GetAwaiter().GetResult();

这是向 Actor 发送第一条消息的位置。

<小时/>

希望这有帮助。

关于azure - 无法确认正在创建任何 Actor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56776396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com