Reliable Actors 如何使用 Service Fabric 平台How Reliable Actors use the Service Fabric platform

本文介绍了 Reliable Actors 如何使用 Azure Service Fabric 平台。This article explains how Reliable Actors work on the Azure Service Fabric platform. Reliable Actors 在有状态的可靠服务(称为执行组件服务)的实现托管的框架中运行。Reliable Actors run in a framework that is hosted in an implementation of a stateful reliable service called the actor service. 执行组件服务包含管理执行组件的生命周期和消息发送所需的所有组件:The actor service contains all the components necessary to manage the lifecycle and message dispatching for your actors:

  • 执行组件运行时管理生命周期、垃圾回收,并强制执行单线程访问。The Actor Runtime manages lifecycle, garbage collection, and enforces single-threaded access.
  • 执行组件服务远程处理侦听器接受对执行组件的远程访问调用,并将它们发送到调度程序,以便路由到合适的执行组件实例。An actor service remoting listener accepts remote access calls to actors and sends them to a dispatcher to route to the appropriate actor instance.
  • 执行组件状态提供程序包装状态提供程序(例如 Reliable Collections 状态提供程序),并为执行组件状态管理提供一个适配器。The Actor State Provider wraps state providers (such as the Reliable Collections state provider) and provides an adapter for actor state management.

这些组件共同构成了 Reliable Actor 框架。These components together form the Reliable Actor framework.

服务分层Service layering

因为执行组件服务本身是一种可靠服务,Reliable Services 的所有应用程序模型、生命周期、打包部署、升级和缩放概念同样适用于执行组件服务。Because the actor service itself is a reliable service, all the application model, lifecycle, packaging, deployment, upgrade, and scaling concepts of Reliable Services apply the same way to actor services.

执行组件服务分层

上图显示了 Service Fabric 应用程序框架和用户代码之间的关系。The preceding diagram shows the relationship between the Service Fabric application frameworks and user code. 蓝色元素代表 Reliable Services 应用程序框架,橙色代表 Reliable Actor 框架,绿色则代表用户代码。Blue elements represent the Reliable Services application framework, orange represents the Reliable Actor framework, and green represents user code.

在 Reliable Services 中,服务会继承 StatefulService 类。In Reliable Services, your service inherits the StatefulService class. 该类本身派生自 StatefulServiceBase(或无状态服务的 StatelessService)。This class is itself derived from StatefulServiceBase (or StatelessService for stateless services). 在 Reliable Actors 中,使用执行组件服务。In Reliable Actors, you use the actor service. 执行组件服务是一种不同的 StatefulServiceBase 类的实现,该类可实现运行执行组件的执行组件模式。The actor service is a different implementation of the StatefulServiceBase class that implements the actor pattern where your actors run. 由于执行组件服务只是 StatefulServiceBase 的一个实现,因此可以编写自己的派生自 ActorService 的服务,并且以与继承 StatefulService 时一样的方式实现服务级别的功能,例如:Because the actor service itself is just an implementation of StatefulServiceBase, you can write your own service that derives from ActorService and implement service-level features the same way you would when inheriting StatefulService, such as:

  • 服务备份和还原。Service backup and restore.
  • 共享给所有执行组件的功能,例如断路器。Shared functionality for all actors, for example, a circuit breaker.
  • 对执行组件服务自身和每个执行组件的远程过程调用。Remote procedure calls on the actor service itself and on each individual actor.

有关详细信息,请参阅在执行组件服务中实现服务级功能For more information, see Implementing service-level features in your actor service.

应用程序模型Application model

执行组件服务都是 Reliable Services,因此服务的应用程序模型是相同的。Actor services are Reliable Services, so the application model is the same. 但是,执行组件框架生成工具生成某些应用程序模型文件。However, the actor framework build tools generate some of the application model files for you.

服务清单Service manifest

