您的位置:首页 > 编程语言

HBase1.0.0版源码分析之HMaster启动代码分析(1)

2015-03-21 11:11 357 查看
本文其实还算不上真正的启动代码解析,本文主要还是从启动流程上分析到startHMaster部分,初次之外本文将就HBase的伪分布式调试方式进行相关的介绍.

我们将源码倒入到Intellij IDE之后会得到如下的代码结构:



这里我们进入hbase-server中在src/main下面的resources中添加hadoop-metrics2-hbase.properties,hbase-site.xml,log4j.properties等文件并进行相应的配置,除了hbase-site.xml文件之外的其他文件都可以直接从conf目录下拷贝过来,我的hbase-site.xml简单配置如下FYI:

<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.rootdir</name>
<value>hdfs://localhost:9000/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/opt/zookeeper/data</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>localhost</value>
</property>

<property>
<name>hbase.defaults.for.version.skip</name>
<value>true</value>
</property>
这样配置完毕之后,就可以启动HMaster和HRegionServer进行代码的调试工作了.源码的配置调试前期工工作就介绍到此.

在介绍HMaster的启动流程之前,我们首先来看一下涉及到流程启动的关键类之间的依赖关系,具体如下图所示:



1.HMaster中有个main方法,这是HMaster启动的开始的地方,如下所示:

public static void main(String [] args) {
VersionInfo.logVersion();
//System.out.println("########################################HMaster started!");
new HMasterCommandLine(HMaster.class).doMain(args);
//System.out.println("HMaster stoped!");
}


从代码中可以看出,这里需要传入启动需要的参数,具体参数的用法如下:

[opts] start|stop|clear

start:启动Master,如果是本地模式,则在同一个JVM上启动Mater和RegionServer

stop: 开始关闭集群,Master向RegionServer发送shutdown信号

clear:在master崩溃之后删除Zookeeper中的节点

[opts]: --minRegionServers=<servers> 最少能够容纳用户表的RegionServers数目,

--localRegionServers=<servers>在本地模式下,master进程中启动的Regionsrevers数目

--masters=<servers> 进程中master的数量

--backup 备份模式启动

2.接下来我们来看看HMasterCommandLine的doMain方法所进行的工作,如下:

public void doMain(String args[]) {
try {
int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
if (ret != 0) {
System.exit(ret);
}
} catch (Exception e) {
LOG.error("Failed to run", e);
System.exit(-1);
}
}
public static int run(Configuration conf, Tool tool, String[] args)
throws Exception{
if(conf == null) {
conf = new Configuration();
}
GenericOptionsParser parser = new GenericOptionsParser(conf, args);
//set the configuration back, so that Tool can configure itself
tool.setConf(conf);

//get the args w/o generic hadoop args
String[] toolArgs = parser.getRemainingArgs();
return tool.run(toolArgs);
}
CommandLIne方法调用了ToolRunner的run方法,继而该方法进行了一些命令参数的解析并调用HMaserCommandline所实现的run方法,该方法主要做一些参数的配置与解析,并就命令传入的参数调用不同的处理方法,如下所示:

String command = remainingArgs.get(0);

if ("start".equals(command)) {
return startMaster();
} else if ("stop".equals(command)) {
return stopMaster();
} else if ("clear".equals(command)) {
return (ZNodeClearer.clear(getConf()) ? 0 : 1);
} else {
usage("Invalid command: " + command);
return 1;
}


到此为止我们就可以看到相应具体的Master启动调用,startMaster启动代码比较复杂,我将会在下一篇文章中进行具体的介绍.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: