HBase1.0.0版源碼分析之HMaster啟動程式碼分析(1),hbase1.0.0hmaster
本文其實還算不上真正的啟動代碼解析,本文主要還是從啟動流程上分析到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啟動代碼比較複雜,我將會在下一篇文章中進行具體的介紹.