HBase的所有请求调用都是通过RPC的机制进行的,RPCServer监听到请求之后会解析请求内容,然后根据解析的方法以及参数调用服务器端实际的方法,这也是远程代理模式的经典做法,createTable的请求最终实现是在HMaster中的,但是实际的表的建立过程是在CreateTableHandler类中的,接下来主要就HBase中表的建立过程进行详细分析。
1. HMaster的createTable实现
如下代码所示,是HMaster中的createTable的流程代码:
public void createTable(HTableDescriptor hTableDescriptor,
byte[][] splitKeys) throws IOException {
if (isStopped()) {
throw new MasterNotRunningException();
}
String namespace = hTableDescriptor.getTableName().getNamespaceAsString();
ensureNamespaceExists(namespace);
HRegionInfo[] newRegions = getHRegionInfos(hTableDescriptor, splitKeys);
checkInitialized();
sanityCheckTableDescriptor(hTableDescriptor);
if (cpHost != null) {
cpHost.preCreateTable(hTableDescriptor, newRegions);
}
LOG.info(getClientIdAuditPrefix() + " create " + hTableDescriptor);
this.service.submit(new CreateTableHandler(this,
this.fileSystemManager, hTableDescriptor, conf,
newRegions, this).prepare());
if (cpHost != null) {
cpHost.postCreateTable(hTableDescriptor, newRegions);
}
}
在正式创建表之前做的几件事情:
1.检查Master是否正常运行
2.检查索要创建的表的namespace是否存在
3.HRegionInfo类包含了HRegion的相关信息getHRegionInfos(),函数按照splitKeys和表描述信息,获取该表对应的HRegion的信息。
这里有必要解释一下HRegion,HRegion存储了table的数据信息,它包含了每一个row的所有columns,一个table包含1到多个hregion,每一个hregion包含多个HStores,每个HStores包含一部分的Rows和对应部分的Columns。
每个HRegion包含了一个[startKey, endKey),用来标识其保存的row的范围,英雌HRegion可以由TableName和key range唯一确定
4.检查Master是否完成初始化
5.检查表信息是否符合规定
6.建表,建表又分为三个过程:
1. cpHost.preCreateTable(hTableDescriptor, newRegions);
2. submit(new CreateTableHandler(this,
this.fileSystemManager, hTableDescriptor, conf,
newRegions, this)
3.cpHost.postCreateTable(hTableDescriptor, newRegions);
其中步骤1和3都是为了协处理器预留的钩子函数,方便应用开发人员动态添加新的功能。
接下来主要分析一下步骤2中的建表所做的操作
2. CreateTableHandler 中的建表实现
其实在代码this.service.submit(new CreateTableHandler(this,
this.fileSystemManager, hTableDescriptor, conf,
newRegions, this).prepare());中调用的过程分为两个部分,一个是prepare,然后才是submit,先来看一下prepare()的工作
2.1 建表之前的准备工作prepare()分析
public CreateTableHandler prepare() throws NotAllMetaRegionsOnlineException, TableExistsException, IOException { // Need hbase:meta availability to create a table try { if (server.getMetaTableLocator().waitMetaRegionLocation( server.getZooKeeper(), timeout) == null) { throw new NotAllMetaRegionsOnlineException(); } // If we are creating the table in service to an RPC request, record the // active user for later, so proper permissions will be applied to the // new table by the AccessController if it is active if (RequestContext.isInRequestContext()) { this.activeUser = RequestContext.getRequestUser(); } else { this.activeUser = UserProvider.instantiate(conf).getCurrent(); } } //acquire the table write lock, blocking. Make sure that it is released. this.tableLock.acquire(); boolean success = false; try { TableName tableName = this.hTableDescriptor.getTableName(); if (MetaTableAccessor.tableExists(this.server.getConnection(), tableName)) { throw new TableExistsException(tableName); } checkAndSetEnablingTable(assignmentManager, tableName); success = true; } finally { if (!success) { releaseTableLock(); } } return this; }
这里的主要工作如下:
1. 获取hbase:meta 信息,meta是hbase的一个特殊表,其存数了HBase上面的RegionServer的信息以及其分布情况。
2. 检查创建表的用