基于HBase-2.1.5
1.1、HbaseAdmin 发起split
1.1.1、split
@Override
public void split(final TableName tableName, final byte[] splitPoint) throws IOException {
checkTableExists(tableName);
for (HRegionLocation loc : connection.locateRegions(tableName, false, false)) {
ServerName sn = loc.getServerName();
if (sn == null) {
continue;
}
RegionInfo r = loc.getRegion();
// check for parents
if (r.isSplitParent()) { // 父region正在split, 则不进行分裂
continue;
}
// if a split point given, only split that particular region
// 只分裂指定分裂点的region
if (r.getReplicaId() != RegionInfo.DEFAULT_REPLICA_ID ||
(splitPoint != null && !r.containsRow(splitPoint))) {
continue;
}
// call out to master to do split now
splitRegionAsync(r, splitPoint); // 发送split request 异步
}
}
1.1.2、splitRegionAsync
Future splitRegionAsync(RegionInfo hri, byte[] splitPoint) throws IOException {
TableName tableName = hri.getTable();
if (hri.getStartKey() != null && splitPoint != null &&
Bytes.compareTo(hri.getStartKey(), splitPoint) == 0) {
throw new IOException("should not give a splitkey which equals to startkey!");
}
// 构建了一个MasterCallable类型的匿名对象,其复写的rpcCall方法真正的处理客户端的请求
SplitTableRegionResponse response = executeCallable(
new MasterCallable(getConnection(), getRpcControllerFactory()) {
Long nonceGroup = ng.getNonceGroup();
Long nonce = ng.newNonce();
@Override
protected SplitTableRegionResponse rpcCall() throws Exception {
setPriority(tableName);
SplitTableRegionRequest request = RequestConverter
.buildSplitTableRegionRequest(hri, splitPoint, nonceGroup, nonce);
// split对应的region, 并返回响应
return master.splitRegion(getRpcController(), request);
}
});
// 创建SplitTableRegionFuture
return new SplitTableRegionFuture(this, tableName, response);
}
private static class SplitTableRegionFuture extends TableFuture {
public SplitTableRegionFuture(final HBaseAdmin admin,
final TableName tableName,
final SplitTableRegionResponse response) {
super(admin, tableName,
(response != null && response.hasProcId()) ? response.getProcId() : null);
}
public SplitTableRegionFuture(
final HBaseAdmin admin,
final TableName tableName,
final Long procId) {
super(admin, tableName, procId);
}
@Override
public String getOperationType() {
return "SPLIT_REGION";
}
}