Hadoop NameNode启动之FSImage、edits重置(三)

本文详细解析Hadoop中Namenode启动过程中的FSImage与edits日志重置操作,包括关闭旧日志文件、重命名目录、保存新的镜像文件及创建新的edits日志等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   Namenode启动过程中,在FSImage载入后,会对FSImage和edits重置,相当于创建一组新的镜像文件和日志文件,但前提是前面的载入都已经成功了,这个操作紧跟fsImage.recoverTransitionRead之后,具体调用流程如下:

Namenode.main ->NameNode.createNameNode -> NameNode.initialize -> FSNameSystem.initialize-> FSDirecotry.loadFSImage -> fsImage.saveNamespace

这个函数内部流程很清晰,会经历如下步骤:

       1、关闭edits日志文件

       2、将当前current目录重命名为lastcheckpoint.tmp

       3、保存新的image到current目录

       4、在current目录中创建edits

       5、将第二步中lastcheckpoint.tmp重命名为previous.checkpoint

       6、打开fsiamge和edits文件

void  saveNamespace(boolean renewCheckpointTime) throws IOException {
    //关闭edits日志文件
    editLog.close();
    if(renewCheckpointTime)
      this.checkpointTime = FSNamesystem.now();
 
   //将当前current目录重命名为lastcheckpoint.tmp
   for(Iterator<StorageDirectory> it = dirIterator(); it.hasNext();) {
      StorageDirectory sd = it.next();
      try {
        //开始重命名操作,实际调用File.renameTo函数
        moveCurrent(sd);
      } catch(IOException ie) {
        LOG.error("Unable to move current for " + sd.getRoot(), ie);
        removeStorageDir(sd.getRoot());
      }
    }
 
    // save images into current
    for (Iterator<StorageDirectory> it =dirIterator(NameNodeDirType.IMAGE);it.hasNext();){
      StorageDirectory sd = it.next();
      try {
       //在这个函数中会创建current目录,保存新的fsimage、创建新的edits,
       //注意此时edits是初始化状态,只写入了一个LAYOUT_VERSION
       //创建VERSION fstime文件
        saveCurrent(sd);
      } catch(IOException ie) {
        LOG.error("Unable to save image for " + sd.getRoot(), ie);
        removeStorageDir(sd.getRoot());
      }
    }
 
    // -NOTE-
    // If NN has image-only and edits-onlystorage directories and
    // fails here
    // the image will have the latest namespace state.
    // During startup the image-only directories will recoverby discarding
    // lastcheckpoint.tmp, while
    // the edits-only directories will recover by fallingback
    // to the old state contained in theirlastcheckpoint.tmp.
    // The edits directories should be discarded duringstartup because their
    // checkpointTime is older than that of imagedirectories.
 
    // recreate edits in current
   for (Iterator<StorageDirectory> it =dirIterator(NameNodeDirType.EDITS);it.hasNext();) {
      StorageDirectory sd = it.next();
      try {
        saveCurrent(sd);
      } catch(IOException ie) {
        LOG.error("Unable to save edits for " + sd.getRoot(), ie);
        removeStorageDir(sd.getRoot());
      }
  }
   //将第二步中lastcheckpoint.tmp重命名为previous.checkpoint
 
   for(Iterator<StorageDirectory> it = dirIterator(); it.hasNext();) {
      StorageDirectory sd = it.next();
      try {
        moveLastCheckpoint(sd);
      } catch(IOException ie) {
        LOG.error("Unable to move last checkpoint for" + sd.getRoot(),ie);
        removeStorageDir(sd.getRoot());
      }
    }
    if(!editLog.isOpen()) editLog.open();
    ckptState = CheckpointStates.UPLOAD_DONE;
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值