创建用于缩放 Batch 池中的计算节点的自动公式Create an automatic formula for scaling compute nodes in a Batch pool

Azure Batch 可以根据定义的参数自动缩放池,节省时间和资金。Azure Batch can automatically scale pools based on parameters that you define, saving you time and money. Batch 通过自动缩放动态控制计算节点的数目,在任务需求提高时向池中添加节点,在任务需求降低时删除节点。With automatic scaling, Batch dynamically adds nodes to a pool as task demands increase, and removes compute nodes as task demands decrease.

若要为某个计算节点池启用自动缩放,请将该池与你所定义的自动缩放公式相关联。To enable automatic scaling on a pool of compute nodes, you associate the pool with an autoscale formula that you define. Batch 服务会使用自动缩放公式确定执行工作负载所需的节点数。The Batch service uses the autoscale formula to determine how many nodes are needed to execute your workload. 这些节点可以是专用节点。These nodes may be dedicated nodes. 然后,Batch 会定期检查服务指标数据,并使用它根据公式和你定义的时间间隔来调整池中的节点数。Batch will then periodically review service metrics data and use it to adjust the number of nodes in the pool based on your formula and at an interval that you define.

可以在创建池时启用自动缩放,也可以将其应用于现有池。You can enable automatic scaling when you create a pool, or apply it to an existing pool. Batch 使可以在将公式分配给池之前先评估公式,以及监视自动缩放运行的状态。Batch enables you to evaluate your formulas before assigning them to pools and to monitor the status of automatic scaling runs. 为池配置自动缩放后,可在以后对该公式做出更改。Once you configure a pool with automatic scaling, you can make changes to the formula later.

重要

创建 Batch 帐户时,可以指定池分配模式,用于确定是要在 Batch 服务订阅中(默认设置)还是在用户订阅中分配池。When you create a Batch account, you can specify the pool allocation mode, which determines whether pools are allocated in a Batch service subscription (the default) or in your user subscription. 如果使用默认的 Batch 服务配置创建了 Batch 帐户,则该帐户受限于可用于处理的最大核心数。If you created your Batch account with the default Batch service configuration, then your account is limited to a maximum number of cores that can be used for processing. Batch 服务最多只能将计算节点数扩展到该核心数限制。The Batch service scales compute nodes only up to that core limit. 出于此原因,Batch 服务可能达不到自动缩放公式所指定的目标计算节点数。For this reason, the Batch service may not reach the target number of compute nodes specified by an autoscale formula. 请参阅 Azure Batch 服务的配额和限制了解有关查看和提高帐户配额的信息。See Quotas and limits for the Azure Batch service for information on viewing and increasing your account quotas.

如果使用用户订阅模式创建了帐户,则该帐户会共享订阅的核心配额。If you created your account with user subscription mode, then your account shares in the core quota for the subscription. 有关详细信息,请参阅 Azure 订阅和服务限制、配额和约束条件中的虚拟机限制For more information, see Virtual Machines limits in Azure subscription and service limits, quotas, and constraints.

自动缩放公式Autoscale formulas

自动缩放公式是你定义的一个字符串值,其中包含一个或多个语句。An autoscale formula is a string value that you define that contains one or more statements. 自动缩放公式会分配给池的 autoScaleFormula 元素 (Batch REST) 或 CloudPool.AutoScaleFormula 属性 (Batch .NET)。The autoscale formula is assigned to a pool's autoScaleFormula element (Batch REST) or CloudPool.AutoScaleFormula property (Batch .NET). Batch 服务使用公式来确定池中可供下一个处理间隔使用的目标计算节点数。The Batch service uses your formula to determine the target number of compute nodes in the pool for the next interval of processing. 公式字符串不能超过 8 KB,最多可以包含 100 个以分号分隔的语句,可以包括换行符和注释。The formula string can't exceed 8 KB, can include up to 100 statements that are separated by semicolons, and can include line breaks and comments.

可以将自动缩放公式视为 Batch 自动缩放“语言”。You can think of automatic scaling formulas as a Batch autoscale "language." 公式语句是自由形式的表达式,可以包括服务定义的变量(由 Batch 服务定义)和用户定义的变量。Formula statements are free-formed expressions that can include both service-defined variables (defined by the Batch service) and user-defined variables. 公式可以通过内置类型、运算符和函数对这些值执行各种操作。Formulas can perform various operations on these values by using built-in types, operators, and functions. 例如,语句可以采用以下格式:For example, a statement might take the following form:

$myNewVariable = function($ServiceDefinedVariable, $myCustomVariable);

公式通常包含多个语句,这些语句对先前语句中获取的值执行操作。Formulas generally contain multiple statements that perform operations on values that are obtained in previous statements. 例如,首先获取 variable1 的值,并将其传递给一个函数来填充 variable2For example, first we obtain a value for variable1, then pass it to a function to populate variable2:

$variable1 = function1($ServiceDefinedVariable);
$variable2 = function2($OtherServiceDefinedVariable, $variable1);

在自动缩放公式中包含这些语句可实现计算节点的目标数。Include these statements in your autoscale formula to arrive at a target number of compute nodes. 专用节点有其自己的目标设置。Dedicated nodes have their own target settings. 自动缩放公式可以包含专用节点的目标值。An autoscale formula can include a target value for dedicated nodes.

节点的目标数可以大于、小于或等于池中该节点类型的当前数目。The target number of nodes may be higher, lower, or the same as the current number of nodes of that type in the pool. Batch 按特定的自动缩放间隔对池的自动缩放公式求值。Batch evaluates a pool's autoscale formula at a specific automatic scaling intervals. Batch 将池中每种节点类型的目标数调整成在求值时自动缩放公式所指定的数目。Batch adjusts the target number of each type of node in the pool to the number that your autoscale formula specifies at the time of evaluation.

示例自动缩放公式Sample autoscale formulas

下面是可以进行调整以适应大多数方案的两个自动缩放公式的示例。Below are examples of two autoscale formulas, which can be adjusted to work for most scenarios. 示例公式中的变量 startingNumberOfVMsmaxNumberofVMs 可以根据需要进行调整。The variables startingNumberOfVMs and maxNumberofVMs in the example formulas can be adjusted to your needs.

待定任务Pending tasks

对于此自动缩放公式,最初使用单个 VM 创建池。With this autoscale formula, the pool is initially created with a single VM. $PendingTasks 指标定义正在运行或已排队的任务数。The $PendingTasks metric defines the number of tasks that are running or queued. 该公式查找过去 180 秒内的平均挂起任务数,并相应地设置 $TargetDedicatedNodes 变量。The formula finds the average number of pending tasks in the last 180 seconds and sets the $TargetDedicatedNodes variable accordingly. 该公式确保专用节点的目标数永远不会超过 25 个 VM。The formula ensures that the target number of dedicated nodes never exceeds 25 VMs. 提交新任务时,池会自动增大。As new tasks are submitted, the pool automatically grows. 任务完成时,VM 会变为可用状态,自动缩放公式会收缩池。As tasks complete, VMs become free and the autoscaling formula shrinks the pool.

此公式缩放专用节点。This formula scales dedicated nodes.

startingNumberOfVMs = 1;
maxNumberofVMs = 25;
pendingTaskSamplePercent = $PendingTasks.GetSamplePercent(180 * TimeInterval_Second);
pendingTaskSamples = pendingTaskSamplePercent < 70 ? startingNumberOfVMs : avg($PendingTasks.GetSample(180 * TimeInterval_Second));
$TargetDedicatedNodes=min(maxNumberofVMs, pendingTaskSamples);
$NodeDeallocationOption = taskcompletion;

变量Variables

可在自动缩放公式中同时使用“服务定义”和“用户定义”的变量。You can use both service-defined and user-defined variables in your autoscale formulas.

