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操作。
本文轉自:http://blog.csdn.net/kntao/article/details/7769199
本文轉自:http://blog.csdn.net/kntao/article/details/7769199