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

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

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

在该段代码中fsNameDirs和fsEditsDirs分别为参数dfs.namenode.name.dir和dfs.namenode.edits.dir设置的目录,默认情况下二者指向相同的目录。如果二者的值相同那么fsimage的类型就为NameNodeDirType.IMAGE_AND_EDITS,否则为NameNodeDirType.IMAGE。需要注意的是,到目前为止虽然根据配置文件创建了表示相应目录的File对象,但还没有在本地文件系统上创建实际的目录。现在回到FSIamge的构造方法中,在创建了NNStorage对象后,接下来创建FSEditLog对象,该类的构造方法为:

/**
  * Constructor for FSEditLog. Underlying journals are constructed, but
  * no streams are opened until open() is called.
  *
  * @param conf The namenode configuration
  * @param storage Storage object used by namenode
  * @param editsDirs List of journals to use
  */
  FSEditLog(Configuration conf, NNStorage storage, List<URI> editsDirs) {
    isSyncRunning = false;
    this.conf = conf;
this.storage = storage;
//在format阶段,metrics的值为null
    metrics = NameNode.getNameNodeMetrics();
    lastPrintTime = now();
    // If this list is empty, an error will be thrown on first use
    // of the editlog, as no journals will exist
    this.editsDirs = Lists.newArrayList(editsDirs);
    this.sharedEditsDirs = FSNamesystem.getSharedEditsDirs(conf);
  }

在创建完了FSEditLog和NNStorageRetentionManager对象后,FSIamge的构造方法执行完毕,回到NameNode的format方法中。接下来就是要根据FSImage对象创建FSNamesystem,前面曾提到FSNamesystem为DataNode执行实际的簿记工作,实际执行的该类的构造方法如下,其中ignoreRetryCache的值为false:

/**
  * Create an FSNamesystem associated with the specified image.
  *
  * Note that this does not load any data off of disk -- if you would
  * like that behavior, use {@link #loadFromDisk(Configuration)}
  *
  * @param conf configuration
  * @param fsImage The FSImage to associate with
  * @param ignoreRetryCache Whether or not should ignore the retry cache setup
  *                        step. For Secondary NN this should be set to true.
  * @throws IOException on bad configuration
  */
  FSNamesystem(Configuration conf, FSImage fsImage, boolean ignoreRetryCache)
      throws IOException

由于该构造方法代码比较多,就不贴出来了。可以概括为在该构造方法中,实例化了诸如BlockManager、DatanodeStatistics、FSDirectory、CacheManager等对象,读取了配置文件中的一些信息(后面计划专门研究该类)。在创建完毕FSNamesystem对象后,执行的是FSEditLog的initJournalsForWrite()方法,该方法的代码如下:

private State state = State.UNINITIALIZED;
public synchronized void initJournalsForWrite() {
    Preconditions.checkState(state == State.UNINITIALIZED ||
        state == State.CLOSED, "Unexpected state: %s", state);
    initJournals(this.editsDirs);
    state = State.BETWEEN_LOG_SEGMENTS;
  }

在该方法中先检查日志的状态,FSEditLog刚创建时的状态为UNINITIALIZED,执行完initJournals()方法后日志的状态为BETWEEN_LOG_SEGMENTS,意味日志还未打开。日志的初始化具体由initJournals()完成,该方法的代码如下:

private synchronized void initJournals(List<URI> dirs) {
    int minimumRedundantJournals = conf.getInt(
        DFSConfigKeys.DFS_NAMENODE_EDITS_DIR_MINIMUM_KEY,
        DFSConfigKeys.DFS_NAMENODE_EDITS_DIR_MINIMUM_DEFAULT);
//管理日志集合的对象
    journalSet = new JournalSet(minimumRedundantJournals);

    for (URI u : dirs) {
      boolean required = FSNamesystem.getRequiredNamespaceEditsDirs(conf)
          .contains(u);
      if (u.getScheme().equals(NNStorage.LOCAL_URI_SCHEME)) {
        StorageDirectory sd = storage.getStorageDirectory(u);
        if (sd != null) {
          journalSet.add(new FileJournalManager(conf, sd, storage),
              required, sharedEditsDirs.contains(u));
        }
      } else {
        journalSet.add(createJournal(u), required,
            sharedEditsDirs.contains(u));
      }
    }
 
    if (journalSet.isEmpty()) {
      LOG.error("No edits directories configured!");
    }
  }

该方法根据传递的目录集合创建FileJournalManager对象,并添加到JournalSet对象中,其中一个FileJournalManager对象管理一个保存edits文件的目录。初始化edits文件后,接下来就是对fsimage进行格式化了,具体代码为:

void format(FSNamesystem fsn, String clusterId) throws IOException {
    long fileCount = fsn.getTotalFiles();
    // Expect 1 file, which is the root inode
    Preconditions.checkState(fileCount == 1,
        "FSImage.format should be called with an uninitialized namesystem, has " +
        fileCount + " files");
    NamespaceInfo ns = NNStorage.newNamespaceInfo();
    LOG.info("Allocated new BlockPoolId: " + ns.getBlockPoolID());
    ns.clusterID = clusterId;
   
    storage.format(ns);
    editLog.formatNonFileJournals(ns);
    saveFSImageInAllDirs(fsn, 0);
  }

在该代码中实际执行创建fsimage文件的代码为最后一行的saveFSImageInAllDirs(fsn,0),该方法将实际的工作委托给下面的方法:

saveFSImageInAllDirs(source, NameNodeFile.IMAGE, txid, null);

在该方法中会根据保存fsimage的目录的个数创建与之相等的线程,这些线程完成实际的创建fsimage的任务。最后会清理旧的edits和检查点文件。

通过上面的分析可以发现,在执行hdfs namenode –format时仅创建了fsimage文件,并没有创建edits文件,但已经创建了相关对象。这一点也可以在执行完format后在本地文件系统中确认,目录中只有fsimage文件。

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/107139.htm

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

       

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