设为首页 收藏本站
查看: 877|回复: 0

【框架解析】Hadoop系统分析(五)--namenode其他

[复制链接]

尚未签到

发表于 2015-11-11 13:37:44 | 显示全部楼层 |阅读模式
  upgrade/rollback/importCheckpoint


  在FsImage.recoverTransitionRead方法中,针对upgrade/rollback/importCheckpoint参数,在启动前做了特殊的操作,代码如下:
  

switch(startOpt) {
case UPGRADE:
doUpgrade();
return false; // upgrade saved image already
case IMPORT:
doImportCheckpoint();
return true;
case ROLLBACK:
doRollback();
break;
case REGULAR:
// just load the image
}
  


  • hadoop namenode -upgrade

    进行hadoop namenode的升级操作。调用FsImage.doUpgrade方法,主要操作是将current保存为previous,升级后的fsimage保存为current。

    if(getDistributedUpgradeState()) {
    // only distributed upgrade need to continue
    // don't do version upgrade
    this.loadFSImage();
    initializeDistributedUpgrade();
    return;
    }
    // Upgrade is allowed only if there are
    // no previous fs states in any of the directories
    for (Iterator<StorageDirectory> it =
    dirIterator(); it.hasNext();) {
    StorageDirectory sd = it.next();
    //如果已经有previous目录,则无法重新upgrade
    if (sd.getPreviousDir().exists())
    throw new InconsistentFSStateException(sd.getRoot(),
    &quot;previous fs state should not exist during upgrade. &quot;
    + &quot;Finalize or rollback first.&quot;);
    }
    // 把硬盘上最近的fsimage加载进内存
    this.loadFSImage();
    // Do upgrade for each directory
    long oldCTime = this.getCTime();
    this.cTime = FSNamesystem.now();  // generate new cTime for the state
    int oldLV = this.getLayoutVersion();
    this.layoutVersion = FSConstants.LAYOUT_VERSION;
    this.checkpointTime = FSNamesystem.now();
    for (Iterator<StorageDirectory> it =
    dirIterator(); it.hasNext();) {
    StorageDirectory sd = it.next();
    LOG.info(&quot;Upgrading image directory &quot; + sd.getRoot()
    + &quot;.\n   old LV = &quot; + oldLV
    + &quot;; old CTime = &quot; + oldCTime
    + &quot;.\n   new LV = &quot; + this.getLayoutVersion()
    + &quot;; new CTime = &quot; + this.getCTime());
    File curDir = sd.getCurrentDir();
    File prevDir = sd.getPreviousDir();
    File tmpDir = sd.getPreviousTmp();
    assert curDir.exists() : &quot;Current directory must exist.&quot;;
    assert !prevDir.exists() : &quot;prvious directory must not exist.&quot;;
    assert !tmpDir.exists() : &quot;prvious.tmp directory must not exist.&quot;;
    // 把current目录改为previous.tmp目录
    rename(curDir, tmpDir);
    // 把内存中的fsimage保存到current中
    saveCurrent(sd);
    // 把previous.tmp目录修改为previous目录
    rename(tmpDir, prevDir);
    //标记升级没有完成(升级成功需要进行finalize,升级失败需要进行rollback来完成升级)
    isUpgradeFinalized = false;
    LOG.info(&quot;Upgrade of &quot; + sd.getRoot() + &quot; is complete.&quot;);
    }
    //初始化升级后操作,主要是计算version写VERSION文件
    initializeDistributedUpgrade();
    //开启editlog
    editLog.open();
  • hadoop namenode -rollback

    升级hadoop失败后回滚,主要是把upgrade时变更的数据目录恢复原状,将current修改为removed.tmp,previous修改回current,最后删除removed.tmp

    // Rollback is allowed only if there is
    // a previous fs states in at least one of the storage directories.
    // Directories that don't have previous state do not rollback
    boolean canRollback = false;
    FSImage prevState = new FSImage();
    prevState.layoutVersion = FSConstants.LAYOUT_VERSION;
    for (Iterator<StorageDirectory> it =
    dirIterator(); it.hasNext();) {
    //将previous目录里的版本信息加载进内存,如果没有previous目录,则加载current目录
    StorageDirectory sd = it.next();
    File prevDir = sd.getPreviousDir();
    if (!prevDir.exists()) {  // use current directory then
    LOG.info(&quot;Storage directory &quot; + sd.getRoot()
    + &quot; does not contain previous fs state.&quot;);
    sd.read(); // read and verify consistency with other directories
    continue;
    }
    StorageDirectory sdPrev = prevState.new StorageDirectory(sd.getRoot());
    sdPrev.read(sdPrev.getPreviousVersionFile());  // read and verify consistency of the prev dir
    canRollback = true;
    }
    if (!canRollback)
    throw new IOException(&quot;Cannot rollback. &quot;
    + &quot;None of the storage directories contain previous fs state.&quot;);
    // Now that we know all directories are going to be consistent
    // Do rollback for each directory containing previous state
    for (Iterator<StorageDirectory> it =
    dirIterator(); it.hasNext();) {
    StorageDirectory sd = it.next();
    File prevDir = sd.getPreviousDir();
    if (!prevDir.exists())
    continue;
    LOG.info(&quot;Rolling back storage directory &quot; + sd.getRoot()
    + &quot;.\n   new LV = &quot; + prevState.getLayoutVersion()
    + &quot;; new CTime = &quot; + prevState.getCTime());
    File tmpDir = sd.getRemovedTmp();
    assert !tmpDir.exists() : &quot;removed.tmp directory must not exist.&quot;;
    // rename current to tmp
    File curDir = sd.getCurrentDir();
    assert curDir.exists() : &quot;Current directory must exist.&quot;;
    //将current目录更改为removed.tmp
    rename(curDir, tmpDir);
    // rename previous to current
    //将previous更改为current
    rename(prevDir, curDir);
    // delete tmp dir
    //删除removed.tmp
    deleteDir(tmpDir);
    LOG.info(&quot;Rollback of &quot; + sd.getRoot()+ &quot; is complete.&quot;);
    }
    isUpgradeFinalized = true;
    // check whether name-node can start in regular mode
    verifyDistributedUpgradeProgress(StartupOption.REGULAR);
  • hadoop namenode -importCheckpoint

    使用备份的checkpoint启动namenode,之后将加载进来的ckp保存为current

    FSImage ckptImage = new FSImage();
    FSNamesystem fsNamesys = FSNamesystem.getFSNamesystem();
    // replace real image with the checkpoint image
    FSImage realImage = fsNamesys.getFSImage();
    assert realImage == this;
    fsNamesys.dir.fsImage = ckptImage;
    // load from the checkpoint dirs
    try {
    //读取checkpointDirs目录与checkpointEditsDirs目录,合并后放入内存
    //checkpointDirs值为fs.checkpoint.dir配置项,如果没有则为null,secondarynamenode上默认值为/tmp/hadoop/dfs/namesecondary
    //checkpointEditsDirs值为fs.checkpoint.edits.dir配置项,如果没有则为null,secondarynamenode上默认值为/tmp/hadoop/dfs/namesecondary
    ckptImage.recoverTransitionRead(checkpointDirs, checkpointEditsDirs,
    StartupOption.REGULAR);
    } finally {
    ckptImage.close();
    }
    // return back the real image
    realImage.setStorageInfo(ckptImage);
    fsNamesys.dir.fsImage = realImage;
    // and save it 保存fsimage
    saveNamespace(false);

