副本和实例Replicas and instances

本文概述了有状态服务副本和无状态服务实例的生命周期。This article gives an overview of the lifecycle of replicas of stateful services and instances of stateless services.

无状态服务的实例Instances of stateless services

无状态服务的实例是在其中一个群集节点上运行的服务逻辑的副本。An instance of a stateless service is a copy of the service logic that runs on one of the nodes of the cluster. 分区内的实例由其 InstanceId 唯一标识 。An instance within a partition is uniquely identified by its InstanceId . 实例的生命周期可在以下图表中进行建模:The lifecycle of an instance is modeled in the following diagram:

实例生命周期

InBuild (IB)InBuild (IB)

群集资源管理器确定实例的放置后,它将进入此生命周期状态。After the Cluster Resource Manager determines a placement for the instance, it enters this lifecycle state. 此实例将在节点上启动。The instance is started on the node. 将启动应用程序主机,创建并打开实例。The application host is started, the instance is created and then opened. 启动完成后,实例将转换为就绪状态。After the startup finishes, the instance transitions to the ready state.

如果此实例的应用程序主机或节点发生故障,它将转换为已删除状态。If the application host or node for this instance crashes, it transitions to the dropped state.

就绪 (RD)Ready (RD)

在就绪状态下,实例已启动并且正在节点上运行。In the ready state, the instance is up and running on the node. 如果此实例是可靠的服务,则已调用 RunAsync。 If this instance is a reliable service, RunAsync has been invoked.

如果此实例的应用程序主机或节点发生故障,它将转换为已删除状态。If the application host or node for this instance crashes, it transitions to the dropped state.

关闭 (CL)Closing (CL)

在关闭状态下,Azure Service Fabric 正在关闭此节点上的实例。In the closing state, Azure Service Fabric is in the process of shutting down the instance on this node. 此关闭可能由多种原因所致,例如应用程序升级、负载均衡或正在删除服务。This shutdown might be due to many reasons--for example, an application upgrade, load balancing, or the service being deleted. 关闭完成后,它将转换为已删除状态。After shutdown finishes, it transitions to the dropped state.

已删除 (DD)Dropped (DD)

在已删除状态下,实例不再在节点上运行。In the dropped state, the instance is no longer running on the node. 此时,Service Fabric 维护关于此实例的元数据,该元数据最终也将被删除。At this point, Service Fabric maintains the metadata about this instance, which is eventually deleted as well.

备注

可通过使用 Remove-ServiceFabricReplica 上的 ForceRemove 选项实现从任意状态到已删除状态的转换。It is possible to transition from any state to the dropped state by using the ForceRemove option on Remove-ServiceFabricReplica.

有状态服务的副本Replicas of stateful services

有状态服务的副本是在其中一个群集节点上运行的服务逻辑的副本。A replica of a stateful service is a copy of the service logic running on one of the nodes of the cluster. 此外,副本会保留该服务的状态的副本。In addition, the replica maintains a copy of the state of that service. 以下两个相关的概念介绍了有状态副本的生命周期和行为:Two related concepts describe the lifecycle and behavior of stateful replicas:

  • 副本生命周期Replica lifecycle
  • 副本角色Replica role

以下讨论介绍了持久的有状态服务。The following discussion describes persisted stateful services. 对于临时的(或内存中)有状态服务,关闭状态和已删除状态等效。For volatile (or in-memory) stateful services, the down and dropped states are equivalent.

副本生命周期

InBuild (IB)InBuild (IB)

InBuild 副本是创建或准备加入副本集的副本。An InBuild replica is a replica that's created or prepared for joining the replica set. 根据副本角色,IB 具有不同的语义。Depending on the replica role, the IB has different semantics.

如果 InBuild 副本的应用程序主机或节点发生故障,它将转换为关闭状态。If the application host or the node for an InBuild replica crashes, it transitions to the down state.

  • 主 InBuild 副本 :主 InBuild 副本是分区的第一个副本。Primary InBuild replicas : Primary InBuild are the first replicas for a partition. 此副本通常发生在创建分区时。This replica usually happens when the partition is being created. 当分区的所有副本均重启或删除时,主 InBuild 副本也会出现。Primary InBuild replicas also arise when all the replicas of a partition restart or are dropped.

  • IdleSecondary InBuild 副本 :这些副本可以是群集资源管理器创建的新副本,也可以是已关闭并需要添加回集的现有副本。IdleSecondary InBuild replicas : These are either new replicas that are created by the Cluster Resource Manager, or existing replicas that went down and need to be added back into the set. 这些副本先由主要副本播种或生成,然后才能加入到副本集作为活动的次要副本并参与操作的仲裁确认。These replicas are seeded or built by the primary before they can join the replica set as ActiveSecondary and participate in quorum acknowledgement of operations.

  • ActiveSecondary InBuild 副本 :此状态可在某些查询中观察到。ActiveSecondary InBuild replicas : This state is observed in some queries. 这是一种优化,其中副本集不会更改,但需要生成一个副本。It is an optimization where the replica set is not changing, but a replica needs to be built. 副本本身遵循常规状态的计算机转换(如副本角色部分所述)。The replica itself follows the normal state machine transitions (as described in the section on replica roles).

