HBase Compaction详解
HBase Compaction策略
RegionServer这种类LSM存储引擎需要不断的进行Compaction来减少磁盘上数据文件的个数和删除无用的数据从而保证读性能。
RegionServer后台有一组负责flush region的线程(MemStoreFlusher),每次从flushQueue中拿出一个flush region请求,会检查这个region是否有某个store包含的storefile个数超过配置
hbase.hstore.blockingStoreFiles,默认7,如果超过,说明storefile个数已经到了会影响读性能的地步,那么就看这个flush region
请求是否已经有blockingWaitTime(hbase.hstore.blockingWaitTime,默认90s)没有执行了,如果是,这时候需要立即执行flush region,为了防止OOM。如果没有超过blockingWaitTime,那么先
看看region是否需要分裂,如果不需要,则向后台的CompactionSplitThread请求做一次
Compaction(从这里可以看出,split优先级比compaction高),然后重新将这个flush region加入
flushQueue,延后做flush.
重点看Compaction
Compaction以store为单位,
CompactSplitThread会为region的每个store生成一个CompactionRequest.
一个Compaction根据它包含的storefile的总大小,可以分为
large compaction和small compaction,这两种compaction分别被两个不同的线程池处理。
系统一般认为small compaction占大多数,所以上文中由于storefile过多系统自动触发的system compaction 默认放入small compaction池子中处理.
//系统自动触发的system compaction,selectNow参数为false,实际选取待compact的
filelist过程延后在CompactionRunner中做.
if (selectNow) {
// 通过hbase shell触发的major compaction,selectNow为true.这里进行实际的选取待compact filelist操作
compaction = selectCompaction(r, s, priority, request);
if (compaction == null) return null; // message logged inside
}
// We assume that most compactions are small. So, put system compactions
//into small pool; we will do selection there, and move to large pool if //necessary.
long size = selectNow ? compaction.getRequest().getSize() : 0;
// 从这里可以看出,用户外部触发的compaction默认放入small compaction线程池中处理,并且
// system compaction 也会放入small compaction线程池中,后续真正执行
// system compaction时,会根据选出的storefile的总大小来决定最终放入large还是small线程池
ThreadPoolExecutor pool = (!selectNow && s.throttleCompaction(size))? largeCompactions : smallCompactions;
pool.execute(new CompactionRunner(s, r, compaction, pool));
看看执行compaction过程的CompactionRunner任务。
// Common case - system compaction without a file selection. Select now.
// system compaction 还没有选择待compact的filelist,为null
if (this.compaction == null) {
int oldPriority = this.queuedPriority;
this.queuedPriority = this.store.getCompactPriority();
if (this.queuedPriority > oldPriority) {
// Store priority decreased while we were in queue (due to some other compaction?),
// requeue with new priority to avoid blocking potential higher priorities.
this.parent.execute(this);
return;
}
try {
// 选择storefile
this.compaction = selectCompaction(this.region, this.store, queuedPriority, null);
} catch (IOException ex) {
LOG.error("Compaction selection failed " + this, ex);
server.checkFileSystem();
return;
}
if (this.compaction == null) return; // nothing to do
// Now see if we are in correct pool for the size; if not, go to the correct one.
// We might end up waiting for a while, so cancel the selection.
assert this.compaction.hasSelection();
// 判断这次compaction放入small还是large池中执行
ThreadPoolExecutor pool = store.throttleCompaction(
compaction.getRequest().getSize()) ? largeCompactions : smallCompactions;
// system compaction应该放入large池
if (this.parent != pool) {
this.store.cancelRequestedCompaction(this.compaction);
this.compaction = null;
this.parent = pool;
// 在large池子中执行
this.parent.execute(this);
return;
}
}
large compaction和small compaction分界线由
hbase.regionserver.thread.compaction.throttle参数决定,如果没有设置,
默认为2 * hbase.hstore.compaction.max * hbase.hregion.memstore.flush.size
全部取默认值等于2*10*128MB = 2.5GB
从以上可以看出,system compaction默认放入small池,当选出storefile list
后,再根据size去判断最终放入small还是large线程池中执行.
对于外部触发的compaction,放入small中执行.
选定池子后,下面看每个store compaction具体的步骤
两个步骤:
- 根据某种策略生成compact的目标storefile集合
- 进行compaction
这两步都在CompactionRunner这个runnable任务中完成。
这里主要说第一个步骤:入口在HStore::requestCompaction.
首先创建storeEngine相应的CompactionContext,这个context用来存各种compact相关的信息,
最重要的就是CompactionRequest,作为上面第二个步骤的输入. HBase 0.98主要有两种存储引擎,DefaultStoreEngine和StripeStoreEngine,这里的存储引擎主要是管理磁盘上的storefile文件和flush 内存中的snapshot memstore到磁盘。StripStoreEngine比较特别,
一个snapshot memstore刷到磁盘上有可能多于一个storefile文件,这里不讨论.大部分人都使用默认的storeEngine.
其次,创建完context后,然后调用compactionPolicy的selectCompaction(),将store下面的所有storefile传进去,供其挑选.HBase的compaction policy可通过
配置项hbase.hstore.defaultengine.compactionpolicy.class配置,默认是
ExploringCompactionPolicy.class
下面看selectCompaction(),主要有几个步骤:
从store下面的storefiles中过滤掉比正在compacting的storefilelist中最新的storefile更老的storefile(输入的storefile按照如下规则排序)
public static final Comparator<StoreFile> SEQ_ID =
Ordering.compound(ImmutableList.of(
Ordering.natural().onResultOf(new GetSeqId()),
Ordering.natural().onResultOf(new GetFileSize()).reverse(),
Ordering.natural().onResultOf(new GetBulkTime()),
Ordering.natural().onResultOf(new GetPathName())
));
seq id是storefile对应的snapshot memstore在flush时,从region内部的全局递增计
数器sequenceId中取到的,可以看到,seq id越大的storefile越新.对多个文件进行compact后产生的新的storefile的seq id被设置为多个文件中最大的seq id
- 如果不是major compaction,就检查:如果配置了删除ttl到期的storefile,并且ttl是一个有限的值,那么这次compaction只会选ttl到期的storefile,如果目前确实存在ttl过期的storefile,则这次compaction选取的文件列表就是这些过期的storefile,选取文件流程结束,CompactionRequest产生。如果没有配置,则过滤掉文件大小大于配置值
hbase.hstore.compaction.max.size(默认是 Long.MAX_VALUE)的storefile(实际上,这块实现有问题) - 根据一些规则和参数,判断是否升级为major compaction,比较烦,直接贴代码
// Force a major compaction if this is a user-requested major compaction,
// or if we do not have too many files to compact and this was requested
// as a major compaction.
// Or, if there are any references among the candidates.
boolean majorCompaction = (
(forceMajor && isUserCompaction)
|| ((forceMajor || isMajorCompaction(candidateSelection))
&& (candidateSelection.size() < comConf.getMaxFilesToCompact()))
|| StoreUtils.hasReferences(candidateSelection)
);
- 如果不是,那么这次compaction是一个minor compaction,做以下几件事
- 过滤掉bulk load的storefile
- 应用ExploringCompactionPolicy重写的applyCompactionPolicy方法,挑选
storefile的思想是:枚举所有的n个连续文件,n位于[hbase.hstore.compaction.min(默认3), hbase.hstore.compaction.max(默认10)]之间, 这是对于minor compaction的文件个数的限制。并且n个连续的文件大小总和不能超过hbase.hstore.compaction.max.size(默认MAX_VALUE),并且n个文件的大小之间的"方差"不能太大. 最后选出n个文件,选择的原则是:删除最多的文件同时这些文件的
大小总和小(消耗更少的磁盘IO)
- 检查是否选出来的storefile个数超过hbase.hstore.compaction.max,如果超过,并且
这只是minor compaction,则从storefile文件集合尾部将多余的storefile过滤掉,如果超过但是是major compaction并且是用户发起的,则不过滤.至此,这次compact的storefile集合产生,结束。
至此,第一个步骤结束,compact的目标storefile选出.
参考资料
https://github.com/apache/hbase/tree/0.98
HBase Compaction详解的更多相关文章
- Mac下安装HBase及详解
Mac下安装HBase及详解 1. 千篇一律的HBase简介 HBase是Hadoop的数据库, 而Hive数据库的管理工具, HBase具有分布式, 可扩展及面向列存储的特点(基于谷歌BigTabl ...
- Hbase存储详解
转自:http://my.oschina.net/mkh/blog/349866 Hbase存储详解 started by chad walters and jim 2006.11 G release ...
- 大数据学习系列之五 ----- Hive整合HBase图文详解
引言 在上一篇 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机) 和之前的大数据学习系列之二 ----- HBase环境搭建(单机) 中成功搭建了Hive和HBase的环 ...
- HUE配置文件hue.ini 的hbase模块详解(图文详解)(分HA集群和非HA集群)
不多说,直接上干货! 我的集群机器情况是 bigdatamaster(192.168.80.10).bigdataslave1(192.168.80.11)和bigdataslave2(192.168 ...
- HBase配置项详解
hbase.tmp.dir:本地文件系统的临时目录,默认是java.io.tmpdir/hbase−java.io.tmpdir/hbase−{user.name}: hbase.rootdir:hb ...
- HBase——强一致性详解
Hbase是一个强一致性数据库,不是“最终一致性”数据库,官网给出的介绍: “Strongly consistent reads/writes: HBase is not an "event ...
- hbase配置详解(转)
转自:http://www.cnblogs.com/viviman/archive/2013/03/21/2973539.html 1 准备工作 因为我只有一台机器,所以,一切都成为了伪分布,但是,其 ...
- Sqoop import加载HBase案例详解
简单写一下如何将订单表sqoop到hbase表中的步骤. 下表: 1.通过hbase shell 打开hbase. 2.创建一个hbase表 create 'so','o' 3.将so表的数据导入到h ...
- HBase API详解
一.Java API和HBase数据模型的关系 在Java中,与HBase数据库存储管理相关的类包括HBaseAdmin.HBaseConfiguration.HTable.HTableDescrip ...
随机推荐
- leetcode--539. Minimum Time Difference
Given a list of -hour clock time points in "Hour:Minutes" format, find the minimum minutes ...
- springboot-5-整合jpa
######## ##springboot-parent.version: ## jdk 1.8 ## ####### 在整合jpa之前, 先说下mysql 步骤: 1), 在application. ...
- C语言利用异或进行两个值的交换
异或有两个很重要的性质: 1. A^A = 0; 2.A^0 = A; 利用这两个性质,我们就能够利用异或进行两个值的交换. 代码如下: #include <stdio.h> int ma ...
- 字符的二进制,php的pack与unpack
$curl = curl_init (); curl_setopt($curl, CURLOPT_URL , 'http://mh.18touch.com/restful/magic'); curl_ ...
- Lucene系列-facet--转
https://blog.csdn.net/whuqin/article/details/42524825 1.facet的直观认识 facet:面.切面.方面.个人理解就是维度,在满足query的前 ...
- 尝试用selenium+appium运行一个简单的demo报错:could not get xcode version. /Library/Developer/Info.plist doest not exist on disk
业余时间抽空搭了个appium+selenium的环境(mac), 在执行第一个脚本的时候遇到个问题纪录下: could not get xcode version. /Library/Develop ...
- Hive集成HBase实践
#step1: create hive table 't_test' hive -e "create table test.t_user(id int,name string,age int ...
- hadoop2.x学习笔记(一):YARN
一.YARN产生的背景 MapReduce1.x存在的问题:单点故障&节点压力大不易扩展. 资源利用率&成本 催生了YARN的诞生 不同计算框架可以共享同一个HDFS集群上的数据,享 ...
- Day1:了解APICloud平台、理解APICloud应用设计思想、掌握平台使用流程。学习如何对一款APP进行需求分析、功能分解和架构设计等编码之前重要的准备工作
学习目标 总体上了解一下APICloud平台,重点介绍相关的学习资源,入门资料,常见的FAQ等 明确我们这七天要开发一个什么样的APP,明确功能需求,跟上每天的课程节奏,可以课前预习 梳理出对于一款A ...
- HttpContext概念讲解
一:HttpContext理论知识: 1:HttpContext类它对Request.Respose.Server等等都进行了封装,并保证在整个请求周期内都可以随时随地的调用:为继承 IHttpMod ...