执行组件框架生成工具自动生成执行组件服务的 ServiceManifest.xml 文件的内容。The actor framework build tools automatically generate the contents of your actor service's ServiceManifest.xml file. 此文件包括:This file includes:

  • 执行组件服务类型。Actor service type. 根据执行组件项目名称生成此类型名称。The type name is generated based on your actor's project name. 根据执行组件的持久性属性,还将相应设置 HasPersistedState 标志。Based on the persistence attribute on your actor, the HasPersistedState flag is also set accordingly.
  • 代码包。Code package.
  • 配置包。Config package.
  • 资源和终结点。Resources and endpoints.

应用程序清单Application manifest

执行组件框架生成工具将为执行组件服务自动创建默认的服务定义。The actor framework build tools automatically create a default service definition for your actor service. 生成工具填充默认的服务属性:The build tools populate the default service properties:

  • 副本集计数由执行组件的持久性属性决定。Replica set count is determined by the persistence attribute on your actor. 每次更改执行组件的持久性属性时,将相应地重置默认服务定义中的副本集计数。Each time the persistence attribute on your actor is changed, the replica set count in the default service definition is reset accordingly.
  • 分区方案和范围设置为具有完整的 Int64 键范围的统一 Int64。Partition scheme and range are set to Uniform Int64 with the full Int64 key range.

针对执行组件的 Service Fabric 分区概念Service Fabric partition concepts for actors

执行组件服务是已分区的有状态服务。Actor services are partitioned stateful services. 执行组件服务的每个分区包含一组执行组件。Each partition of an actor service contains a set of actors. 服务分区在 Service Fabric 的多个节点中自动分布。Service partitions are automatically distributed over multiple nodes in Service Fabric. 因此,执行组件实例分布到各个节点中。Actor instances are distributed as a result.

执行组件分区和分布

可使用不同的分区方案和分区键范围创建 Reliable Services。Reliable Services can be created with different partition schemes and partition key ranges. 执行组件服务使用具有完整的 Int64 键范围的 Int64 分区方案将执行组件映射到分区。The actor service uses the Int64 partitioning scheme with the full Int64 key range to map actors to partitions.

执行组件 IDActor ID

服务中创建的每个执行组件具有与之关联的唯一 ID,并使用 ActorId 类表示。Each actor that's created in the service has a unique ID associated with it, represented by the ActorId class. ActorId 是一个不透明的 ID 值,通过生成随机 ID,可将此值用于在各个服务分区中统一分布执行组件:ActorId is an opaque ID value that can be used for uniform distribution of actors across the service partitions by generating random IDs:

ActorProxy.Create<IMyActor>(ActorId.CreateRandom());
ActorProxyBase.create<MyActor>(MyActor.class, ActorId.newId());

每个 ActorId 都经过哈希算法转换为 Int64 类型值。Every ActorId is hashed to an Int64. 因此,执行组件服务必须使用具有完整 Int64 键范围的 Int64 分区方案。This is why the actor service must use an Int64 partitioning scheme with the full Int64 key range. 不过,ActorID 也可以使用自定义 ID 值,包括 GUID/UUID、字符串和 Int64。However, custom ID values can be used for an ActorID, including GUIDs/UUIDs, strings, and Int64s.

ActorProxy.Create<IMyActor>(new ActorId(Guid.NewGuid()));
ActorProxy.Create<IMyActor>(new ActorId("myActorId"));
ActorProxy.Create<IMyActor>(new ActorId(1234));
ActorProxyBase.create(MyActor.class, new ActorId(UUID.randomUUID()));
ActorProxyBase.create(MyActor.class, new ActorId("myActorId"));
ActorProxyBase.create(MyActor.class, new ActorId(1234));

使用 GUID/UUID 和字符串时,这些值将经过哈希算法转换为 Int64。When you're using GUIDs/UUIDs and strings, the values are hashed to an Int64. 但是,如果向 ActorId 显式提供 Int64,此 Int64 将直接映射到分区,而无需进行哈希转换。However, when you're explicitly providing an Int64 to an ActorId, the Int64 will map directly to a partition without further hashing. 可以使用此方法来控制将执行组件置于哪个分区。You can use this technique to control which partition the actors are placed in.

后续步骤Next steps