手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop异步rpc通信机制--org.apache.hadoop.ipc.Server

[日期:2013-02-27] 来源:Linux社区  作者:ckl_soft [字体:大 中 小]

Responder线程任务是将响应队列中的响应写回客户端。
在run内定期doPurge,根据Call.timestamp判断该请求是否长时间未响应,如果是,关闭掉对应的连接。

/**
    * 调用异步写doAsyncWrite写回响应消息,并周期性doPurge清理不响应的连接
    */
    public void run() {
      LOG.info(getName() + ": starting");
      SERVER.set(Server.this);//保存server
      long lastPurgeTime = 0;  // last check for old calls.

      while (running) {
        try {
          waitPending();    // If a channel is being registered, wait.
          writeSelector.select(PURGE_INTERVAL);//选择一组键,其相应的通道已为 I/O 操作准备就绪。 最多阻塞 timeout 毫秒
          Iterator<SelectionKey> iter = writeSelector.selectedKeys().iterator();
          while (iter.hasNext()) {
            SelectionKey key = iter.next();
            iter.remove();
            try {
              if (key.isValid() && key.isWritable()) {//测试此键的通道是否已准备好进行写入。
                  doAsyncWrite(key);//调用异步写
              }
            } catch (IOException e) {
              LOG.info(getName() + ": doAsyncWrite threw exception " + e);
            }
          }
          long now = System.currentTimeMillis();
          if (now < lastPurgeTime + PURGE_INTERVAL) {
            continue;
          }
          lastPurgeTime = now;
          //
          // If there were some calls that have not been sent out for a
          // long time, discard them.
          //
          LOG.debug("Checking for old call responses.");
          ArrayList<Call> calls;
         
          // get the list of channels from list of keys.
          synchronized (writeSelector.keys()) {//找出正常的call构成calls
            calls = new ArrayList<Call>(writeSelector.keys().size());
            iter = writeSelector.keys().iterator();
            while (iter.hasNext()) {
              SelectionKey key = iter.next();
              Call call = (Call)key.attachment();
              if (call != null && key.channel() == call.connection.channel) {
                calls.add(call);
              }
            }
          }
          //关掉正常calls中长久不响应的连接
          for(Call call : calls) {
            try {
              doPurge(call, now);
            } catch (IOException e) {
              LOG.warn("Error in purging old calls " + e);
            }
          }
        } catch (OutOfMemoryError e) {
          //
          // we can run out of memory if we have too many threads
          // log the event and sleep for a minute and give
          // some thread(s) a chance to finish
          //
          LOG.warn("Out of Memory in server select", e);
          try { Thread.sleep(60000); } catch (Exception ie) {}
        } catch (Exception e) {
          LOG.warn("Exception in Responder " +
                  StringUtils.stringifyException(e));
        }
      }
      LOG.info("Stopping " + this.getName());
    }

 

/**调用processResponse进行异步写响应消息
    * */
    private void doAsyncWrite(SelectionKey key) throws IOException {
      Call call = (Call)key.attachment();//获取当前的附加对象call。在processResponse内将Call作为附件注册到写通道
      if (call == null) {//只有当Handler线程有注册写事件并将call附加到通道上,call才不为null
        return;
      }
      //根据得到的附加对象call,将call所在的connection的responseQueue上的response写回给客户端
      if (key.channel() != call.connection.channel) {
        throw new IOException("doAsyncWrite: bad channel");
      }
      synchronized(call.connection.responseQueue) {
        if (processResponse(call.connection.responseQueue, false)) {// 调用processResponse处理与调用关联的响应数据 
          try {
            key.interestOps(0);//将此键的 interest 集合设置为给定值0。
          } catch (CancelledKeyException e) {
            /* The Listener/reader might have closed the socket.
            * We don't explicitly cancel the key, so not sure if this will
            * ever fire.
            * This warning could be removed.
            */
            LOG.warn("Exception while changing ops : " + e);
          }
        }
      }
    }

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款