2012-03-05 109 views
0

Netty Channel.close()偶爾掛起。在我們的特殊使用案例中,我們擁有一批渠道,我們的測試檢查是否容忍網絡故障。所以,在我們的測試中,我們試圖關閉一個頻道。Netty Channel.close()間歇性掛起

在下面的代碼片段中,我們在調用Channel.close()之前,在Channel.close()之後,以及在ChannelFuture.await()之後立即打印調試語句。爲了確保線程不被中斷,我們檢查InterruptedException。

 Channel c = partitionChannelMap.get(partition); 
     if (c != null) { 
      for (int retries = 0; retries < numRetries; retries++) { 
       try { 
        logger.debug("Attempt {}: Closing channel to partition {}", retries + 1, partition); 
        logger.debug("Channel Properties - isBound() isConnected() isOpen() " + c.isBound() + " " 
          + c.isConnected() + " " + c.isOpen()); 
        ChannelFuture closeFuture = c.close(); 
        logger.debug("About to wait"); 
        closeFuture.await(nettyTimeout); 
        if (closeFuture.isSuccess()) { 
         logger.debug("Attempt {}: CLOSED channel to partition {}", retries + 1, partition); 
         partitionChannelMap.remove(partition); 
         break; 
        } else { 
         logger.error("Attempt {}: FAILED to close partition {}", retries + 1, partition); 
         continue; 
        } 
       } catch (InterruptedException e) { 
        logger.error("Attempt {}: FAILED to close partition {}", retries + 1, partition); 
        e.printStackTrace(); 
        continue; 
       } 
      } 
     } 
    } 

在一些運行(錯誤的)的,Channel.close前的調試語句()被執行,而不要立即之後。由於Channel.close()是異步的,我們期待它立即返回。在這些情況下,調用Channel.close()後執行掛起。

我在這裏假設或做錯了什麼?

樣品輸出錯誤執行 -

15:12:32.497 [Thread-7] DEBUG org.apache.s4.comm.tcp.TCPEmitter - Attempt 1: Closing channel to partition 0 
15:12:32.497 [Thread-7] DEBUG org.apache.s4.comm.tcp.TCPEmitter - Channel Properties - isBound() isConnected() isOpen() true true true 

我真的很感激有這方面的幫助。

謝謝

回答

0

問題在於我的代碼。

我在同步塊中調用Channel.close()。 close()會干擾併發傳輸中的消息並異步調用失敗傳輸的operationComplete()。恰巧,operationComplete()處理程序也試圖關閉相同的通道,導致死鎖。