您好,登錄后才能下訂單哦!
本篇文章為大家展示了HDFS中addBlock函數(shù)的作用是什么,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
public LocatedBlock addBlock(String src, String clientMachine) throws IOException {
int retries = 5;//設(shè)定重試次數(shù)為5
Object results[] = namesystem.getAdditionalBlock(new UTF8(src), new UTF8(clientMachine));
//獲取新的一個(gè)Block和DataNode節(jié)點(diǎn)信息
while (results != null && results[0] == null && retries > 0) {
try {//發(fā)生錯(cuò)誤,且不超過(guò)錯(cuò)誤次數(shù),就重新獲取
Thread.sleep(100);
} catch (InterruptedException ie) {
}
results = namesystem.getAdditionalBlock(new UTF8(src), new UTF8(clientMachine));
retries--;
}
//無(wú)論如何,都結(jié)束了!
if (results == null) {//沒(méi)找到?返回錯(cuò)誤
throw new IOException("Cannot obtain additional block for file " + src);
} else if (results[0] == null) {
return null;//沒(méi)有Block?返回null
} else {
Block b = (Block) results[0];
DatanodeInfo targets[] = (DatanodeInfo[]) results[1];
return new LocatedBlock(b, targets);//這個(gè)簡(jiǎn)單,返回Block和DatanodeInfo
}
}
=====================
C1:OKAY c2:OKAY c3:OKAY
=====================
下面開(kāi)始講解
public synchronized Object[] getAdditionalBlock(UTF8 src, UTF8 clientMachine) {
---
public synchronized Object[] getAdditionalBlock(UTF8 src, UTF8 clientMachine) {
Object results[] = null;
if (dir.getFile(src) == null && pendingCreates.get(src) != null) {
//文件系統(tǒng)dir中沒(méi)有此文件且此文件正在創(chuàng)建過(guò)程中
results = new Object[2];//創(chuàng)建返回的變量數(shù)組
//
// If we fail this, bad things happen!
//這里的checkFileProgress很關(guān)鍵,
//后面會(huì)分析checkFileProgress的作用
if (checkFileProgress(src)) {
// Get the array of replication targets //這個(gè)仍然很簡(jiǎn)單,之前分析過(guò)了
DatanodeInfo targets[] = chooseTargets(this.desiredReplication, null, clientMachine);
if (targets.length < this.minReplication) {
return null;
}
// Create next block返回不多說(shuō)!
results[0] = allocateBlock(src);
results[1] = targets;
}
}
return results;
}
===
接下來(lái)是函數(shù)
synchronized boolean checkFileProgress(UTF8 src) {
這個(gè)函數(shù)還是蠻重要的!
/**
* Check that the indicated file's blocks are present and
* replicated. If not, return false.
*/
synchronized boolean checkFileProgress(UTF8 src) {
Vector v = (Vector) pendingCreates.get(src);//獲取文件名src對(duì)應(yīng)的每個(gè)塊
for (Iterator it = v.iterator(); it.hasNext(); ) {
Block b = (Block) it.next();//對(duì)于當(dāng)前塊來(lái)說(shuō)
TreeSet containingNodes = (TreeSet) blocksMap.get(b);//獲取這個(gè)塊對(duì)應(yīng)的datanode節(jié)點(diǎn)信息
if (containingNodes == null || containingNodes.size() < this.minReplication) {
return false;//如果此Block對(duì)應(yīng)的datanode個(gè)數(shù)不足最小備份數(shù),則禁止返回Block
}
}
return true;//順利返回true.
}
上述內(nèi)容就是HDFS中addBlock函數(shù)的作用是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(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)容。