在Hadoop中fsimage保存最新的检查点信息,edits保存自最新检查点后的命名空间的变化。在分析hdfs namenode–format的源代码时,已经明确了该过程根据配置文件的信息创建fsimage和edits文件,这篇文章具体分析一下创建fsimage和edits文件的源代码。在NameNode的format方法中,有如下的代码:
FSImage fsImage = new FSImage(conf, nameDirsToFormat, editDirsToFormat);
try {
FSNamesystem fsn = new FSNamesystem(conf, fsImage);
fsImage.getEditLog().initJournalsForWrite();
if (!fsImage.confirmFormat(force, isInteractive)) {
return true; // aborted
}
fsImage.format(fsn, clusterId);
} catch (IOException ioe) {
LOG.warn("Encountered exception during format: ", ioe);
fsImage.close();
throw ioe;
}
在这段代码中主要涉及到了三个类,分别为FSImage、FSNamesystem和FSEditLog,其中FSImage负责检查点,FSEditLog维护命名空间变动的日志,FSNamesystem为DataNode执行实际的簿记工作。创建fsImage对象的源代码为
/**
* Construct the FSImage. Set the default checkpoint directories.
*
* Setup storage and initialize the edit log.
*
* @param conf Configuration
* @param imageDirs Directories the image can be stored in.
* @param editsDirs Directories the editlog can be stored in.
* @throws IOException if directories are invalid.
*/
protected FSImage(Configuration conf,Collection<URI> imageDirs,
List<URI> editsDirs)throws IOException {
this.conf = conf;
/* NNStorage负责管理NameNode使用的StorageDirectories*/
storage = new NNStorage(conf, imageDirs, editsDirs);
/*根据dfs.namenode.name.dir.restore的值决定是否尝试重新存储失败的存储目录
* 默认值为false
*/
if(conf.getBoolean(DFSConfigKeys.DFS_NAMENODE_NAME_DIR_RESTORE_KEY,
DFSConfigKeys.DFS_NAMENODE_NAME_DIR_RESTORE_DEFAULT)) {
storage.setRestoreFailedStorage(true);
}
this.editLog = new FSEditLog (conf, storage, editsDirs);
/*NNStorageRetentionManager负责检查NameNode的存储目录,
*并在fsimage和edits文件上执行保留策略。
*/
archivalManager = new NNStorageRetentionManager(conf, storage, editLog);
}
FSImage的构造方法中,创建了NNStorage、FSEditLog和NNStorageRetentionManager对象,NNStorage的构造方法源代码如下:
public NNStorage(Configuration conf, Collection<URI> imageDirs, Collection<URI> editsDirs) throws IOException {
super(NodeType.NAME_NODE);
storageDirs = new CopyOnWriteArrayList<StorageDirectory>();
// this may modify the editsDirs, so copy before passing in
setStorageDirectories(imageDirs,
Lists.newArrayList(editsDirs),
FSNamesystem.getSharedEditsDirs(conf));
}
NNStorage中的setStorageDirectories方法用于初始化存储fsimage和edits文件的目录。这里不分析该方法的全部源代码,主要分析初始化fsimage的部分,如下:
// 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("file") == 0) {
this.addStorageDir(new StorageDirectory(new File(dirName.getPath()),
dirType,
sharedEditsDirs.contains(dirName))); // Don't lock the dir if it's shared.
}
}
Ubuntu 13.04上搭建Hadoop环境