hbase 原代码分析 (10) region 创建过程



今天在研究WAL时时候发现一个region维持这一个WAL。所以首先明白region的创建过程。

先写结论:
1)region是由HMaster创建的。在创建表的时候。
2)region创建首先是写HDFS文件。
hdfs = root/ data/namespace/qualifierName/
  1. //这个方法里,首先创建目录,
  2. //然后新建一个临时文件./temp/.regioninfo
    //然后写入regionInfo 作为MATA的恢复资料
  3. //然后将.regionInfo移动到当前目录。
3)会创建WAL
4)region信息放在MATA里。
5)region会分配给RegionService ,并给予他管理。
6)region会随着数据增加会一分为二。,

入口:
1)创建表的过程可以参考我的第三篇和第四篇文章。
类:CreateTableProcedure.java。
方法
   
   
  1. @Override
  2. protected Flow executeFromState(final MasterProcedureEnv env, final CreateTableState state)
  3. throws InterruptedException {
  4. ... 此处有省略
  5. preCreate(env);
  6. setNextState(CreateTableState.CREATE_TABLE_WRITE_FS_LAYOUT);
  7. break;
  8. case CREATE_TABLE_WRITE_FS_LAYOUT:
  9. newRegions = createFsLayout(env, hTableDescriptor, newRegions);
  10. setNextState(CreateTableState.CREATE_TABLE_ADD_TO_META);
  11. break;
  12. case CREATE_TABLE_ADD_TO_META:
  13. newRegions = addTableToMeta(env, hTableDescriptor, newRegions);
  14. setNextState(CreateTableState.CREATE_TABLE_ASSIGN_REGIONS);
  15. break;
  16. case CREATE_TABLE_ASSIGN_REGIONS:
  17. assignRegions(env, getTableName(), newRegions);
  18. setNextState(CreateTableState.CREATE_TABLE_UPDATE_DESC_CACHE);
  19. break;
  20. case CREATE_TABLE_UPDATE_DESC_CACHE:
  21. updateTableDescCache(env, getTableName());
  22. setNextState(CreateTableState.CREATE_TABLE_POST_OPERATION);
  23. break;
  24. case CREATE_TABLE_POST_OPERATION:
  25. postCreate(env);
  26. return Flow.NO_MORE_STATE;
  27. ... 此处有省略
  28. }
assignRegions 分配过程在源代码分析(4) 已经分析过。

2) createFsLayout过程
目录:
hdfs = root/ data/namespace/qualifierName/
主要方法:ModifyRegionUtils.java
   
   
  1. public static List<HRegionInfo> createRegions(final ThreadPoolExecutor exec,
  2. final Configuration conf, final Path rootDir, final Path tableDir,
  3. final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
  4. final RegionFillTask task) throws IOException {
  5. ... 此处有省略
  6. for (final HRegionInfo newRegion : newRegions) {
  7. completionService.submit(new Callable<HRegionInfo>() {
  8.     //启动多线程来创建region
  9. @Override
  10. public HRegionInfo call() throws IOException {
  11. return createRegion(conf, rootDir, tableDir, hTableDescriptor, newRegion, task);
  12. }
  13. });
  14. }
  15. try {
  16. // 等待全部创建完成
  17. for (int i = 0; i < regionNumber; i++) {
  18. regionInfos.add(completionService.take().get());
  19. }
  20. }
  21. ... 此处有省略
  22. }

这后面涉及好几个类,写到一块去。
HRegin。java
   
   
  1. public static HRegion createHRegion(final HRegionInfo info, final Path rootDir,
  2. final Path tableDir, final Configuration conf, final HTableDescriptor hTableDescriptor,
  3. final WAL wal, final boolean initialize, final boolean ignoreWAL)
  4. throws IOException {
  5. ... 此处有省略
  6. FileSystem fs = FileSystem.get(conf);
  7. //这个方法里,首先创建目录,
  8. //  然后新建一个临时文件./temp/.regioninfo //然后写入regionInfo 作为MATA的恢复资料
  9. // 然后将.regionInfo移动到当前目录。
  10. HRegionFileSystem.createRegionOnFileSystem(conf, fs, tableDir, info);
  11.     
  12. WAL effectiveWAL = wal;
  13. if (wal == null && !ignoreWAL) {
  14. Configuration confForWAL = new Configuration(conf);
  15. confForWAL.set(HConstants.HBASE_DIR, rootDir.toString());
  16. effectiveWAL = (new WALFactory(confForWAL,
  17. Collections.<WALActionsListener>singletonList(new MetricsWAL()),
  18. "hregion-" + RandomStringUtils.randomNumeric(8))).
  19. getWAL(info.getEncodedNameAsBytes(), info.getTable().getNamespace());
  20. }
  21. HRegion region = HRegion.newHRegion(tableDir,
  22. effectiveWAL, fs, conf, info, hTableDescriptor, null);
  23. if (initialize) region.initialize(null);
  24. return region;
  25. }
这里还有几个重要地方。创建了WAL 。这个暂时不分析。留到专门的一节。
在这里WAL 也写入Region了。


3)写入META表
    
    
  1. protected static List<HRegionInfo> addTableToMeta(final MasterProcedureEnv env,
  2. final HTableDescriptor hTableDescriptor,
  3. final List<HRegionInfo> regions) throws IOException {
  4.  
  5. ... 此处有省略
  6. // Add regions to META
  7. //这里里面就是new 很多put
  8. //然后 table.put 就OK了
  9. //Table t = getMetaHTable(connection); 这里的表就是meta
  10. //return connection.getTable(TableName.META_TABLE_NAME);
  11. addRegionsToMeta(env, hTableDescriptor, regions);
  12. // Add replicas if needed
  13. List<HRegionInfo> newRegions = addReplicas(env, hTableDescriptor, regions);
  14. // Setup replication for region replicas if needed
  15. if (hTableDescriptor.getRegionReplication() > 1) {
  16. ServerRegionReplicaUtil.setupRegionReplicaReplication(env.getMasterConfiguration());
  17. }


然后是写副本ServerRegionRepicaUtil.java
    
    
  1. public static void setupRegionReplicaReplication(Configuration conf) throws IOException {
  2. if (!isRegionReplicaReplicationEnabled(conf)) {
  3. return;
  4. }
  5. ReplicationAdmin repAdmin = new ReplicationAdmin(conf);
  6. try {
  7. if (repAdmin.getPeerConfig(REGION_REPLICA_REPLICATION_PEER) == null) {
  8. ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
  9. peerConfig.setClusterKey(ZKConfig.getZooKeeperClusterKey(conf));
  10. peerConfig.setReplicationEndpointImpl(RegionReplicaReplicationEndpoint.class.getName());
  11. repAdmin.addPeer(REGION_REPLICA_REPLICATION_PEER, peerConfig, null);
  12. }
  13. } catch (ReplicationException ex) {
  14. throw new IOException(ex);
  15. } finally {
  16. repAdmin.close();
  17. }
  18. }
副本的问题这里也不细讲了,留到以后把。

之后是region 分配给regionService 管理,里面用到的算法是robin。

详细可以见源代码分析(4)region assign 过程

未完待续.....

上一个章节:

hbase 源代码分析 (9) hbase启动过程

http://blog.csdn.net/chenfenggang/article/details/75136991



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值