服务移动成本Service movement cost

尝试确定要对群集进行哪些更改时,Service Fabric 群集资源管理器考虑的一个因素是这些更改的成本。A factor that the Service Fabric Cluster Resource Manager considers when trying to determine what changes to make to a cluster is the cost of those changes. “成本”这一概念根据能够改进的群集量而权衡。The notion of "cost" is traded off against how much the cluster can be improved. 移动服务以满足均衡、碎片整理和其他要求时,成本是一项考虑因素。Cost is factored in when moving services for balancing, defragmentation, and other requirements. 目标是以最稳妥或最便宜的方式满足这些要求。The goal is to meet the requirements in the least disruptive or expensive way.

移动服务可将 CPU 时间和网络带宽的成本降到最低。Moving services costs CPU time and network bandwidth at a minimum. 对于有状态服务,需要复制消耗额外内存和磁盘的这些服务的状态。For stateful services, it requires copying the state of those services, consuming additional memory and disk. 将 Azure Service Fabric 群集资源管理器所提出的解决方案成本降到最低有助于避免使用不必要的群集资源。Minimizing the cost of solutions that the Azure Service Fabric Cluster Resource Manager comes up with helps ensure that the cluster's resources aren't spent unnecessarily. 但是,也不希望忽略可大幅改善群集中资源分配的解决方案。However, you also don't want to ignore solutions that would significantly improve the allocation of resources in the cluster.

群集资源管理器提供两种方式来计算和限制成本,同时尝试管理群集。The Cluster Resource Manager has two ways of computing costs and limiting them while it tries to manage the cluster. 第一种机制只需计算它所进行的每次移动。The first mechanism is simply counting every move that it would make. 如果生成的两个解决方案的均衡值(分数)大致相同,则群集资源管理器倾向于采用成本(移动总量)最低的解决方案。If two solutions are generated with about the same balance (score), then the Cluster Resource Manager prefers the one with the lowest cost (total number of moves).

此策略很适用。This strategy works well. 但是,对于默认负载或静态负载,在任何复杂的系统中不太可能所有移动都一样。But as with default or static loads, it's unlikely in any complex system that all moves are equal. 有些移动的成本可能要昂贵得多。Some are likely to be much more expensive.

设置移动成本Setting Move Costs

可在服务创建时为其指定默认的移动成本:You can specify the default move cost for a service when it is created:


New-ServiceFabricService -ApplicationName $applicationName -ServiceName $serviceName -ServiceTypeName $serviceTypeName -Stateful -MinReplicaSetSize 3 -TargetReplicaSetSize 3 -PartitionSchemeSingleton -DefaultMoveCost Medium


FabricClient fabricClient = new FabricClient();
StatefulServiceDescription serviceDescription = new StatefulServiceDescription();
//set up the rest of the ServiceDescription
serviceDescription.DefaultMoveCost = MoveCost.Medium;
await fabricClient.ServiceManager.CreateServiceAsync(serviceDescription);

此外,还可在服务创建后为该服务动态指定或更新 MoveCost:You can also specify or update MoveCost dynamically for a service after the service has been created:


Update-ServiceFabricService -Stateful -ServiceName "fabric:/AppName/ServiceName" -DefaultMoveCost High


StatefulServiceUpdateDescription updateDescription = new StatefulServiceUpdateDescription();
updateDescription.DefaultMoveCost = MoveCost.High;
await fabricClient.ServiceManager.UpdateServiceAsync(new Uri("fabric:/AppName/ServiceName"), updateDescription);

动态指定每个副本的移动成本Dynamically specifying move cost on a per-replica basis

前面的代码片段全部用于从服务本身外部同时指定整个服务的 MoveCost。The preceding snippets are all for specifying MoveCost for a whole service at once from outside the service itself. 但是,当特定服务对象的移动成本在其生命周期内发生变化时,移动成本最为有用。However, move cost is most useful is when the move cost of a specific service object changes over its lifespan. 由于服务本身可能最了解移动给定时间的成本是多少,因此服务可以在运行时使用 API 来报告自己的移动成本。Since the services themselves probably have the best idea of how costly they are to move a given time, there's an API for services to report their own individual move cost during runtime.



