hadoop2.4源碼分析

本篇內(nèi)容介紹了“hadoop2.4源碼分析”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到尉犁網(wǎng)站設(shè)計(jì)與尉犁網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋尉犁地區(qū)。

ZKFailoverController是整個(gè)HA的協(xié)調(diào)者。下面我們將分析幾個(gè)實(shí)際的問(wèn)題。

1.怎么協(xié)調(diào)選舉的?怎么選舉出來(lái)active的?

2.active宕機(jī)后,做了什么事情,如何切換的?

下面,我們來(lái)分析第一個(gè)問(wèn)題 怎么協(xié)調(diào)選舉的?怎么選舉出來(lái)active的?

 

hadoop2.4源碼分析

步驟1:參看NameNode源碼,可以看出,對(duì)于使用HA的NN來(lái)說(shuō),進(jìn)入Standby是必須的。 升級(jí)除外

protected HAState createHAState(StartupOption startOpt) {
    if (!haEnabled || startOpt == StartupOption.UPGRADE) {
      return ACTIVE_STATE;
    } else {
      return STANDBY_STATE; //standby狀態(tài)
    }
  }

步驟2:此時(shí)的HealthMonitor監(jiān)控NN,發(fā)現(xiàn)是HEALTH的狀態(tài),會(huì)執(zhí)行:

if (healthy) {
     //設(shè)置狀態(tài),用于通知回調(diào)函數(shù)
        enterState(State.SERVICE_HEALTHY);
      }

enterState會(huì)通知回調(diào)函數(shù),進(jìn)行處理。對(duì)于HEALTH狀態(tài)的開(kāi)始執(zhí)行選舉方法。

elector.joinElection(targetToData(localTarget));

通過(guò)創(chuàng)建零時(shí)節(jié)點(diǎn),來(lái)?yè)屨脊?jié)點(diǎn),獲取Active

createLockNodeAsync();

對(duì)于創(chuàng)建節(jié)點(diǎn),會(huì)觸發(fā)ZK的EVENT時(shí)間。

對(duì)于事件的處理,見(jiàn)源碼部分:

public synchronized void processResult(int rc, String path, Object ctx,
      String name) {
    if (isStaleClient(ctx)) return;
    LOG.debug("CreateNode result: " + rc + " for path: " + path
        + " connectionState: " + zkConnectionState +
        " for " + this);
    Code code = Code.get(rc);//為了方便使用,這里自定義了一組狀態(tài)
    if (isSuccess(code)) {//成功返回,成功創(chuàng)建zklocakpath節(jié)點(diǎn)
      // we successfully created the znode. we are the leader. start monitoring
      if (becomeActive()) {//要將本節(jié)點(diǎn)上的NN變成active
        monitorActiveStatus();//繼續(xù)監(jiān)控節(jié)點(diǎn)狀態(tài)
      } else {
        reJoinElectionAfterFailureToBecomeActive();//失敗,繼續(xù)選舉嘗試
      }
      return;
    }
    if (isNodeExists(code)) {//節(jié)點(diǎn)存在,說(shuō)明已經(jīng)有active,wait即可
      if (createRetryCount == 0) {
        // znode exists and we did not retry the operation. so a different
        // instance has created it. become standby and monitor lock.
        becomeStandby();
      }
      // if we had retried then the znode could have been created by our first
      // attempt to the server (that we lost) and this node exists response is
      // for the second attempt. verify this case via ephemeral node owner. this
      // will happen on the callback for monitoring the lock.
      monitorActiveStatus();//不過(guò)努力成為active的動(dòng)作不能停
      return;
    }
    String errorMessage = "Received create error from Zookeeper. code:"
        + code.toString() + " for path " + path;
    LOG.debug(errorMessage);
    if (shouldRetry(code)) {
      if (createRetryCount < maxRetryNum) {
        LOG.debug("Retrying createNode createRetryCount: " + createRetryCount);
        ++createRetryCount;
        createLockNodeAsync();
        return;
      }
      errorMessage = errorMessage
          + ". Not retrying further znode create connection errors.";
    } else if (isSessionExpired(code)) {
      // This isn't fatal - the client Watcher will re-join the election
      LOG.warn("Lock acquisition failed because session was lost");
      return;
    }
    fatalError(errorMessage);
  }

對(duì)于獲取Active的機(jī)器,調(diào)用becomeActive()方法

private synchronized void becomeActive() throws ServiceFailedException {
    LOG.info("Trying to make " + localTarget + " active...");
    try {
      HAServiceProtocolHelper.transitionToActive(localTarget.getProxy(
          conf, FailoverController.getRpcTimeoutToNewActive(conf)),
          createReqInfo());
      String msg = "Successfully transitioned " + localTarget +
          " to active state";
      LOG.info(msg);
      serviceState = HAServiceState.ACTIVE;
      recordActiveAttempt(new ActiveAttemptRecord(true, msg));
    } catch (Throwable t) {
      String msg = "Couldn't make " + localTarget + " active";
      LOG.fatal(msg, t);
     
      recordActiveAttempt(new ActiveAttemptRecord(false, msg + "\n" +
          StringUtils.stringifyException(t)));
      if (t instanceof ServiceFailedException) {
        throw (ServiceFailedException)t;
      } else {
        throw new ServiceFailedException("Couldn't transition to active",
            t);
      }

通過(guò)對(duì)RPC進(jìn)過(guò)一系列的調(diào)用,最終執(zhí)行NameNode的

synchronized void transitionToActive()
      throws ServiceFailedException, AccessControlException {
    namesystem.checkSuperuserPrivilege();
    if (!haEnabled) {
      throw new ServiceFailedException("HA for namenode is not enabled");
    }
    state.setState(haContext, ACTIVE_STATE);
  }

OVER


2.active宕機(jī)后,做了什么事情,如何切換的?

active宕機(jī)后或者異常會(huì)導(dǎo)致ZK節(jié)點(diǎn)的消失或監(jiān)控狀態(tài)的UNHEALTH,這些都會(huì)導(dǎo)致新一輪的選舉,原理同上。

“hadoop2.4源碼分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章名稱(chēng):hadoop2.4源碼分析
網(wǎng)頁(yè)鏈接:http://muchs.cn/article30/ghjipo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、定制開(kāi)發(fā)、網(wǎng)站導(dǎo)航、網(wǎng)站營(yíng)銷(xiāo)微信公眾號(hào)網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)