接触HBase2个月了,公司使用了0.92版本,大概浏览过代码,对于我等菜鸟来说过于复杂,入门甚难,故找来最原始的版本来看看,顺便瞻仰下当年大神的原迹。
原始版本可以在github上找到,在以后的几篇文章中笔者也将仔细的分析HBase中几个比较重要类的逻辑。
Master初始化
1.查看是hdfs文件系统,如果是则等待hdfs退出safemode
this.fs = FileSystem.get(conf);
if (this.fs instanceof DistributedFileSystem) {
// Make sure dfs is not in safe mode
String message = "Waiting for dfs to exit safe mode...";
while (((DistributedFileSystem) fs).setSafeMode(
FSConstants.SafeModeAction.SAFEMODE_GET)) {
LOG.info(message);
try {
Thread.sleep(this.threadWakeFrequency);
} catch (InterruptedException e) {
//continue
}
}
}
2.创建Root Meta Region ,创建Region过程比较复杂会另起文章分析
HRegion root = HRegion.createHRegion(HRegionInfo.rootRegionInfo,
this.rootdir, this.conf);
HRegion meta = HRegion.createHRegion(HRegionInfo.firstMetaRegionInfo,
this.rootdir, this.conf);
// Add first region from the META table to the ROOT region.
HRegion.addRegionToMETA(root, meta);
root.close();
root.getLog().closeAndDelete();
meta.close();
meta.getLog().closeAndDelete();
3.初始化lease对象、Server对象、HServerAddress对象、HConnection对象
lease对象负责定时清除过期的任务,具体实现可以参考http://www.cnblogs.com/luweiseu/archive/2012/08/06/2625768.html
Server对象的功能是HBase的rpc,负责与其他server或者client进行rpc
HServerAddress类简单封装了InetSocketAddress对象
HConnection对象维护了servername-》ServerAddress的map,帮助master能够连接到任何一个server
4.启动RootScanner和MetaScanner线程,初始化Sleeper,准备对外服务。
Master主循环
1.启动master的诸多守护进程:RootScanner MetaScanner ServerLease infoServer(web展示页面)RPCServer
Threads.setDaemonThreadRunning(this.rootScannerThread,
threadName + ".rootScanner");
Threads.setDaemonThreadRunning(this.metaScannerThread,
threadName + ".metaScanner");
// Leases are not the same as Chore threads. Set name differently.
this.serverLeases.setName(threadName + ".leaseChecker");
this.serverLeases.start();
// Put up info server.
int port = this.conf.getInt("hbase.master.info.port", 60010);
if (port >= 0) {
String a = this.conf.get("hbase.master.info.bindAddress", "0.0.0.0");
this.infoServer = new InfoServer(MASTER, a, port, false);
this.infoServer.setAttribute(MASTER, this);
this.infoServer.start();
}
// Start the server so everything else is running before we start
// receiving requests.
this.server.start();
2.主循环不停的从todoqueue里拿出regionserver的operation干活,如果没有活干就休息,如果收到shutdownrequest就进入shutdown master流程,主循环伪代码如下:
while (!closed.get()) {
RegionServerOperation op = null;
if (shutdownRequested && serversToServerInfo.size() == 0) {
startShutdown();
break;
}
if (op == null ) {
try {
op = toDoQueue.poll(threadWakeFrequency, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// continue
}
op.process();
}