HBase1.0.0的RPC機制分析與源碼解讀(一),hbase1.0.0rpc

來源:互聯網
上載者:User

HBase1.0.0的RPC機制分析與源碼解讀(一),hbase1.0.0rpc
HBase的RPC機制,除了使用protocal buf的工具之外都是利用java的原生API進行構造,RPC機制的解讀包括用戶端和伺服器端兩個部分,本文主要就伺服器端的服務運行機制以及工作流程進行簡要分析.
首先我們來分析一下一個典型的RPC伺服器短的處理流程,如所示:

可以清楚的看到當用戶端向伺服器端發送一個請求的時候,最開始是被RPCServer中的Listener所監聽到的,如下面的代碼所示,HBase的RpcServer啟動的時候會啟動幾個處理線程:

    responder.start();    listener.start();    scheduler.start();
其中,Responder線程負責資料的request的回複工作,listener負責監聽用戶端的請求,scheduler負責具體call的調度工作
      while (running) {        SelectionKey key = null;        try {          selector.select(); // FindBugs IS2_INCONSISTENT_SYNC          Iterator<SelectionKey> iter = selector.selectedKeys().iterator();          while (iter.hasNext()) {            key = iter.next();            iter.remove();            try {              if (key.isValid()) {                if (key.isAcceptable())                  doAccept(key);              }            } catch (IOException ignored) {              if (LOG.isTraceEnabled()) LOG.trace("ignored", ignored);            }            key = null;          }        }
listener的實現過程中運用了java nio的一些特性,主要是每個Listener線程又會管理這一個Reader的線程池,這些Reader具體負責從Socket Channel中讀取資料,並解析資料中的相關項目,進而構造出可啟動並執行CallRunner:
      Call call = new Call(id, this.service, md, header, param, cellScanner, this, responder,              totalRequestSize,              traceInfo);      scheduler.dispatch(new CallRunner(RpcServer.this, call, userProvider));
RpcScheduler調用對應的RpcExcutor進行相應的處理,RpcExcutor中啟動了多個處理線程,這些線程從隊列中取出任務並且執行,
  protected void startHandlers(final String nameSuffix, final int numHandlers,      final List<BlockingQueue<CallRunner>> callQueues,      final int qindex, final int qsize, final int port) {    final String threadPrefix = name + Strings.nullToEmpty(nameSuffix);    for (int i = 0; i < numHandlers; i++) {      final int index = qindex + (i % qsize);      Thread t = new Thread(new Runnable() {        @Override        public void run() {          consumerLoop(callQueues.get(index));        }      });      t.setDaemon(true);      t.setName(threadPrefix + "RpcServer.handler=" + handlers.size() +        ",queue=" + index + ",port=" + port);      t.start();      LOG.debug(threadPrefix + " Start Handler index=" + handlers.size() + " queue=" + index);      handlers.add(t);    }  }
  protected void consumerLoop(final BlockingQueue<CallRunner> myQueue) {    boolean interrupted = false;    double handlerFailureThreshhold =        conf == null ? 1.0 : conf.getDouble(HConstants.REGION_SERVER_HANDLER_ABORT_ON_ERROR_PERCENT,          HConstants.DEFAULT_REGION_SERVER_HANDLER_ABORT_ON_ERROR_PERCENT);    try {      while (running) {        try {          CallRunner task = myQueue.take();          try {            activeHandlerCount.incrementAndGet();            task.run();

其實繼續跟進代碼就會發現CallRunner的run方法最主要動作有兩個一個是擷取操作結果一個是返回操作結果給客戶:
       resultPair = this.rpcServer.call(call.service, call.md, call.param, call.cellScanner,          call.timestamp, this.status);
  if (!call.isDelayed() || !call.isReturnValueDelayed()) {        Message param = resultPair != null ? resultPair.getFirst() : null;        CellScanner cells = resultPair != null ? resultPair.getSecond() : null;        call.setResponse(param, cells, errorThrowable, error);      }      call.sendResponseIfReady();
到此結束就簡要介紹了一個完整的服務端RPC處理流程,該流程中涉及到的相關的類的關係如所示:







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.