【ZooKeeper Notes 29】 修复“ZooKeeper客户端打印当前连接的服务器地址为null”的Bug

本文涉及的产品
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
日志服务 SLS,月写入数据量 50GB 1个月
简介:

 问题描述

    公司之前进行了几次机房容灾演习中,经常是模拟一个机房挂掉的场景,把一个机房的网络切掉,使得这个机房内部网络通信正常,与外部的网络不通。在容灾演习过程中,我们发现ZK的客户端应用中出现大量类似这样的日志:

 
  1. An exception was thrown while closing send thread for ession 0x for server null, unexpected error, closing socket connection and attempting  

从这个日志中,红色部分出现的是null。当时看到这个情况,觉得,正常情况正在,这个地方应用出现的是那个被隔离的机房中部署的ZK的机器IP的,但是这里出现的是null,非常困惑。

    具体描述也可以在这里查看:https://issues.apache.org/jira/browse/ZOOKEEPER-1480

 问题定位

    看了下3.4.3及其以前版本的ZooKeeper代码,发现问题出在这里,日志打印的逻辑在这里:

 
  1. catch (Throwable e) {  
  2.     if (closing) {  
  3.         if (LOG.isDebugEnabled()) {  
  4.             // closing so this is expected  
  5.             LOG.debug("An exception was thrown while closing send thread for session 0x"  
  6.                     + Long.toHexString(getSessionId())  
  7.                     + " : " + e.getMessage());  
  8.         }  
  9.         break;  
  10.     } else {  
  11.         // this is ugly, you have a better way speak up  
  12.         if (e instanceof SessionExpiredException) {  
  13.             LOG.info(e.getMessage() + ", closing socket connection");  
  14.         } else if (e instanceof SessionTimeoutException) {  
  15.             LOG.info(e.getMessage() + RETRY_CONN_MSG);  
  16.         } else if (e instanceof EndOfStreamException) {  
  17.             LOG.info(e.getMessage() + RETRY_CONN_MSG);  
  18.         } else if (e instanceof RWServerFoundException) {  
  19.             LOG.info(e.getMessage());  
  20.         } else {  
  21.             LOG.warn(  
  22.                     "Session 0x"  
  23.                             + Long.toHexString(getSessionId())  
  24.                             + " for server "  
  25.                             + clientCnxnSocket.getRemoteSocketAddress()  
  26.                             + ", unexpected error"  
  27.                             + RETRY_CONN_MSG, e);  
  28.         }  

可以看到,在打印日志过程,是通过clientCnxnSocket.getRemoteSocketAddress() 来获取当前连接的服务器地址的,那再来看下这个方法:

 
  1. /** 
  2.      * Returns the address to which the socket is connected. 
  3.      * @return ip address of the remote side of the connection or null if not connected 
  4.      */ 
  5.     @Override 
  6.     SocketAddress getRemoteSocketAddress() { 
  7.         // a lot could go wrong here, so rather than put in a bunch of code 
  8.         // to check for nulls all down the chain let's do it the simple 
  9.         // yet bulletproof way 
  10.         try { 
  11.             return ((SocketChannel) sockKey.channel()).socket() 
  12.                     .getRemoteSocketAddress(); 
  13.         } catch (NullPointerException e) { 
  14.             return null
  15.         } 
  16.     /** 
  17.      * Returns the address of the endpoint this socket is connected to, or 
  18.      * <code>null</code> if it is unconnected. 
  19.      * @return a <code>SocketAddress</code> reprensenting the remote endpoint of this 
  20.      *         socket, or <code>null</code> if it is not connected yet. 
  21.      * @see #getInetAddress() 
  22.      * @see #getPort() 
  23.      * @see #connect(SocketAddress, int) 
  24.      * @see #connect(SocketAddress) 
  25.      * @since 1.4 
  26.      */ 
  27.     public SocketAddress getRemoteSocketAddress() { 
  28.       if (!isConnected()) 
  29.         return null
  30.       return new InetSocketAddress(getInetAddress(), getPort()); 

所以,现在基本就可以定位问题了,如果服务器端非正常关闭socket连接(例如容灾演习的时候把机房网络切断),那么getRemoteSocketAddress这个方法就会返回null了,也就是日志中为什么出现null的原因了。 

 问题解决

    这个日志输出对于开发人员来说非常重要,在排查问题过程中可以清楚的定位当时是哪台服务器出现问题,但是这里一旦输出null,那么将无从下手。这里我做了一些改进,确保出现问题的时候,客户端能够输出当前出现问题的服务器IP。在这里下载补丁:https://github.com/downloads/nileader/taokeeper/getCurrentZooKeeperAddr_for_3.4.3.patch

    首先是给org.apache.zookeeper.client.HostProvider类添加两个接口,分别用于获取“当前地址列中正在使用的地址序号”和获取“所有地址列表”。关于ZooKeeper客户端地址列表获取和随机原理,具体可以查看这个文章《ZooKeeper客户端地址列表的随机原理》。

 
  1. public interface HostProvider { 
  2.     …… …… 
  3.     /**  
  4.      * Get current index that is connecting or connected.  
  5.      * @see ZOOKEEPER-1480:https://issues.apache.org/jira/browse/ZOOKEEPER-1480 
  6.      * */ 
  7.     public int getCurrentIndex(); 
  8.     /** 
  9.      * Get all server address that config when use zookeeper client. 
  10.      * @return List  
  11.      * @see ZOOKEEPER-1480:https://issues.apache.org/jira/browse/ZOOKEEPER-1480 
  12.      */ 
  13.     public List<InetSocketAddress> getAllServerAddress(); 
  14.      

 其次是修改org.apache.zookeeper.ClientCnxn类中日志输出逻辑:

 
  1. /** 
  2.          * Get current zookeeper addr that client is connected or connecting.<br> 
  3.          * Note:The method will return null if can't not get host ip. 
  4.          * */ 
  5.         private InetSocketAddress getCurrentZooKeeperAddr(){ 
  6.             try { 
  7.                 InetSocketAddress addr = null
  8.                 ifnull == hostProvider || null == hostProvider.getAllServerAddress() ) 
  9.                     return addr; 
  10.                 int index = hostProvider.getCurrentIndex(); 
  11.                 if ( index >= 0  ) { 
  12.                     addr = hostProvider.getAllServerAddress().get( index ); 
  13.                 } 
  14.                 return addr; 
  15.             } catch ( Exception e ) { 
  16.                 return null
  17.             } 
  18.         } 
  19. …… …… 
  20.         //get current ZK host to log 
  21.         InetSocketAddress addr = getCurrentZooKeeperAddr(); 
  22.          
  23.         LOG.warn( 
  24.             "Session 0x" 
  25.                     + Long.toHexString(getSessionId()) 
  26.                     + " for server ip: " + addr + ", detail conn: " 
  27.                     + clientCnxnSocket.getRemoteSocketAddress() 
  28.                     + ", unexpected error" 
  29.                     + RETRY_CONN_MSG, e); 

 本文转自 nileader 51CTO博客,原文链接:http://blog.51cto.com/nileader/1049470,如需转载请自行联系原作者


相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
15天前
|
人工智能 搜索推荐 程序员
用 Go 语言轻松构建 MCP 客户端与服务器
本文介绍了如何使用 mcp-go 构建一个完整的 MCP 应用,包括服务端和客户端两部分。 - 服务端支持注册工具(Tool)、资源(Resource)和提示词(Prompt),并可通过 stdio 或 sse 模式对外提供服务; - 客户端通过 stdio 连接服务器,支持初始化、列出服务内容、调用远程工具等操作。
188 3
|
1月前
|
机器学习/深度学习 人工智能 运维
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
101 14
|
1月前
|
网络协议 开发者 Python
Socket如何实现客户端和服务器间的通信
通过上述示例,展示了如何使用Python的Socket模块实现基本的客户端和服务器间的通信。Socket提供了一种简单且强大的方式来建立和管理网络连接,适用于各种网络编程应用。理解和掌握Socket编程,可以帮助开发者构建高效、稳定的网络应用程序。
79 10
|
3月前
|
存储 开发工具 git
[Git] 深入理解 Git 的客户端与服务器角色
Git 的核心设计理念是分布式,每个仓库既可以是客户端也可以是服务器。通过 GitHub 远程仓库和本地仓库的协作,Git 实现了高效的版本管理和代码协作。GitHub 作为远程裸仓库,存储项目的完整版本历史并支持多客户端协作;本地仓库则通过 `.git` 文件夹独立管理版本历史,可在离线状态下进行提交、回滚等操作,并通过 `git pull` 和 `git push` 与远程仓库同步。这种分布式特性使得 Git 在代码协作中具备强大的灵活性和可靠性。
95 18
[Git] 深入理解 Git 的客户端与服务器角色
|
4月前
|
存储 人工智能 自然语言处理
ChatMCP:基于 MCP 协议开发的 AI 聊天客户端,支持多语言和自动化安装 MCP 服务器
ChatMCP 是一款基于模型上下文协议(MCP)的 AI 聊天客户端,支持多语言和自动化安装。它能够与多种大型语言模型(LLM)如 OpenAI、Claude 和 OLLama 等进行交互,具备自动化安装 MCP 服务器、SSE 传输支持、自动选择服务器、聊天记录管理等功能。
1611 16
ChatMCP:基于 MCP 协议开发的 AI 聊天客户端,支持多语言和自动化安装 MCP 服务器
|
4月前
|
JSON 前端开发 Java
【Bug合集】——Java大小写引起传参失败,获取值为null的解决方案
类中成员变量命名问题引起传送json字符串,但是变量为null的情况做出解释,@Data注解(Spring自动生成的get和set方法)和@JsonProperty
|
5月前
|
开发框架 .NET C#
在 ASP.NET Core 中创建 gRPC 客户端和服务器
本文介绍了如何使用 gRPC 框架搭建一个简单的“Hello World”示例。首先创建了一个名为 GrpcDemo 的解决方案,其中包含一个 gRPC 服务端项目 GrpcServer 和一个客户端项目 GrpcClient。服务端通过定义 `greeter.proto` 文件中的服务和消息类型,实现了一个简单的问候服务 `GreeterService`。客户端则通过 gRPC 客户端库连接到服务端并调用其 `SayHello` 方法,展示了 gRPC 在 C# 中的基本使用方法。
98 5
在 ASP.NET Core 中创建 gRPC 客户端和服务器
|
6月前
|
Python
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
使用Python的socket库实现客户端到服务器端的图片传输,包括客户端和服务器端的代码实现,以及传输结果的展示。
256 3
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
|
6月前
|
JSON 数据格式 Python
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
本文介绍了如何使用Python的socket模块实现客户端到服务器端的文件传输,包括客户端发送文件信息和内容,服务器端接收并保存文件的完整过程。
325 1
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
|
6月前
|
网络协议 Unix Linux
一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
152 4

热门文章

最新文章