1、中繼資料(Metadata):維護HDFS檔案系統中檔案和目錄的資訊,分為記憶體中繼資料和中繼資料檔案兩種。NameNode維護整個中繼資料。HDFS實現時,沒有採用定期匯出中繼資料的方法,而是採用中繼資料鏡像檔案(FSImage)+日子檔案(edits)的備份機制。2、Block:檔案內容而言。尋路徑流程: 路徑資訊 bocks[] triplets[] Client ------------》INode---------------------》BlockInfo --------------------------》DataNode。INode:檔案的基本元素:檔案和目錄BlockInfo: 檔案內容對象DatanodeDescriptor:具體儲存物件。3 、 FSImage和edits的checkPoint。FSImage有2個狀態,分別是FsImage和FsImage.ckpt,後者表示正在checkpoint的過程中,上傳後將會修改為FSImage檔案,同理edits也有兩個狀態,edits和edits.new。4、NameNode format情景分析:
- 遍曆中繼資料存放區目錄,提示使用者是否格式化?(NameNode.java裡format函數)
private static boolean format( Configuration conf , boolean isConfirmationNeeded ) throws IOException { Collection<URI > dirsToFormat = FSNamesystem. getNamespaceDirs(conf ); Collection<URI > editDirsToFormat = FSNamesystem .getNamespaceEditsDirs (conf ); for( Iterator< URI> it = dirsToFormat.iterator (); it. hasNext() { File curDir = new File (it .next (). getPath()) ; if (! curDir. exists()) continue; if (isConfirmationNeeded ) { System .err .print ("Re-format filesystem in " + curDir + " ? (Y or N) "); if (! (System .in .read () == 'Y')) { System .err .println ("Format aborted in " + curDir ); return true ; } while(System .in .read () != '\n') ; // discard the enter-key } } FSNamesystem nsys = new FSNamesystem (new FSImage(dirsToFormat , editDirsToFormat ), conf) ; nsys.dir.fsImage .format (); return false; }
- 建立中繼資料記憶體鏡像,包括類FSNamesystem執行個體化對象,類FSDirectory執行個體化對象,類FSImage對象,類Edits對象。建立FsNameSystem對象主要完成:BlockManager,FSDirectory對象以及初始化成員變數。FSImage對象主要完成對layoutVersion、namespaceID,CTime賦值為0,執行個體化FSEditLog。在類FSDirectory,建立了HDFS根目錄節點rootDir。
FSNamesystem( FSImage fsImage, Configuration conf ) throws IOException { this. blockManager = new BlockManager (this, conf) ; setConfigurationParameters (conf ); this. dir = new FSDirectory(fsImage , this, conf ); dtSecretManager = createDelegationTokenSecretManager (conf ); } FSImage( Collection< URI> fsDirs , Collection< URI> fsEditsDirs ) throws IOException { this() ; setStorageDirectories( fsDirs, fsEditsDirs ); } void setStorageDirectories(Collection <URI > fsNameDirs, Collection< URI> fsEditsDirs ) throws IOException { this. storageDirs = new ArrayList <StorageDirectory >() ; this. removedStorageDirs = new ArrayList <StorageDirectory >() ; // Add all name dirs with appropriate NameNodeDirType for (URI dirName : fsNameDirs ) { checkSchemeConsistency (dirName ); boolean isAlsoEdits = false; for (URI editsDirName : fsEditsDirs) { if (editsDirName .compareTo (dirName ) == 0) { isAlsoEdits = true; fsEditsDirs .remove (editsDirName ); break; } } NameNodeDirType dirType = (isAlsoEdits ) ? NameNodeDirType .IMAGE_AND_EDITS : NameNodeDirType .IMAGE ; // Add to the list of storage directories, only if the // URI is of type file:// if(dirName .getScheme (). compareTo( JournalType.FILE .name (). toLowerCase()) == 0){ this.addStorageDir (new StorageDirectory(new File(dirName. getPath()) , dirType )); } } // Add edits dirs if they are different from name dirs for (URI dirName : fsEditsDirs ) { checkSchemeConsistency (dirName ); // Add to the list of storage directories, only if the // URI is of type file:// if(dirName .getScheme (). compareTo( JournalType.FILE .name (). toLowerCase()) == 0) this.addStorageDir (new StorageDirectory(new File(dirName. getPath()) , NameNodeDirType .EDITS )); } }
- 對記憶體鏡像資料中的資料結構進行初始化:主要有FSImage的format函數完成,layoutVersion:軟體所處的版本。namespaceID:在Format時候產生,當data node註冊到Name Node後,會獲得該NameNode的NameSpaceID,並作為後續與NameNode通訊的身份標識。對於未知身份的Data Node,NameNode拒絕通訊。CTime:表示FSimage產生的時間。checkpointTime:表示NameSpace第一次checkpoint的時間。
public void format () throws IOException { this. layoutVersion = FSConstants .LAYOUT_VERSION ; this. namespaceID = newNamespaceID (); this. cTime = 0L ; this. checkpointTime = FSNamesystem .now (); for (Iterator <StorageDirectory > it = dirIterator (); it. hasNext() { StorageDirectory sd = it .next (); format (sd ); } }
- 對記憶體鏡像寫入中繼資料備份目錄。FSImage的format方法會遍曆所有的目錄進行備份。如果是FSImage的檔案目錄,則調用saveFSImage儲存FSImage,如果是Edits,則調用editLog.createEditLogFile,最後調用sd.write方法建立fstime和VERSION檔案。VERSION檔案通常最後寫入。
void format(StorageDirectory sd ) throws IOException { sd.clearDirectory (); // create currrent dir sd.lock (); try { saveCurrent (sd ); } finally { sd .unlock (); } LOG.info ("Storage directory " + sd. getRoot() + " has been successfully formatted."); }
最後分析一下中繼資料應用的情境:1、格式化時。2、Hadoop啟動時。3、中繼資料更新操作時。4、如果NameNode與Secondary NameNode、Backup Node或checkpoint Node配合使用時,會進行checkPoint操作。