You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by li...@apache.org on 2016/06/01 07:35:07 UTC
kylin git commit: KYLIN-1753 Use dedicated thread pool for
coprocessor call
Repository: kylin
Updated Branches:
refs/heads/master 669916eff -> d6bb49984
KYLIN-1753 Use dedicated thread pool for coprocessor call
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/d6bb4998
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/d6bb4998
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/d6bb4998
Branch: refs/heads/master
Commit: d6bb49984b59e07b3944d622a560e0d026b80bb2
Parents: 669916e
Author: Li Yang <li...@apache.org>
Authored: Wed Jun 1 15:34:28 2016 +0800
Committer: Li Yang <li...@apache.org>
Committed: Wed Jun 1 15:34:52 2016 +0800
----------------------------------------------------------------------
.../apache/kylin/common/KylinConfigBase.java | 4 +-
.../kylin/storage/hbase/HBaseConnection.java | 523 ++++++++++---------
.../hbase/cube/v2/CubeHBaseEndpointRPC.java | 5 +-
3 files changed, 289 insertions(+), 243 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kylin/blob/d6bb4998/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java
----------------------------------------------------------------------
diff --git a/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java b/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java
index a75688b..faf54d2 100644
--- a/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java
+++ b/core-common/src/main/java/org/apache/kylin/common/KylinConfigBase.java
@@ -755,8 +755,8 @@ abstract public class KylinConfigBase implements Serializable {
return getPropertiesByPrefix("kylin.query.udf.");
}
- public String getHBaseMaxConnectionThreads() {
- return getOptional("kylin.query.hbase.hconnection.threads.max", "3072");
+ public int getHBaseMaxConnectionThreads() {
+ return Integer.parseInt(getOptional("kylin.query.hbase.hconnection.threads.max", "3072"));
}
public long getStorageCleanupTimeThreshold() {
http://git-wip-us.apache.org/repos/asf/kylin/blob/d6bb4998/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/HBaseConnection.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/HBaseConnection.java b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/HBaseConnection.java
index bf5d5f6..09c14e1 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/HBaseConnection.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/HBaseConnection.java
@@ -1,238 +1,285 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
-*/
-
-package org.apache.kylin.storage.hbase;
-
-import java.io.IOException;
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.apache.commons.lang.StringUtils;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.hbase.HBaseConfiguration;
-import org.apache.hadoop.hbase.HColumnDescriptor;
-import org.apache.hadoop.hbase.HConstants;
-import org.apache.hadoop.hbase.HTableDescriptor;
-import org.apache.hadoop.hbase.TableName;
-import org.apache.hadoop.hbase.client.HBaseAdmin;
-import org.apache.hadoop.hbase.client.HConnection;
-import org.apache.hadoop.hbase.client.HConnectionManager;
-import org.apache.kylin.common.KylinConfig;
-import org.apache.kylin.common.persistence.StorageException;
-import org.apache.kylin.engine.mr.HadoopUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @author yangli9
- *
- */
-public class HBaseConnection {
-
- public static final String HTABLE_UUID_TAG = "UUID";
-
- private static final Logger logger = LoggerFactory.getLogger(HBaseConnection.class);
-
- private static final Map<String, Configuration> ConfigCache = new ConcurrentHashMap<String, Configuration>();
- private static final Map<String, HConnection> ConnPool = new ConcurrentHashMap<String, HConnection>();
-
- static {
- Runtime.getRuntime().addShutdownHook(new Thread() {
- @Override
- public void run() {
- for (HConnection conn : ConnPool.values()) {
- try {
- conn.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- });
- }
-
- public static void clearConnCache() {
- ConnPool.clear();
- }
-
- private static final ThreadLocal<Configuration> hbaseConfig = new ThreadLocal<>();
-
- public static Configuration getCurrentHBaseConfiguration() {
- if (hbaseConfig.get() == null) {
- String storageUrl = KylinConfig.getInstanceFromEnv().getStorageUrl();
- hbaseConfig.set(newHBaseConfiguration(storageUrl));
- }
- return hbaseConfig.get();
- }
-
- private static Configuration newHBaseConfiguration(String url) {
- Configuration conf = HBaseConfiguration.create(HadoopUtil.getCurrentConfiguration());
-
- // using a hbase:xxx URL is deprecated, instead hbase config is always loaded from hbase-site.xml in classpath
- if (!(StringUtils.isEmpty(url) || "hbase".equals(url)))
- throw new IllegalArgumentException("to use hbase storage, pls set 'kylin.storage.url=hbase' in kylin.properties");
-
- // support hbase using a different FS
- String hbaseClusterFs = KylinConfig.getInstanceFromEnv().getHBaseClusterFs();
- if (StringUtils.isNotEmpty(hbaseClusterFs)) {
- conf.set(FileSystem.FS_DEFAULT_NAME_KEY, hbaseClusterFs);
- }
-
- // https://issues.apache.org/jira/browse/KYLIN-953
- if (StringUtils.isBlank(conf.get("hadoop.tmp.dir"))) {
- conf.set("hadoop.tmp.dir", "/tmp");
- }
- if (StringUtils.isBlank(conf.get("hbase.fs.tmp.dir"))) {
- conf.set("hbase.fs.tmp.dir", "/tmp");
- }
-
- // reduce rpc retry
- conf.set(HConstants.HBASE_CLIENT_PAUSE, "3000");
- conf.set(HConstants.HBASE_CLIENT_RETRIES_NUMBER, "5");
- conf.set(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, "60000");
- // conf.set(ScannerCallable.LOG_SCANNER_ACTIVITY, "true");
-
- // set hbase.hconnection.threads.max to large to unblock region server requests from client threadpool.
- conf.set("hbase.hconnection.threads.max", KylinConfig.getInstanceFromEnv().getHBaseMaxConnectionThreads());
-
- return conf;
- }
-
- public static String makeQualifiedPathInHBaseCluster(String path) {
- try {
- FileSystem fs = FileSystem.get(getCurrentHBaseConfiguration());
- return fs.makeQualified(new Path(path)).toString();
- } catch (IOException e) {
- throw new IllegalArgumentException("Cannot create FileSystem from current hbase cluster conf", e);
- }
- }
-
- // ============================================================================
-
- // returned HConnection can be shared by multiple threads and does not require close()
- @SuppressWarnings("resource")
- public static HConnection get(String url) {
- // find configuration
- Configuration conf = ConfigCache.get(url);
- if (conf == null) {
- conf = newHBaseConfiguration(url);
- ConfigCache.put(url, conf);
- }
-
- HConnection connection = ConnPool.get(url);
- try {
- while (true) {
- // I don't use DCL since recreate a connection is not a big issue.
- if (connection == null || connection.isClosed()) {
- logger.info("connection is null or closed, creating a new one");
- connection = HConnectionManager.createConnection(conf);
- ConnPool.put(url, connection);
- }
-
- if (connection == null || connection.isClosed()) {
- Thread.sleep(10000);// wait a while and retry
- } else {
- break;
- }
- }
-
- } catch (Throwable t) {
- logger.error("Error when open connection " + url, t);
- throw new StorageException("Error when open connection " + url, t);
- }
-
- return connection;
- }
-
- public static boolean tableExists(HConnection conn, String tableName) throws IOException {
- HBaseAdmin hbase = new HBaseAdmin(conn);
- try {
- return hbase.tableExists(TableName.valueOf(tableName));
- } finally {
- hbase.close();
- }
- }
-
- public static boolean tableExists(String hbaseUrl, String tableName) throws IOException {
- return tableExists(HBaseConnection.get(hbaseUrl), tableName);
- }
-
- public static void createHTableIfNeeded(String hbaseUrl, String tableName, String... families) throws IOException {
- createHTableIfNeeded(HBaseConnection.get(hbaseUrl), tableName, families);
- }
-
- public static void deleteTable(String hbaseUrl, String tableName) throws IOException {
- deleteTable(HBaseConnection.get(hbaseUrl), tableName);
- }
-
- public static void createHTableIfNeeded(HConnection conn, String tableName, String... families) throws IOException {
- HBaseAdmin hbase = new HBaseAdmin(conn);
-
- try {
- if (tableExists(conn, tableName)) {
- logger.debug("HTable '" + tableName + "' already exists");
- return;
- }
-
- logger.debug("Creating HTable '" + tableName + "'");
-
- HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
-
- if (null != families && families.length > 0) {
- for (String family : families) {
- HColumnDescriptor fd = new HColumnDescriptor(family);
- fd.setInMemory(true); // metadata tables are best in memory
- desc.addFamily(fd);
- }
- }
-
- desc.setValue(HTABLE_UUID_TAG, UUID.randomUUID().toString());
- hbase.createTable(desc);
-
- logger.debug("HTable '" + tableName + "' created");
- } finally {
- hbase.close();
- }
- }
-
- public static void deleteTable(HConnection conn, String tableName) throws IOException {
- HBaseAdmin hbase = new HBaseAdmin(conn);
-
- try {
- if (!tableExists(conn, tableName)) {
- logger.debug("HTable '" + tableName + "' does not exists");
- return;
- }
-
- logger.debug("delete HTable '" + tableName + "'");
-
- if (hbase.isTableEnabled(tableName)) {
- hbase.disableTable(tableName);
- }
- hbase.deleteTable(tableName);
-
- logger.debug("HTable '" + tableName + "' deleted");
- } finally {
- hbase.close();
- }
- }
-
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+
+package org.apache.kylin.storage.hbase;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.client.HConnection;
+import org.apache.hadoop.hbase.client.HConnectionManager;
+import org.apache.hadoop.hbase.util.Threads;
+import org.apache.kylin.common.KylinConfig;
+import org.apache.kylin.common.persistence.StorageException;
+import org.apache.kylin.engine.mr.HadoopUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author yangli9
+ *
+ */
+public class HBaseConnection {
+
+ public static final String HTABLE_UUID_TAG = "UUID";
+
+ private static final Logger logger = LoggerFactory.getLogger(HBaseConnection.class);
+
+ private static final Map<String, Configuration> configCache = new ConcurrentHashMap<String, Configuration>();
+ private static final Map<String, HConnection> connPool = new ConcurrentHashMap<String, HConnection>();
+ private static final ThreadLocal<Configuration> configThreadLocal = new ThreadLocal<>();
+
+ private static ExecutorService coprocessorPool = null;
+
+ static {
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ closeCoprocessorPool();
+
+ for (HConnection conn : connPool.values()) {
+ try {
+ conn.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ });
+ }
+
+ public static ExecutorService getCoprocessorPool() {
+ if (coprocessorPool != null) {
+ return coprocessorPool;
+ }
+
+ synchronized (HBaseConnection.class) {
+ if (coprocessorPool != null) {
+ return coprocessorPool;
+ }
+
+ KylinConfig config = KylinConfig.getInstanceFromEnv();
+
+ // copy from HConnectionImplementation.getBatchPool()
+ int maxThreads = config.getHBaseMaxConnectionThreads();
+ int coreThreads = 256;
+ long keepAliveTime = 60;
+ LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(maxThreads * 100);
+ ThreadPoolExecutor tpe = new ThreadPoolExecutor(coreThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS, workQueue, //
+ Threads.newDaemonThreadFactory("kylin-coproc-"));
+ tpe.allowCoreThreadTimeOut(true);
+
+ logger.info("Creating coprocessor thread pool with max of " + maxThreads);
+
+ coprocessorPool = tpe;
+ return coprocessorPool;
+ }
+ }
+
+ private static void closeCoprocessorPool() {
+ if (coprocessorPool == null)
+ return;
+
+ coprocessorPool.shutdown();
+ try {
+ if (!coprocessorPool.awaitTermination(10, TimeUnit.SECONDS)) {
+ coprocessorPool.shutdownNow();
+ }
+ } catch (InterruptedException e) {
+ coprocessorPool.shutdownNow();
+ }
+ }
+
+ public static void clearConnCache() {
+ connPool.clear();
+ }
+
+ public static Configuration getCurrentHBaseConfiguration() {
+ if (configThreadLocal.get() == null) {
+ String storageUrl = KylinConfig.getInstanceFromEnv().getStorageUrl();
+ configThreadLocal.set(newHBaseConfiguration(storageUrl));
+ }
+ return configThreadLocal.get();
+ }
+
+ private static Configuration newHBaseConfiguration(String url) {
+ Configuration conf = HBaseConfiguration.create(HadoopUtil.getCurrentConfiguration());
+
+ // using a hbase:xxx URL is deprecated, instead hbase config is always loaded from hbase-site.xml in classpath
+ if (!(StringUtils.isEmpty(url) || "hbase".equals(url)))
+ throw new IllegalArgumentException("to use hbase storage, pls set 'kylin.storage.url=hbase' in kylin.properties");
+
+ // support hbase using a different FS
+ String hbaseClusterFs = KylinConfig.getInstanceFromEnv().getHBaseClusterFs();
+ if (StringUtils.isNotEmpty(hbaseClusterFs)) {
+ conf.set(FileSystem.FS_DEFAULT_NAME_KEY, hbaseClusterFs);
+ }
+
+ // https://issues.apache.org/jira/browse/KYLIN-953
+ if (StringUtils.isBlank(conf.get("hadoop.tmp.dir"))) {
+ conf.set("hadoop.tmp.dir", "/tmp");
+ }
+ if (StringUtils.isBlank(conf.get("hbase.fs.tmp.dir"))) {
+ conf.set("hbase.fs.tmp.dir", "/tmp");
+ }
+
+ // reduce rpc retry
+ conf.set(HConstants.HBASE_CLIENT_PAUSE, "3000");
+ conf.set(HConstants.HBASE_CLIENT_RETRIES_NUMBER, "5");
+ conf.set(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, "60000");
+ // conf.set(ScannerCallable.LOG_SCANNER_ACTIVITY, "true");
+
+ return conf;
+ }
+
+ public static String makeQualifiedPathInHBaseCluster(String path) {
+ try {
+ FileSystem fs = FileSystem.get(getCurrentHBaseConfiguration());
+ return fs.makeQualified(new Path(path)).toString();
+ } catch (IOException e) {
+ throw new IllegalArgumentException("Cannot create FileSystem from current hbase cluster conf", e);
+ }
+ }
+
+ // ============================================================================
+
+ // returned HConnection can be shared by multiple threads and does not require close()
+ @SuppressWarnings("resource")
+ public static HConnection get(String url) {
+ // find configuration
+ Configuration conf = configCache.get(url);
+ if (conf == null) {
+ conf = newHBaseConfiguration(url);
+ configCache.put(url, conf);
+ }
+
+ HConnection connection = connPool.get(url);
+ try {
+ while (true) {
+ // I don't use DCL since recreate a connection is not a big issue.
+ if (connection == null || connection.isClosed()) {
+ logger.info("connection is null or closed, creating a new one");
+ connection = HConnectionManager.createConnection(conf);
+ connPool.put(url, connection);
+ }
+
+ if (connection == null || connection.isClosed()) {
+ Thread.sleep(10000);// wait a while and retry
+ } else {
+ break;
+ }
+ }
+
+ } catch (Throwable t) {
+ logger.error("Error when open connection " + url, t);
+ throw new StorageException("Error when open connection " + url, t);
+ }
+
+ return connection;
+ }
+
+ public static boolean tableExists(HConnection conn, String tableName) throws IOException {
+ HBaseAdmin hbase = new HBaseAdmin(conn);
+ try {
+ return hbase.tableExists(TableName.valueOf(tableName));
+ } finally {
+ hbase.close();
+ }
+ }
+
+ public static boolean tableExists(String hbaseUrl, String tableName) throws IOException {
+ return tableExists(HBaseConnection.get(hbaseUrl), tableName);
+ }
+
+ public static void createHTableIfNeeded(String hbaseUrl, String tableName, String... families) throws IOException {
+ createHTableIfNeeded(HBaseConnection.get(hbaseUrl), tableName, families);
+ }
+
+ public static void deleteTable(String hbaseUrl, String tableName) throws IOException {
+ deleteTable(HBaseConnection.get(hbaseUrl), tableName);
+ }
+
+ public static void createHTableIfNeeded(HConnection conn, String tableName, String... families) throws IOException {
+ HBaseAdmin hbase = new HBaseAdmin(conn);
+
+ try {
+ if (tableExists(conn, tableName)) {
+ logger.debug("HTable '" + tableName + "' already exists");
+ return;
+ }
+
+ logger.debug("Creating HTable '" + tableName + "'");
+
+ HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
+
+ if (null != families && families.length > 0) {
+ for (String family : families) {
+ HColumnDescriptor fd = new HColumnDescriptor(family);
+ fd.setInMemory(true); // metadata tables are best in memory
+ desc.addFamily(fd);
+ }
+ }
+
+ desc.setValue(HTABLE_UUID_TAG, UUID.randomUUID().toString());
+ hbase.createTable(desc);
+
+ logger.debug("HTable '" + tableName + "' created");
+ } finally {
+ hbase.close();
+ }
+ }
+
+ public static void deleteTable(HConnection conn, String tableName) throws IOException {
+ HBaseAdmin hbase = new HBaseAdmin(conn);
+
+ try {
+ if (!tableExists(conn, tableName)) {
+ logger.debug("HTable '" + tableName + "' does not exists");
+ return;
+ }
+
+ logger.debug("delete HTable '" + tableName + "'");
+
+ if (hbase.isTableEnabled(tableName)) {
+ hbase.disableTable(tableName);
+ }
+ hbase.deleteTable(tableName);
+
+ logger.debug("HTable '" + tableName + "' deleted");
+ } finally {
+ hbase.close();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/kylin/blob/d6bb4998/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/cube/v2/CubeHBaseEndpointRPC.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/cube/v2/CubeHBaseEndpointRPC.java b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/cube/v2/CubeHBaseEndpointRPC.java
index 773c9ed..1ebc736 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/cube/v2/CubeHBaseEndpointRPC.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/cube/v2/CubeHBaseEndpointRPC.java
@@ -21,7 +21,6 @@ package org.apache.kylin.storage.hbase.cube.v2;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
-import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -52,7 +51,6 @@ import org.apache.kylin.common.util.LoggableCachedThreadPool;
import org.apache.kylin.common.util.Pair;
import org.apache.kylin.cube.CubeSegment;
import org.apache.kylin.cube.cuboid.Cuboid;
-import org.apache.kylin.engine.mr.HadoopUtil;
import org.apache.kylin.gridtable.GTInfo;
import org.apache.kylin.gridtable.GTRecord;
import org.apache.kylin.gridtable.GTScanRange;
@@ -344,7 +342,8 @@ public class CubeHBaseEndpointRPC extends CubeHBaseRPC {
Map<byte[], CubeVisitProtos.CubeVisitResponse> results;
try {
- results = getResults(builder.build(), conn.getTable(cubeSeg.getStorageLocationIdentifier()), epRange.getFirst(), epRange.getSecond());
+ HTableInterface htable = conn.getTable(cubeSeg.getStorageLocationIdentifier(), HBaseConnection.getCoprocessorPool());
+ results = getResults(builder.build(), htable, epRange.getFirst(), epRange.getSecond());
} catch (Throwable throwable) {
throw new RuntimeException(logHeader + "Error when visiting cubes by endpoint", throwable);
}