你好,游客 登录 注册 搜索
背景:
阅读新闻

Hadoop-2.4.1学习之创建fsimage和edits源码分析

[日期:2014-09-24] 来源:Linux社区  作者:skywalker_only [字体:大 中 小]

在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环境 http://www.linuxidc.com/Linux/2013-06/86106.htm

Ubuntu 12.10 +Hadoop 1.2.1版本集群配置 http://www.linuxidc.com/Linux/2013-09/90600.htm

Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) http://www.linuxidc.com/Linux/2013-01/77681.htm

Ubuntu下Hadoop环境的配置 http://www.linuxidc.com/Linux/2012-11/74539.htm

单机版搭建Hadoop环境图文教程详解 http://www.linuxidc.com/Linux/2012-02/53927.htm

搭建Hadoop环境(在Winodws环境下用虚拟机虚拟两个Ubuntu系统进行搭建) http://www.linuxidc.com/Linux/2011-12/48894.htm

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-09/107139p2.htm

linux
相关资讯       Hadoop 2.4.1 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款