设为首页 加入收藏

TOP

2.2.3 伪异步I/O弊端分析
2014-07-09 12:30:33 来源: 作者: 【 】 浏览:82
Tags:2.2.3 异步 I/O 弊端 分析

2.2.3  伪异步I/O弊端分析

要对伪异步I/O的弊端进行深入分析,首先我们看两个Java同步I/O的API说明,随后我们结合代码进行详细分析。

代码清单2-6  Java输入流InputStream
 

  1. /**  
  2.      * Reads some number of bytes from the input stream and stores them into  
  3.      * the buffer array <code>b</code>. The number of bytes actually read is  
  4.      * returned as an integer.  This method blocks until input data is  
  5.      * available, end of file is detected, or an exception is thrown.  
  6.      *  
  7.      * <p> If the length of <code>b</code> is zero, then no bytes are read and  
  8.      * <code>0</code> is returned; otherwise, there is an attempt to read at  
  9.      * least one byte. If no byte is available because the stream is at the  
  10.      * end of the file, the value <code>-1</code> is returned; otherwise, at  
  11.      * least one byte is read and stored into <code>b</code>.  
  12.      *  
  13.      * <p> The first byte read is stored into element <code>b[0]</code>, the  
  14.      * next one into <code>b[1]</code>, and so on. The number of bytes read is,  
  15.      * at most, equal to the length of <code>b</code>. Let <i>k</i> be the  
  16.      * number of bytes actually read; these bytes will be stored in elements  
  17.      * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,  
  18.      * leaving elements <code>b[</code><i>k</i><code>]</code> through  
  19.      * <code>b[b.length-1]</code> unaffected.  
  20.      *  
  21.      * @param      b   the buffer into which the data is read.  
  22.      * @return     the total number of bytes read into the buffer, or  
  23.      *             <code>-1</code> if there is no more data because the end of  
  24.      *             the stream has been reached.  
  25.      * @exception  IOException  If the first byte cannot be read for any reason  
  26.      * other than the end of the file, if the input stream has been closed, or  
  27.      * if some other I/O error occurs.  
  28.      * @exception  NullPointerException  if <code>b</code> is <code>null </code>.  
  29.      */  
  30.     public int read(byte b[]) throws IOException {  
  31.         return read(b, 0, b.length);  

请注意加粗斜体字部分的API说明,当对Socket的输入流进行读取操作的时候,它会一直阻塞下去,直到发生如下三种事件。

有数据可读;

可用数据已经读取完毕;

发生空指针或者I/O异常。

这意味着当对方发送请求或者应答消息比较缓慢、或者网络传输较慢时,读取输入流一方的通信线程将被长时间阻塞,如果对方要60s才能够将数据发送完成,读取一方的I/O线程也将会被同步阻塞60s,在此期间,其他接入消息只能在消息队列中排队。

下面我们接着对输出流进行分析,还是看JDK I/O类库输出流的API文档,然后结合文档说明进行故障分析。

代码清单2-7  Java输入流OutputStream
 

  1. public void write(byte b[]) throws IOException  
  2. *Writes an array of bytes. This method will block until the bytes are *actually written.  
  3. Parameters:  
  4. b - the data to be written  
  5. Throws: IOException  
  6. If an I/O error has occurred. 

当调用OutputStream的write方法写输出流的时候,它将会被阻塞,直到所有要发送的字节全部写入完毕,或者发生异常。学习过TCP/IP相关知识的人都知道,当消息的接收方处理缓慢的时候,将不能及时地从TCP缓冲区读取数据,这将会导致发送方的TCP window size不断减小,直到为0,双方处于Keep-Alive状态,消息发送方将不能再向TCP缓冲区写入消息,这时如果采用的是同步阻塞I/O,write操作将会被无限期阻塞,直到TCP window size大于0或者发生I/O异常。

通过对输入和输出流的API文档进行分析,我们了解到读和写操作都是同步阻塞的,阻塞的时间取决于对方I/O线程的处理速度和网络I/O的传输速度。本质上来讲,我们无法保证生产环境的网络状况和对端的应用程序能足够快,如果我们的应用程序依赖对方的处理速度,它的可靠性就非常差。也许在实验室进行的性能测试结果令人满意,但是一旦上线运行,面对恶劣的网络环境和良莠不齐的第三方系统,问题就会如火山一样喷发。

伪异步I/O实际上仅仅只是对之前I/O线程模型的一个简单优化,它无法从根本上解决同步I/O导致的通信线程阻塞问题。下面我们就简单分析下如果通信对方返回应答时间过长,会引起的级联故障。

(1)服务端处理缓慢,返回应答消息耗费60s,平时只需要10ms。

(2)采用伪异步I/O的线程正在读取故障服务节点的响应,由于读取输入流是阻塞的,因此,它将会被同步阻塞60s。

(3)假如所有的可用线程都被故障服务器阻塞,那后续所有的I/O消息都将在队列中排队。

(4)由于线程池采用阻塞队列实现,当队列积满之后,后续入队列的操作将被阻塞。

(5)由于前端只有一个Accptor线程接收客户端接入,它被阻塞在线程池的同步阻塞队列之后,新的客户端请求消息将被拒绝,客户端会发生大量的连接超时。

(6)由于几乎所有的连接都超时,调用者会认为系统已经崩溃,无法接收新的请求消息。

如何破解这个难题?下节的NIO将给出答案。
 

喜欢的朋友可以添加我们的微信账号:

51CTO读书频道二维码


51CTO读书频道活动讨论群:342347198

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.2.2 伪异步式I/O创建的TimeServ.. 下一篇2.3 NIO编程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言指针从入门到基 (2025-12-26 05:21:36)
·【C语言指针初阶】C (2025-12-26 05:21:33)
·C语言指针的定义和使 (2025-12-26 05:21:31)
·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)