分布式查找过程[HBase]Region location

2023-11-08

HBase的table是该region切分的,client操作一个row的时候,如何知道这个row对应的region是在哪台Region server上呢?这里有个region location过程。主要涉及到2张系统表,-ROOT-,.META.。其结构见图

 

在zookeeper的/hbase/root-region-server节点中存着-ROOT-表所在的Region Server地址。

-ROOT-表的一个row代表着META的一个region信息,其key的结构是META表名,META表Region的startkey,RegionId。其value的主要保存regioninfo和server信息。ROOT表不能split

.META.表的一个row代表着用户表的一个region信息,其key的结构是其实就是用户表的regionName,用户表名,startKey,RegionId。其value同样保存着regioninfo和server信息。META表可以split,但是一个region默认有128M,可以存上亿个用户表的region信息,所以一般不会split。

其查找过程如下:

1.通过zk getData拿-ROOT-表的location

2.RPC -ROOT-表的rs,getClosestRowBefore,拿row对应的meta表的region location

3.RPC .META.表的某一个region,拿该row在真实table所在的region location

4.RPC对应region

 

region location需要3次网络IO,为了提升性能,client会cache数据。

LocationCache是一个2级Map,第一级的key是tableName的hash值,第二级的key是starRow,用SoftValueSortedMap包装了TreeMap实现,用软引用实现cache,内存不够时才会回收。Cache里存在META表和用户表的region location信息。

其代码实现如下,0.94版本:

HConnectionManager locateRegion入口

