hbase 0.98.9客户端的两个参数调优

来源:互联网 发布:淘宝模板市场 编辑:程序博客网 时间:2024/05/17 02:02

公司的项目有用到hbase数据库,而我正好负责hbase客户端的接口代码编写工作;实际就是为hbase中的各个表,提供增,删,改,查的功能。
前段时间,同事对接口进行测试时,跟我反馈:在使用visualVM在查看线程运行状态时,发现hbase客户端的线程很多,具体干什么不清楚,但其中很多线程处于等待状态。起初,没时间就没在意。这段时间,功能差不多了,就也用visualvm来查看线程状态。
一,使用visualvm来查看线程状态
这里写图片描述
从图片可以看到,有256个名称为“hconnection-0x14ba9a2-shared-pool12-tX”的线程;其中很多线程是处于等待状态,只有位为数不多的线程在执行客户端对hbase数据库的操作。这里应该是可以进行优化的。
先说一下,我们项目大概是如何使用hbase客户端的。代码类似如下:

//表名String tableName = "TableName";//创建配置对象Configuration c = new Configuration();Configuration hbaseConfiguration = HBaseConfiguration.create(c);//创建连接HConnection hbaseConnection = HConnectionManager.createConnection(hbaseConfiguration);//获取某个表的HTableInterfaceHTableInterface table = hbaseConnection.getTable(tableName);//do something heretable.close();

从代码中,可以看到并没有涉及到以上的线程数配置。只好从源代码上,查看有没有相关的配置。
关键的代码在类HConnectionImplementation:

//HConnectionImplementation获取单个表的HTableInterfacepublic HTableInterface getTable(String tableName) throws IOException {      return getTable(TableName.valueOf(tableName));    }//实际调用了public HTableInterface getTable(TableName tableName) throws IOException {      return getTable(tableName, getBatchPool());    }//在方法getBatchPool()中会创建一个线程池private ExecutorService getBatchPool() {      if (batchPool == null) {        // shared HTable thread executor not yet initialized        synchronized (this) {          if (batchPool == null) {            int maxThreads = conf.getInt("hbase.hconnection.threads.max", 256);            int coreThreads = conf.getInt("hbase.hconnection.threads.core", 256);            if (maxThreads == 0) {              maxThreads = Runtime.getRuntime().availableProcessors() * 8;            }            if (coreThreads == 0) {              coreThreads = Runtime.getRuntime().availableProcessors() * 8;            }            long keepAliveTime = conf.getLong("hbase.hconnection.threads.keepalivetime", 60);            LinkedBlockingQueue<Runnable> workQueue =              new LinkedBlockingQueue<Runnable>(maxThreads *                conf.getInt(HConstants.HBASE_CLIENT_MAX_TOTAL_TASKS,                  HConstants.DEFAULT_HBASE_CLIENT_MAX_TOTAL_TASKS));            ThreadPoolExecutor tpe = new ThreadPoolExecutor(                coreThreads,                maxThreads,                keepAliveTime,                TimeUnit.SECONDS,                workQueue,                Threads.newDaemonThreadFactory(toString() + "-shared-"));            tpe.allowCoreThreadTimeOut(true);            this.batchPool = tpe;          }          this.cleanupPool = true;        }      }      return this.batchPool;    }

从以上代码可以看到,在创建一个线程池时,用到了三个关键配置项,分别是:
1.hbase.hconnection.threads.max 线程池维护线程的最大数量
2.hbase.hconnection.threads.core 线程池维护线程的最少数量
3.hbase.hconnection.threads.keepalivetime 线程池维护线程所允许的空闲时间
因此,我们就可以按照需要来配置了。修改后的代码如下:

“`
String tableName = “TableName”;
Configuration c = new Configuration();
Configuration hbaseConfiguration = HBaseConfiguration.create(c);
//在这里,我们重新hbase客户端的线程池变量
hbaseConfiguration.setInt(“hbase.hconnection.threads.max”, 512);
//最大数量改为512
hbaseConfiguration.setInt(“hbase.hconnection.threads.core”, 32);
//最少数量改为32
hbaseConfiguration.setLong(“hbase.hconnection.threads.keepalivetime”, 300);
//空闲时间改为300秒
hbaseConnection = HConnectionManager.createConnection(hbaseConfiguration);
HTableInterface table = hbaseConnection.getTable(tableName);

//do something here

table.close();
`前面两个

通过调节以上参数后,再次运行测试用例,结果如下:
这里写图片描述
可以看,确实只有32个线程在运行了。
 

0 0