服务定义的变量内置在 Batch 服务中。The service-defined variables are built in to the Batch service. 有些服务定义的变量是可读写的,有些是只读的。Some service-defined variables are read-write, and some are read-only.

用户定义的变量是定义的变量。User-defined variables are variables that you define. 在上述示例公式中,$TargetDedicatedNodes$PendingTasks 是服务定义的变量,而 startingNumberOfVMsmaxNumberofVMs 则是用户定义的变量。In the example formula shown above, $TargetDedicatedNodes and $PendingTasks are service-defined variables, while startingNumberOfVMs and maxNumberofVMs are user-defined variables.

备注

服务定义的变量始终前面带有美元符号 ($)。Service-defined variables are always preceded by a dollar sign ($). 对于用户定义的变量,美元符号是可选的。For user-defined variables, the dollar sign is optional.

下表显示了 Batch 服务定义的读写和只读变量。The following tables show the read-write and read-only variables that are defined by the Batch service.

可读写的服务定义变量Read-write service-defined variables

可以获取和设置这些服务定义的变量的值,以管理池中计算节点的数目。You can get and set the values of these service-defined variables to manage the number of compute nodes in a pool.

变量Variable 说明Description
$TargetDedicatedNodes$TargetDedicatedNodes 池的专用计算节点的目标数。The target number of dedicated compute nodes for the pool. 此项被指定为目标,因为池可能不会始终获得所需节点数。This is specified as a target because a pool may not always achieve the desired number of nodes. 例如,如果在池达到初始目标数之前专用节点的目标数被自动缩放评估修改,则池可能不会达到目标数。For example, if the target number of dedicated nodes is modified by an autoscale evaluation before the pool has reached the initial target, the pool may not reach the target.

如果目标数超过了 Batch 帐户节点或核心配额,则使用 Batch 服务模式创建的帐户中的池无法达到其目标。A pool in an account created in Batch service mode may not achieve its target if the target exceeds a Batch account node or core quota. 如果目标数超过了订阅的共享核心配额,则使用用户订阅模式创建的帐户中的池无法达到其目标。A pool in an account created in user subscription mode may not achieve its target if the target exceeds the shared core quota for the subscription.
$NodeDeallocationOption$NodeDeallocationOption 从池中删除计算节点时发生的操作。The action that occurs when compute nodes are removed from a pool. 可能的值包括:Possible values are:
  • requeue:默认值。requeue: The default value. 立即终止任务并将其放回作业队列,以便重新计划这些任务。Ends tasks immediately and puts them back on the job queue so that they are rescheduled. 此操作可确保尽快达到目标节点数。This action ensures the target number of nodes is reached as quickly as possible. 但是,其效率可能较低,因为任何正在运行的任务都会被中断,必须彻底重启。However, it may be less efficient, as any running tasks will be interrupted and will then have to be completely restarted.
  • terminate:立即终止任务并将其从作业队列中删除。terminate: Ends tasks immediately and removes them from the job queue.
  • taskcompletion:等待当前运行的任务完成,然后从池中删除节点。taskcompletion: Waits for currently running tasks to finish and then removes the node from the pool. 使用此选项可以避免任务被中断和重新排队,因此不会浪费任务已完成的任何工作。Use this option to avoid tasks being interrupted and requeued, wasting any work the task has done.
  • retaineddata:等待节点上本地任务保留的所有数据清理完毕,然后从池中删除节点。retaineddata: Waits for all the local task-retained data on the node to be cleaned up before removing the node from the pool.

备注

还可以使用别名 $TargetDedicated 指定 $TargetDedicatedNodes 变量。The $TargetDedicatedNodes variable can also be specified using the alias $TargetDedicated. 如果全名变量及其别名由公式设置,则分配给全名变量的值会优先处理。If the fully named variable and its alias are set by the formula, the value assigned to the fully named variable will take precedence.

只读服务定义的变量Read-only service-defined variables

可以获取这些服务定义的变量的值,以根据 Batch 服务中的指标进行调整。You can get the value of these service-defined variables to make adjustments that are based on metrics from the Batch service.

重要

作业释放任务当前未包含在提供任务计数的变量(例如 $ActiveTasks 和 $PendingTasks)中。Job release tasks are not currently included in variables that provide task counts, such as $ActiveTasks and $PendingTasks. 根据你的自动缩放公式,这可能会导致节点被删除,并且没有节点可用于运行作业释放任务。Depending on your autoscale formula, this can result in nodes being removed with no nodes available to run job release tasks.

变量Variable 说明Description
$CPUPercent$CPUPercent CPU 使用率的平均百分比。The average percentage of CPU usage.
$WallClockSeconds$WallClockSeconds 使用的秒数。The number of seconds consumed.
$MemoryBytes$MemoryBytes 使用的平均 MB 数。The average number of megabytes used.
$DiskBytes$DiskBytes 本地磁盘上使用的平均 GB 数。The average number of gigabytes used on the local disks.
$DiskReadBytes$DiskReadBytes 读取的字节数。The number of bytes read.
$DiskWriteBytes$DiskWriteBytes 写入的字节数。The number of bytes written.
$DiskReadOps$DiskReadOps 执行的读取磁盘操作数。The count of read disk operations performed.
$DiskWriteOps$DiskWriteOps 执行的写入磁盘操作数。The count of write disk operations performed.
$NetworkInBytes$NetworkInBytes 入站字节数。The number of inbound bytes.
$NetworkOutBytes$NetworkOutBytes 出站字节数。The number of outbound bytes.
$SampleNodeCount$SampleNodeCount 计算节点数。The count of compute nodes.
$ActiveTasks$ActiveTasks 已准备好执行但尚未执行的任务数。The number of tasks that are ready to execute but are not yet executing. 这包括处于活动状态且其依赖关系都得到满足的所有任务。This includes all tasks that are in the active state and whose dependencies have been satisfied. 处于活动状态但不满足其依赖关系的所有任务会从 $ActiveTasks 计数中排除。Any tasks that are in the active state but whose dependencies have not been satisfied are excluded from the $ActiveTasks count. 对于多实例任务,$ActiveTasks 将包含任务上设置的实例数。For a multi-instance task, $ActiveTasks will include the number of instances set on the task.
$RunningTasks$RunningTasks 处于运行状态的任务数。The number of tasks in a running state.
$PendingTasks$PendingTasks $ActiveTasks 和 $RunningTasks 的总和。The sum of $ActiveTasks and $RunningTasks.
$SucceededTasks$SucceededTasks 成功完成的任务数。The number of tasks that finished successfully.
$FailedTasks$FailedTasks 失败的任务数。The number of tasks that failed.
$CurrentDedicatedNodes$CurrentDedicatedNodes 当前的专用计算节点数。The current number of dedicated compute nodes.

提示

这些只读的服务定义变量是对象,它们提供了各种方法来访问与其关联的数据。These read-only service-defined variables are objects that provide various methods to access data associated with each. 有关详细信息,请参阅本文稍后的获取样本数据For more information, see Obtain sample data later in this article.

类型Types

自动缩放公式支持以下类型:Autoscale formulas support the following types:

  • Doubledouble
  • doubleVecdoubleVec
  • doubleVecListdoubleVecList
  • stringstring
  • timestamp--一个包含以下成员的复合结构:timestamp--a compound structure that contains the following members:
    • yearyear
    • month (1-12)month (1-12)
    • day (1-31)day (1-31)
    • weekday(采用数字格式,例如 1 表示星期一)weekday (in the format of number; for example, 1 for Monday)
    • hour(采用 24 时制数字格式,例如 13 表示下午 1 点)hour (in 24-hour number format; for example, 13 means 1 PM)
    • minute (00-59)minute (00-59)
    • second (00-59)second (00-59)
  • timeintervaltimeinterval
    • TimeInterval_ZeroTimeInterval_Zero
    • TimeInterval_100nsTimeInterval_100ns
    • TimeInterval_MicrosecondTimeInterval_Microsecond
    • TimeInterval_MillisecondTimeInterval_Millisecond
    • TimeInterval_SecondTimeInterval_Second
    • TimeInterval_MinuteTimeInterval_Minute
    • TimeInterval_HourTimeInterval_Hour
    • TimeInterval_DayTimeInterval_Day
    • TimeInterval_WeekTimeInterval_Week
    • TimeInterval_YearTimeInterval_Year