就绪 (RD)Ready (RD)

就绪副本是参与复制和操作的仲裁确认的副本。A Ready replica is a replica that's participating in replication and quorum acknowledgement of operations. 就绪状态适用于主要副本和活动的次要副本。The ready state is applicable to primary and active secondary replicas.

如果就绪副本的应用程序主机或节点发生故障,它将转换为关闭状态。If the application host or the node for a ready replica crashes, it transitions to the down state.

关闭 (CL)Closing (CL)

在以下情况下,副本会进入关闭状态:A replica enters the closing state in the following scenarios:

  • 关闭副本代码 :Service Fabric 可能需要关闭副本的运行代码。Shutting down the code for the replica : Service Fabric might need to shut down the running code for a replica. 此关闭可能由多种原因导致。This shutdown might be for many reasons. 例如,它可能由应用程序、构造或基础结构升级导致,或由副本报告的错误导致。For example, it can happen because of an application, fabric, or infrastructure upgrade, or because of a fault reported by the replica. 副本关闭完成后,副本将转换为关闭状态。When the replica close finishes, the replica transitions to the down state. 与存储在磁盘上的此副本关联的持久状态未得到清理。The persisted state associated with this replica that's stored on disk is not cleaned up.

  • 从群集中删除副本 :Service Fabric 可能需要删除持久状态并关闭副本的运行代码。Removing the replica from the cluster : Service Fabric might need to remove the persisted state and shut down the running code for a replica. 此关闭可能由多种原因导致,例如负载均衡。This shutdown might be for many reasons, for example, load balancing.

已删除 (DD)Dropped (DD)

在已删除状态下,实例不再在节点上运行。In the dropped state, the instance is no longer running on the node. 节点上也没有保留任何状态。There is also no state left on the node. 此时,Service Fabric 维护关于此实例的元数据,该元数据最终也将被删除。At this point, Service Fabric maintains the metadata about this instance, which is eventually deleted as well.

关闭 (D)Down (D)

在关闭状态下,副本代码未运行,但该副本的持久状态存在于该节点上。In the down state, the replica code is not running, but the persisted state for that replica exists on that node. 副本可能会因多种原因而关闭,例如节点关闭、副本代码发生故障、应用程序升级或副本错误。A replica can be down for many reasons--for example, the node being down, a crash in the replica code, an application upgrade, or replica faults.

关闭的副本可按需由 Service Fabric 打开,例如当完成节点上的升级时。A down replica is opened by Service Fabric as required, for example, when the upgrade finishes on the node.

副本角色在关闭状态下不相关。The replica role is not relevant in the down state.

打开 (OP)Opening (OP)

当 Service Fabric 需要再次备份副本时,关闭的副本将进入打开状态。A down replica enters the opening state when Service Fabric needs to bring the replica back up again. 例如,此状态可能出现在应用程序的代码升级在节点上完成后。For example, this state might be after a code upgrade for the application finishes on a node.

如果打开的副本的应用程序主机或节点发生故障,它将转换为关闭状态。If the application host or the node for an opening replica crashes, it transitions to the down state.

副本角色在打开状态下不相关。The replica role is not relevant in the opening state.

备用 (SB)StandBy (SB)

备用副本是在关闭后打开的持久服务的副本。A StandBy replica is a replica of a persisted service that went down and was then opened. 如果需要将另一副本添加到副本集,此副本可供 Service Fabric 使用(因为此副本已经具有状态的某些部分,生成过程将更快)。This replica might be used by Service Fabric if it needs to add another replica to the replica set (because the replica already has some portion of the state and the build process is faster). 在 StandByReplicaKeepDuration 到期后,备用副本将被丢弃。After the StandByReplicaKeepDuration expires, the standby replica is discarded.

