netty学习(四)----伪异步IO的弊端


    要对伪异步IO的弊端进行深入的分析,首先我们看两个JAVA同步I/O的API说明:

    /**
     * Reads some number of bytes from the input stream and stores them into
     * the buffer array <code>b</code>. The number of bytes actually read is
     * returned as an integer.  <span style="color:#FF0000;">This method blocks until input data is
     * available, end of file is detected, or an exception is thrown.</span>
     *
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
     * <code>0</code> is returned; otherwise, there is an attempt to read at
     * least one byte. If no byte is available because the stream is at the
     * end of the file, the value <code>-1</code> is returned; otherwise, at
     * least one byte is read and stored into <code>b</code>.
     *
     * <p> The first byte read is stored into element <code>b[0]</code>, the
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
     * number of bytes actually read; these bytes will be stored in elements
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
     * <code>b[b.length-1]</code> unaffected.
     *
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
     *
     * @param      b   the buffer into which the data is read.
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the stream has been reached.
     * @exception  IOException  If the first byte cannot be read for any reason
     * other than the end of the file, if the input stream has been closed, or
     * if some other I/O error occurs.
     * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
     * @see        java.io.InputStream#read(byte[], int, int)
     */
    public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

注意红色字体部分(第四行代码与第五行代码<span>标签的中间部分)的API说明,当对Socket的输入流进行读取操作的时候,它会一直阻塞下去,一直发生下面三种事件。

(1)有数据可读

(2)可用数据已经读取完毕

(3)发生空指针和IO异常

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


以上是对输入流做的分析,现在咱们对输出流做一下分析,首先看一下JDK IO类库输出流的API文档,然后结合文档说明进行故障分析

   /**
     * Writes <code>b.length</code> bytes from the specified byte array
     * to this output stream. The general contract for <code>write(b)</code>
     * is that it should have exactly the same effect as the call
     * <code>write(b, 0, b.length)</code>.
     *<span style="color:#FF0000;">Writes an array of bytes.This method will block until the bytes are *actually written</span>
     * @param      b   the data.
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.OutputStream#write(byte[], int, int)
     */
    public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }

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

    通过对输入输出流的api文档进行分析,我们了解到了读和写操作都是同步阻塞的,阻塞的时间取决于对方I/O线程的处理速度和网络I/O的传输速度!!!!

     如果通信双方返回应答时间过长会引起的级联故障有:

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

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

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

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

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

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

分析完毕~~~~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值