移动成本的影响Impact of move cost

MoveCost 有五个级别:Zero、Low、Medium、High 和 VeryHigh。MoveCost has five levels: Zero, Low, Medium, High and VeryHigh. 以下规则适用:The following rules apply:

  • MoveCost 是相互的,但 Zero 和 VeryHigh 除外。MoveCosts are relative to each other, except for Zero and VeryHigh.
  • “零”移动成本表示移动不会产生成本,不应计入解决方案的分数。Zero move cost means that movement is free and should not count against the score of the solution.
  • 将移动成本设置为 High 或 VeryHigh 并不能确保副本不会被移动。 Setting your move cost to High or VeryHigh does not provide a guarantee that the replica will never be moved.
  • 移动成本为 VeryHigh 的副本进行移动的前提是在群集中存在约束冲突,该冲突无法通过任何其他方式解决(即使需要移动许多其他的副本来解决冲突)Replicas with VeryHigh move cost will be moved only if there is a constraint violation in the cluster that cannot be fixed in any other way (even if it requires moving many other replicas to fix the violation)


MoveCost 可帮助我们在达成对等的均衡时,查找整体导致最少中断且最容易实现的解决方案。MoveCost helps you find the solutions that cause the least disruption overall and are easiest to achieve while still arriving at equivalent balance. 服务的成本概念可以相对于许多事项。A service's notion of cost can be relative to many things. 计算移动成本时的最常见因素包括:The most common factors in calculating your move cost are:

  • 服务必须移动的状态或数据量。The amount of state or data that the service has to move.
  • 客户端断开连接的成本。The cost of disconnection of clients. 移动主要副本的成本通常比移动次要副本的成本更高。Moving a primary replica is usually more costly than the cost of moving a secondary replica.
  • 中断某些进行中操作的成本。The cost of interrupting an in-flight operation. 某些数据存储级别的操作,或者为了响应客户端调用而执行的操作,成本都很高。Some operations at the data store level or operations performed in response to a client call are costly. 在特定的时间点后,除非有必要,否则我们不会停止这些操作。After a certain point, you don't want to stop them if you don't have to. 因此,当操作正在进行时,提高该服务对象的移动成本可以降低其移动的可能性。So while the operation is going on, you increase the move cost of this service object to reduce the likelihood that it moves. 当操作完成之后,可以将成本重新设置为正常。When the operation is done, you set the cost back to normal.


应慎重考虑使用 VeryHigh 移动成本,因为它会极大地限制群集资源管理器在群集中查找全局最佳放置解决方案的功能。Using the VeryHigh move cost should be carefully considered as it significantly restricts the ability of Cluster Resource Manager to find a globally-optimal placement solution in the cluster. 移动成本为 VeryHigh 的副本进行移动的前提是在群集中存在约束冲突,该冲突无法通过任何其他方式解决(即使需要移动许多其他的副本来解决冲突)Replicas with VeryHigh move cost will be moved only if there is a constraint violation in the cluster that cannot be fixed in any other way (even if it requires moving many other replicas to fix the violation)

在群集中启用移动成本Enabling move cost in your cluster

为了考虑到更为精细的 MoveCost,必须在群集中启用 MoveCost。In order for the more granular MoveCosts to be taken into account, MoveCost must be enabled in your cluster. 如不进行此设置,则会使用计数移动的默认模式来计算 MoveCost,并忽略 MoveCost 报告。Without this setting, the default mode of counting moves is used for calculating MoveCost, and MoveCost reports are ignored.


<Section Name="PlacementAndLoadBalancing">
    <Parameter Name="UseMoveCostReports" Value="true" />

通过用于独立部署的 ClusterConfig.json 或用于 Azure 托管群集的 Template.json:via ClusterConfig.json for Standalone deployments or Template.json for Azure hosted clusters:

"fabricSettings": [
    "name": "PlacementAndLoadBalancing",
    "parameters": [
          "name": "UseMoveCostReports",
          "value": "true"

后续步骤Next steps