Java代码   收藏代码
  1. private HRegionLocation locateRegion(final byte [] tableName,  
  2.       final byte [] row, boolean useCache)  
  3.     throws IOException {  
  4.     .......  
  5.     //检查下都应的zkTracker是否启动  
  6.       ensureZookeeperTrackers();  
  7.     //如果是-ROOT-表,则通过zk节点/hbase/root-region-server获取-ROOT-表所在的Location  
  8.       if (Bytes.equals(tableName, HConstants.ROOT_TABLE_NAME)) {  
  9.         try {  
  10.         //通过zk的getData接口拿节点数据,此处会等待节点数据就位或者超时  
  11.           ServerName servername = this.rootRegionTracker.waitRootRegionLocation(this.rpcTimeout);  
  12.           LOG.debug("Looked up root region location, connection=" + this +  
  13.             "; serverName=" + ((servername == null)? "": servername.toString()));  
  14.           if (servername == nullreturn null;  
  15.     //返回一个拼装的HRegionLocation,因为-ROOT-表只有一个region,而且不会split  
  16.           return new HRegionLocation(HRegionInfo.ROOT_REGIONINFO,  
  17.             servername.getHostname(), servername.getPort());  
  18.         } catch (InterruptedException e) {  
  19.           Thread.currentThread().interrupt();  
  20.           return null;  
  21.         }  
  22.       }   
  23.     //如果是.META.表,则请求.META.表,这里的row其实就是请求row拼装的regionName,类似test,key1,99999999999999  
  24.     //如果没命中cache,则继续请求-ROOT-表,拿到这个row对应的.META.表的region location  
  25.       else if (Bytes.equals(tableName, HConstants.META_TABLE_NAME)) {  
  26.         return locateRegionInMeta(HConstants.ROOT_TABLE_NAME, tableName, row,  
  27.             useCache, metaRegionLock);  
  28.       }   
  29.     //如果是用户表,则请求用户表,这里的row就是key1  
  30.     //如果没命中cache,则请求.META.表,获取该row对应的region location  
  31.       else {  
  32.         // Region not in the cache - have to go to the meta RS  
  33.         return locateRegionInMeta(HConstants.META_TABLE_NAME, tableName, row,  
  34.             useCache, userRegionLock);  
  35.       }  
  36.     }  

 locateRegionInMeta方法

Java代码   收藏代码
  1. private HRegionLocation locateRegionInMeta(final byte [] parentTable,  
  2.       final byte [] tableName, final byte [] row, boolean useCache,  
  3.       Object regionLockObject)  
  4.     throws IOException {  
  5.       HRegionLocation location;  
  6.       // If we are supposed to be using the cache, look in the cache to see if  
  7.       // we already have the region.  
  8.     //先读cache,cache没有再往上找  
  9.     //注意如果rowkey的region locatin变化了,RPC的时候会失败,客户端做重试的时候useCache是false  
  10.       if (useCache) {  
  11.         location = getCachedLocation(tableName, row);  
  12.         if (location != null) {  
  13.           return location;  
  14.         }  
  15.       }  
  16.   
  17.       // build the key of the meta region we should be looking for.  
  18.       // the extra 9's on the end are necessary to allow "exact" matches  
  19.       // without knowing the precise region names.  
  20.     //先拼一个想查找的key,类似于test,key1,99999999999999  
  21.       byte [] metaKey = HRegionInfo.createRegionName(tableName, row,  
  22.         HConstants.NINES, false);  
  23.     //默认重试10次  
  24.       for (int tries = 0true; tries++) {  
  25.     //找不到  
  26.         if (tries >= numRetries) {  
  27.           throw new NoServerForRegionException("Unable to find region for "  
  28.             + Bytes.toStringBinary(row) + " after " + numRetries + " tries.");  
  29.         }  
  30.       
  31.         HRegionLocation metaLocation = null;  
  32.         try {  
  33.           // locate the root or meta region  
  34.         //递归查找parentTable  
  35.           metaLocation = locateRegion(parentTable, metaKey);  
  36.           // If null still, go around again.  
  37.           if (metaLocation == nullcontinue;  
  38.         //找到对应Region server地址之后,可以发起RPC请求了。  
  39.         //这里先生成一个RPC Proxy对象,具体RPC分析见后文  
  40.           HRegionInterface server =  
  41.             getHRegionConnection(metaLocation.getHostname(), metaLocation.getPort());  
  42.   
  43.           Result regionInfoRow = null;  
  44.           // This block guards against two threads trying to load the meta  
  45.           // region at the same time. The first will load the meta region and  
  46.           // the second will use the value that the first one found.  
  47.           synchronized (regionLockObject) {  
  48.             // If the parent table is META, we may want to pre-fetch some  
  49.             // region info into the global region cache for this table.  
  50.         //如果parentTable是.META.表,则预先获取.META.的一些数据,默认10条  
  51.             if (Bytes.equals(parentTable, HConstants.META_TABLE_NAME) &&  
  52.                 (getRegionCachePrefetch(tableName)) )  {  
  53.               prefetchRegionCache(tableName, row);  
  54.             }  
  55.   
  56.             // Check the cache again for a hit in case some other thread made the  
  57.             // same query while we were waiting on the lock. If not supposed to  
  58.             // be using the cache, delete any existing cached location so it won't  
  59.             // interfere.  
  60.             if (useCache) {  
  61.               location = getCachedLocation(tableName, row);  
  62.               if (location != null) {  
  63.                 return location;  
  64.               }  
  65.             }   
  66.         //如果不使用cache,则清除之,比如row对应的region发生了分裂,用老的location启动rpc时会抛异常,此时通过useCache=fasle重新  
  67.         //寻址,并把老的cache删掉  
  68.         else {  
  69.               deleteCachedLocation(tableName, row);  
  70.             }  
  71.   
  72.             // Query the root or meta region for the location of the meta region  
  73.         //发起RPC请求,获取<=该key的行  
  74.             regionInfoRow = server.getClosestRowBefore(  
  75.             metaLocation.getRegionInfo().getRegionName(), metaKey,  
  76.             HConstants.CATALOG_FAMILY);  
  77.           }  
  78.           if (regionInfoRow == null) {  
  79.             throw new TableNotFoundException(Bytes.toString(tableName));  
  80.           }  
  81.         //region信息,做校验,region会处于不稳定状态  
  82.           byte [] value = regionInfoRow.getValue(HConstants.CATALOG_FAMILY,  
  83.               HConstants.REGIONINFO_QUALIFIER);  
  84.           if (value == null || value.length == 0) {  
  85.             throw new IOException("HRegionInfo was null or empty in " +  
  86.               Bytes.toString(parentTable) + ", row=" + regionInfoRow);  
  87.           }  
  88.           // convert the row result into the HRegionLocation we need!  
  89.         //反序列化  
  90.           HRegionInfo regionInfo = (HRegionInfo) Writables.getWritable(  
  91.               value, new HRegionInfo());  
  92.           // possible we got a region of a different table...  
  93.         //一些校验  
  94.           if (!Bytes.equals(regionInfo.getTableName(), tableName)) {  
  95.             throw new TableNotFoundException(  
  96.                   "Table '" + Bytes.toString(tableName) + "' was not found, got: " +  
  97.                   Bytes.toString(regionInfo.getTableName()) + ".");  
  98.           }  
  99.           if (regionInfo.isSplit()) {  
  100.             throw new RegionOfflineException("the only available region for" +  
  101.               " the required row is a split parent," +  
  102.               " the daughters should be online soon: " +  
  103.               regionInfo.getRegionNameAsString());  
  104.           }  
  105.           if (regionInfo.isOffline()) {  
  106.             throw new RegionOfflineException("the region is offline, could" +  
  107.               " be caused by a disable table call: " +  
  108.               regionInfo.getRegionNameAsString());  
  109.           }  
  110.         //该region的server location  
  111.           value = regionInfoRow.getValue(HConstants.CATALOG_FAMILY,  
  112.               HConstants.SERVER_QUALIFIER);  
  113.           String hostAndPort = "";  
  114.           if (value != null) {  
  115.             hostAndPort = Bytes.toString(value);  
  116.           }  
  117.           ......  
  118.   
  119.           // Instantiate the location  
  120.           String hostname = Addressing.parseHostname(hostAndPort);  
  121.           int port = Addressing.parsePort(hostAndPort);  
  122.           location = new HRegionLocation(regionInfo, hostname, port);  
  123.         //cache之  
  124.           cacheLocation(tableName, location);  
  125.           return location;  
  126.         } catch (TableNotFoundException e) {  
  127.           // if we got this error, probably means the table just plain doesn't  
  128.           // exist. rethrow the error immediately. this should always be coming  
  129.           // from the HTable constructor.  
  130.           throw e;  
  131.         } catch (IOException e) {  
  132.           if (e instanceof RemoteException) {  
  133.             e = RemoteExceptionHandler.decodeRemoteException((RemoteException) e);  
  134.           }  
  135.           if (tries < numRetries - 1) {  
  136.             .......  
  137.           } else {  
  138.             throw e;  
  139.           }  
  140.           // Only relocate the parent region if necessary  
  141.         //网络有问题,则重新找  
  142.           if(!(e instanceof RegionOfflineException ||  
  143.               e instanceof NoServerForRegionException)) {  
  144.             relocateRegion(parentTable, metaKey);  
  145.           }  
  146.         }  
  147.         //重试次数越多,sleep越长,interrupt则退出重试  
  148.         try{  
  149.           Thread.sleep(ConnectionUtils.getPauseTime(this.pause, tries));  
  150.         } catch (InterruptedException e) {  
  151.           Thread.currentThread().interrupt();  
  152.           throw new IOException("Giving up trying to location region in " +  
  153.             "meta: thread is interrupted.");  
  154.         }  
  155.       }  
  156.     }  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

分布式查找过程[HBase]Region location 的相关文章

  • 云数据库MySQL的选择

    架构介绍 xff1a 云数据库MySQL支持四种架构 xff1a 高可用版 金融版 单节点高IO版 基础版 其中单节点高IO版的只用于只读 版本对比 xff1a 企业级别 xff0c 刚刚好公司没有自己的服务器机房的时候可以做对比选择 一般
  • 淘宝TDDL数据库分库分表

    淘宝TDDL数据库分库分表 2014 06 04 23 18 3334人阅读 评论 0 收藏 举报 分类 数据库 1 分库分表 而且分库规则非常灵活 2 主键生成策略 目前TDDL提供的id生成主要还是依托数据库来进行的 oracle可以直
  • Hadoop Core、HBase 、ZooKeeper

    adoop HBase ZooKeeper三者关系与安装配置 复制链接 qqjue 论坛徽章 18 电梯直达 1
  • Hypertable 快速安装,仅需上载一个RPM包,零编译

    Hypertable 快速安装 仅需上载一个RPM包 零编译 Hypertable 快速安装 仅需下载一个RPM包 零编译 本文采用 单机安装 1 Hypertable 安装 Hypertable 的几种安装方式 单机 安装于单机 采用本地
  • Hadoop 2.4.0+zookeeper3.4.6+hbase0.98.3分布式集群搭建

    Hadoop 2 4 0 zookeeper3 4 6 hbase0 98 3分布式集群搭建 博客分类 hadoop Ip 主机名 程序 进程 192 168 137 11 h1 Jdk Hadoop hbase Namenode DFSZ
  • 阿里云数据库配置IP白名单操作方法(以MySQL为例)

    阿里云数据库RDS创建成功后 首次连接访问RDS需要配置IP白名单 在阿里云RDS控制台即可配置IP白名单 阿里云百科来详细说下阿里云服务器RDS配置白名单的方法 阿里云服务器配置IP白名单 阿里云百科以MySQL云数据库为例 RDS My
  • Hbase split的三种方式和split的过程

    Hbase split的三种方式和split的过程 在Hbase中split是一个很重要的功能 Hbase是通过把数据分配到一定数量的region来达到负载均衡的 一个table会被分配到一个或多个region中 这些region会被分配到
  • Spanner vs. F1:谷歌两大数据管理利器的整体对比及关联 2016-05-22 20:36 757人阅读 评论(0) 收藏 举报 目录(?)[+] http://www.csdn.net/a

    Spanner vs F1 谷歌两大数据管理利器的整体对比及关联 2016 05 22 20 36 757人阅读 评论 0 收藏 举报 目录 http www csdn net article 2013 10 10 2817138 f1 a
  • 分布式系统设计的求生之路

    作者 作者 Simon 腾讯后台开发高级工程师 链接 http wetest qq com lab view id 105 著作权归作者所有 商业转载请联系WeTest获得授权 非商业转载请注明出处 分布式系统理念渐渐成为了后台架构技术的重
  • 分布式数据库资料

    Hadoop是很多组件的集合 主要包括但不限于MapReduce HDFS HBase ZooKeeper MapReduce模仿了Google MapReduce HDFS模仿了Google File System HBase模仿了Goo
  • Hash算法的使用

    Hash算法的使用 标签 默认分类 发表时间 2011 08 06 06 35 作者 GliderX khsing 分享到 出处 http hi baidu com gliderx 在对语料文本进行2 3元切分时 需要借助hash表来获得切
  • 分布式查找过程[HBase]Region location

    HBase的table是该region切分的 client操作一个row的时候 如何知道这个row对应的region是在哪台Region server上呢 这里有个region location过程 主要涉及到2张系统表 ROOT META
  • 一、MapReduce已死,Spark称霸

    一 MapReduce已死 Spark称霸 2014 09 17 11 20 王家林 Spark亚太研究院 字号 T T 综合评级 想读 35 在读 13 已读 2 品书斋鉴 0 已有50人发表书评 Spark亚太研究院系列丛书 Spark
  • Hypertable sql

    First create a new namespace called Test CREATE NAMESPACE Test and make it the current namespace USE Test Now let s crea
  • HBase介绍(列存储)

    HBase介绍 列存储 2013 11 26 23 25 5871人阅读 评论 2 收藏 举报 分类 云存储 2 Hbase简介 started by chad walters and jim 2006 11 G release paper
  • Hypertable 简介 一个 C++ 的Bigtable开源实现

    1 Introduction 随着互联网技术的发展 尤其是云计算平台的出现 分布式应用程序需要处理大量的数据 PB级 在一个或多个云计算平台中 成千上万的计算主机 如何保证数据的有效存储和组织 为应用提供高效和可靠的访问接口 并且保持良好的
  • 分布式数据库需要考虑的(BigTable VS Dynamo)

    分布式数据库需要考虑的 BigTable VS Dynamo 在设计 评价分布式数据库的时候需要考虑一些最基本的特性 我想这些特性可能包括 1 存储系统 一种是类似BigTable将存储交给GFS去做 GFS会保证写入数据的完整 另外一种是
  • 1.1.3 Hadoop生态系统

    1 1 3 Hadoop生态系统 2013 05 08 09 38 16 我来说两句 收藏 我要投稿 本文所属图书 gt Hadoop技术内幕 深入解析Hadoop Common和HDFS架构设计与实现原理 Hadoop技术内幕共两册 分别
  • 云数据库知识学习——概述

    一 云计算是云数据库兴起的基础 云计算是分布式计算 并行计算 效用计算 网络存储 虚拟化 负载均衡等计算机和网络技术发展融合的产物 云计算是由一系列可以动态升级和被虚拟化的资源组成的 用户无需掌握云计算的技术 只要通过网络就可以访问这些资源
  • hadoop使用(五)

    博客园 闪存 首页 新随笔 联系 管理 订阅 随笔 247 文章 122 评论 571 hadoop使用 五 第1章 引言 1 1 编写目的 对关于hadoop的文档及资料进行进一步的整理 1 2 相关网站 毋庸置疑 http hadoop

随机推荐

  • 页面性能优化,如何减少回流

    在开发时 不可避免的会遇到性能优化的问题 怎么做性能才会更好 说到页面性能优化 我们就谈谈两个概念重绘和回流 1 什么是重绘 什么是回流 重绘 当渲染树中的一些元素需要更新属性 而这些属性只是影响元素的外观 风格 而不会影响布局的操作 比如
  • windows搭建WEB打印机

    文章目录 Web Print 添加一台虚拟打印机 名称为 CS Print 发布到AD域 客户端们都能够通过访问 https print www chinaskills com 查看打印机 证书由CSK2021 ROOTCA进行签署颁发 1
  • skywalking和jpa冲突

    1 报错 org springframework security authentication InternalAuthenticationServiceException No MethodInvocation found Check
  • 【C语言】通讯录的动态存储版本

    目录 一 前言 二 为什么要动态存储 1 动态存储的作用 2动态与静态存储的区别 三 动态存储的实现 1 通讯录容量 2 初始化通讯录 3 增加 减少通讯录成员 增加通讯录成员 判断及实现扩容函数的实现 减少通讯录成员 判断及实现减容函数的
  • iOS开发常见错误代码对照表---真机调试常见错误及解决方案

    iOS真机调试常见错误及解决方案 地址https developer apple com library ios technotes tn2250 index html apple ref doc uid DTS40009933 CH1 T
  • 11下滑半个屏幕_看完小米11发布会,2万粉购买小米,雷军给苹果的致命一击

    2020年12月28日 小米11发布会正式召开 这次的小米很聪明很聪慧 让我们看到了对用户满满的诚意 首先就是跑分 对于大部分用户而言 手机的跑分就代表着手机的性能 而小米搭配的高通骁龙888芯片 就让我们有了全新的认知 最终Antutu综
  • 依赖注入的几种方式

    获取bean对象 也称为对象装配 对象注入 依赖注入 对象装配的实现方法有3种 1 属性注入 2 构造方法注入 3 Setter注入 再讲本节内容之前 我们先来提两个传参的方式 首先呢 上节的文章里边 我们提到了五大类注解和 Bean注解
  • STM32F407ZGTE6利用模拟PWM驱动42步进电机(与pwm驱动led闪烁一样)

    前言 lmf老师来帮我解决42步进电机 预转不转 的问题 利用引脚模拟pwm波形驱动42步进电机 成功找到原因 还顺便给我留下了另一种驱动思路 直接引脚驱动 解决问题 42电机原地不动的原因是 线接触不良 采用杜邦线拼接 拟解决方案 重新换
  • C++程序设计期末考试抱佛脚

    大一上的 今早的计算机概论压中一道大题 我惊呆了 先放 点我看学友的复习总结 if嵌套配对 书p45 内嵌平衡语句 if if else else if else else总是与写在它前面的 最靠近的 尚未与其他else配对的if配对 其他
  • 栈破坏下crash的分析方法

    在众多的coredump中 有一类crash调试起来是最麻烦的 那就是 栈被破坏 导致的函数调用回溯结构破坏引发的coredump 本文 主要讲讲这一类crash的成因 原理以及调试方法 1 SMTC show me the code 首先
  • 数据包络分析--保证域方法(assurance region method)附python代码以及案例

    Data envelopment analysis Assurance region method 保证域方法 Data envelopment analysis Assurance region method model AR 有效 py
  • ARIMA时间序列预测MATLAB代码模板(无需调试)

    小白专用 直接改成自己的数据运行即可完成预测并画图 我的数据在评论区自取 clear clc 小白专用 需要自己输入 仅在有这种注释的地方改成自己的数据即可 一共有4个地方 DD readmatrix B xlsx 这里输入自己的单序列数据
  • 云服务的应用场景,为什么要用云服务(学习心得)

    降低IT支出成本 云服务器无需硬件设施及机房 兼具优质扩容能力 门户网站 社区网站 电商网站 免除硬件配置与升级的忧虑 专注于服务功能的开发 大大减少网站IT基础设施成本 及运维成本 弹性扩展云服务器 当服务器需求不固定 预先估计不准确时
  • 基于动态规划的强化学习方法

    quad quad 基于动态规划的强化学习方法 quad 动态规划 dynamic programming 是程序设计算法中非常重要的内容 能够高效解决一些经典问题
  • VScode搭建Vue环境(2)

    VScode搭建Vue环境 2 目录 1 全局安装下载vue cli 2 创建项目 1 命令行创建 2 GUI创建 3 如果无法在终端打开 请看下方图片 方法1 方法2 在上一篇文章中我们下载安装并配置好了nodejs 并可以在全局环境中下
  • jQuery的Ajax实例(附完整代码)

    目录 写在前边 什么是Ajax Ajax基本结构 实例 实例1 实例2 小结 写在前边 作为一个前端刚入门没多久的小白 想在这里分享一下我的学习内容 就算是学习笔记了 因为前端的大部分学习都是通过网站上的教程 所以遇到不懂得问题 也只有求助
  • linux下保护模式之CPL,RPL,DPL总结

    linux下保护模式之CPL RPL DPL总结 先说下特权级的概念 在保护模式下 系统依靠特权级来实施代码和数据的保护 相当于权限啦 特权级共有4 个级别 0 1 2 3 数字越小特权越高 较为核心的代码和数据放在较高 靠内 的层级中 处
  • RabbitMQ-安装与配置-linux

    1 安装RabbitMQ 安装方式一 下载rabbitmq镜像 3 management为带管理界面的rabbitmq 启动rabbitmq镜像 5672端口是客户端和容器的通信端口 15672端口是web管理界面的通信端口 访问并登录we
  • Python批量插入数据

    Python批量插入数据到mysql中 相较于jdbc 简化了长度 占位符使用的 s而不是 import mysql connector as conn def getConn db conn connect host localhost
  • 分布式查找过程[HBase]Region location

    HBase的table是该region切分的 client操作一个row的时候 如何知道这个row对应的region是在哪台Region server上呢 这里有个region location过程 主要涉及到2张系统表 ROOT META