溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

hadoop 2.4 namenode源碼分析

發(fā)布時間:2021-12-10 09:36:55 來源:億速云 閱讀:151 作者:iii 欄目:云計算

這篇文章主要介紹“hadoop 2.4 namenode源碼分析”,在日常操作中,相信很多人在hadoop 2.4 namenode源碼分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”hadoop 2.4 namenode源碼分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

在hadoop nn的HA,對于主備節(jié)點(diǎn)的選舉,是通過ActiveStandbyElector來實(shí)現(xiàn)的。源碼上有針對該類的解釋。

hadoop 2.4 namenode源碼分析

小弟英文不才,翻譯一下。該類主要使用了zookeeper實(shí)現(xiàn)了主節(jié)點(diǎn)的選舉,對于成功選舉的主節(jié)點(diǎn),會在zookeeper上創(chuàng)建零時節(jié)點(diǎn)。如果創(chuàng)建成功,NN會變成active,而其余nn節(jié)點(diǎn)會成備用節(jié)點(diǎn)。


下面還是來具體分析一下ActiveStandbyElector類的作用,ActiveStandbyElector主要實(shí)現(xiàn)了選舉,選舉流程主要是通過創(chuàng)建零時節(jié)點(diǎn)的方式實(shí)現(xiàn),如果創(chuàng)建成功??梢哉J(rèn)為是獲取到對應(yīng)的LOCK,該節(jié)點(diǎn)可以成為active。如果沒有成功創(chuàng)建該節(jié)點(diǎn),可以認(rèn)為為standby節(jié)點(diǎn),對于standby節(jié)點(diǎn),需要一直監(jiān)聽該LOCK節(jié)點(diǎn)的狀態(tài)。如果發(fā)生節(jié)點(diǎn)的事件,就去嘗試選舉。基本流程就是這樣。

下面,來看一下ActiveStandbyElector類的主要方法和流程。對于熟悉zookeeper的同學(xué)來說,zookeeper的必須要實(shí)現(xiàn)watcher接口,其中可以實(shí)現(xiàn)自己的各種事件的處理邏輯。

在ActiveStandbyElector中,采用了 hadoop 2.4 namenode源碼分析

內(nèi)部類來實(shí)現(xiàn)Watcher接口,其process方法,調(diào)用了processWatchEvent來實(shí)現(xiàn)具體的業(yè)務(wù)處理。

hadoop 2.4 namenode源碼分析


下面來分析該processWatchEvent的具體邏輯:

 //處理zk的事件

synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) {
    Event.EventType eventType = event.getType();
    if (isStaleClient(zk)) return;
    LOG.debug("Watcher event type: " + eventType + " with state:"
        + event.getState() + " for path:" + event.getPath()
        + " connectionState: " + zkConnectionState
        + " for " + this);

    if (eventType == Event.EventType.None) {
    	//會話本身的時間,如連接。失去連接。
      // the connection state has changed
      switch (event.getState()) {
      case SyncConnected:
        LOG.info("Session connected.");
        // if the listener was asked to move to safe state then it needs to
        // be undone
        ConnectionState prevConnectionState = zkConnectionState;
        zkConnectionState = ConnectionState.CONNECTED;
        if (prevConnectionState == ConnectionState.DISCONNECTED &&
            wantToBeInElection) {
          monitorActiveStatus();//監(jiān)控節(jié)點(diǎn)
        }
        break;
      case Disconnected:
        LOG.info("Session disconnected. Entering neutral mode...");

        // ask the app to move to safe state because zookeeper connection
        // is not active and we dont know our state
        zkConnectionState = ConnectionState.DISCONNECTED;
        enterNeutralMode();
        break;
      case Expired:
        // the connection got terminated because of session timeout
        // call listener to reconnect
        LOG.info("Session expired. Entering neutral mode and rejoining...");
        enterNeutralMode();
        reJoinElection(0);//參與選舉
        break;
      case SaslAuthenticated:
        LOG.info("Successfully authenticated to ZooKeeper using SASL.");
        break;
      default:
        fatalError("Unexpected Zookeeper watch event state: "
            + event.getState());
        break;
      }

      return;
    }
    
    // a watch on lock path in zookeeper has fired. so something has changed on
    // the lock. ideally we should check that the path is the same as the lock
    // path but trusting zookeeper for now
    //節(jié)點(diǎn)事件
    String path = event.getPath();
    if (path != null) {
      switch (eventType) {
      case NodeDeleted:
        if (state == State.ACTIVE) {
          enterNeutralMode();//該方法目前未實(shí)現(xiàn)
        }
        joinElectionInternal();//開始選舉
        break;
      case NodeDataChanged:
        monitorActiveStatus();//繼續(xù)監(jiān)控該節(jié)點(diǎn),嘗試成為active
        break;
      default:
        LOG.debug("Unexpected node event: " + eventType + " for path: " + path);
        monitorActiveStatus();
      }

      return;
    }

    // some unexpected error has occurred
    fatalError("Unexpected watch error from Zookeeper");
  }




而joinElectionInternal,選舉的核心方法就是, hadoop 2.4 namenode源碼分析


選舉就是通過對zkLokFilePath節(jié)點(diǎn)的創(chuàng)建,來完成。這個采用了zk的異步回調(diào)。 hadoop 2.4 namenode源碼分析

從該類的定義,可以看出,本身就是實(shí)現(xiàn)了zk的兩個接口。

StatCallback需要實(shí)現(xiàn)的方法,如下: hadoop 2.4 namenode源碼分析

hadoop 2.4 namenode源碼分析


對于兩個方法的實(shí)現(xiàn),ActiveStandbyElector內(nèi)部實(shí)現(xiàn)幾乎是一樣的。這里不再貼上源碼,有興趣的可以自己去看源碼。

貼上實(shí)現(xià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)存在,說明已經(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();//不過努力成為active的動作不能停
      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);
  }

對于becomeStandby,becomeActive這些狀態(tài)的改變,有ZKFailoverController來實(shí)現(xiàn)。

到此,關(guān)于“hadoop 2.4 namenode源碼分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI