本篇內(nèi)容介紹了“hadoop2.4源碼分析”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
ZKFailoverController是整個(gè)HA的協(xié)調(diào)者。下面我們將分析幾個(gè)實(shí)際的問題。
1.怎么協(xié)調(diào)選舉的?怎么選舉出來active的?
2.active宕機(jī)后,做了什么事情,如何切換的?
下面,我們來分析第一個(gè)問題 怎么協(xié)調(diào)選舉的?怎么選舉出來active的?
步驟1:參看NameNode源碼,可以看出,對(duì)于使用HA的NN來說,進(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)的開始執(zhí)行選舉方法。
elector.joinElection(targetToData(localTarget));
通過創(chuàng)建零時(shí)節(jié)點(diǎn),來搶占節(jié)點(diǎn),獲取Active
createLockNodeAsync();
對(duì)于創(chuàng)建節(jié)點(diǎn),會(huì)觸發(fā)ZK的EVENT時(shí)間。
對(duì)于事件的處理,見源碼部分:
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的動(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); }
通過對(duì)RPC進(jìn)過一系列的調(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)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。