提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
第一篇 基于hbase2.4.2源码分析 HMaster启动分析
前言
本文不紧紧教大家看hbase源码,还介绍怎么看hbase源码1.看什么?
看HMaster类说明
看main()入口
看初始化了哪些重要组件
如何看:看过程中画图,或者把方法的调用关系,对象的调用按照调用关系记录下来,本文按照第二种方式进行编写
一、HMaster类继承关系
class HMaster extends HRegionServer implements MasterServices
class HRegionServer extends Thread implements
RegionServerServices, LastSequenceId, ConfigurationObserver
从性质看:
HMaster 继承HRegionServer, 实现MasterServices接口
HRegionServer 实现了Runnable接口,肯定是作为一个线程运行,核心方法在run()里面
二、Hmaster入口分析
1.Hmaster.main()入口
代码如下(示例):
/**
* @see org.apache.hadoop.hbase.master.HMasterCommandLine
*/
public static void main(String [] args) {
VersionInfo.logVersion();
new HMasterCommandLine(HMaster.class).doMain(args);
}
HMasterCommandLine.doMain()如下
/**
* Parse and run the given command line. This may exit the JVM if
* a nonzero exit code is returned from <code>run()</code>.
*/
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);
}
}
发现贴源码占用篇幅很大,我改为贴核心流程哈, 下一行空一格,说明是这个方法里面的调用
HMaster.main()
HMasterCommandLine(HMaster.class).doMain
ToolRunner.run(HBaseConfiguration.create(), this, args
tool.run(toolArgs) (tool指HMasterCommandLine)
HMasterCommandLine.run()
做两件事
1.参数检查
2.startMaster() 或者stopMaster()
我们在看启动流程,不要看错方向了哦
startMaster进去看看,发现有两种模式的判断,我们看集群模式,不要第一次两个都看,记得自己在干什么,不要迷路了
local模式
集群模式
HMaster master = HMaster.constructMaster(masterClass, conf);
Constructor<? extends HMaster> c = masterClass.getConstructor(Configuration.class);
return c.newInstance(conf);
master.start(); master线程启动,run()方法开始
master.join(); 通过线程的join等待结束
很明显,构造master,启动master
HMaster(Configuration)的构造器
两个东西:
1.super(conf); 父类初始化
2.HMaster特有的东西初始化
看看super(conf) 就是HRegionServer的初始化
一些flush,compact间隔的配置
threadWakeFrequency 10s
compactionCheckFrequency 10s
flushCheckFrequency 10s
核心组件的初始化:
rpcServices = createRpcServices()
zooKeeper = new ZKWatcher
MasterAddressTracker
ClusterStatusTracker
putUpWebUI()
只看到一些东西初始化,不知他们具体怎么工作,抓大放小,直接跳过。。。开始看master.start()干了什么,master是个Thread,看run方法
2.HMaster.run()
代码如下(示例):
HMaster.run()
做两件事情 1.启动jettyServer 2.启动ActiveMasterManager
int infoPort = putUpJettyServer();
startActiveMasterManager(infoPort);
startActiveMasterManager方法里面:进行master选举
/**
* Block until becoming the active master.
*
* Method blocks until there is not another active master and our attempt
* to become the new active master is successful.
*
* This also makes sure that we are watching the master znode so will be
* notified if another master dies.
* @param checkInterval the interval to check if the master is stopped
* @param startupStatus the monitor status to track the progress
* @return True if no issue becoming active master else false if another
* master was running or if some other problem (zookeeper, stop flag has been
* set on this Master)
*/
if (activeMasterManager.blockUntilBecomingActiveMaster(timeout, status)) {
finishActiveMasterInitialization(status);
master选举机制: 在zk的/master 目录竞争创建临时节点,创建成功就成为master
创建失败的,block住,一直等待创建成功
选举成功的会做哪些事情?
成为master的 执行finishActiveMasterInitialization() 完成active Master的初始化
这里,初始化了几十个对象。。。
不要在意他们干什么,起码不是刚看就要了解,使用到的时候,再看他们做什么