NameNode之启动流程分析

来源:互联网 发布:新开淘宝店铺怎么宣传 编辑:程序博客网 时间:2024/05/18 02:32

NameNode启动流程分析

  public staticvoidmain(Stringargv[])throws Exception {

    if (DFSUtil.parseHelpArgument(argv, NameNode.USAGE, System.out,true)) {

      System.exit(0);

    }

 

    try {

      StringUtils.startupShutdownMessage(NameNode.class,argv,LOG);

      //创建NameNode实例

      NameNodenamenode =createNameNode(argv,null);

      if (namenode !=null) {

       namenode.join();

      }

    } catch (Throwable e) {

      LOG.error("Failed to start namenode.",e);

      terminate(1, e);

    }

  }

调用NameNode方法:

publicstatic NameNode createNameNode(Stringargv[], Configurationconf)

      throws IOException {

    LOG.info("createNameNode " + Arrays.asList(argv));

//加载HDFS配置文件   

if (conf ==null)

      conf = new HdfsConfiguration();

//解析传递的参数   

StartupOptionstartOpt =parseArguments(argv);

    if (startOpt ==null) {

      printUsage(System.err);

      return null;

    }

    setStartupOption(conf, startOpt);

//对参数进行判断,然后进行不同处理

    switch (startOpt) {

      case FORMAT: {

       booleanaborted = format(conf,startOpt.getForceFormat(),

           startOpt.getInteractiveFormat());

       terminate(aborted ? 1 : 0);

       returnnull;// avoidjavac warning

      }

      case GENCLUSTERID: {

        System.err.println("Generating new cluster id:");

        System.out.println(NNStorage.newClusterID());

       terminate(0);

       returnnull;

      }

      case FINALIZE: {

        System.err.println("Use of the argument '" + StartupOption.FINALIZE +

           "' is no longer supported. To finalizean upgrade, start the NN " +

           " and then run `hdfs dfsadmin-finalizeUpgrade'");

       terminate(1);

       returnnull;// avoidjavac warning

      }

      case ROLLBACK: {

       booleanaborted = doRollback(conf,true);

       terminate(aborted ? 1 : 0);

       returnnull;// avoidwarning

      }

      case BOOTSTRAPSTANDBY: {

        StringtoolArgs[] = Arrays.copyOfRange(argv, 1,argv.length);

       intrc = BootstrapStandby.run(toolArgs,conf);

       terminate(rc);

       returnnull;// avoidwarning

      }

      case INITIALIZESHAREDEDITS: {

       booleanaborted = initializeSharedEdits(conf,

           startOpt.getForceFormat(),

           startOpt.getInteractiveFormat());

       terminate(aborted ? 1 : 0);

       returnnull;// avoidwarning

      }

      case BACKUP:

      case CHECKPOINT: {

        NamenodeRolerole =startOpt.toNodeRole();

        DefaultMetricsSystem.initialize(role.toString().replace(" ",""));

       returnnew BackupNode(conf,role);

      }

      case RECOVER: {

        NameNode.doRecovery(startOpt,conf);

       returnnull;

      }

      case METADATAVERSION: {

       printMetadataVersion(conf);

       terminate(0);

       returnnull;// avoidjavac warning

      }

      case UPGRADEONLY: {

        DefaultMetricsSystem.initialize("NameNode");

       new NameNode(conf);

       terminate(0);

       returnnull;

      }

      default: {

        DefaultMetricsSystem.initialize("NameNode");

       returnnew NameNode(conf);

      }

    }

  }

 

参数是format:

从配置文件获取NameNodeNameServicesId

获取所有的NameNode的id

进行初始化工作

>>登陆使用dfs.namenode.kerberos.principal作为用户名,否则使用当前linux的user作为用户。

>>如果当前NameNode角色是NameNode,则启动HttpServer,如果NameNode是backup NameNode或者是CheckPoint     NameNode则略过这个步骤

privatevoidstartHttpServer(final Configurationconf)throws IOException {

    //创建NameNode HttpServer

httpServer =new NameNodeHttpServer(conf,this, getHttpServerBindAddress(conf));

//启动http server

    httpServer.start();

    httpServer.setStartupProgress(startupProgress);

  }