hadoop namenode -finalize

升级完成后,使用hadoop namenode -finalize来进行确认,finalize之后就不能rollback了

回到NameNode.createNameNode方法中,进入FINALIZE分支

case FINALIZE:
aborted = finalize(conf, true);
System.exit(aborted ? 1 : 0);之后进入FSImage.doFinalize进行完成升级,主要工作就是删除previous目录,标记升级结束

File prevDir = sd.getPreviousDir();
if (!prevDir.exists()) { // already discarded
LOG.info(&quot;Directory &quot; + prevDir + &quot; does not exist.&quot;);
LOG.info(&quot;Finalize upgrade for &quot; + sd.getRoot()+ &quot; is not required.&quot;);
return;
}
LOG.info(&quot;Finalizing upgrade for storage directory &quot;
+ sd.getRoot() + &quot;.&quot;
+ (getLayoutVersion()==0 ? &quot;&quot; :
&quot;\n   cur LV = &quot; + this.getLayoutVersion()
+ &quot;; cur CTime = &quot; + this.getCTime()));
assert sd.getCurrentDir().exists() : &quot;Current directory must exist.&quot;;
final File tmpDir = sd.getFinalizedTmp();
// rename previous to tmp and remove
//将previous修改为finalized.tmp
rename(prevDir, tmpDir);
//删除finalized.tmp
deleteDir(tmpDir);
isUpgradeFinalized = true;
LOG.info(&quot;Finalize upgrade for &quot; + sd.getRoot()+ &quot; is complete.&quot;);

版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-137935-1-1.html 上篇帖子: Hadoop 1.2.1升级2.6.0的一次崎岖之旅(包括Hive、HBase对应的升级) 下篇帖子: 【Hadoop/Hbase】centos上安装并设置Snappy/LZO压缩方式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表