创建元数据内存镜像,包括类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 ));
}
}