【发布时间】:2021-04-26 21:18:54
【问题描述】:
我们有一个在 AWS Fargate 中运行的 Airflow 实例。它连接到本地 Postgres 服务器(在 Windows 上)并尝试从(复杂的)视图加载数据。它为此使用PostgresHook。但是,DAG 中的任务在 Airflow 中失败并出现以下错误:
File "/usr/local/lib/python3.7/site-packages/airflow/hooks/dbapi_hook.py", line 120, in get_records
cur.execute(sql)
psycopg2.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
不久前,错误发生在大约 10-15 分钟后。现在,它发生得更快,5 分钟后甚至更快。
我查看了 Postgres 日志,显示(令人困惑)是客户端关闭了连接:
LOG: could not send data to client: An existing connection was forcibly closed by the remote host.
FATAL: connection to client lost
我已经尝试了很多可能的解决方案。
无气流
在 Airflow 之外连接到服务器,直接使用psycopg2:工作(使用复杂视图)。
不同的表 尝试从云中 Airflow 的不同表加载数据:有效,也很快完成。所以这个“超时”只是因为查询需要一段时间才发生。
在本地运行 Airflow 容器
起初我可以重现这个问题,但我(想我)通过在 postgres 连接字符串中添加一些额外的参数来解决它:keepalives=1&keepalives_idle=60&keepalives_interval=60。但是,我无法在云中的 Airflow 中重现此修复,因为当我在那里添加这些参数时,错误仍然存在。
增加超时时间
见上文,我添加了keepalives,但我也尝试推断其他潜在的超时。我在 DAG 参数中添加了超时 execution_timeout,但无济于事。我们还检查了网络超时,但鉴于连接失败的不规则模式,这听起来不像是硬超时...
我在这里不知所措。有什么建议吗?
【问题讨论】:
标签: python postgresql amazon-web-services airflow psycopg2