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

Hadoop源码分析之一(RPC机制之Server)

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

下面主要结合实际代码来介绍一下Hadoop RPC Server端整个的处理过程:(整个核心逻辑都在org.apache.hadoop.ipc.Server这个类中实现)

首先在Server的start方法中启用相关的线程,start方法会在相关的Server中调用,如NameNode, DataNode等

  public synchronized void start() {
    responder.start();
    listener.start();
    handlers = new Handler[handlerCount];
   
    for (int i = 0; i < handlerCount; i++) {
      handlers[i] = new Handler(i);
      handlers[i].start();
    }
  }

如上可以看到启动了listener,responer和handler threand pool,可以通过ipc.server.handler.queue.size来配置handler的个数,默认为100个。
在Server中有一个Listener的内部类,Listener的创造函数中会启动相应的Reader Thread Pool,可以通过ipc.server.read.threadpool.size来配置Reader的线程数,默认为1个。Listener会监听OP_ACCEPT事件,并在doAccpet中创建一个Connection传递给Reader,如下:

while ((channel = server.accept()) != null) {
        channel.configureBlocking(false);
        channel.socket().setTcpNoDelay(tcpNoDelay);
        //get one reader instance for reader pool
        Reader reader = getReader();
        try {
      //lock this reader
          reader.startAdd();
          SelectionKey readKey = reader.registerChannel(channel);
          //build Connection instance
          c = new Connection(readKey, channel, Time.now());
          readKey.attach(c);
          synchronized (connectionList) {
            connectionList.add(numConnections, c);
            numConnections++;
          }   
        } finally {
          //unlock this reader
          reader.finishAdd();
        }
      }
    }

在Reader中主要通用Connection来读取Requeset并封装成Call,读取Request分为2种处理,一种为权限读取,另一种为普通读取,具体的实现可以查看hadoop源码,这里不粘出。但是这里会介绍一下整个request在整个传送过程中的结构,request由head和conent组成。

Head的组成如下:
* +----------------------------------+
* | "hrpc" 4 bytes |
* +----------------------------------+
* | Version (1 bytes) |
* +----------------------------------+
* | Authmethod (1 byte) |
* +----------------------------------+
* | IpcSerializationType (1 byte) |
* +----------------------------------+
conent主要有4 bytes的是date length和具体的data byte array组成。
Handler怎么处理具体的Call会在后续具体介绍,这里不做具体展开。
Responder主要处理Call产生的Response并通过NIO传输给具体的client, 使用的socket channel与request为同一个,Responder会做相应的check,如下:
 
    //check select key channel is same as call.connection.channel
    if (key.channel() != call.connection.channel) {
        throw new IOException("doAsyncWrite: bad channel");
    }
 
    // Extract the first call
    call = responseQueue.removeFirst();
    SocketChannel channel = call.connection.channel;
    // Using call channel to send as much data as we can in the non-blocking fashion
    int numBytes = channelWrite(channel, call.rpcResponse);
 
整个Hadoop RPC Server处理的机制就介绍到这里,后续会介绍call怎么处理到具体的处理及Client如何向Server发送信息。
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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