複本和執行個體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:

執行個體生命週期

建置中 (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. 如果這個執行個體是可靠服務,便已叫用 RunAsyncIf 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.

複本生命週期

建置中 (IB)InBuild (IB)

建置中複本是正在建立中或已準備好加入複本集的複本。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.

如果建置中複本的應用程式主機或節點損毀,則會轉換成關閉狀態。If the application host or the node for an InBuild replica crashes, it transitions to the down state.

  • 主要建置中複本:主要建置中複本是分割區的第一個複本。Primary InBuild replicas: Primary InBuild are the first replicas for a partition. 這個複本通常發生分割區正在建立時。This replica usually happens when the partition is being created. 當分割區所有的複本重新啟動或已卸除時,也會引發主要建置中複本。Primary InBuild replicas also arise when all the replicas of a partition restart or are dropped.

  • IdleSecondary 建置中複本:這些是叢集資源管理員建立的新複本,或是已關閉且需要新增回複本集的現有複本。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. 這些複本在由主要複本植入或建置後,才能夠加入複本集作為 ActiveSecondary,並參與作業的仲裁認知。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 建置中複本:這個狀態能在某些查詢中觀察到。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.
  • ActiveSecondary (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.
  • IdleSecondary (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.
  • I -> 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#