您好,登錄后才能下訂單哦!
這篇文章主要介紹了HDFS2.X中NameNode塊報(bào)告處理的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
NameNode會(huì)接收兩種情況的塊報(bào)告,DataNode全部塊報(bào)告與增量塊報(bào)告。
目前全量報(bào)告以周期性進(jìn)行報(bào)告,既然已經(jīng)有啟動(dòng)時(shí)候的全量數(shù)據(jù)塊報(bào)告,錯(cuò)誤塊報(bào)告,增量塊報(bào)告(包括刪除塊報(bào)告),為什么還需要周期性全量塊報(bào)告呢?比如某DataNode接受到數(shù)據(jù)塊但是增量報(bào)告失敗,那就需要周期性報(bào)告來解決了,或者NameNode給DN發(fā)送了刪除塊的命令,但是由于網(wǎng)絡(luò)等異常,DN沒收收到刪除命令,這樣DN再把這些數(shù)據(jù)塊報(bào)告上來就是無效塊,需要再次放入無效隊(duì)列,下次心跳再命令DN刪除;同時(shí)比如每次塊報(bào)告會(huì)清理DatanodeDescriptor對(duì)象維護(hù)的塊列表還有某個(gè)塊的信息,但是DN節(jié)點(diǎn)再也沒有報(bào)告上來,定時(shí)清除這些無效信息,有助于提高塊列表的操作性能,從而提供NameNode的性能。同時(shí)我們可以考慮分析是否還有其他原因可能影響NameNode的性能。
為了提高HDFS啟動(dòng)速度,在Hadoop2.0版本中全量塊報(bào)告分為了兩種:啟動(dòng)時(shí)候塊報(bào)告與非啟動(dòng)的時(shí)候塊報(bào)告,即是否是第一次塊報(bào)告。那么具體又是如何來提高啟動(dòng)速度的呢?在啟動(dòng)的時(shí)候,不計(jì)算哪些文件元數(shù)據(jù)需要?jiǎng)h除,不計(jì)算無效快,這些處理都推遲到下一次塊報(bào)告進(jìn)行處理
對(duì)于第一次塊報(bào)告,代碼調(diào)用流程為:NameNodeRpcServer.blockReport()->BlockManager. processReport()->BlockManager.processFirstBlockReport().對(duì)Standby節(jié)點(diǎn),如果報(bào)告的數(shù)據(jù)塊所相關(guān)元數(shù)據(jù)日志從節(jié)點(diǎn)還沒有加載完畢,則會(huì)將報(bào)告的塊信息加入一個(gè)隊(duì)列,當(dāng)Standby節(jié)點(diǎn)加載元數(shù)據(jù)后,再處理該消息隊(duì)列,第一次塊報(bào)告處理詳細(xì)代碼如下,可以看到,為了提高報(bào)告速度,只有簡單的幾步進(jìn)行塊報(bào)告處理,僅有驗(yàn)證塊是否損壞,然后直接判斷塊狀態(tài)是否為FINALIZED狀態(tài),如果是,就直接建立塊與DN節(jié)點(diǎn)的映射。
[java] view plain copy
private void processFirstBlockReport(final DatanodeDescriptor node,
final BlockListAsLongs report) throws IOException {
if (report == null) return;
assert (namesystem.hasWriteLock());
assert (node.numBlocks() == 0);
BlockReportIterator itBR = report.getBlockReportIterator();
while(itBR.hasNext()) {
Block iblk = itBR.next();
ReplicaState reportedState = itBR.getCurrentReplicaState();
//對(duì)于從節(jié)點(diǎn)shouldPostponeBlocksFromFuture為true;判斷塊時(shí)間戳//是否大于目前時(shí)間
if (shouldPostponeBlocksFromFuture&&
namesystem.isGenStampInFuture(iblk.getGenerationStamp())) {
//將塊信息加入隊(duì)列,從節(jié)點(diǎn)消化完相關(guān)日志,會(huì)處理該隊(duì)列
queueReportedBlock(node, iblk, reportedState,
QUEUE_REASON_FUTURE_GENSTAMP);
continue;
}
BlockInfo storedBlock = blocksMap.getStoredBlock(iblk);
// If block does not belong to any file, we are done.
if (storedBlock == null) continue;
// If block is corrupt, mark it and continue to next block.
BlockUCState ucState = storedBlock.getBlockUCState();
BlockToMarkCorrupt c = checkReplicaCorrupt(
iblk, reportedState, storedBlock, ucState, node);
if (c != null) {
//對(duì)于從節(jié)點(diǎn),先將塊信息加入pendingDNMessages隊(duì)列
//將塊信息加入隊(duì)列,從節(jié)點(diǎn)消化完相關(guān)日志,會(huì)處理該隊(duì)列,如果該塊還是被損壞,就真的是損壞了
if (shouldPostponeBlocksFromFuture) {
// In the Standby, we may receive a block report for a file that we
// just have an out-of-date gen-stamp or state for, for example.
queueReportedBlock(node, iblk, reportedState,
QUEUE_REASON_CORRUPT_STATE);
} else {
//對(duì)于主節(jié)點(diǎn),有塊損壞,直接標(biāo)記為損壞
markBlockAsCorrupt(c, node);
}
continue;
}
// If block is under construction, add this replica to its list
if (isBlockUnderConstruction(storedBlock, ucState, reportedState)) {
((BlockInfoUnderConstruction)storedBlock).addReplicaIfNotPresent(
node, iblk, reportedState);
//and fall through to next clause
}
//add replica if appropriate
if (reportedState == ReplicaState.FINALIZED) {
addStoredBlockImmediate(storedBlock, node);
}
}
}
而對(duì)于非第一次塊報(bào)告,情況就要復(fù)雜一些了,對(duì)于報(bào)告的每個(gè)塊信息,不僅會(huì)建立塊與DN的映射,而且均會(huì)檢查塊是否損壞,塊是是否無效,元數(shù)據(jù)是否已經(jīng)無效應(yīng)該刪除,是否為UC狀態(tài)的塊等,該過程主要由方法processReport來完成
[java] view plain copy
private void processReport(final DatanodeDescriptor node,
final BlockListAsLongs report) throws IOException {
// Normal case:
// Modify the (block-->datanode) map, according to the difference
// between the old and new block report.
//
Collection<BlockInfo> toAdd = new LinkedList<BlockInfo>();
Collection<Block> toRemove = new LinkedList<Block>();
Collection<Block> toInvalidate = new LinkedList<Block>();
Collection<BlockToMarkCorrupt> toCorrupt = new LinkedList<BlockToMarkCorrupt>();
Collection<StatefulBlockInfo> toUC = new LinkedList<StatefulBlockInfo>();
//統(tǒng)計(jì)塊,并且判斷塊是否應(yīng)該刪除,是否應(yīng)該添加到blocksMap列表等
reportDiff(node, report, toAdd, toRemove, toInvalidate, toCorrupt, toUC);
// Process the blocks on each queue
for (StatefulBlockInfo b : toUC) {
addStoredBlockUnderConstruction(b.storedBlock, node, b.reportedState);
}
for (Block b : toRemove) {
removeStoredBlock(b, node);
}
for (BlockInfo b : toAdd) {
addStoredBlock(b, node, null, true);
}
for (Block b : toInvalidate) {
NameNode.stateChangeLog.info("BLOCK* processReport: block "
+ b + " on " + node + " size " + b.getNumBytes()
+ " does not belong to any file.");
addToInvalidates(b, node);
}
for (BlockToMarkCorrupt b : toCorrupt) {
markBlockAsCorrupt(b, node);
}
}
在reportDiff方法內(nèi),實(shí)現(xiàn)如下:
[java] view plain copy
private void reportDiff(DatanodeDescriptor dn,
BlockListAsLongs newReport,
Collection<BlockInfo> toAdd, // add to DatanodeDescriptor
Collection<Block> toRemove, // remove from DatanodeDescriptor
Collection<Block> toInvalidate, // should be removed from DN
Collection<BlockToMarkCorrupt> toCorrupt, // add to corrupt replicas list
Collection<StatefulBlockInfo> toUC) { // add to under-construction list
// place a delimiter分隔符 in the list which separates blocks
// that have been reported from those that have not
BlockInfo delimiter = new BlockInfo(new Block(), 1);
boolean added = dn.addBlock(delimiter);
assert added : "Delimiting block cannot be present in the node";
int headIndex = 0; //currently the delimiter is in the head of the list
int curIndex;
if (newReport == null)
newReport = new BlockListAsLongs();
// scan the report and process newly reported blocks
BlockReportIterator itBR = newReport.getBlockReportIterator();
while(itBR.hasNext()) {
Block iblk = itBR.next();
ReplicaState iState = itBR.getCurrentReplicaState();
BlockInfo storedBlock = processReportedBlock(dn, iblk, iState,
toAdd, toInvalidate, toCorrupt, toUC);
// move block to the head of the list
if (storedBlock != null && (curIndex = storedBlock.findDatanode(dn)) >= 0) {
headIndex = dn.moveBlockToHead(storedBlock, curIndex, headIndex);
}
}
// collect blocks that have not been reported
// all of them are next to the delimiter
//收集DN對(duì)象中所有沒有被DN節(jié)點(diǎn)報(bào)告上來的塊,將這些塊信息從DN對(duì)象維護(hù)的列表中刪除,這樣可以有效控制DN塊列表中存在大量的無效塊,
//影響NameNode的操作性能
Iterator<? extends Block> it = new DatanodeDescriptor.BlockIterator(
delimiter.getNext(0), dn);
while(it.hasNext())
toRemove.add(it.next());
dn.removeBlock(delimiter);
}
相比于全量塊報(bào)告方式,增量報(bào)告報(bào)告DN節(jié)點(diǎn)很短時(shí)間內(nèi)已經(jīng)接收完成,或者正在接受或者刪除的塊,而且為了提高文件上傳的效率, DN節(jié)點(diǎn)應(yīng)該盡快將接受到的塊報(bào)告給NameNode,現(xiàn)在引入了RECEIVING_BLOCK這個(gè)一個(gè)塊狀態(tài),有可能就是為了提高寫入速度。
增量塊報(bào)告流程圖
正在接收的塊與已經(jīng)接收完的塊,除了在數(shù)據(jù)塊狀態(tài)不一樣外,其他基本相同,其接收塊代碼調(diào)用流程如下:NameNodeRpcServer.blockReceivedAndDeleted()->BlockManager.processIncrementalBlockReport()->BlockManager. addBlock()->BlockManager.processAndHandleReportedBlock()->BlockManager.processReportedBlock(),在方法processReportedBlock中,首先判斷報(bào)告的塊是否元數(shù)據(jù)已經(jīng)從主節(jié)點(diǎn)讀取到,如果沒有加入消息列表
[java] view plain copy
//postpone延期
//如果是從節(jié)點(diǎn),可能雖然DN節(jié)點(diǎn)將塊信息報(bào)告上來,但是元數(shù)據(jù)還沒有從日志中消化到
if (shouldPostponeBlocksFromFuture &&
namesystem.isGenStampInFuture(block.getGenerationStamp())) {
queueReportedBlock(dn, block, reportedState,
QUEUE_REASON_FUTURE_GENSTAMP);
return null;
}
<br font-size:16px;white-space:normal;background-color:#FFFFFF;" />然后從blocksMap中查詢到數(shù)據(jù)塊對(duì)于文件inode,判斷文件是否存在;如果判斷塊屬于損害塊,冗余分?jǐn)?shù)是否不夠等情況,如果塊一切正常,且狀態(tài)為完成,將將其加入blocksMap等集合列表。具體代碼如下:
[java] view plain copy
//檢查塊是否已經(jīng)被損害
BlockToMarkCorrupt c = checkReplicaCorrupt(
block, reportedState, storedBlock, ucState, dn);
if (c != null) {
if (shouldPostponeBlocksFromFuture) {
// If the block is an out-of-date generation stamp or state,
// but we're the standby, we shouldn't treat it as corrupt,
// but instead just queue it for later processing.
queueReportedBlock(dn, storedBlock, reportedState,
QUEUE_REASON_CORRUPT_STATE);
} else {
//將其加入損害列表
toCorrupt.add(c);
}
return storedBlock;
}
//如果該數(shù)據(jù)塊正在被構(gòu)建,加入構(gòu)建列表
if (isBlockUnderConstruction(storedBlock, ucState, reportedState)) {
toUC.add(new StatefulBlockInfo(
(BlockInfoUnderConstruction)storedBlock, reportedState));
return storedBlock;
}
//add replica if appropriate
//如果報(bào)告的塊狀態(tài)為FINALIZED且該DN沒有報(bào)告該塊,則加入添加隊(duì)列
if (reportedState == ReplicaState.FINALIZED
&& storedBlock.findDatanode(dn) < 0) {
toAdd.add(storedBlock);
}
return storedBlock;
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“HDFS2.X中NameNode塊報(bào)告處理的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。