排查环境更新和升级问题

Managed Airflow(第 3 代) | Managed Airflow(第 2 代) | Managed Airflow(旧版第 1 代)

本页面提供了问题排查信息,可帮助您解决在更新或升级 Managed Service for Apache Airflow 环境时可能遇到的问题。

如需了解与创建环境相关的问题排查信息,请参阅排查环境创建问题。

更新 Managed Airflow 环境时,大多数问题都是由以下原因引起的:

  • 服务账号权限问题
  • PyPI 依赖项问题
  • Airflow 数据库的大小

权限不足,无法更新或升级环境

如果 Managed Airflow 因权限不足而无法更新或升级环境,则会输出以下出错提示:

ERROR: (gcloud.composer.environments.update) PERMISSION_DENIED: The caller does not have permission

解决方案:按照访问权限控制中的说明,为您的账号和环境的服务账号分配角色。

环境的服务账号权限不足

创建 Managed Airflow 环境时,您需要指定一个服务账号来执行该环境的大部分操作。如果此服务账号没有足够的权限来执行请求的操作,Managed Airflow 会输出错误:

    UPDATE operation on this environment failed 3 minutes ago with the
    following error message:
    Composer Backend timed out. Currently running tasks are [stage:
    CP_COMPOSER_AGENT_RUNNING
    description: "No agent response published."
    response_timestamp {
      seconds: 1618203503
      nanos: 291000000
    }
    ].

解决方案:按照访问权限控制中的说明,为您的 Google 账号和环境的服务账号分配角色。

Airflow 数据库太大,无法执行此操作

升级操作可能无法成功,因为 Airflow 数据库的大小过大,升级操作无法成功。

如果 Airflow 数据库的大小超过 16 GB,Managed Airflow 会输出以下错误:

Airflow database uses more than 16 GB. Please clean the database before upgrading.

解决方案:执行 Airflow 数据库清理,如清理 Airflow 数据库中所述。

由于 PyPI 软件包冲突,升级到新的 Managed Airflow 版本失败

升级安装了自定义 PyPI 软件包的环境时,您可能会遇到与 PyPI 软件包冲突相关的错误。这可能是因为新的 Managed Service for Apache Airflow 映像包含更高版本的预安装软件包。这可能会导致与您在环境中安装的 PyPI 软件包发生依赖项冲突。

解决方案:

  • 如需获取有关软件包冲突的详细信息,请运行升级检查。
  • 放宽已安装的自定义 PyPI 软件包的版本限制条件。例如,不要将版本指定为 ==1.0.1,而应指定为 >=1.0.1。
  • 如需详细了解如何更改版本要求以解决冲突的依赖项,请参阅 pip 文档。

无法将环境升级到仍受支持的版本

Managed Airflow 环境只能升级到几个最新版本和先前版本。

创建新环境和升级现有环境的版本限制不同。您在创建新环境时选择的 Managed Airflow 版本可能不适用于升级现有环境。

您可以使用 Google Cloud CLI、API 或 Terraform 执行升级操作。在 Google Cloud 控制台中,只有最新版本可作为升级选项。

环境不健康(活跃性检查失败)

只有在环境状态报告为健康时,才能升级环境。

导致状态不正常的最常见原因之一是环境组件接近配置的资源限制,并持续以最大负载运行。由于某些环境组件无法报告其状态,因此活跃度检查 DAG 会将环境状态报告为不健康。

为解决此问题,我们建议您提高资源限制。虽然我们建议您始终让环境不接近限制,但您也可以仅在环境升级期间这样做。

无法连接到 DNS 可能会导致在执行升级或更新时出现问题

此类连接问题可能会导致如下日志条目:

WARNING - Compute Engine Metadata server unavailable attempt 1 of 5. Reason: [Errno -3] Temporary failure in name resolution Error

这通常意味着没有指向 DNS 的路由,因此请确保可以从集群、Pod 和 Service 网络中将 metadata.google.internal DNS 名称解析为 IP 地址。检查您是否在创建环境的 VPC(在宿主项目或服务项目中)中启用了专用 Google 访问通道。

触发器 CPU 超出 1 个 vCPU 的限制

Managed Airflow 2.4.4 及更高版本引入了不同的触发器资源分配策略,以提高性能伸缩。如果您在执行环境更新时遇到与触发器 CPU 相关的错误,则表示您当前的触发器配置为每个触发器使用超过 1 个 vCPU。

解决方案:

检查迁移失败警告

将 Airflow 升级到更高版本时,有时会对 Airflow 数据库应用新的限制。如果无法应用这些限制条件,Airflow 会创建新表来存储无法应用限制条件的行。Airflow 界面会显示警告消息,直到移动的数据表被重命名或删除。

解决方案:

您可以使用以下两个 DAG 来检查移动的数据并重命名表。

list_moved_tables_after_upgrade_dag DAG 会列出从无法应用限制条件的每个表中移出的行。检查数据并决定是否要保留。如需保留该数据,您需要手动修复 Airflow 数据库中的数据。例如,通过添加包含正确数据的行。

如果您不需要这些数据,或者已经修复了这些数据,则可以运行 rename_moved_tables_after_upgrade_dag DAG。此 DAG 会重命名已移动的表。 表及其数据不会被删除,因此您可以在以后查看数据。

"""
When upgrading Airflow to a newer version,
it might happen that some data cannot be migrated,
often because of constraint changes in the metadata base.
This file contains 2 DAGs:

1. 'list_moved_tables_after_upgrade_dag'
  Prints the rows which failed to be migrated.
2. 'rename_moved_tables_after_upgrade_dag'
  Renames the table which contains the failed migrations. This will remove the
  warning message from airflow.
"""

import datetime
import logging

from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.settings import AIRFLOW_MOVED_TABLE_PREFIX


def get_moved_tables():
    hook = PostgresHook(postgres_conn_id="airflow_db")
    return hook.get_records(
        "SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename"
        f" LIKE '{AIRFLOW_MOVED_TABLE_PREFIX}_%'"
    )


def list_moved_records():
    tables = get_moved_tables()
    if not tables:
        logging.info("No moved tables found")
        return

    hook = PostgresHook(postgres_conn_id="airflow_db")
    for schema, table in tables:
        df = hook.get_pandas_df(f"SELECT * FROM {schema}.{table}")
        logging.info(df.to_markdown())


def rename_moved_tables():
    tables = get_moved_tables()
    if not tables:
        return

    hook = PostgresHook(postgres_conn_id="airflow_db")
    for schema, table in tables:
        hook.run(f"ALTER TABLE {schema}.{table} RENAME TO _abandoned_{table}")


with DAG(
    dag_id="list_moved_tables_after_upgrade_dag",
    start_date=datetime.datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False,
):
    t1 = PythonOperator(
        task_id="list_moved_records", python_callable=list_moved_records
    )

with DAG(
    dag_id="rename_moved_tables_after_upgrade_dag",
    start_date=datetime.datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False,
) as dag:
    t1 = PythonOperator(
        task_id="rename_moved_tables", python_callable=rename_moved_tables
    )

环境操作无限期处于失败状态

Managed Airflow(第 2 代)环境依赖于 Pub/Sub 主题和订阅,以便在环境操作期间与环境的租户项目中的资源进行通信。

如果您的项目中停用了 Pub/Sub API,或者环境的主题或订阅被删除,则环境操作可能会失败并无限期保持失败状态。此类环境会变得无法恢复。

后续步骤