【ZooKeeper Notes 29】 修复“ZooKeeper客户端打印当前连接的服务器地址为null”的Bug
2012-11-04 08:21
441 查看
转载请注明:@ni掌柜 nileader@gmail.com
问题描述
公司之前进行了几次机房容灾演习中,经常是模拟一个机房挂掉的场景,把一个机房的网络切掉,使得这个机房内部网络通信正常,与外部的网络不通。在容灾演习过程中,我们发现ZK的客户端应用中出现大量类似这样的日志:
从这个日志中,红色部分出现的是null。当时看到这个情况,觉得,正常情况正在,这个地方应用出现的是那个被隔离的机房中部署的ZK的机器IP的,但是这里出现的是null,非常困惑。
具体描述也可以在这里查看:https://issues.apache.org/jira/browse/ZOOKEEPER-1480
问题定位
看了下3.4.3及其以前版本的ZooKeeper代码,发现问题出在这里,日志打印的逻辑在这里:
可以看到,在打印日志过程,是通过clientCnxnSocket.getRemoteSocketAddress() 来获取当前连接的服务器地址的,那再来看下这个方法:
所以,现在基本就可以定位问题了,如果服务器端非正常关闭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客户端地址列表的随机原理》。
其次是修改org.apache.zookeeper.ClientCnxn类中日志输出逻辑:
本文出自 “ni掌柜的IT专栏” 博客,请务必保留此出处http://nileader.blog.51cto.com/1381108/1049470
问题描述
公司之前进行了几次机房容灾演习中,经常是模拟一个机房挂掉的场景,把一个机房的网络切掉,使得这个机房内部网络通信正常,与外部的网络不通。在容灾演习过程中,我们发现ZK的客户端应用中出现大量类似这样的日志:
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代码,发现问题出在这里,日志打印的逻辑在这里:
} catch (Throwable e) { if (closing) { if (LOG.isDebugEnabled()) { // closing so this is expected LOG.debug("An exception was thrown while closing send thread for session 0x" + Long.toHexString(getSessionId()) + " : " + e.getMessage()); } break; } else { // this is ugly, you have a better way speak up if (e instanceof SessionExpiredException) { LOG.info(e.getMessage() + ", closing socket connection"); } else if (e instanceof SessionTimeoutException) { LOG.info(e.getMessage() + RETRY_CONN_MSG); } else if (e instanceof EndOfStreamException) { LOG.info(e.getMessage() + RETRY_CONN_MSG); } else if (e instanceof RWServerFoundException) { LOG.info(e.getMessage()); } else { LOG.warn( "Session 0x" + Long.toHexString(getSessionId()) + " for server " + clientCnxnSocket.getRemoteSocketAddress() + ", unexpected error" + RETRY_CONN_MSG, e); }
可以看到,在打印日志过程,是通过clientCnxnSocket.getRemoteSocketAddress() 来获取当前连接的服务器地址的,那再来看下这个方法:
/** * Returns the address to which the socket is connected. * @return ip address of the remote side of the connection or null if not connected */ @Override SocketAddress getRemoteSocketAddress() { // a lot could go wrong here, so rather than put in a bunch of code // to check for nulls all down the chain let's do it the simple // yet bulletproof way try { return ((SocketChannel) sockKey.channel()).socket() .getRemoteSocketAddress(); } catch (NullPointerException e) { return null; } } /** * Returns the address of the endpoint this socket is connected to, or * <code>null</code> if it is unconnected. * @return a <code>SocketAddress</code> reprensenting the remote endpoint of this * socket, or <code>null</code> if it is not connected yet. * @see #getInetAddress() * @see #getPort() * @see #connect(SocketAddress, int) * @see #connect(SocketAddress) * @since 1.4 */ public SocketAddress getRemoteSocketAddress() { if (!isConnected()) return null; 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客户端地址列表的随机原理》。
public interface HostProvider { …… …… /** * Get current index that is connecting or connected. * @see ZOOKEEPER-1480:https://issues.apache.org/jira/browse/ZOOKEEPER-1480 * */ public int getCurrentIndex(); /** * Get all server address that config when use zookeeper client. * @return List * @see ZOOKEEPER-1480:https://issues.apache.org/jira/browse/ZOOKEEPER-1480 */ public List<InetSocketAddress> getAllServerAddress(); }
其次是修改org.apache.zookeeper.ClientCnxn类中日志输出逻辑:
/** * Get current zookeeper addr that client is connected or connecting.<br> * Note:The method will return null if can't not get host ip. * */ private InetSocketAddress getCurrentZooKeeperAddr(){ try { InetSocketAddress addr = null; if( null == hostProvider || null == hostProvider.getAllServerAddress() ) return addr; int index = hostProvider.getCurrentIndex(); if ( index >= 0 ) { addr = hostProvider.getAllServerAddress().get( index ); } return addr; } catch ( Exception e ) { return null; } } …… …… //get current ZK host to log InetSocketAddress addr = getCurrentZooKeeperAddr(); LOG.warn( "Session 0x" + Long.toHexString(getSessionId()) + " for server ip: " + addr + ", detail conn: " + clientCnxnSocket.getRemoteSocketAddress() + ", unexpected error" + RETRY_CONN_MSG, e);
本文出自 “ni掌柜的IT专栏” 博客,请务必保留此出处http://nileader.blog.51cto.com/1381108/1049470
相关文章推荐
- 【ZooKeeper Notes 29】 修复“ZooKeeper客户端打印当前连接的服务器地址为null”的Bug问题描述
- 查询Oracle服务器地址和当前连接的客户端地址
- 监控远程数据库服务器上当前连接用户IP地址
- zookeeper客户端 和服务器连接时版本问题
- 修复windows环境zookeeper服务器重启有可能会导致客户端CPU占用率100%的问题
- 客户端C和服务器S之间建立了一个TCP连接,TCP最大段长度为1KB,客户端C当前的拥塞窗口是16KB,向服务器S连续发送2个最大段之后,成功收到服务器S发送的第一段的确认段,确认段中通告的接受窗口大
- FL2440 通过Socket使开发板作为服务器支持多个客户端连接并与之通信,可通过命令获取当前温度。
- WCF客户端动态设置WCF服务器主机的地址的方法参考,可以连接多个相同WCF主机的方法
- zookeeper客户端 和服务器连接时版本问题
- 客户端连接XENAPP 6.5 提示 该地址未配置xenapp服务器
- WCF客户端动态设置WCF服务器主机的地址的方法参考,可以连接多个相同WCF主机的方法
- ZooKeeper客户端 和服务器连接时版本问题
- zookeeper客户端 和服务器连接时版本问题
- 如何查看连接到Domino服务器上的Notes客户端的IP地址
- ZooKeeper客户端 和服务器连接时版本问题
- zookeeper客户端 和服务器连接时版本问题
- WCF客户端动态设置WCF服务器主机的地址的方法参考,可以连接多个相同WCF主机的方法
- 服务器更改IP(公网)地址后,Program Neighborhood客户端无法连接服务器
- java客户端测试连接zookeeper服务器
- Android 使用Socket实现服务器与手机客户端的长连接五:使用队列封装请求