

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 故障排除：CloudWatch Logs 和 CloudTrail 错误
<a name="t-cloudwatch-cloudtrail-logs"></a>

本页主题包含您在 Amazon MWAA 环境中可能遇到的 Amazon CloudWatch Logs 和 AWS CloudTrail 错误的解决方法。

**Contents**
+ [日志](#troubleshooting-view-logs)
  + [我找不到我的任务日志，或者我收到了 `Reading remote log from Cloudwatch log_group` 错误](#t-task-logs)
  + [任务在没有任何日志的情况下失败](#t-task-failing-no-logs)
  + [我在 CloudTrail 中遇到了 `ResourceAlreadyExistsException` 错误](#t-cloudtrail)
  + [我在 CloudTrail 中看到了 `Invalid request` 错误](#t-cloudtrail-bucket)
  + [我在 Apache Airflow 日志中看到了 `Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory`](#t-plugins-logs)
  + [我在我的计划程序日志中看到了 psycopg2“服务器意外关闭了连接”](#scheduler-postgres-library)
  + [我在我的 DAG 处理日志中看到了 `Executor reports task instance %s finished (%s) although the task says its %s`](#long-running-tasks)
  + [我在我的任务日志中看到了 `Could not read remote logs from log_group: airflow-*{*environmentName}-Task log_stream:* {*DAG_ID}/*{*TASK_ID}/*{*time}/*{*n}.log.`](#t-task-fail-permission)

## 日志
<a name="troubleshooting-view-logs"></a>

以下主题描述了您在访问 Apache Airflow 日志时可能收到的错误。

### 我找不到我的任务日志，或者我收到了 `Reading remote log from Cloudwatch log_group` 错误
<a name="t-task-logs"></a>

Amazon MWAA 已将 Apache Airflow 配置为直接从 Amazon CloudWatch Logs 读取和写入日志。如果工作线程无法启动任务或未能写入任何日志，您将看到错误：

```
*** Reading remote log from Cloudwatch log_group: airflow-environmentName-Task log_stream: DAG_ID/TASK_ID/timestamp/n.log.Could not read remote logs from log_group: airflow-environmentName-Task log_stream: DAG_ID/TASK_ID/time/n.log.
```
+ 我们建议您完成以下步骤：

  1. 确认您已在环境 `INFO` 级别上启用任务日志。有关更多信息，请参阅[访问 Amazon 中的 Airflow 日志 CloudWatch](monitoring-airflow.md)。

  1. 验证环境[执行角色](mwaa-create-role.md)的权限策略是否正确。

  1. 验证运算符或任务是否正常运行，是否有足够的资源来解析 DAG，以及是否有相应的 Python 库可供加载。要验证依赖项是否正确，请尝试取消导入，直到找到导致问题的依赖项。我们建议使用 [aws-mwaa-docker-images](https://github.com/aws/amazon-mwaa-docker-images) 测试 Python 依赖项。

### 任务在没有任何日志的情况下失败
<a name="t-task-failing-no-logs"></a>

如果工作流程中的任务失败并且您找不到失败任务的任何日志，请检查是否在默认参数中设置了 `queue` 参数，如下所列。

```
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.utils.dates import days_ago

# Setting queue argument to default.
default_args = {
	"start_date": days_ago(1),
	"queue": "default"
}

with DAG(dag_id="any_command_dag", schedule_interval=None, catchup=False, default_args=default_args) as dag:
    cli_command = BashOperator(
        task_id="bash_command",
        bash_command="{{ dag_run.conf['command'] }}"
    )
```

要解决此问题，请从代码中删除 `queue`，然后再次调用 DAG。

### 我在 CloudTrail 中遇到了 `ResourceAlreadyExistsException` 错误
<a name="t-cloudtrail"></a>

```
"errorCode": "ResourceAlreadyExistsException",
    "errorMessage": "The specified log stream already exists",
    "requestParameters": {
        "logGroupName": "airflow-MyAirflowEnvironment-DAGProcessing",
        "logStreamName": "scheduler_cross-account-eks.py.log"
    }
```

某些 Python 要求，例如 `apache-airflow-backport-providers-amazon` 将 Amazon MWAA 用于与 CloudWatch 通信的 `watchtower` 库回滚到旧版本。我们建议您完成以下步骤：
+ 将以下库添加到 `requirements.txt`。

  ```
  watchtower==1.0.6
  ```

### 我在 CloudTrail 中看到了 `Invalid request` 错误
<a name="t-cloudtrail-bucket"></a>

```
Invalid request provided: Provided role does not have sufficient permissions for s3 location airflow-xxx-xxx/dags
```

如果您使用相同的 CloudFormation 模板创建 Amazon MWAA 环境和 Amazon S3 存储桶，则需要在 CloudFormation 模板中添加 `DependsOn` 部分。这两个资源（*MWAA 环境*和 *MWAA 执行策略*）在 CloudFormation 中有依赖关系。我们建议您完成以下步骤：
+ 将以下 **DependsOn** 语句添加到 CloudFormation 模板中。

  ```
  ...
    MaxWorkers: 5
    NetworkConfiguration:
      SecurityGroupIds:
        - !GetAtt SecurityGroup.GroupId
      SubnetIds: !Ref subnetIds
    WebserverAccessMode: PUBLIC_ONLY
  DependsOn: MwaaExecutionPolicy
  
   MwaaExecutionPolicy:
   Type: AWS::IAM::ManagedPolicy
   Properties:
     Roles:
      - !Ref MwaaExecutionRole
    PolicyDocument:
      Version: 2012-10-17		 	 	 
      Statement:
        - Effect: Allow
          Action: airflow:PublishMetrics
          Resource:
  ...
  ```

  有关具体示例，请参阅 [Amazon MWAA 的快速入门教程](quick-start.md)。

### 我在 Apache Airflow 日志中看到了 `Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory`
<a name="t-plugins-logs"></a>
+ 我们建议您完成以下步骤：

  1. 如果您使用的是 Apache Airflow v2，请添加 `core.lazy_load_plugins : False` 为 Apache Airflow 配置选项。要了解更多信息，请参阅 [2 中的使用配置选项加载插件](configuring-env-variables.md#configuring-2.0-airflow-override)。

### 我在我的计划程序日志中看到了 psycopg2“服务器意外关闭了连接”
<a name="scheduler-postgres-library"></a>

如果您看到类似于以下内容的错误，则说明 Apache Airflow 计划程序可能已耗尽资源。

```
2021-06-14T10:20:24.581-05:00    sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
2021-06-14T10:20:24.633-05:00    This probably means the server terminated abnormally
2021-06-14T10:20:24.686-05:00    before or while processing the request.
```

我们建议您完成以下步骤：
+ 考虑升级到 Apache Airflow v2.0.2，此版本允许您指定多达 5 个计划程序。

### 我在我的 DAG 处理日志中看到了 `Executor reports task instance %s finished (%s) although the task says its %s`
<a name="long-running-tasks"></a>

如果您看到类似于以下内容的错误，则说明长时间运行的任务可能已达到 Amazon MWAA 上的任务时间限制。Amazon MWAA 对任何一个 Airflow 任务的限制为 12 小时，以防止任务卡在队列中并阻止自动扩缩等活动。

```
Executor reports task instance %s finished (%s) although the task says its %s. (Info: %s) Was the task killed externally
```

我们建议您完成以下步骤：
+ 考虑将任务分解为多个运行时间较短的任务。Airflow 通常有运算符异步模型。它调用外部系统上的活动，Apache Airflow 传感器会进行轮询以检查其何时完成。如果传感器出现故障，则可以在不影响运算符功能的情况下安全地重试。

### 我在我的任务日志中看到了 `Could not read remote logs from log_group: airflow-*{*environmentName}-Task log_stream:* {*DAG_ID}/*{*TASK_ID}/*{*time}/*{*n}.log.`
<a name="t-task-fail-permission"></a>

如果您看到类似于以下内容的错误，则环境的执行角色可能不包含为任务日志创建日志流的权限策略。

```
Could not read remote logs from log_group: airflow-*{*environmentName}-Task log_stream:* {*DAG_ID}/*{*TASK_ID}/*{*time}/*{*n}.log.
```

我们建议您完成以下步骤：
+ 使用 [Amazon MWAA 执行角色](mwaa-create-role.md) 中的示例策略之一修改环境的执行角色。

您可能还在 `requirements.txt` 文件中指定了与 Apache Airflow 版本不兼容的提供程序包。例如，如果您使用的是 Apache Airflow v2.0.2，则可能已经指定了程序包，例如 [apache-airflow-providers-databricks](https://airflow.apache.org/docs/apache-airflow-providers-databricks/stable/index.html) 程序包，它仅与 Airflow 2.1\$1 兼容。

我们建议您完成以下步骤：

1. 如果您使用的是 Apache Airflow v2.0.2，请修改 `requirements.txt` 文件并添加 `apache-airflow[databricks]`。这将安装与 Apache Airflow v2.0.2 兼容的 Databricks 程序包的正确版本。

1. 使用 GitHub 上的 [aws-mwaa-docker-images](https://github.com/aws/amazon-mwaa-docker-images) 在本地测试 DAG、自定义插件和 Python 依赖项。