mosn icon indicating copy to clipboard operation
mosn copied to clipboard

An exception may occur when a new connection in the gracefully shutdown during non-hot upgrade

Open wangfakang opened this issue 2 years ago • 3 comments

issue

There will be exceptions when a client creates a new connection in the graceful shutdown scenario during non-hot upgrade. In this scenario, we should close the listener immediately and wait gracefully for the established connection to exit.

https://github.com/mosn/mosn/blob/master/pkg/network/listener.go#L226-L232

@hui-cha

wangfakang avatar Jan 12 '23 09:01 wangfakang

/assign @hui-cha

wangfakang avatar Jan 12 '23 09:01 wangfakang

OK,最近会做一下这个事情

hui-cha avatar Feb 02 '23 09:02 hui-cha

问题描述: 我们目前在使用 Mosn 的优雅下线能力,通信协议是 Bolt 协议,场景是网关代理场景,不使用 Mosn 的热升级。最近我们发现在触发优雅下线,向已有链接发送 Goaway 之后关停服务,客户端会出现 UpstreamConnectionTermination 的错误,这个是不符合预期的,因为触发了优雅下线之后 Mosn 关闭 Listener 以拒绝新链接。

排查下来发现是 Mosn 的处理流程有些问题。

优雅下线是在 StageManager 中的 Stop 方法中处理的 https://github.com/mosn/mosn/blob/3ba03cde9afe8fdfc2d4e38540f3949ba305ac74/pkg/stagemanager/stage_manager.go#L380

这个方法的 387 行的 runGracefulStopStage 方法最终会触发 Listener 的 Shutdown https://github.com/mosn/mosn/blob/3ba03cde9afe8fdfc2d4e38540f3949ba305ac74/pkg/network/listener.go#L226

这个 Shutdown 方法第一步是停止 Accpect 新链接,但是这个地方是没有关闭 Listener 的,只是让 Mosn 退出了 Accpect 的 Loop,新的链接仍然是可以链接上的

第二步是向已有链接上发送 Goaway 包 ( 如果支持 ),之后在 586 行同步等待这些已有链接上的请求都处理完成,这个等待的时长可能会较长,因为一般业务的请求处理耗时通常都是几十或者几百毫秒 https://github.com/mosn/mosn/blob/3ba03cde9afe8fdfc2d4e38540f3949ba305ac74/pkg/server/handler.go#L576

这样在已有链接上的请求处理完成之前,仍然可能有新链接连到 Mosn 上,但是此时 Mosn 已经退出了 Accpect Loop,这个链接是不会被处理的,这样这个链接上的请求就都会积压住,最终全部会失败

hui-cha avatar Feb 15 '23 12:02 hui-cha