今天在研究WAL时时候发现一个region维持这一个WAL。所以首先明白region的创建过程。
先写结论:
1)region是由HMaster创建的。在创建表的时候。
2)region创建首先是写HDFS文件。
hdfs = root/
data/namespace/qualifierName/
-
//这个方法里,首先创建目录,
-
//然后新建一个临时
文件./temp/.regioninfo
//然后写入regionInfo 作为MATA的恢复资料
- //然后将.regionInfo移动到当前目录。
3)会创建WAL
4)region信息放在MATA里。
5)region会分配给RegionService ,并给予他管理。
6)region会随着数据增加会一分为二。,
入口:
1)创建表的过程可以参考我的第三篇和第四篇文章。
类:CreateTableProcedure.java。
方法
@Override
protected Flow executeFromState(final MasterProcedureEnv env, final CreateTableState state)
throws InterruptedException {
... 此处有省略
preCreate(env);
setNextState(CreateTableState.CREATE_TABLE_WRITE_FS_LAYOUT);
break;
case CREATE_TABLE_WRITE_FS_LAYOUT:
newRegions = createFsLayout(env, hTableDescriptor, newRegions);
setNextState(CreateTableState.CREATE_TABLE_ADD_TO_META);
break;
case CREATE_TABLE_ADD_TO_META:
newRegions = addTableToMeta(env, hTableDescriptor, newRegions);
setNextState(CreateTableState.CREATE_TABLE_ASSIGN_REGIONS);
break;
case CREATE_TABLE_ASSIGN_REGIONS:
assignRegions(env, getTableName(), newRegions);
setNextState(CreateTableState.CREATE_TABLE_UPDATE_DESC_CACHE);
break;
case CREATE_TABLE_UPDATE_DESC_CACHE:
updateTableDescCache(env, getTableName());
setNextState(CreateTableState.CREATE_TABLE_POST_OPERATION);
break;
case CREATE_TABLE_POST_OPERATION:
postCreate(env);
return Flow.NO_MORE_STATE;
... 此处有省略
}
assignRegions 分配过程在源代码分析(4) 已经分析过。
2) createFsLayout过程
目录:
hdfs = root/
data/namespace/qualifierName/
主要方法:ModifyRegionUtils.java
public static List<HRegionInfo> createRegions(final ThreadPoolExecutor exec,
final Configuration conf, final Path rootDir, final Path tableDir,
final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
final RegionFillTask task) throws IOException {
... 此处有省略
for (final HRegionInfo newRegion : newRegions) {
completionService.submit(new Callable<HRegionInfo>() {
//启动多线程来创建region
@Override
public HRegionInfo call() throws IOException {
return createRegion(conf, rootDir, tableDir, hTableDescriptor, newRegion, task);
}
});
}
try {
// 等待全部创建完成
for (int i = 0; i < regionNumber; i++) {
regionInfos.add(completionService.take().get());
}
}
... 此处有省略
}
这后面涉及好几个类,写到一块去。
HRegin。java
public static HRegion createHRegion(final HRegionInfo info, final Path rootDir,
final Path tableDir, final Configuration conf, final HTableDescriptor hTableDescriptor,
final WAL wal, final boolean initialize, final boolean ignoreWAL)
throws IOException {
... 此处有省略
FileSystem fs = FileSystem.get(conf);
//这个方法里,首先创建目录,
// 然后新建一个临时
文件./temp/.regioninfo//然后写入regionInfo 作为MATA的恢复资料
- // 然后将.regionInfo移动到当前目录。
HRegionFileSystem.createRegionOnFileSystem(conf, fs, tableDir, info);
WAL effectiveWAL = wal;
if (wal == null && !ignoreWAL) {
- Configuration confForWAL = new Configuration(conf);
- confForWAL.set(HConstants.HBASE_DIR, rootDir.toString());
- effectiveWAL = (new WALFactory(confForWAL,
- Collections.<WALActionsListener>singletonList(new MetricsWAL()),
- "hregion-" + RandomStringUtils.randomNumeric(8))).
- getWAL(info.getEncodedNameAsBytes(), info.getTable().getNamespace());
- }
- HRegion region = HRegion.newHRegion(tableDir,
- effectiveWAL, fs, conf, info, hTableDescriptor, null);
- if (initialize) region.initialize(null);
- return region;
- }
这里还有几个重要地方。创建了WAL 。这个暂时不分析。留到专门的一节。
在这里WAL 也写入Region了。
3)写入META表
protected static List<HRegionInfo> addTableToMeta(final MasterProcedureEnv env,
final HTableDescriptor hTableDescriptor,
final List<HRegionInfo> regions) throws IOException {
... 此处有省略
- // Add regions to META
- //这里里面就是new 很多put
- //然后 table.put 就OK了
- //Table t = getMetaHTable(connection); 这里的表就是meta
- //return connection.getTable(TableName.META_TABLE_NAME);
addRegionsToMeta(env, hTableDescriptor, regions);
// Add replicas if needed
List<HRegionInfo> newRegions = addReplicas(env, hTableDescriptor, regions);
// Setup replication for region replicas if needed
if (hTableDescriptor.getRegionReplication() > 1) {
ServerRegionReplicaUtil.setupRegionReplicaReplication(env.getMasterConfiguration());
}
然后是写副本ServerRegionRepicaUtil.java
public static void setupRegionReplicaReplication(Configuration conf) throws IOException {
if (!isRegionReplicaReplicationEnabled(conf)) {
return;
}
ReplicationAdmin repAdmin = new ReplicationAdmin(conf);
try {
if (repAdmin.getPeerConfig(REGION_REPLICA_REPLICATION_PEER) == null) {
ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
peerConfig.setClusterKey(ZKConfig.getZooKeeperClusterKey(conf));
peerConfig.setReplicationEndpointImpl(RegionReplicaReplicationEndpoint.class.getName());
repAdmin.addPeer(REGION_REPLICA_REPLICATION_PEER, peerConfig, null);
}
} catch (ReplicationException ex) {
throw new IOException(ex);
} finally {
repAdmin.close();
}
}
副本的问题这里也不细讲了,留到以后把。
之后是region 分配给regionService 管理,里面用到的算法是robin。
详细可以见源代码分析(4)region assign 过程
未完待续.....
上一个章节:
http://blog.csdn.net/chenfenggang/article/details/75136991