

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon MWAA 上 Apache Airflow 的效能調校
<a name="best-practices-tuning"></a>

本主題說明如何使用 調整 Amazon Managed Workflows for Apache Airflow 環境的效能[在 Amazon MWAA 上使用 Apache Airflow 組態選項](configuring-env-variables.md)。

**Contents**
+ [新增 Apache Airflow 組態選項](#best-practices-tuning-console-add)
+ [Apache Airflow 排程器](#best-practices-tuning-scheduler)
  + [Parameters](#best-practices-tuning-scheduler-params)
  + [限制](#best-practices-tuning-scheduler-limits)
+ [DAG 資料夾](#best-practices-tuning-dag-folders)
  + [Parameters](#best-practices-tuning-dag-folders-params)
+ [DAG 檔案](#best-practices-tuning-dag-files)
  + [Parameters](#best-practices-tuning-dag-files-params)
+ [任務​](#best-practices-tuning-tasks)
  + [Parameters](#best-practices-tuning-tasks-params)

## 新增 Apache Airflow 組態選項
<a name="best-practices-tuning-console-add"></a>

使用下列程序將 Airflow 組態選項新增至您的環境。

1. 在 Amazon MWAA 主控台上開啟[環境](https://console.aws.amazon.com/mwaa/home#/environments)頁面。

1. 選擇環境。

1. 選擇**編輯**。

1. 選擇**下一步**。

1. 在 Airflow **組態選項窗格中選擇新增自訂**組態。 ****

1. 從下拉式清單中選擇組態並輸入值，或輸入自訂組態並輸入值。

1. 針對您要新增的每個組態，選擇**新增自訂**組態。

1. 選擇**儲存**。

若要進一步了解，請參閱 [在 Amazon MWAA 上使用 Apache Airflow 組態選項](configuring-env-variables.md)。

## Apache Airflow 排程器
<a name="best-practices-tuning-scheduler"></a>

Apache Airflow 排程器是 Apache Airflow 的核心元件。排程器的問題可防止剖析 DAGs 和排程任務。如需 Apache Airflow 排程器調校的詳細資訊，請參閱 Apache Airflow 文件網站上的[微調排程器效能](https://airflow.apache.org/docs/apache-airflow/2.2.2/concepts/scheduler.html#fine-tuning-your-scheduler-performance)。

### Parameters
<a name="best-practices-tuning-scheduler-params"></a>

本節說明 Apache Airflow 排程器 (Apache Airflow v2 和更新版本） 可用的組態選項及其使用案例。

------
#### [ Apache Airflow v3 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[celery.sync\$1parallelism](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#parallelism)** Celery Executor 用來同步任務狀態的程序數目。 **預設值**：1  |  您可以使用此選項來限制 Celery Executor 使用的程序，以防止佇列衝突。根據預設， 值會設定為 `1` ，以防止將任務日誌交付至 CloudWatch Logs 時發生錯誤。將值設定為 `0`表示使用最大數量的程序，但在交付任務日誌時可能會導致錯誤。  | 
|  **[scheduler.scheduler\$1idle\$1sleep\$1time](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#scheduler-idle-sleep-time)** 排程器「迴圈」中連續 DAG 檔案處理之間的等待秒數。 **預設值**：1  |  您可以使用此選項來釋放排程器上的 CPU 用量，方法是**增加**排程器在完成擷取 DAG 剖析結果後休眠的時間、尋找和佇列任務，以及在*執行器*中執行排入佇列的任務。增加此值會耗用 `dag_processor.parsing_processes` Apache Airflow v2 和 Apache Airflow v3 在 環境中執行的排程器執行緒數目。這可以減少排程器剖析 DAGs 的容量，並增加 DAGs 在 Web 伺服器中填入所需的時間。  | 
|  **[scheduler.max\$1dagruns\$1to\$1create\$1per\$1loop](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#max-dagruns-to-create-per-loop)** 為每個排程器「迴圈」建立 *DagRuns* 的 DAGs 數目上限。 **預設值**：10  |  您可以使用此選項，藉由**減少**排程器「迴圈」的 *DagRuns* 數目上限，釋放用於排程任務的資源。  | 
|  **[dag\$1processor.parsing\$1processes](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#parsing-processes)** 排程器可以平行執行以排程 DAGs執行緒數目。 **預設：**使用 `(2 * number of vCPUs) - 1`  |  您可以使用此選項，藉由**減少**排程器平行執行以剖析 DAGs 的程序數目來釋放資源。如果 DAG 剖析影響任務排程，建議您將此數字保持較低。**您必須**指定小於您環境中 vCPU 計數的值。若要進一步了解，請參閱 [限制](#best-practices-tuning-scheduler-limits)。  | 

------
#### [ Apache Airflow v2 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[celery.sync\$1parallelism](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#parallelism)** Celery Executor 用來同步任務狀態的程序數目。 **預設值**：1  |  您可以使用此選項來限制 Celery Executor 使用的程序，以防止佇列衝突。根據預設， 值會設定為 `1` ，以防止將任務日誌交付至 CloudWatch Logs 時發生錯誤。將值設定為 `0`表示使用最大數量的程序，但在交付任務日誌時可能會導致錯誤。  | 
|  **[scheduler.idle\$1sleep\$1time](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#scheduler-idle-sleep-time)** 排程器「迴圈」中連續 DAG 檔案處理之間的等待秒數。 **預設值**：1  |  您可以使用此選項來釋放排程器上的 CPU 用量，方法是**增加**排程器在完成擷取 DAG 剖析結果後休眠的時間、尋找和佇列任務，以及在*執行器*中執行排入佇列的任務。增加此值會耗用 `scheduler.parsing_processes` Apache Airflow v2 和 Apache Airflow v3 在 環境中執行的排程器執行緒數目。這可以減少排程器剖析 DAGs 的容量，並增加 DAGs 在 Web 伺服器中填入所需的時間。  | 
|  **[scheduler.max\$1dagruns\$1to\$1create\$1per\$1loop](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#max-dagruns-to-create-per-loop)** 為每個排程器「迴圈」建立 *DagRuns* 的 DAGs 數目上限。 **預設值**：10  |  您可以使用此選項，藉由**減少**排程器「迴圈」的 *DagRuns* 數目上限，釋放用於排程任務的資源。  | 
|  **[scheduler.parsing\$1processes](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#parsing-processes)** 排程器可以平行執行以排程 DAGs執行緒數目。 **預設：**使用 `(2 * number of vCPUs) - 1`  |  您可以使用此選項，藉由**減少**排程器平行執行以剖析 DAGs 的程序數目來釋放資源。如果 DAG 剖析影響任務排程，建議您將此數字保持較低。**您必須**指定小於您環境中 vCPU 計數的值。若要進一步了解，請參閱 [限制](#best-practices-tuning-scheduler-limits)。  | 

------

### 限制
<a name="best-practices-tuning-scheduler-limits"></a>

本節說明調整排程器的預設參數時應考慮的限制。<a name="scheduler-considerations"></a>

**scheduler.parsing\$1processes、scheduler.max\$1threads （僅限 v2)**  
環境類別的每個 vCPU 允許兩個執行緒。環境類別的排程器必須至少保留一個執行緒。如果您注意到任務排程延遲，您可能需要增加[環境類別](environment-class.md)。例如，大型環境的排程器具有 4 個 vCPU Fargate 容器執行個體。這表示執行緒`7`總數上限為 ，可用於其他程序。也就是說，兩個執行緒會乘以四個 vCPUs，減去排程器本身的一個 vCPU。您在 `scheduler.max_threads`（僅限 v2) 中指定的值，且`scheduler.parsing_processes`不得超過 環境類別可用的執行緒數目，如下所示：  
+ **mw1.small** – 不得超過其他程序的`1`執行緒。剩餘的執行緒會保留給排程器。
+ **mw1.medium** – 不得超過其他程序的`3`執行緒。剩餘的執行緒會保留給排程器。
+ **mw1.large** – 不得超過其他程序的`7`執行緒。剩餘的執行緒會保留給排程器。

## DAG 資料夾
<a name="best-practices-tuning-dag-folders"></a>

Apache Airflow 排程器會持續掃描您環境中DAGs 資料夾。任何包含`plugins.zip`的檔案，或包含「airflow」匯入陳述式的 Python (`.py`) 檔案。然後，任何產生的 Python DAG 物件都會放入 *DagBag* 中，以供排程器處理該檔案，以決定需要排程哪些任務。無論檔案是否包含任何可行的 DAG 物件，都會進行大型檔案剖析。

### Parameters
<a name="best-practices-tuning-dag-folders-params"></a>

本節說明 DAGs (Apache Airflow v2 及更新版本） 可用的組態選項及其使用案例。

------
#### [ Apache Airflow v3 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[dag\$1processor.refresh\$1interval](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#config-dag-processor-refresh-interval)** 必須掃描 DAGs是否有新檔案的秒數。 **預設：**300 秒  |  您可以使用此選項，透過**增加**剖析 DAGs秒數來釋放資源。如果您在 中遇到長時間的剖析時間`total_parse_time metrics`，建議您增加此值，這可能是因為 DAGs資料夾中有大量檔案所致。  | 
|  **[dag\$1processor.min\$1file\$1process\$1interval](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#min-file-process-interval)** 排程器剖析 DAG 並反映 DAG 更新的秒數。 **預設：**30 秒  |  您可以使用此選項，透過**增加**排程器在剖析 DAG 之前等待的秒數來釋放資源。例如，如果您指定 的值`30`，則會每 30 秒剖析一次 DAG 檔案。我們建議您將此數字保持高，以減少環境中的 CPU 用量。  | 

------
#### [ Apache Airflow v2 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[scheduler.dag\$1dir\$1list\$1interval](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#dag-dir-list-interval)** 必須掃描 DAGs是否有新檔案的秒數。 **預設：**300 秒  |  您可以使用此選項，透過**增加**剖析 DAGs秒數來釋放資源。如果您在 中遇到長時間的剖析時間`total_parse_time metrics`，建議您增加此值，這可能是因為 DAGs資料夾中有大量檔案所致。  | 
|  **[scheduler.min\$1file\$1process\$1interval](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#min-file-process-interval)** 排程器剖析 DAG 並反映 DAG 更新的秒數。 **預設：**30 秒  |  您可以使用此選項，透過**增加**排程器在剖析 DAG 之前等待的秒數來釋放資源。例如，如果您指定 的值`30`，則會每 30 秒剖析一次 DAG 檔案。我們建議您將此數字保持高，以減少環境中的 CPU 用量。  | 

------

## DAG 檔案
<a name="best-practices-tuning-dag-files"></a>

做為 Apache Airflow 排程器迴圈的一部分，會剖析個別 DAG 檔案以擷取 DAG Python 物件。在 Apache Airflow v2 和更新版本中，排程器會同時剖析最多 個[剖析程序](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#parsing-processes)。`scheduler.min_file_process_interval` (v2) 或 `dag_processor.min_file_process_interval`(v3) 中指定的秒數必須先通過，才能再次剖析相同的檔案。

### Parameters
<a name="best-practices-tuning-dag-files-params"></a>

本節說明 Apache Airflow DAG 檔案 (Apache Airflow v2 及更新版本） 可用的組態選項及其使用案例。

------
#### [ Apache Airflow v3 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[dag\$1processor.dag\$1file\$1processor\$1timeout](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#dag-file-processor-timeout)** *DagFileProcessor* 逾時處理 DAG 檔案之前的秒數。 **預設：**50 秒  |  您可以使用此選項，透過**增加** *DagFileProcessor* 逾時之前所需的時間來釋放資源。如果您在 DAG 處理日誌中遇到逾時，導致沒有載入可行DAGs，建議您增加此值。  | 
|  **[core.dagbag\$1import\$1timeout](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#dagbag-import-timeout)** 匯入 Python 檔案逾時之前的秒數。 **預設：**30 秒  |  您可以使用此選項，藉由**在**匯入 Python 檔案以擷取 DAG 物件時，增加排程器逾時之前所花費的時間來釋放資源。此選項會做為排程器「迴圈」的一部分處理，且必須包含小於 中指定值的值`dag_processor.dag_file_processor_timeout`。  | 
|  **[core.min\$1serialized\$1dag\$1update\$1interval](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#min-serialized-dag-update-interval)** 更新資料庫中序列化 DAGs的最小秒數。 **預設：**30  |  您可以使用此選項，透過**增加**資料庫中序列化 DAGs更新後的秒數來釋放資源。如果您有大量 DAGs 或複雜的 DAGs，建議您增加此值。隨著 DAGs 序列化，增加此值可減少排程器和資料庫的負載。  | 
|  **[core.min\$1serialized\$1dag\$1fetch\$1interval](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#min-serialized-dag-fetch-interval)** 當已載入 DagBag 時，從資料庫重新擷取序列化 DAG 的秒數。 **預設值**：10  |  您可以使用此選項，透過**增加**重新擷取序列化 DAG 的秒數來釋放資源。值必須大於 中指定的值`core.min_serialized_dag_update_interval`，以降低資料庫「寫入」速率。隨著 DAGs序列化，增加此值可減少 Web 伺服器和資料庫的負載。  | 

------
#### [ Apache Airflow v2 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[core.dag\$1file\$1processor\$1timeout](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#dag-file-processor-timeout)** *DagFileProcessor* 逾時處理 DAG 檔案之前的秒數。 **預設：**50 秒  |  您可以使用此選項，透過**增加** *DagFileProcessor* 逾時之前所需的時間來釋放資源。如果您在 DAG 處理日誌中遇到逾時，導致沒有載入可行DAGs，建議您增加此值。  | 
|  **[core.dagbag\$1import\$1timeout](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#dagbag-import-timeout)** 匯入 Python 檔案逾時之前的秒數。 **預設：**30 秒  |  您可以使用此選項，藉由**在**匯入 Python 檔案以擷取 DAG 物件時，增加排程器逾時之前所花費的時間來釋放資源。此選項會做為排程器「迴圈」的一部分處理，且必須包含小於 中指定值的值`core.dag_file_processor_timeout`。  | 
|  **[core.min\$1serialized\$1dag\$1update\$1interval](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#min-serialized-dag-update-interval)** 更新資料庫中序列化 DAGs的最小秒數。 **預設：**30  |  您可以使用此選項，透過**增加**資料庫中序列化 DAGs更新後的秒數來釋放資源。如果您有大量 DAGs 或複雜的 DAGs，建議您增加此值。隨著 DAGs 序列化，增加此值可減少排程器和資料庫的負載。  | 
|  **[core.min\$1serialized\$1dag\$1fetch\$1interval](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#min-serialized-dag-fetch-interval)** 當已載入 DagBag 時，從資料庫重新擷取序列化 DAG 的秒數。 **預設值**：10  |  您可以使用此選項，透過**增加**重新擷取序列化 DAG 的秒數來釋放資源。值必須大於 中指定的值`core.min_serialized_dag_update_interval`，以降低資料庫「寫入」速率。隨著 DAGs序列化，增加此值可減少 Web 伺服器和資料庫的負載。  | 

------

## 任務​
<a name="best-practices-tuning-tasks"></a>

Apache Airflow 排程器和工作者都參與佇列和取消佇列任務。排程器會將準備從**無**狀態排程的剖析任務轉換為**已排程**狀態。執行器也會在 Fargate 的排程器容器上執行，將這些任務排入佇列，並將其狀態設定為**已排**入佇列。當工作者有容量時，會從佇列中取得任務，並將狀態設定為**執行**中，然後****根據任務是否成功，將其狀態變更為**成功**或失敗。

### Parameters
<a name="best-practices-tuning-tasks-params"></a>

本節說明 Apache Airflow 任務可用的組態選項及其使用案例。

Amazon MWAA 覆寫的預設組態選項會以*紅色*標示。

------
#### [ Apache Airflow v3 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[core.parallelism](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#parallelism)** 可具有 `Running` 狀態的任務執行個體數目上限。 **預設：**根據 動態設定`(maxWorkers * maxCeleryWorkers) / schedulers * 1.5`。  |  您可以使用此選項，透過**增加**可同時執行的任務執行個體數量來釋放資源。指定的值必須是可用工作者的數量乘以工作者的任務密度。我們建議您只有在遇到大量任務卡在「執行中」或「佇列」狀態時，才變更此值。  | 
|  **[core.execute\$1tasks\$1new\$1python\$1interpreter](https://airflow.apache.org/docs/apache-airflow/3.0.6/configurations-ref.html#execute-tasks-new-python-interpreter)** 決定 Apache Airflow 是否透過強制執行父程序或建立新的 Python 程序來執行任務。 **預設**：`True`  |  設定為 時`True`，Apache Airflow 會將您對外掛程式所做的變更辨識為新的 Python 程序，以便建立以執行任務。  | 
|  **[celery.worker\$1concurrency](https://airflow.apache.org/docs/apache-airflow-providers-celery/stable/configurations-ref.html#worker-concurrency)** Amazon MWAA 會覆寫此選項的 Airflow 基本安裝，以擴展工作者作為其自動擴展元件的一部分。 **預設：**不適用  |  *此選項指定的任何值都會遭到忽略。*  | 
|  **[celery.worker\$1autoscale](https://airflow.apache.org/docs/apache-airflow-providers-celery/stable/configurations-ref.html#worker-autoscale)** 工作者的任務並行。 **預設值：** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/mwaa/latest/userguide/best-practices-tuning.html)  |  您可以使用此選項，透過**減少**工作者的任務`minimum`並行`maximum`來釋放資源。工作者最多接受設定的`maximum`並行任務，無論是否有足夠資源可以這樣做。如果任務排程時沒有足夠的資源，任務會立即失敗。我們建議將此值變更為資源密集型任務，方法是將值減少為小於預設值，以允許每個任務更多的容量。  | 

------
#### [ Apache Airflow v2 ]


| Configuration | 使用案例 | 
| --- | --- | 
|  **[core.parallelism](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#parallelism)** 可具有 `Running` 狀態的任務執行個體數目上限。 **預設：**根據 動態設定`(maxWorkers * maxCeleryWorkers) / schedulers * 1.5`。  |  您可以使用此選項，透過**增加**可同時執行的任務執行個體數量來釋放資源。指定的值必須是可用工作者的數量乘以工作者的任務密度。我們建議您只有在遇到大量任務卡在「執行中」或「佇列」狀態時，才變更此值。  | 
|  **[core.dag\$1concurrency](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#dag-concurrency)** 允許為每個 DAG 同時執行的任務執行個體數目。 **預設：**10000  |  您可以使用此選項，透過**增加**允許同時執行的任務執行個體數量來釋放資源。例如，如果您有 100 個具有 10 個平行任務DAGs，而且您希望所有 DAGs 同時執行，則可以計算最大平行處理，將可用工作者數量乘以 中的工作者任務密度`celery.worker_concurrency`，再除以 DAGs 數量。  | 
|  **[core.execute\$1tasks\$1new\$1python\$1interpreter](https://airflow.apache.org/docs/apache-airflow/2.10.3/configurations-ref.html#execute-tasks-new-python-interpreter)** 決定 Apache Airflow 是否透過強制執行父程序或建立新的 Python 程序來執行任務。 **預設**：`True`  |  設定為 時`True`，Apache Airflow 會將您對外掛程式所做的變更辨識為新的 Python 程序，以便建立以執行任務。  | 
|  **[celery.worker\$1concurrency](https://airflow.apache.org/docs/apache-airflow-providers-celery/stable/configurations-ref.html#worker-concurrency)** Amazon MWAA 會覆寫此選項的 Airflow 基本安裝，以擴展工作者作為其自動擴展元件的一部分。 **預設：**不適用  |  *此選項指定的任何值都會遭到忽略。*  | 
|  **[celery.worker\$1autoscale](https://airflow.apache.org/docs/apache-airflow-providers-celery/stable/configurations-ref.html#worker-autoscale)** 工作者的任務並行。 **預設值：** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/mwaa/latest/userguide/best-practices-tuning.html)  |  您可以使用此選項，透過**減少**工作者的任務`minimum`並行`maximum`來釋放資源。工作者最多接受設定的`maximum`並行任務，無論是否有足夠資源可以這樣做。如果任務排程時沒有足夠的資源，任務會立即失敗。我們建議將此值變更為資源密集型任務，方法是將值減少為小於預設值，以允許每個任務更多的容量。  | 

------