【ZooKeeper Notes 29】 修復「ZooKeeper客戶端打印當前鏈接的服務器地址爲null」的Bug

轉載請註明:@ni掌櫃 nileader@gmail.comgit

 問題描述github

    公司以前進行了幾回機房容災演習中,常常是模擬一個機房掛掉的場景,把一個機房的網絡切掉,使得這個機房內部網絡通訊正常,與外部的網絡不通。在容災演習過程當中,咱們發現ZK的客戶端應用中出現大量相似這樣的日誌:apache

  
  
           
  
  
  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-1480session

 問題定位socket

    看了下3.4.3及其之前版本的ZooKeeper代碼,發現問題出在這裏,日誌打印的邏輯在這裏:ide

  
  
           
  
  
  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() 來獲取當前鏈接的服務器地址的,那再來看下這個方法:this

  
  
           
  
  
  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的緣由了。 spa

 問題解決

    這個日誌輸出對於開發人員來講很是重要,在排查問題過程當中能夠清楚的定位當時是哪臺服務器出現問題,可是這裏一旦輸出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); 
相關文章
相關標籤/搜索