【问题标题】:Postgresql WalReceiver process waits on connecting master regardless of "connect_timeout"无论“connect_timeout”如何,Postgresql WalReceiver 进程都会等待连接主机
【发布时间】:2020-02-03 23:50:23
【问题描述】:

我正在尝试在 kubernetes 上部署一个自动化的高可用 PostgreSQL 集群。在 master 故障转移或 master 临时故障的情况下,standby 会丢失流复制连接,并且在重试时,它需要很长时间才能失败并重试。

我使用 PostgreSQL 10 和流式复制(cluster-main-cluster-master-service 是一个始终路由到主服务器的服务,所有副本都连接到该服务进行复制)。我尝试在recovery.confprimary_conninfo 中设置connect_timeoutkeepalive 等配置,在待机postgresql.conf 中设置wal_receiver_timeout,但我无法使用它们取得任何进展。

首先,当 master 宕机时,复制停止并出现以下错误(状态 1):

2019-10-06 14:14:54.042 +0330 [3039] LOG:  replication terminated by primary server
2019-10-06 14:14:54.042 +0330 [3039] DETAIL:  End of WAL reached on timeline 17 at 0/33000098.
2019-10-06 14:14:54.042 +0330 [3039] FATAL:  could not send end-of-streaming message to primary: no COPY in progress
2019-10-06 14:14:55.534 +0330 [12] LOG:  record with incorrect prev-link 0/2D000028 at 0/33000098

在调查 Postgres 活动后,我发现 WalReceiver 进程卡在 LibPQWalReceiverConnectwait_event(状态 2),但超时比我配置的要长得多(虽然我将 connect_timeout 设置为 10 秒,但大约需要 2 分钟)。然后,它失败并出现以下错误(状态 3):

2019-10-06 14:17:06.035 +0330 [3264] FATAL:  could not connect to the primary server: could not connect to server: Connection timed out
        Is the server running on host "cluster-main-cluster-master-service" (192.168.0.166) and accepting
        TCP/IP connections on port 5432?

在下一次尝试中,它成功连接了主节点(状态 4):

2019-10-06 14:17:07.892 +0330 [5786] LOG:  started streaming WAL from primary at 0/33000000 on timeline 17

我还尝试在发生卡住事件时终止进程(状态 2),当我这样做时,它会再次启动进程并连接然后正常流式传输(跳转到状态 4)。

查看netstat后,我还发现在walreceiver进程中(在故障转移情况下)与旧master有SYN_SENT状态的连接。

【问题讨论】:

    标签: postgresql kubernetes tcp


    【解决方案1】:

    connect_timeout 控制 PostgreSQL 等待复制连接成功的时间,但这不包括建立 TCP 连接。

    要减少内核等待成功响应 TCP SYN 请求的时间,请减少重试次数。在/etc/sysctl.conf,设置:

    net.ipv4.tcp_syn_retries = 3
    

    然后运行sysctl -p

    这应该会大大减少时间。

    过多地降低该值可能会使您的系统不太稳定。

    【讨论】:

    • 感谢您的回复。实际上我已经尝试过了,效果很好,时间也减少了。但我担心这会对整个系统产生什么后果和副作用。此外,我认为应该有一个应用程序级别的超时,它设置一个超时,无论程序处于哪个步骤。 (无论是否建立连接)
    • 如果您认为这种行为令人惊讶,您可以将其作为 PostgreSQL 错误提出。
    猜你喜欢
    • 2022-01-02
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2023-03-09
    • 2018-07-01
    • 2014-11-08
    相关资源
    最近更新 更多