From dc4adbd675ff06fd7d4e35c0c30dd57083e23a49 Mon Sep 17 00:00:00 2001 From: 10069681 Date: Tue, 9 May 2017 10:47:50 +0800 Subject: [PATCH 1/1] KYLIN-2557-The-kylin-will-start-failed --- .../apache/kylin/storage/hbase/HBaseConnection.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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 f0b94288e..ef826410d 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 @@ -33,6 +33,7 @@ import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.util.Threads; import org.apache.hadoop.hdfs.DFSConfigKeys; import org.apache.kylin.common.KylinConfig; +import org.apache.kylin.common.lock.DistributedLock; import org.apache.kylin.common.persistence.StorageException; import org.apache.kylin.common.util.HadoopUtil; import org.slf4j.Logger; @@ -66,6 +67,8 @@ public class HBaseConnection { private static ExecutorService coprocessorPool = null; + private static DistributedLock lock = null; + static { Runtime.getRuntime().addShutdownHook(new Thread() { @Override @@ -267,6 +270,7 @@ public class HBaseConnection { public static void createHTableIfNeeded(Connection conn, String table, String... families) throws IOException { Admin hbase = conn.getAdmin(); TableName tableName = TableName.valueOf(table); + boolean hasLock = false; try { if (tableExists(conn, table)) { logger.debug("HTable '" + table + "' already exists"); @@ -290,6 +294,14 @@ public class HBaseConnection { return; } + lock = KylinConfig.getInstanceFromEnv().getDistributedLockFactory().lockForCurrentProcess(); + hasLock = lock.lock(getLockPath(table), Long.MAX_VALUE); + + if (tableExists(conn, table)) { + logger.debug("HTable '" + table + "' already exists"); + return; + } + logger.debug("Creating HTable '" + table + "'"); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(table)); @@ -307,6 +319,9 @@ public class HBaseConnection { logger.debug("HTable '" + table + "' created"); } finally { hbase.close(); + if (hasLock && lock != null) { + lock.unlock(getLockPath(table)); + } } } @@ -350,4 +365,8 @@ public class HBaseConnection { } } -} + private static String getLockPath(String pathName) { + return "/create_htable/" + pathName + "/lock"; + } + +} \ No newline at end of file -- 2.11.0.windows.1