共用方式為


依狀態計算作業和節點數目以監視 Batch 解決方案

若要監視和管理大規模的 Azure Batch 解決方案,您可能需要判斷各種狀態中的資源計數。 Azure Batch 提供有效的作業來取得 Batch 工作和計算節點的計數。 您可以使用這些作業,而不是可能耗時的清單查詢,以傳回大量工作或節點集合的詳細資訊。

  • 取得工作計數可取得作業中的作用中、執行中和已完成工作的彙總計數,以及成功或失敗的工作計數。 藉由計算每個狀態中的工作,您可以輕鬆地向使用者顯示作業進度,或偵測可能影響作業的非預期延遲或失敗。

  • 列出集區節點計數可取得每個集區中處於各種狀態的專用和現成計算節點數目:creating、idle、offline、preempted、rebooting、reimaging、starting 等等。 藉由計算每個狀態中的節點,您可以判斷何時有足夠的計算資源來執行作業,並找出集區的潛在問題。

有時,這些作業傳回的數位可能不是最新的。 如果您需要確定計數正確,請使用清單查詢來計算這些資源。 清單查詢也可讓您取得其他 Batch 資源的相關信息,例如應用程式。 如需將篩選套用至清單查詢的詳細資訊,請參閱 建立查詢以有效率地列出 Batch 資源

工作狀態計數

Get 工作計數」作業會依下列狀態計算工作數目:

  • 作用中:已排入佇列且已準備好執行,但目前未指派給任何計算節點的工作。 如果工作相依於尚未完成的父工作,也計會為 active
  • 執行:已指派給計算節點但尚未完成的工作。 處於 preparingrunning 狀態的工作會計為 running,如Get 工作相關資訊作業所示。
  • 已完成:不再有資格執行的工作,因為它已順利完成,或未成功完成,也耗盡了重試限制。
  • 成功:工作執行結果為 success的工作。 Batch 會檢查 TaskExecutionResultexecutionInfo 屬性的 屬性,以判斷工作是否成功或失敗。
  • 失敗:工作執行結果為 failure的工作。

下列 .NET 程式代碼範例示範如何依狀態擷取工作計數。

var taskCounts = await batchClient.JobOperations.GetJobTaskCountsAsync("job-1");

Console.WriteLine("Task count in active state: {0}", taskCounts.Active);
Console.WriteLine("Task count in preparing or running state: {0}", taskCounts.Running);
Console.WriteLine("Task count in completed state: {0}", taskCounts.Completed);
Console.WriteLine("Succeeded task count: {0}", taskCounts.Succeeded);
Console.WriteLine("Failed task count: {0}", taskCounts.Failed);

您可以使用類似 REST 的模式和其他支援的語言來取得作業的工作計數。

節點狀態計數

「列出集區節點計數」作業會依據每個集區中的下列狀態為計算節點計數。 每個集區中的專用節點和現成節點都有個別的彙總計數。

  • Creating:由 Azure 配置、尚未開始加入集區的 VM。
  • 閑置:可用的計算節點,目前未執行任何工作。
  • LeavingPool:因為由使用者明確移除,或因為集區調整大小或自動縮小而離開集區的節點。
  • 離線:Batch 無法用來排程新工作的節點。
  • Preempted:因 Azure 回收 VM 而已從集區中移除的現成節點。 preempted 節點可以在可用替代 Spot VM 容量時重新初始化。
  • 重新啟動:正在重新啟動的節點。
  • Reimaging:正在重新安裝作業系統的節點。
  • 運行:運行一個或多個任務的節點(不包括開始任務)。
  • 啟動:Batch 服務啟動所在的節點。
  • StartTaskFailed啟動工作在所有重試後失敗且已啟用 waitForSuccess 的節點。 此節點無法執行工作。
  • 未知:失去與 Batch 服務連絡且狀態未知的節點。
  • 無法使用:因錯誤而無法用於工作執行的節點。
  • WaitingForStartTask:啟動工作執行中但已啟用 waitForSuccess 且尚未完成的節點。

下列 C# 代碼段示範如何列出目前帳戶中所有集區的節點計數:

foreach (var nodeCounts in batchClient.PoolOperations.ListPoolNodeCounts())
{
    Console.WriteLine("Pool Id: {0}", nodeCounts.PoolId);

    Console.WriteLine("Total dedicated node count: {0}", nodeCounts.Dedicated.Total);

    // Get dedicated node counts in Idle and Offline states; you can get additional states.
    Console.WriteLine("Dedicated node count in Idle state: {0}", nodeCounts.Dedicated.Idle);
    Console.WriteLine("Dedicated node count in Offline state: {0}", nodeCounts.Dedicated.Offline);

    Console.WriteLine("Total Spot node count: {0}", nodeCounts.LowPriority.Total);

    // Get Spot node counts in Running and Preempted states; you can get additional states.
    Console.WriteLine("Spot node count in Running state: {0}", nodeCounts.LowPriority.Running);
    Console.WriteLine("Spot node count in Preempted state: {0}", nodeCounts.LowPriority.Preempted);
}

下列 C# 代碼段示範如何列出目前帳戶中指定集區的節點計數。

foreach (var nodeCounts in batchClient.PoolOperations.ListPoolNodeCounts(new ODATADetailLevel(filterClause: "poolId eq 'testpool'")))
{
    Console.WriteLine("Pool Id: {0}", nodeCounts.PoolId);

    Console.WriteLine("Total dedicated node count: {0}", nodeCounts.Dedicated.Total);

    // Get dedicated node counts in Idle and Offline states; you can get additional states.
    Console.WriteLine("Dedicated node count in Idle state: {0}", nodeCounts.Dedicated.Idle);
    Console.WriteLine("Dedicated node count in Offline state: {0}", nodeCounts.Dedicated.Offline);

    Console.WriteLine("Total Spot node count: {0}", nodeCounts.LowPriority.Total);

    // Get Spot node counts in Running and Preempted states; you can get additional states.
    Console.WriteLine("Spot node count in Running state: {0}", nodeCounts.LowPriority.Running);
    Console.WriteLine("Spot node count in Preempted state: {0}", nodeCounts.LowPriority.Preempted);
}

您可以針對 REST 和其他支援的語言使用類似的模式,以取得集區的節點計數。

後續步驟