操作Operations

上一部分中列出的类型允许以下操作。These operations are allowed on the types that are listed in the previous section.

操作Operation 支持的运算符Supported operators 结果类型Result type
double operator doubledouble operator double +, -, *, /+, -, *, / Doubledouble
double operator timeintervaldouble operator timeinterval * timeintervaltimeinterval
doubleVec operator doubledoubleVec operator double +, -, *, /+, -, *, / doubleVecdoubleVec
doubleVec operator doubleVecdoubleVec operator doubleVec +, -, *, /+, -, *, / doubleVecdoubleVec
timeinterval operator doubletimeinterval operator double *, /*, / timeintervaltimeinterval
timeinterval operator timeintervaltimeinterval operator timeinterval +, -+, - timeintervaltimeinterval
timeinterval operator timestamptimeinterval operator timestamp + timestamptimestamp
timestamp operator timeintervaltimestamp operator timeinterval + timestamptimestamp
timestamp operator timestamptimestamp operator timestamp - timeintervaltimeinterval
operator doubleoperator double -, !-, ! Doubledouble
operator timeintervaloperator timeinterval - timeintervaltimeinterval
double operator doubledouble operator double <, <=, ==, >=, >, !=<, <=, ==, >=, >, != Doubledouble
string operator stringstring operator string <, <=, ==, >=, >, !=<, <=, ==, >=, >, != Doubledouble
timestamp operator timestamptimestamp operator timestamp <, <=, ==, >=, >, !=<, <=, ==, >=, >, != Doubledouble
timeinterval operator timeintervaltimeinterval operator timeinterval <, <=, ==, >=, >, !=<, <=, ==, >=, >, != Doubledouble
double operator doubledouble operator double &&, ||&&, || Doubledouble

使用三元运算符 (double ? statement1 : statement2) 测试双精度值时,非零值为 true,零值为 falseWhen testing a double with a ternary operator (double ? statement1 : statement2), nonzero is true, and zero is false.

函数Functions

在定义自动缩放公式时,可以使用这些预定义的函数You can use these predefined functions when defining an autoscale formula.

函数Function 返回类型Return type 说明Description
avg(doubleVecList)avg(doubleVecList) Doubledouble 返回 doubleVecList 中所有值的平均值。Returns the average value for all values in the doubleVecList.
len(doubleVecList)len(doubleVecList) Doubledouble 返回从 doubleVecList 创建的向量的长度。Returns the length of the vector that is created from the doubleVecList.
lg(double)lg(double) Doubledouble 返回 double 的对数底数 2。Returns the log base 2 of the double.
lg(doubleVecList)lg(doubleVecList) doubleVecdoubleVec 返回 doubleVecList 的分量对数底数 2。Returns the component-wise log base 2 of the doubleVecList. 必须为参数显式传递 vec(double)。A vec(double) must be explicitly passed for the parameter. 否则会采用 double lg(double) 版本。Otherwise, the double lg(double) version is assumed.
ln(double)ln(double) Doubledouble 返回 double 的自然对数。Returns the natural log of the double.
ln(doubleVecList)ln(doubleVecList) doubleVecdoubleVec 返回 double 的自然对数。Returns the natural log of the double.
log(double)log(double) Doubledouble 返回 double 的对数底数 10。Returns the log base 10 of the double.
log(doubleVecList)log(doubleVecList) doubleVecdoubleVec 返回 doubleVecList 的分量对数底数 10。Returns the component-wise log base 10 of the doubleVecList. 对于单一的 double 参数,必须显式传递 vec(double)。A vec(double) must be explicitly passed for the single double parameter. 否则会采用 double log(double) 版本。Otherwise, the double log(double) version is assumed.
max(doubleVecList)max(doubleVecList) Doubledouble 返回 doubleVecList 中的最大值。Returns the maximum value in the doubleVecList.
min(doubleVecList)min(doubleVecList) Doubledouble 返回 doubleVecList 中的最小值。Returns the minimum value in the doubleVecList.
norm(doubleVecList)norm(doubleVecList) Doubledouble 返回从 doubleVecList 创建的向量的二范数。Returns the two-norm of the vector that is created from the doubleVecList.
percentile(doubleVec v, double p)percentile(doubleVec v, double p) Doubledouble 返回向量 v 的百分位元素。Returns the percentile element of the vector v.
rand()rand() Doubledouble 返回介于 0.0 和 1.0 之间的随机值。Returns a random value between 0.0 and 1.0.
range(doubleVecList)range(doubleVecList) Doubledouble 返回 doubleVecList 中最小值和最大值之间的差。Returns the difference between the min and max values in the doubleVecList.
std(doubleVecList)std(doubleVecList) Doubledouble 返回 doubleVecList 中值的样本标准偏差。Returns the sample standard deviation of the values in the doubleVecList.
stop()stop() 停止对自动缩放表达式求值。Stops evaluation of the autoscaling expression.
sum(doubleVecList)sum(doubleVecList) Doubledouble 返回 doubleVecList 的所有组成部分之和。Returns the sum of all the components of the doubleVecList.
time(string dateTime="")time(string dateTime="") timestamptimestamp 如果未传递参数,则返回当前时间的时间戳;如果传递了参数,则返回 dateTime 字符串的时间戳。Returns the time stamp of the current time if no parameters are passed, or the time stamp of the dateTime string if that is passed. 支持的 dateTime 格式为 W3C-DTF 和 RFC 1123。Supported dateTime formats are W3C-DTF and RFC 1123.
val(doubleVec v, double i)val(doubleVec v, double i) Doubledouble 返回在起始索引为零的向量 v 中,位置 i 处的元素的值。Returns the value of the element that is at location i in vector v, with a starting index of zero.

上表中描述的某些函数可以接受列表作为参数。Some of the functions that are described in the previous table can accept a list as an argument. 逗号分隔列表为 doubledoubleVec 的任意组合。The comma-separated list is any combination of double and doubleVec. 例如:For example:

doubleVecList := ( (double | doubleVec)+(, (double | doubleVec) )* )?

doubleVecList 值在计算之前会转换为单个 doubleVecThe doubleVecList value is converted to a single doubleVec before evaluation. 例如,如果 v = [1,2,3],则调用 avg(v) 相当于调用 avg(1,2,3)For example, if v = [1,2,3], then calling avg(v) is equivalent to calling avg(1,2,3). 调用 avg(v, 7) 相当于调用 avg(1,2,3,7)Calling avg(v, 7) is equivalent to calling avg(1,2,3,7).

指标Metrics

在定义公式时,可以同时使用资源和任务指标。You can use both resource and task metrics when you're defining a formula. 可根据获取和求值的度量值数据对池中专用节点的目标数目进行调整。You adjust the target number of dedicated nodes in the pool based on the metrics data that you obtain and evaluate. 有关每个指标的详细信息,请参阅上面的变量部分。For more information on each metric, see the Variables section above.

指标Metric 说明Description
资源Resource

资源指标基于计算节点的 CPU、带宽和内存使用量以及节点数。Resource metrics are based on the CPU, the bandwidth, the memory usage of compute nodes, and the number of nodes.

这些服务定义的变量可用于根据节点计数进行调整:These service-defined variables are useful for making adjustments based on node count:

  • $TargetDedicatedNodes$TargetDedicatedNodes
  • $CurrentDedicatedNodes$CurrentDedicatedNodes
  • $SampleNodeCount$SampleNodeCount

这些服务定义的变量可用于根据节点资源使用量进行调整:These service-defined variables are useful for making adjustments based on node resource usage:

  • $CPUPercent$CPUPercent
  • $WallClockSeconds$WallClockSeconds
  • $MemoryBytes$MemoryBytes
  • $DiskBytes$DiskBytes
  • $DiskReadBytes$DiskReadBytes
  • $DiskWriteBytes$DiskWriteBytes
  • $DiskReadOps$DiskReadOps
  • $DiskWriteOps$DiskWriteOps
  • $NetworkInBytes$NetworkInBytes
  • $NetworkOutBytes$NetworkOutBytes

TaskTask

任务指标基于任务的状态(例如活动、挂起和已完成)。Task metrics are based on the status of tasks, such as Active, Pending, and Completed. 以下服务定义的变量可用于根据任务度量值调整池大小:The following service-defined variables are useful for making pool-size adjustments based on task metrics:

  • $ActiveTasks$ActiveTasks
  • $RunningTasks$RunningTasks
  • $PendingTasks$PendingTasks
  • $SucceededTasks$SucceededTasks
  • $FailedTasks$FailedTasks

获取示例数据Obtain sample data

自动缩放公式的核心操作是获取任务和资源指标数据(样本),并根据该数据调整池大小。The core operation of an autoscale formula is to obtain task and resource metric data (samples), and then adjust pool size based on that data. 因此,请务必明确知道自动缩放公式如何与样本进行交互。As such, it's important to have a clear understanding of how autoscale formulas interact with samples.

方法Methods

自动缩放公式使用 Batch 服务提供的指标数据样本。Autoscale formulas act on samples of metric data provided by the Batch service. 公式会根据所获得的值来扩大或缩小池。A formula will grow or shrink the pool size based on the values that it obtains. 服务定义的变量是一些对象,它们提供了方法来访问与该对象关联的数据。Service-defined variables are objects that provide methods to access data that is associated with that object. 例如,以下表达式显示了一个用于获取过去五分钟 CPU 使用率的请求:For example, the following expression shows a request to get the last five minutes of CPU usage:

$CPUPercent.GetSample(TimeInterval_Minute * 5)

可以使用以下方法获取有关服务定义的变量的样本数据。The following methods may be used to obtain sample data about service-defined variables.

方法Method 说明Description
GetSample()GetSample() GetSample() 方法返回数据样本的向量。The GetSample() method returns a vector of data samples.

一个样本最好包含 30 秒钟的度量值数据。A sample is 30 seconds worth of metrics data. 换而言之,将每隔 30 秒获取一次样本。In other words, samples are obtained every 30 seconds. 但如下所示,样本在收集后需经历一定的延迟才能供公式使用。But as noted below, there is a delay between when a sample is collected and when it is available to a formula. 因此,并非一段指定时间内的所有样本都可用于公式求值。As such, not all samples for a given time period may be available for evaluation by a formula.
  • doubleVec GetSample(double count):指定从已收集的最近样本中获得的样本数。doubleVec GetSample(double count): Specifies the number of samples to obtain from the most recent samples that were collected. GetSample(1) 返回最后一个可用样本。GetSample(1) returns the last available sample. 但对于 $CPUPercent 这样的变量,不应使用 GetSample(1),因为无法获知样本是何时收集的。For metrics like $CPUPercent, however, GetSample(1) shouldn't be used, because it's impossible to know when the sample was collected. 它可能是最近收集的数据,也可能是旧得多的数据(由于存在系统问题)。It could be recent, or, because of system issues, it might be much older. 在此类情况下,最好使用如下所示的时间间隔。In such cases, it's better to use a time interval as shown below.
  • doubleVec GetSample((timestamp or timeinterval) startTime [, double samplePercent]):指定收集样本数据的时间范围。doubleVec GetSample((timestamp or timeinterval) startTime [, double samplePercent]): Specifies a time frame for gathering sample data. (可选)它还指定必须在请求的时间范围内提供的样本的百分比。Optionally, it also specifies the percentage of samples that must be available in the requested time frame. 例如,如果 CPUPercent 历史记录中存在过去 10 分钟的所有样本,$CPUPercent.GetSample(TimeInterval_Minute * 10) 会返回 20 个样本。For example, $CPUPercent.GetSample(TimeInterval_Minute * 10) would return 20 samples if all samples for the last 10 minutes are present in the CPUPercent history. 如果最后一分钟的历史记录不可用,则只返回 18 个样本。If the last minute of history wasn't available, only 18 samples would be returned. 在这种情况下,$CPUPercent.GetSample(TimeInterval_Minute * 10, 95) 会失败,因为仅 90% 的样本可用,但 $CPUPercent.GetSample(TimeInterval_Minute * 10, 80) 会成功。In this case $CPUPercent.GetSample(TimeInterval_Minute * 10, 95) would fail because only 90 percent of the samples are available, but $CPUPercent.GetSample(TimeInterval_Minute * 10, 80) would succeed.
  • doubleVec GetSample((timestamp or timeinterval) startTime, (timestamp or timeinterval) endTime [, double samplePercent]):指定收集数据的时间范围(包括开始时间和结束时间)。doubleVec GetSample((timestamp or timeinterval) startTime, (timestamp or timeinterval) endTime [, double samplePercent]): Specifies a time frame for gathering data, with both a start time and an end time. 如前所述,在收集样本后,需等待一段时间,然后该样本才能供公式使用。As mentioned above, there is a delay between when a sample is collected and when it becomes available to a formula. 使用 GetSample 方法时,请考虑到这种延迟。Consider this delay when you use the GetSample method. 请参阅下面的 GetSamplePercentSee GetSamplePercent below.
GetSamplePeriod()GetSamplePeriod() 返回在历史样本数据集中采样的期间。Returns the period of samples that were taken in a historical sample data set.
Count()Count() 返回度量值历史记录中的样本总数。Returns the total number of samples in the metric history.
HistoryBeginTime()HistoryBeginTime() 返回度量值最旧可用数据样本的时间戳。Returns the time stamp of the oldest available data sample for the metric.
GetSamplePercent()GetSamplePercent() 返回给定时间间隔的可用样本百分比。Returns the percentage of samples that are available for a given time interval. 例如,doubleVec GetSamplePercent( (timestamp or timeinterval) startTime [, (timestamp or timeinterval) endTime] )For example, doubleVec GetSamplePercent( (timestamp or timeinterval) startTime [, (timestamp or timeinterval) endTime] ). 由于 GetSample 方法失败,因此如果返回的样本百分比低于指定的 samplePercent,则可先使用 GetSamplePercent 方法进行检查。Because the GetSample method fails if the percentage of samples returned is less than the samplePercent specified, you can use the GetSamplePercent method to check first. 然后,如果存在的样本数量不足,用户可以执行其他操作,无需停止自动缩放评估。Then you can perform an alternate action if insufficient samples are present, without halting the automatic scaling evaluation.

示例Samples

Batch 服务定期获取任务和资源指标的样本,使其可供自动缩放公式使用。The Batch service periodically takes samples of task and resource metrics and makes them available to your autoscale formulas. 这些样本每 30 秒由 Batch 服务记录一次。These samples are recorded every 30 seconds by the Batch service. 但是,记录样本的时间与样本可供自动缩放公式使用(与读取)的时间之间通常存在延迟。However, there is typically a delay between when those samples were recorded and when they are made available to (and can be read by) your autoscale formulas. 此外,由于各种因素(例如网络或其他基础结构问题),可能无法记录特定间隔的样本。Additionally, samples may not be recorded for a particular interval because of factors such as network or other infrastructure issues.

样本百分比Sample percentage

samplePercent 传递到 GetSample() 方法,或调用 GetSamplePercent() 方法时,“百分比”是指 Batch 服务记录的样本可能的总数与自动缩放公式可用的样本数之间的比值。When samplePercent is passed to the GetSample() method or the GetSamplePercent() method is called, percent refers to a comparison between the total possible number of samples that are recorded by the Batch service and the number of samples that are available to your autoscale formula.

让我们以 10 分钟的时间跨度为例。Let's look at a 10-minute timespan as an example. 由于每隔 30 秒记录一次样本,因此在 10 分钟的时间跨度内,Batch 服务所记录的样本总数会达到 20 个(每分钟 2 个)。Because samples are recorded every 30 seconds within that 10-minute timespan, the maximum total number of samples recorded by Batch would be 20 samples (2 per minute). 但是,由于报告机制固有的延迟,并且 Azure 中存在其他问题,可能只有 15 个样本可供自动缩放公式读取。However, due to the inherent latency of the reporting mechanism and other issues within Azure, there may be only 15 samples that are available to your autoscale formula for reading. 因此,举例来说,在这 10 分钟内,记录的样本总数只有 75% 可供公式使用。So, for example, for that 10-minute period, only 75% of the total number of samples recorded may be available to your formula.

GetSample() 和样本范围GetSample() and sample ranges

自动缩放公式会通过添加或删除节点对池进行扩大和缩小操作。Your autoscale formulas will grow and shrink your pools by adding or removing nodes. 由于节点耗费资金,请确保公式所使用的智能分析方法以足够的数据为基础。Because nodes cost you money, be sure that your formulas use an intelligent method of analysis that is based on sufficient data. 建议在公式中使用趋势类型的分析。We recommend that you use a trending-type analysis in your formulas. 此类型会根据所收集样本的范围来扩大和缩小池。This type grows and shrinks your pools based on a range of collected samples.

为此,请使用 GetSample(interval look-back start, interval look-back end) 返回样本的矢量:To do so, use GetSample(interval look-back start, interval look-back end) to return a vector of samples:

$runningTasksSample = $RunningTasks.GetSample(1 * TimeInterval_Minute, 6 * TimeInterval_Minute);

Batch 评估上述代码行后,会以值的向量形式返回样本范围。When the above line is evaluated by Batch, it returns a range of samples as a vector of values. 例如:For example:

$runningTasksSample=[1,1,1,1,1,1,1,1,1,1];

收集样本向量后,便可使用 min()max()avg() 等函数从所收集的范围派生有意义的值。Once you've collected the vector of samples, you can then use functions like min(), max(), and avg() to derive meaningful values from the collected range.

为了提高安全性,如果特定时间段小于特定的样本百分比,可强制公式求值失败。For additional security, you can force a formula evaluation to fail if less than a certain sample percentage is available for a particular time period. 强制公式求值失败会指示 Batch 在无法提供指定百分比的样本数时停止进一步的公式求值。When you force a formula evaluation to fail, you instruct Batch to cease further evaluation of the formula if the specified percentage of samples is not available. 在这种情况下,不更改池大小。In this case, no change is made to the pool size. 要指定求值成功所需的样本百分比,请将其指定为 GetSample()的第三个参数。To specify a required percentage of samples for the evaluation to succeed, specify it as the third parameter to GetSample(). 下面指定要求 75% 的样本:Here, a requirement of 75 percent of samples is specified:

$runningTasksSample = $RunningTasks.GetSample(60 * TimeInterval_Second, 120 * TimeInterval_Second, 75);

由于样本可用性可能存在延迟,因此应始终指定一个回查开始时间早于一分钟的时间范围。Because there may be a delay in sample availability, you should always specify a time range with a look-back start time that is older than one minute. 样本需要花大约一分钟的时间才能传播到整个系统,因此可能无法使用 (0 * TimeInterval_Second, 60 * TimeInterval_Second) 范围内的样本。It takes approximately one minute for samples to propagate through the system, so samples in the range (0 * TimeInterval_Second, 60 * TimeInterval_Second) may not be available. 同样地,可以使用 GetSample() 百分比参数来强制实施特定样本百分比要求。Again, you can use the percentage parameter of GetSample() to force a particular sample percentage requirement.

重要

强烈建议不要仅依赖自动缩放公式中的 GetSample(1)We strongly recommend that you avoid relying only on GetSample(1) in your autoscale formulas. 这是因为,GetSample(1) 基本上只是向 Batch 服务表明:“不论多久以前检索最后一个样本,请将它提供给我。”This is because GetSample(1) essentially says to the Batch service, "Give me the last sample you have, no matter how long ago you retrieved it." 由于它只是单个样本,而且可能是较旧的样本,因此可能无法代表最近任务或资源状态的全貌。Since it is only a single sample, and it may be an older sample, it may not be representative of the larger picture of recent task or resource state. 如果使用 GetSample(1),请确保它是更大语句的一部分,而不是公式所依赖的唯一数据点。If you do use GetSample(1), make sure that it's part of a larger statement and not the only data point that your formula relies on.

编写自动缩放公式Write an autoscale formula

构造自动缩放公式时,可以使用上述组件来生成语句,并将这些语句组合成完整的公式即可。You build an autoscale formula by forming statements that use the above components, then combine those statements into a complete formula. 本部分创建一个自动缩放公式示例,它可以执行实际缩放决策并进行调整。In this section, we create an example autoscale formula that can perform real-world scaling decisions and make adjustments.

首先,定义新自动缩放公式的要求。First, let's define the requirements for our new autoscale formula. 该公式应可以:The formula should:

  • 如果 CPU 使用率高,则增加池中专用计算节点的目标数。Increase the target number of dedicated compute nodes in a pool if CPU usage is high.
  • 如果 CPU 使用率低,则减少池中专用计算节点的目标数。Decrease the target number of dedicated compute nodes in a pool when CPU usage is low.
  • 始终将最大专用节点数限制为 400。Always restrict the maximum number of dedicated nodes to 400.
  • 减少节点数量时,不要删除正在运行任务的节点;如有必要,请等到任务完成后再删除节点。When reducing the number of nodes, don't remove nodes that are running tasks; if necessary, wait until tasks have finished before removing nodes.

在 CPU 使用率高时,公式中的第一个语句将增加节点数。The first statement in our formula will increase the number of nodes during high CPU usage. 我们需定义一个语句,规定仅当过去 10 分钟内的最小平均 CPU 使用率高于 70% 时,该语句才会向用户定义的变量 ($totalDedicatedNodes) 填充一个值,值的大小为专用节点当前目标数的 110%。We'll define a statement that populates a user-defined variable ($totalDedicatedNodes) with a value that is 110 percent of the current target number of dedicated nodes, but only if the minimum average CPU usage during the last 10 minutes was above 70 percent. 否则,它会使用专用节点的当前数值。Otherwise, it uses the value for the current number of dedicated nodes.

$totalDedicatedNodes =
    (min($CPUPercent.GetSample(TimeInterval_Minute * 10)) > 0.7) ?
    ($CurrentDedicatedNodes * 1.1) : $CurrentDedicatedNodes;

如果过去 60 分钟的平均 CPU 使用率低于 20%,则公式中的下一个语句会将同一 $totalDedicatedNodes 变量设置为专用节点当前目标数的 90%,这是为了在 CPU 使用率低时减少专用节点数。To decrease the number of dedicated nodes during low CPU usage, the next statement in our formula sets the same $totalDedicatedNodes variable to 90 percent of the current target number of dedicated nodes, if average CPU usage in the past 60 minutes was under 20 percent. 否则,它会使用我们在以上语句中填充的 $totalDedicatedNodes 的当前值。Otherwise, it uses the current value of $totalDedicatedNodes that we populated in the statement above.

$totalDedicatedNodes =
    (avg($CPUPercent.GetSample(TimeInterval_Minute * 60)) < 0.2) ?
    ($CurrentDedicatedNodes * 0.9) : $totalDedicatedNodes;

现在,我们需将专用计算节点的目标数限制为最大值 400。Now, we'll limit the target number of dedicated compute nodes to a maximum of 400.

$TargetDedicatedNodes = min(400, $totalDedicatedNodes)

最后,我们需确保在节点任务完成之前不会删除节点。Finally, we'll ensure that nodes aren't removed until their tasks are finished.

$NodeDeallocationOption = taskcompletion;

下面是完整公式:Here's the complete formula:

$totalDedicatedNodes =
    (min($CPUPercent.GetSample(TimeInterval_Minute * 10)) > 0.7) ?
    ($CurrentDedicatedNodes * 1.1) : $CurrentDedicatedNodes;
$totalDedicatedNodes =
    (avg($CPUPercent.GetSample(TimeInterval_Minute * 60)) < 0.2) ?
    ($CurrentDedicatedNodes * 0.9) : $totalDedicatedNodes;
$TargetDedicatedNodes = min(400, $totalDedicatedNodes)
$NodeDeallocationOption = taskcompletion;

备注

可以根据需要在公式字符串中包含注释和分行符。If you choose to, you can include both comments and line breaks in formula strings.

自动缩放间隔Automatic scaling interval

默认情况下,Batch 服务根据其自动缩放公式每隔 15 分钟调整池大小。By default, the Batch service adjusts a pool's size according to its autoscale formula every 15 minutes. 可使用以下池属性配置此间隔:This interval is configurable by using the following pool properties:

最小间隔为 5 分钟,最大间隔为 168 小时。The minimum interval is five minutes, and the maximum is 168 hours. 如果指定的间隔超出此范围,Batch 服务会返回“错误的请求(400)”错误。If an interval outside this range is specified, the Batch service returns a Bad Request (400) error.

备注

自动缩放目前不能以低于一分钟的时间响应更改,而只能在运行工作负荷时逐步调整池大小。Autoscaling is not currently intended to respond to changes in less than a minute, but rather is intended to adjust the size of your pool gradually as you run a workload.

使用 Batch SDK 创建支持自动缩放的池Create an autoscale-enabled pool with Batch SDKs

可以使用 Batch SDKBatch REST APIBatch PowerShell cmdletBatch CLI 中的任一个来配置池自动缩放。Pool autoscaling can be configured using any of the Batch SDKs, the Batch REST API Batch PowerShell cmdlets, and the Batch CLI. 在此部分,可以查看 .NET 和 Python 的示例。In this section, you can see examples for both .NET and Python.

.NET.NET

若要在 .NET 中创建启用自动缩放的池,请遵循以下步骤:To create a pool with autoscaling enabled in .NET, follow these steps:

  1. 使用 BatchClient.PoolOperations.CreatePool 创建池。Create the pool with BatchClient.PoolOperations.CreatePool.
  2. CloudPool.AutoScaleEnabled 属性设置为 trueSet the CloudPool.AutoScaleEnabled property to true.
  3. 使用自动缩放公式设置 CloudPool.AutoScaleFormula 属性。Set the CloudPool.AutoScaleFormula property with your autoscale formula.
  4. (可选)设置 CloudPool.AutoScaleEvaluationInterval 属性(默认值为 15 分钟)。(Optional) Set the CloudPool.AutoScaleEvaluationInterval property (default is 15 minutes).
  5. 使用 CloudPool.CommitCommitAsync 提交池。Commit the pool with CloudPool.Commit or CommitAsync.

以下示例使用 .NET 创建启用自动缩放的池。The following example creates an autoscale-enabled pool in .NET. 该池的自动缩放公式在星期一将专用节点的目标数设置为 5,在每周的其他日期将该目标数设置为 1。The pool's autoscale formula sets the target number of dedicated nodes to 5 on Mondays, and to 1 on every other day of the week. 自动缩放间隔设置为 30 分钟。The automatic scaling interval is set to 30 minutes. 在本文的此部分与其他 C# 代码片段中,myBatchClientBatchClient 类的适当初始化的实例。In this and the other C# snippets in this article, myBatchClient is a properly initialized instance of the BatchClient class.

CloudPool pool = myBatchClient.PoolOperations.CreatePool(
                    poolId: "mypool",
                    virtualMachineSize: "standard_d1_v2",
                    cloudServiceConfiguration: new CloudServiceConfiguration(osFamily: "5"));
pool.AutoScaleEnabled = true;
pool.AutoScaleFormula = "$TargetDedicatedNodes = (time().weekday == 1 ? 5:1);";
pool.AutoScaleEvaluationInterval = TimeSpan.FromMinutes(30);
await pool.CommitAsync();

重要

创建启用自动缩放的池时,请不要在 CreatePool 调用中指定 targetDedicatedNodes 参数或 targetLowPriorityNodes 参数。When you create an autoscale-enabled pool, don't specify the targetDedicatedNodes parameter or the targetLowPriorityNodes parameter on the call to CreatePool. 应该指定池中的 AutoScaleEnabledAutoScaleFormula 属性。Instead, specify the AutoScaleEnabled and AutoScaleFormula properties on the pool. 这些属性的值确定每种类型的节点的目标数。The values for these properties determine the target number of each type of node.

若要手动重设启用自动缩放功能的池的大小(例如,使用 BatchClient.PoolOperations.ResizePoolAsync 来重设大小),必须先禁用该池的自动缩放,然后重设其大小。To manually resize an autoscale-enabled pool (for example, with BatchClient.PoolOperations.ResizePoolAsync), you must first disable automatic scaling on the pool, then resize it.

PythonPython

若要通过 Python SDK 创建启用自动缩放的池,请执行以下操作:To create autoscale-enabled pool with the Python SDK:

  1. 创建池并指定其配置。Create a pool and specify its configuration.
  2. 将池添加到服务客户端。Add the pool to the service client.
  3. 使用编写的公式在池中启用自动缩放。Enable autoscale on the pool with a formula you write.

以下示例演示了这些步骤。The following example illustrates these steps.

# Create a pool; specify configuration
new_pool = batch.models.PoolAddParameter(
    id="autoscale-enabled-pool",
    virtual_machine_configuration=batchmodels.VirtualMachineConfiguration(
        image_reference=batchmodels.ImageReference(
          publisher="Canonical",
          offer="UbuntuServer",
          sku="18.04-LTS",
          version="latest"
            ),
        node_agent_sku_id="batch.node.ubuntu 18.04"),
    vm_size="STANDARD_D1_v2",
    target_dedicated_nodes=0,
    target_low_priority_nodes=0
)
batch_service_client.pool.add(new_pool) # Add the pool to the service client

formula = """$curTime = time();
             $workHours = $curTime.hour >= 8 && $curTime.hour < 18;
             $isWeekday = $curTime.weekday >= 1 && $curTime.weekday <= 5;
             $isWorkingWeekdayHour = $workHours && $isWeekday;
             $TargetDedicated = $isWorkingWeekdayHour ? 20:10;""";

# Enable autoscale; specify the formula
response = batch_service_client.pool.enable_auto_scale(pool_id, auto_scale_formula=formula,
                                            auto_scale_evaluation_interval=datetime.timedelta(minutes=10),
                                            pool_enable_auto_scale_options=None,
                                            custom_headers=None, raw=False)

提示

如需 Python SDK 使用方面的更多示例,可以参阅 GitHub 上的 Batch Python 快速入门存储库More examples of using the Python SDK can be found in the Batch Python Quickstart repository on GitHub.

启用现有池的自动缩放功能Enable autoscaling on an existing pool

每个 Batch SDK 都提供了启用自动缩放的方式。Each Batch SDK provides a way to enable automatic scaling. 例如:For example:

在现有池上启用自动缩放时,请注意:When you enable autoscaling on an existing pool, keep in mind:

  • 如果目前禁用了池的自动缩放功能,则在发出该请求时必须指定有效的自动缩放公式。If autoscaling is currently disabled on the pool, you must specify a valid autoscale formula when you issue the request. 你还可以指定自动缩放间隔。You can optionally specify an automatic scaling interval. 如果未指定时间间隔,则使用默认值 15 分钟。If you don't specify an interval, the default value of 15 minutes is used.
  • 如果池中目前已启用自动缩放,则可指定新公式和/或新间隔。If autoscaling is currently enabled on the pool, you can specify a new formula, a new interval, or both. 必须至少指定其中的一个属性。You must specify at least one of these properties.
    • 如果指定新的自动缩放间隔,则会停止现有的计划并启动新的计划。If you specify a new automatic scaling interval, the existing schedule is stopped and a new schedule is started. 新计划的开始时间是发出启用自动缩放的请求的时间。The new schedule's start time is the time at which the request to enable autoscaling was issued.
    • 如果省略自动缩放公式或间隔,则 Batch 服务会继续使用该设置的当前值。If you omit either the autoscale formula or interval, the Batch service will continue to use the current value of that setting.

备注

如果使用 .NET 创建池时在 CreatePool 方法中指定了 targetDedicatedNodestargetLowPriorityNodes 参数的值,或者使用其他语言指定了相应参数的值,则评估自动缩放公式时会忽略这些值。If you specified values for the targetDedicatedNodes or targetLowPriorityNodes parameters of the CreatePool method when you created the pool in .NET, or for the comparable parameters in another language, then those values are ignored when the autoscale formula is evaluated.

此 C# 示例使用 Batch.NET 库启用现有池的自动缩放。This C# example uses the Batch .NET library to enable autoscaling on an existing pool.

// Define the autoscaling formula. This formula sets the target number of nodes
// to 5 on Mondays, and 1 on every other day of the week
string myAutoScaleFormula = "$TargetDedicatedNodes = (time().weekday == 1 ? 5:1);";

// Set the autoscale formula on the existing pool
await myBatchClient.PoolOperations.EnableAutoScaleAsync(
    "myexistingpool",
    autoscaleFormula: myAutoScaleFormula);

更新自动缩放公式Update an autoscale formula

若要更新现有的已启用自动缩放的池的公式,请使用新公式再次调用启用自动缩放的操作。To update the formula on an existing autoscale-enabled pool, call the operation to enable autoscaling again with the new formula. 例如,如果在执行以下 .NET 代码时已在 myexistingpool 上启用自动缩放,则自动缩放公式会替换为 myNewFormula 的内容。For example, if autoscaling is already enabled on myexistingpool when the following .NET code is executed, its autoscale formula is replaced with the contents of myNewFormula.

await myBatchClient.PoolOperations.EnableAutoScaleAsync(
    "myexistingpool",
    autoscaleFormula: myNewFormula);

更新自动缩放时间间隔Update the autoscale interval

若要更新现有的已启用自动缩放的池的自动缩放评估间隔,请使用新间隔再次调用启用自动缩放的操作。To update the autoscale evaluation interval of an existing autoscale-enabled pool, call the operation to enable autoscaling again with the new interval. 例如,将 .NET 中已启用自动缩放的池的自动缩放评估间隔设置为 60 分钟:For example, to set the autoscale evaluation interval to 60 minutes for a pool that's already autoscale-enabled in .NET:

await myBatchClient.PoolOperations.EnableAutoScaleAsync(
    "myexistingpool",
    autoscaleEvaluationInterval: TimeSpan.FromMinutes(60));

评估自动缩放公式Evaluate an autoscale formula

在将公式应用于池之前可以对公式进行计算。You can evaluate a formula before applying it to a pool. 这样就可以在将公式的结果用于生产环境中之前对其进行测试。This lets you test the formula's results before you put it into production.

在评估自动缩放公式之前,必须先通过有效的公式(例如单行公式 $TargetDedicatedNodes = 0)为池启用自动缩放。Before you can evaluate an autoscale formula, you must first enable autoscaling on the pool with a valid formula, such as the one-line formula $TargetDedicatedNodes = 0. 然后,使用以下任何一个方法来计算想要测试的公式:Then, use one of the following to evaluate the formula you want to test:

Batch.NET 示例评估自动缩放公式。This Batch .NET example evaluates an autoscale formula. 如果池尚未使用自动缩放,请先启用该功能。If the pool doesn't already use autoscaling, we enable it first.

// First obtain a reference to an existing pool
CloudPool pool = await batchClient.PoolOperations.GetPoolAsync("myExistingPool");

// If autoscaling isn't already enabled on the pool, enable it.
// You can't evaluate an autoscale formula on a non-autoscale-enabled pool.
if (pool.AutoScaleEnabled == false)
{
    // We need a valid autoscale formula to enable autoscaling on the
    // pool. This formula is valid, but won't resize the pool:
    await pool.EnableAutoScaleAsync(
        autoscaleFormula: "$TargetDedicatedNodes = $CurrentDedicatedNodes;",
        autoscaleEvaluationInterval: TimeSpan.FromMinutes(5));

    // Batch limits EnableAutoScaleAsync calls to once every 30 seconds.
    // Because we want to apply our new autoscale formula below if it
    // evaluates successfully, and we *just* enabled autoscaling on
    // this pool, we pause here to ensure we pass that threshold.
    Thread.Sleep(TimeSpan.FromSeconds(31));

    // Refresh the properties of the pool so that we've got the
    // latest value for AutoScaleEnabled
    await pool.RefreshAsync();
}

// We must ensure that autoscaling is enabled on the pool prior to
// evaluating a formula
if (pool.AutoScaleEnabled == true)
{
    // The formula to evaluate - adjusts target number of nodes based on
    // day of week and time of day
    string myFormula = @"
        $curTime = time();
        $workHours = $curTime.hour >= 8 && $curTime.hour < 18;
        $isWeekday = $curTime.weekday >= 1 && $curTime.weekday <= 5;
        $isWorkingWeekdayHour = $workHours && $isWeekday;
        $TargetDedicatedNodes = $isWorkingWeekdayHour ? 20:10;
    ";

    // Perform the autoscale formula evaluation. Note that this code does not
    // actually apply the formula to the pool.
    AutoScaleRun eval =
        await batchClient.PoolOperations.EvaluateAutoScaleAsync(pool.Id, myFormula);

    if (eval.Error == null)
    {
        // Evaluation success - print the results of the AutoScaleRun.
        // This will display the values of each variable as evaluated by the
        // autoscale formula.
        Console.WriteLine("AutoScaleRun.Results: " +
            eval.Results.Replace("$", "\n    $"));

        // Apply the formula to the pool since it evaluated successfully
        await batchClient.PoolOperations.EnableAutoScaleAsync(pool.Id, myFormula);
    }
    else
    {
        // Evaluation failed, output the message associated with the error
        Console.WriteLine("AutoScaleRun.Error.Message: " +
            eval.Error.Message);
    }
}

如果此代码片段中所示的公式评估成功,会生成如下所示的结果:Successful evaluation of the formula shown in this code snippet produces results similar to:

AutoScaleRun.Results:
    $TargetDedicatedNodes=10;
    $NodeDeallocationOption=requeue;
    $curTime=2016-10-13T19:18:47.805Z;
    $isWeekday=1;
    $isWorkingWeekdayHour=0;
    $workHours=0

获取有关自动缩放运行的信息Get information about autoscale runs

为确保公式按预期执行,建议定期检查 Batch 在池上执行的自动缩放运行的结果。To ensure that your formula is performing as expected, we recommend that you periodically check the results of the autoscaling runs that Batch performs on your pool. 为此,请获取(或刷新)对池的引用,然后检查其上一次自动缩放运行的属性。To do so, get (or refresh) a reference to the pool, then examine the properties of its last autoscale run.

在 Batch .NET 中,CloudPool.AutoScaleRun 属性具有多个属性,其提供了有关在池上执行的最新自动缩放运行的信息:In Batch .NET, the CloudPool.AutoScaleRun property has several properties that provide information about the latest automatic scaling run performed on the pool:

在 REST API 中,获取有关池的信息请求返回有关池的信息,其中包括 autoScaleRun 属性中最新自动缩放运行的信息。In the REST API, the Get information about a pool request returns information about the pool, which includes the latest automatic scaling run information in the autoScaleRun property.

以下 C# 示例使用 Batch .NET 库来输出在池 myPool 中的最新自动缩放运行的信息。The following C# example uses the Batch .NET library to print information about the last autoscaling run on pool myPool.

await Cloud pool = myBatchClient.PoolOperations.GetPoolAsync("myPool");
Console.WriteLine("Last execution: " + pool.AutoScaleRun.Timestamp);
Console.WriteLine("Result:" + pool.AutoScaleRun.Results.Replace("$", "\n  $"));
Console.WriteLine("Error: " + pool.AutoScaleRun.Error);

前述示例的示例输出:Sample output from the preceding example:

Last execution: 10/14/2016 18:36:43
Result:
  $TargetDedicatedNodes=10;
  $NodeDeallocationOption=requeue;
  $curTime=2016-10-14T18:36:43.282Z;
  $isWeekday=1;
  $isWorkingWeekdayHour=0;
  $workHours=0
Error:

自动缩放公式示例Example autoscale formulas

让我们查看一些公式,它们演示了调整池中计算资源数量的不同方法。Let's look at a few formulas that show different ways to adjust the amount of compute resources in a pool.

示例 1:基于时间的调整Example 1: Time-based adjustment

假设要根据星期日期和当天的时间来调整池的大小。Suppose you want to adjust the pool size based on the day of the week and time of day. 此示例演示如何相应地增加或减少池中的节点数。This example shows how to increase or decrease the number of nodes in the pool accordingly.

该公式首先获取当前时间。The formula first obtains the current time. 如果日期是工作日(周一到周五)且时间是工作时间(早 8 点到晚 6 点),则会将目标池大小设置为 20 个节点。If it's a weekday (1-5) and within working hours (8 AM to 6 PM), the target pool size is set to 20 nodes. 否则,设为 10 个节点。Otherwise, it's set to 10 nodes.

$curTime = time();
$workHours = $curTime.hour >= 8 && $curTime.hour < 18;
$isWeekday = $curTime.weekday >= 1 && $curTime.weekday <= 5;
$isWorkingWeekdayHour = $workHours && $isWeekday;
$TargetDedicatedNodes = $isWorkingWeekdayHour ? 20:10;
$NodeDeallocationOption = taskcompletion;

通过将 time()TimeZoneInterval_Hour 和 UTC 时差的乘积相加,可以调整 $curTime,使之反映本地时区。$curTime can be adjusted to reflect your local time zone by adding time() to the product of TimeZoneInterval_Hour and your UTC offset. 例如,使用 $curTime = time() + (-6 * TimeInterval_Hour); 作为山地夏令时 (MDT)。For instance, use $curTime = time() + (-6 * TimeInterval_Hour); for Mountain Daylight Time (MDT). 请记住,需要在夏令时开始和结束时调整时差(如果适用)。Keep in mind that the offset would need to be adjusted at the start and end of daylight saving time (if applicable).

示例 2:基于任务的调整Example 2: Task-based adjustment

在此 C# 示例中,池大小根据队列中的任务数进行调整。In this C# example, the pool size is adjusted based on the number of tasks in the queue. 公式字符串中包含了注释和分行符。We've included both comments and line breaks in the formula strings.

// Get pending tasks for the past 15 minutes.
$samples = $PendingTasks.GetSamplePercent(TimeInterval_Minute * 15);
// If we have fewer than 70 percent data points, we use the last sample point,
// otherwise we use the maximum of last sample point and the history average.
$tasks = $samples < 70 ? max(0,$PendingTasks.GetSample(1)) : max( $PendingTasks.GetSample(1), avg($PendingTasks.GetSample(TimeInterval_Minute * 15)));
// If number of pending tasks is not 0, set targetVM to pending tasks, otherwise
// half of current dedicated.
$targetVMs = $tasks > 0? $tasks:max(0, $TargetDedicatedNodes/2);
// The pool size is capped at 20, if target VM value is more than that, set it
// to 20. This value should be adjusted according to your use case.
$TargetDedicatedNodes = max(0, min($targetVMs, 20));
// Set node deallocation mode - let running tasks finish before removing a node
$NodeDeallocationOption = taskcompletion;

示例 3:考虑并行任务Example 3: Accounting for parallel tasks

此 C# 示例根据任务数调整池大小。This C# example adjusts the pool size based on the number of tasks. 此公式还考虑为池设置的 MaxTasksPerComputeNode 值。This formula also takes into account the MaxTasksPerComputeNode value that has been set for the pool. 在对池启用了并行任务执行的情况下,此方法特别有效。This approach is useful in situations where parallel task execution has been enabled on your pool.

// Determine whether 70 percent of the samples have been recorded in the past
// 15 minutes; if not, use last sample
$samples = $ActiveTasks.GetSamplePercent(TimeInterval_Minute * 15);
$tasks = $samples < 70 ? max(0,$ActiveTasks.GetSample(1)) : max( $ActiveTasks.GetSample(1),avg($ActiveTasks.GetSample(TimeInterval_Minute * 15)));
// Set the number of nodes to add to one-fourth the number of active tasks
// (the MaxTasksPerComputeNode property on this pool is set to 4, adjust
// this number for your use case)
$cores = $TargetDedicatedNodes * 4;
$extraVMs = (($tasks - $cores) + 3) / 4;
$targetVMs = ($TargetDedicatedNodes + $extraVMs);
// Attempt to grow the number of compute nodes to match the number of active
// tasks, with a maximum of 3
$TargetDedicatedNodes = max(0,min($targetVMs,3));
// Keep the nodes active until the tasks finish
$NodeDeallocationOption = taskcompletion;

示例 4:设置初始池大小Example 4: Setting an initial pool size

此示例演示了一个 C# 示例,其中的自动缩放公式可在初始时间段内将池大小设置为指定的节点数。This example shows a C# example with an autoscale formula that sets the pool size to a specified number of nodes for an initial time period. 然后,该公式会根据正在运行的和处于活动状态的任务的数目调整池大小。After that, it adjusts the pool size based on the number of running and active tasks.

具体说来,此公式执行以下操作:Specifically, this formula does the following:

  • 将初始池大小设置为 4 个节点。Sets the initial pool size to four nodes.
  • 在池生命周期的最初 10 分钟内不调整池大小。Does not adjust the pool size within the first 10 minutes of the pool's lifecycle.
  • 10 分钟后,获取过去 60 分钟内正在运行和处于活动状态的任务数目的最大值。After 10 minutes, obtains the max value of the number of running and active tasks within the past 60 minutes.
    • 如果这两个值均为 0(表示过去 60 分钟没有正在运行或处于活动状态的任务),则池大小会设置为 0。If both values are 0 (indicating that no tasks were running or active in the last 60 minutes), the pool size is set to 0.
    • 如果其中一个值大于零,则不进行任何更改。If either value is greater than zero, no change is made.
string now = DateTime.UtcNow.ToString("r");
string formula = string.Format(@"
    $TargetDedicatedNodes = {1};
    lifespan         = time() - time(""{0}"");
    span             = TimeInterval_Minute * 60;
    startup          = TimeInterval_Minute * 10;
    ratio            = 50;

    $TargetDedicatedNodes = (lifespan > startup ? (max($RunningTasks.GetSample(span, ratio), $ActiveTasks.GetSample(span, ratio)) == 0 ? 0 : $TargetDedicatedNodes) : {1});
    ", now, 4);

后续步骤Next steps