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

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

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

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

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

工作狀態計數

「取得工作計數」作業會依下列狀態計算工作數目:

  • Active:已排入佇列且能夠執行、但目前尚未指派給計算節點的工作。 如果工作相依於尚未完成的父工作,也會計為 active
  • Running:已指派給計算節點、但尚未完成的工作。 處於 preparingrunning 狀態的工作會計為 running,如取得工作相關資訊作業所示。
  • Completed:因已順利完成,或未順利完成但已耗盡其重試限制,而不再有資格執行的工作。
  • Succeeded:工作執行結果為 success 的工作。 Batch 會藉由檢查 executionInfo 屬性的 TaskExecutionResult 屬性來判斷工作是否成功。
  • Failed:工作執行結果為 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。
  • Idle:目前未執行工作的可用計算節點。
  • LeavingPool:因為由使用者明確移除,或因為集區調整大小或自動縮小而離開集區的節點。
  • Offline:無法供 Batch 用來排程新工作的節點。
  • Preempted:因 Azure 回收 VM 而已從集區中移除的現成節點。 有替補的現成 VM 容量可用時,可以重新初始化 preempted 節點。
  • Rebooting:正在重新啟動的節點。
  • Reimaging:正在重新安裝作業系統的節點。
  • Running:正在執行一或多個工作 (而非啟動工作) 的節點。
  • Starting:正在啟動 Batch 服務的節點。
  • StartTaskFailed啟動工作失敗並耗盡所有重試次數,且啟動工作設定為 waitForSuccess 的節點。 此類節點無法用來執行工作。
  • Unknown:失去與 Batch 服務的聯繫、且狀態不明的節點。
  • Unusable:因發生錯誤而無法用來執行工作的節點。
  • 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 和其他支援的語言,以取得集區的節點計數。

下一步