使用 zookeeper C 客户端的进程在 SIGTERM 上断开连接

Process using zookeeper C client gets disconnected on SIGTERM

我们在我们的应用程序中使用 Apache Zookeeper 客户端 C 绑定。客户端库版本为 3.5.1。当 Zookeeper 连接断开时,应用程序配置为退出,错误代码为 116。

Systemd 被用来自动化 starting/stopping 应用程序。单元文件不会覆盖 KillMode 的默认设置,即 send SIGTERM to the application.

当使用 systemctl stop 指令停止进程时,Zookeeper 客户端线程似乎正在尝试重新连接到 Zookeeper:

2016-04-12 22:34:45,799:4506(0xf14f7b40):ZOO_ERROR@handle_socket_error_msg@2363: Socket [128.0.0.4:61758] zk retcode=-4, errno=112(Host is down): failed while receiving a server response
2016-04-12 22:34:45,799:4506(0xf14f7b40):ZOO_INFO@check_events@2345: initiated connection to server [128.0.0.4:61758]
Apr 12 22:34:45 main thread:   zookeeperWatcher: event type ZOO_SESSION_EVENT state ZOO_CONNECTING_STATE path
2016-04-12 22:34:45,801:4506(0xf14f7b40):ZOO_INFO@check_events@2397: session establishment complete on server [128.0.0.4:61758], sessionId=0x40000015b8d0077, negotiated timeout=20000
2016-04-12 22:34:46,476:4506(0xf14f7b40):ZOO_WARN@zookeeper_interest@2191: Delaying connection after exhaustively trying all servers [128.0.0.4:61758]
2016-04-12 22:34:46,810:4506(0xf14f7b40):ZOO_INFO@check_events@2345: initiated connection to server [128.0.0.4:61758]
2016-04-12 22:34:46,811:4506(0xf14f7b40):ZOO_ERROR@handle_socket_error_msg@2382: Socket [128.0.0.4:61758] zk retcode=-112, errno=116(Stale file handle): sessionId=0x40000015b8d0077 h

因此,进程正在退出并显示错误代码。 Systemd 在退出时会看到失败代码,并且不会尝试重新启动应用程序。有谁知道为什么客户端会断开连接?

我知道我可以通过在单元文件中设置 SuccessExitStatus=116 来解决这个问题,但我不想掩盖真正的错误。我已经尝试为 SIGTERM 注册一个信号处理程序并在处理程序中关闭 Zookeeper 客户端。但是当我发出 systemctl stop 时,处理程序代码似乎永远不会被击中。

编辑:处理程序未被调用,因为我将其设为异步 - 它没有在收到信号后立即执行。 OTOH 该进程在 Zookeeper 断开连接后立即退出。

加载 SIGTERM 的处理程序并发出 systemctrl stop 时会发生什么? 如果什么都没有发生,那么你可能有一个屏蔽信号的面具(我猜不是)。 如果应用程序继续退出并显示相同的错误代码,那么我建议您确保正确加载了信号处理程序。

这是预期的工作,应用程序编写者有责任指定如何正常关闭服务,如果您不想使用发送 SIGTERM 的默认设置,您可以使用 ExecStop 来制作自己的停止命令在单元文件中:

ExecStart=/usr/bin/app
ExecStop=/usr/bin/app -stop

有关详细信息,请参阅文档 https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStop=

这个问题无关,有人 运行 一个正在终止连接的脚本。谢谢大家的帮助!