如果备用副本的应用程序主机或节点发生故障,它将转换为关闭状态。If the application host or the node for a standby replica crashes, it transitions to the down state.

副本角色在备用状态下不相关。The replica role is not relevant in the standby state.

备注

任何未关闭或删除的副本将被视为可启动 的副本。Any replica that's not down or dropped is considered to be up .

备注

可通过使用 Remove-ServiceFabricReplica 上的 ForceRemove 选项实现从任意状态到已删除状态的转换。It's possible to transition from any state to the dropped state by using the ForceRemove option on Remove-ServiceFabricReplica.

副本角色Replica role

副本的角色确定其在副本集内的功能:The role of the replica determines its function in the replica set:

  • 主要副本 (P) :副本集有一个主要副本,该副本负责执行读取和写入操作。Primary (P) : There is one primary in the replica set that is responsible for performing read and write operations.
  • 活动的次要副本 (S) :此类副本用于从主要副本接收状态更新、应用这些更新,然后发送回确认。ActiveSecondary (S) : These are replicas that receive state updates from the primary, apply them, and then send back acknowledgements. 副本集内有多个活动的次要副本。There are multiple active secondaries in the replica set. 这些活动次要副本的数量确定服务可以处理的错误数量。The number of these active secondaries determines the number of faults the service can handle.
  • 空闲的次要副本 (I) :这些副本通过主要副本生成。IdleSecondary (I) : These replicas are being built by the primary. 在将其升级到活动次要副本之前,它们接收来自主要副本的状态。They are receiving state from the primary before they can be promoted to active secondary.
  • 无 (N) :这些副本在副本集内没有任何职责。None (N) : These replicas don't have a responsibility in the replica set.
  • 未知 (U) :在副本从 Service Fabric 接收任何 ChangeRole API 调用前,该角色为副本的初始角色 。Unknown (U) : This is the initial role of a replica before it receives any ChangeRole API call from Service Fabric.

以下图表说明了副本角色转换以及可能发生此类转换的一些示例方案:The following diagram illustrates the replica role transitions and some example scenarios in which they can occur:

副本角色

  • U -> P:创建新的主要副本。U -> P: Creation of a new primary replica.
  • U -> I:创建新的空闲副本。U -> I: Creation of a new idle replica.
  • U -> N:删除备用副本。U -> N: Deletion of a standby replica.
  • I -> S:空闲的次要副本升级为活动的次要副本,以便其确认构成仲裁。I -> S: Promotion of the idle secondary to active secondary so that its acknowledgements contribute toward quorum.
  • I -> P:空闲的次要副本升级为主要副本。I -> P: Promotion of the idle secondary to primary. 当空闲的次要副本成为主要副本的正确候选项时,在特殊的重新配置下可能发生此情况。This can happen under special reconfigurations when the idle secondary is the correct candidate to be primary.
  • I -> N:删除空闲的次要副本。I -> N: Deletion of the idle secondary replica.
  • S -> P:活动的次要副本升级为主要副本。S -> P: Promotion of the active secondary to primary. 这可能由主要副本的故障转移或由群集资源管理器启动的主要副本移动导致。This can be due to failover of the primary or a primary movement initiated by the Cluster Resource Manager. 例如,主要副本可能响应应用程序升级或负载均衡。For example, it might be in response to an application upgrade or load balancing.
  • S -> N:删除活动的次要副本。S -> N: Deletion of the active secondary replica.
  • P -> S:主要副本降级。P -> S: Demotion of the primary replica. 这可能由群集资源管理器启动的主要副本移动导致。This can be due to a primary movement initiated by the Cluster Resource Manager. 例如,主要副本可能响应应用程序升级或负载均衡。For example, it might be in response to an application upgrade or load balancing.
  • P -> N:删除主要副本。P -> N: Deletion of the primary replica.

备注

更高级别的编程模型(如 Reliable ActorsReliable Services)对开发人员隐藏副本角色的概念。Higher-level programming models, such as Reliable Actors and Reliable Services, hide the concept of replica roles from the developer. 在 Actors 中,角色的概念是不必要的。In Actors, the notion of a role is unnecessary. 在 Services 中,大多数情况下它进行了很大程度的简化。In Services, it's largely simplified for most scenarios.

后续步骤Next steps

有关 Service Fabric 概念的详细信息,请参阅以下文章:For more information on Service Fabric concepts, see the following article:

Reliable Services 生命周期 - C#Reliable Services lifecycle - C#