Java nio的一个严重BUG

 

这个BUG会在linux上导致cpu 100%,使得nio server/client不可用,具体的详情可以看这里http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933 。令人失望的是这个BUG直到jdk 6u4才解决,sun的拖沓让人难以相信。这个BUGserver端容易出现,因为server端有频繁地接入断开连接。 使用jdk 6u4之前版本的nio框架都有这个隐患,除非你的框架很好地处理了这个可能的隐患。Grizzly的处理方式比较简单,也就是BUG报告里面提到的方式,在SelectionKey.cancel()之后马上进行了一次select调用将fdpoll(epoll)中移除:

 

Java代码 复制代码  收藏代码
  1. this.selectionKey.cancel();   
  2. try {   
  3.             // cancel key,then select now to remove file descriptor   
  4.             this.selector.selectNow();   
  5.  } catch (IOException e) {   
  6.          onException(e);   
  7.         log.error("Selector selectNow fail", e);   
  8. }  
this.selectionKey.cancel();
try {
            // cancel key,then select now to remove file descriptor
            this.selector.selectNow();
 } catch (IOException e) {
         onException(e);
        log.error("Selector selectNow fail", e);
}
 

 

 

实际上这样的解决方式还是留有隐患的,因为key的取消和这个selectNow操作很可能跟Selector.select操作并发地在进行,在两个操作之间仍然留有一个极小的时间窗口可能发生这个BUG。因此,你需要更安全地方式处理这个问题,jetty的处理方式是这样,连续的 select(timeout)操作没有阻塞并返回0,并且次数超过了一个指定阀值,那么就遍历整个key set,将key仍然有效并且interestOps等于0的所有key主动取消掉;如果在这次修正后,仍然继续出现select(timeout)不阻塞并且返回0的情况,那么就重新创建一个新的Selector,并将Old Selector的有效channel和对应的key转移到新的Selector上。

 

 

Java代码 复制代码  收藏代码
  1. long before=now;   
  2.                     int selected=selector.select(wait);   
  3.                     now = System.currentTimeMillis();   
  4.                     _idleTimeout.setNow(now);   
  5.                     _timeout.setNow(now);   
  6.   
  7.                     // Look for JVM bugs   
  8.                     // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933   
  9.                     if (__JVMBUG_THRESHHOLD>0 && selected==0 && wait>__JVMBUG_THRESHHOLD && (now-before)<(wait/2) )   
  10.                     {   
  11.                         _jvmBug++;   
  12.                         if (_jvmBug>=(__JVMBUG_THRESHHOLD2))   
  13.                         {   
  14.                             synchronized (this)   
  15.                             {   
  16.                                 _lastJVMBug=now;// BLOODY SUN BUG !!!  Try refreshing the entire selector.   
  17.                                 final Selector new_selector = Selector.open();   
  18.                                 for (SelectionKey k: selector.keys())   
  19.                                 {   
  20.                                     if (!k.isValid() || k.interestOps()==0)   
  21.                                         continue;   
  22.                                        
  23.                                     final SelectableChannel channel = k.channel();   
  24.                                     final Object attachment = k.attachment();   
  25.                                        
  26.                                     if (attachment==null)   
  27.                                         addChange(channel);   
  28.                                     else  
  29.                                         addChange(channel,attachment);   
  30.                                 }   
  31.                                 _selector.close();   
  32.                                 _selector=new_selector;   
  33.                                 _jvmBug=0;   
  34.                                 return;   
  35.                             }   
  36.                         }   
  37.                         else if (_jvmBug==__JVMBUG_THRESHHOLD || _jvmBug==__JVMBUG_THRESHHOLD1)   
  38.                         {   
  39.                             // Cancel keys with 0 interested ops   
  40.                             for (SelectionKey k: selector.keys())   
  41.                             {   
  42.                                 if (k.isValid()&&k.interestOps()==0)   
  43.                                 {   
  44.                                     k.cancel();   
  45.                                 }   
  46.                             }   
  47.                             return;   
  48.                         }   
  49.                     }   
  50.                     else  
  51.                         _jvmBug=0;  
long before=now;
                    int selected=selector.select(wait);
                    now = System.currentTimeMillis();
                    _idleTimeout.setNow(now);
                    _timeout.setNow(now);

                    // Look for JVM bugs
                    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6403933
                    if (__JVMBUG_THRESHHOLD>0 && selected==0 && wait>__JVMBUG_THRESHHOLD && (now-before)<(wait/2) )
                    {
                        _jvmBug++;
                        if (_jvmBug>=(__JVMBUG_THRESHHOLD2))
                        {
                            synchronized (this)
                            {
                                _lastJVMBug=now;// BLOODY SUN BUG !!!  Try refreshing the entire selector.
                                final Selector new_selector = Selector.open();
                                for (SelectionKey k: selector.keys())
                                {
                                    if (!k.isValid() || k.interestOps()==0)
                                        continue;
                                    
                                    final SelectableChannel channel = k.channel();
                                    final Object attachment = k.attachment();
                                    
                                    if (attachment==null)
                                        addChange(channel);
                                    else
                                        addChange(channel,attachment);
                                }
                                _selector.close();
                                _selector=new_selector;
                                _jvmBug=0;
                                return;
                            }
                        }
                        else if (_jvmBug==__JVMBUG_THRESHHOLD || _jvmBug==__JVMBUG_THRESHHOLD1)
                        {
                            // Cancel keys with 0 interested ops
                            for (SelectionKey k: selector.keys())
                            {
                                if (k.isValid()&&k.interestOps()==0)
                                {
                                    k.cancel();
                                }
                            }
                            return;
                        }
                    }
                    else
                        _jvmBug=0;
 

 

 

这个方案能比较好的在jdk 6u4之前的版本上解决这个BUG可能导致的问题。MinaNetty没有看到有处理这个BUG的代码,如果我看错了,请留言告诉我。Yanf4j一直采用的是grizzly的方式,准备加上jetty的处理方案。当然,最简单的方案就是升级你的JDK。

 

俺实际上被这个BUG害的很惨, 因为降到了jdk 6u4 CPU仍然会时不时的增高, 原因是低版本的Jetty同样在这方面处理的不是很合理. Jetty关于这个BUG的描述http://jira.codehaus.org/browse/JETTY-937,用JETTY做为应用, JETTY1.6.22 + JDK 6U4俺现在测下来, CPU基本正常.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值