kylin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liy...@apache.org
Subject [18/31] kylin git commit: KYLIN-2050: Better close resource for AutoCloseable object
Date Tue, 04 Oct 2016 08:54:12 GMT
KYLIN-2050: Better close resource for AutoCloseable object


Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/31b80905
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/31b80905
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/31b80905

Branch: refs/heads/master-cdh5.7
Commit: 31b80905ec3645ecf2c0e8bb5c02bdc451974648
Parents: d366dc1
Author: Yiming Liu <liuyiming.vip@gmail.com>
Authored: Sun Sep 25 21:01:33 2016 +0800
Committer: Li Yang <liyang@apache.org>
Committed: Wed Sep 28 13:37:26 2016 +0800

----------------------------------------------------------------------
 .../org/apache/kylin/common/util/DBUtils.java   | 89 ++++++++++++++++++++
 .../apache/kylin/common/util/ZipFileUtils.java  |  3 +-
 .../apache/kylin/dict/lookup/LookupTable.java   |  3 +-
 .../apache/kylin/metadata/MetadataManager.java  |  4 +-
 .../java/org/apache/kylin/query/QueryCli.java   | 13 +--
 .../kylin/query/enumerator/HiveEnumerator.java  | 19 +----
 .../apache/kylin/rest/service/CubeService.java  |  5 +-
 .../apache/kylin/rest/service/QueryService.java | 23 +----
 .../storage/hbase/steps/CubeHTableUtil.java     |  7 +-
 .../storage/hbase/steps/DeprecatedGCStep.java   |  8 +-
 .../hbase/util/HBaseRegionSizeCalculator.java   |  3 +-
 .../storage/hbase/util/HbaseStreamingInput.java |  5 +-
 .../kylin/storage/hbase/util/PingHBaseCLI.java  | 13 +--
 13 files changed, 123 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/core-common/src/main/java/org/apache/kylin/common/util/DBUtils.java
----------------------------------------------------------------------
diff --git a/core-common/src/main/java/org/apache/kylin/common/util/DBUtils.java b/core-common/src/main/java/org/apache/kylin/common/util/DBUtils.java
new file mode 100644
index 0000000..9ecd3c3
--- /dev/null
+++ b/core-common/src/main/java/org/apache/kylin/common/util/DBUtils.java
@@ -0,0 +1,89 @@
+/*
+ * 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.common.util;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DBUtils {
+
+    private static final Logger logger = LoggerFactory.getLogger(DBUtils.class);
+    
+    /**
+     * Closes an <code>ResultSet</code> unconditionally.
+     * <p>
+     * Equivalent to {@link ResultSet#close()}, except any exceptions will be ignored.
+     * This is typically used in finally blocks.
+     * <p>
+     *
+     * @param output the ResultSet to close, may be null or already closed
+     */
+    public static void closeQuietly(final ResultSet rs) {
+        closeQuietly((AutoCloseable) rs);
+    }
+
+    /**
+     * Closes an <code>Statement</code> unconditionally.
+     * <p>
+     * Equivalent to {@link Statement#close()}, except any exceptions will be ignored.
+     * This is typically used in finally blocks.
+     * <p>
+     *
+     * @param output the ResultSet to close, may be null or already closed
+     */
+    public static void closeQuietly(final Statement stmt) {
+        closeQuietly((AutoCloseable) stmt);
+    }
+
+    /**
+     * Closes an <code>Connection</code> unconditionally.
+     * <p>
+     * Equivalent to {@link Connection#close()}, except any exceptions will be ignored.
+     * This is typically used in finally blocks.
+     * <p>
+     *
+     * @param output the ResultSet to close, may be null or already closed
+     */
+    public static void closeQuietly(final Connection conn) {
+        closeQuietly((AutoCloseable) conn);
+    }
+
+    /**
+     * Closes a <code>AutoCloseable</code> unconditionally.
+     * <p>
+     * Equivalent to {@link AutoCloseable#close()}, except any exceptions will be ignored.
This is typically used in
+     * finally blocks.
+     * <p>
+     *
+     * @param closeable the objects to close, may be null or already closed
+     */
+    public static void closeQuietly(final AutoCloseable closeable) {
+        try {
+            if (closeable != null) {
+                closeable.close();
+            }
+        } catch (final Exception ioe) {
+            logger.debug("", ioe);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/core-common/src/main/java/org/apache/kylin/common/util/ZipFileUtils.java
----------------------------------------------------------------------
diff --git a/core-common/src/main/java/org/apache/kylin/common/util/ZipFileUtils.java b/core-common/src/main/java/org/apache/kylin/common/util/ZipFileUtils.java
index 525a34c..401c556 100644
--- a/core-common/src/main/java/org/apache/kylin/common/util/ZipFileUtils.java
+++ b/core-common/src/main/java/org/apache/kylin/common/util/ZipFileUtils.java
@@ -28,11 +28,12 @@ import java.util.zip.ZipOutputStream;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ZipFileUtils {
 
-    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ZipFileUtils.class);
+    private static final Logger logger = LoggerFactory.getLogger(ZipFileUtils.class);
 
     public static void compressZipFile(String sourceDir, String zipFilename) throws IOException
{
         if (!validateZipFilename(zipFilename)) {

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/LookupTable.java
----------------------------------------------------------------------
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/LookupTable.java b/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/LookupTable.java
index c0983d8..9aae755 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/LookupTable.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/lookup/LookupTable.java
@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.kylin.common.util.Array;
 import org.apache.kylin.common.util.Pair;
 import org.apache.kylin.metadata.model.TableDesc;
@@ -67,7 +68,7 @@ abstract public class LookupTable<T> {
                 initRow(reader.getRow(), keyIndex);
             }
         } finally {
-            reader.close();
+            IOUtils.closeQuietly(reader);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/core-metadata/src/main/java/org/apache/kylin/metadata/MetadataManager.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/MetadataManager.java b/core-metadata/src/main/java/org/apache/kylin/metadata/MetadataManager.java
index 8d77131..4e9c1ed 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/MetadataManager.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/MetadataManager.java
@@ -32,6 +32,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.common.persistence.JsonSerializer;
 import org.apache.kylin.common.persistence.RawResource;
@@ -380,8 +381,7 @@ public class MetadataManager {
         try {
             attrs.putAll(JsonUtil.readValue(is, HashMap.class));
         } finally {
-            if (is != null)
-                is.close();
+            IOUtils.closeQuietly(is);
         }
 
         // parse table identity from file name

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/query/src/main/java/org/apache/kylin/query/QueryCli.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/QueryCli.java b/query/src/main/java/org/apache/kylin/query/QueryCli.java
index 898cb22..d3f1521 100644
--- a/query/src/main/java/org/apache/kylin/query/QueryCli.java
+++ b/query/src/main/java/org/apache/kylin/query/QueryCli.java
@@ -33,6 +33,7 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import org.apache.kylin.common.KylinConfig;
+import org.apache.kylin.common.util.DBUtils;
 import org.apache.kylin.query.schema.OLAPSchemaFactory;
 
 public class QueryCli {
@@ -74,15 +75,9 @@ public class QueryCli {
                 }
             }
         } finally {
-            if (rs != null) {
-                rs.close();
-            }
-            if (stmt != null) {
-                stmt.close();
-            }
-            if (conn != null) {
-                conn.close();
-            }
+            DBUtils.closeQuietly(rs);
+            DBUtils.closeQuietly(stmt);
+            DBUtils.closeQuietly(conn);
         }
 
     }

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
----------------------------------------------------------------------
diff --git a/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java b/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
index b0113f9..f5ada16 100644
--- a/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
+++ b/query/src/main/java/org/apache/kylin/query/enumerator/HiveEnumerator.java
@@ -26,6 +26,7 @@ import java.sql.Statement;
 import java.util.List;
 
 import org.apache.calcite.linq4j.Enumerator;
+import org.apache.kylin.common.util.DBUtils;
 import org.apache.kylin.query.relnode.OLAPContext;
 
 /**
@@ -69,22 +70,8 @@ public class HiveEnumerator implements Enumerator<Object[]> {
         } catch (SQLException e) {
             throw new IllegalStateException(url + " can't execute query " + sql, e);
         } finally {
-            if (stmt != null) {
-                try {
-                    stmt.close();
-                } catch (SQLException ex) {
-                    ex.printStackTrace();
-                }
-            }
-            stmt = null;
-            if (conn != null) {
-                try {
-                    conn.close();
-                } catch (SQLException ex) {
-                    ex.printStackTrace();
-                }
-                conn = null;
-            }
+            DBUtils.closeQuietly(stmt);
+            DBUtils.closeQuietly(conn);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java b/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
index 4fa07b6..ea32ef7 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/service/CubeService.java
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.WeakHashMap;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.client.HTable;
 import org.apache.kylin.common.KylinConfig;
@@ -433,9 +434,7 @@ public class CubeService extends BasicService {
             hr.setTableSize(tableSize);
             hr.setRegionCount(regionCount);
         } finally {
-            if (null != table) {
-                table.close();
-            }
+            IOUtils.closeQuietly(table);
         }
 
         htableInfoCache.put(tableName, hr);

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java b/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
index 8303cee..cda4a52 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
@@ -52,6 +52,7 @@ import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Result;
 import org.apache.kylin.common.KylinConfig;
 import org.apache.kylin.common.util.Bytes;
+import org.apache.kylin.common.util.DBUtils;
 import org.apache.kylin.cube.CubeInstance;
 import org.apache.kylin.cube.CubeManager;
 import org.apache.kylin.cube.cuboid.Cuboid;
@@ -535,25 +536,9 @@ public class QueryService extends BasicService {
 
     private static void close(ResultSet resultSet, Statement stat, Connection conn) {
         OLAPContext.clearParameter();
-
-        if (resultSet != null)
-            try {
-                resultSet.close();
-            } catch (SQLException e) {
-                logger.error("failed to close", e);
-            }
-        if (stat != null)
-            try {
-                stat.close();
-            } catch (SQLException e) {
-                logger.error("failed to close", e);
-            }
-        if (conn != null)
-            try {
-                conn.close();
-            } catch (SQLException e) {
-                logger.error("failed to close", e);
-            }
+        DBUtils.closeQuietly(resultSet);
+        DBUtils.closeQuietly(stat);
+        DBUtils.closeQuietly(conn);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/CubeHTableUtil.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/CubeHTableUtil.java
b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/CubeHTableUtil.java
index 9b487a7..2814ad6 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/CubeHTableUtil.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/CubeHTableUtil.java
@@ -20,6 +20,7 @@ package org.apache.kylin.storage.hbase.steps;
 
 import java.io.IOException;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HColumnDescriptor;
@@ -103,7 +104,7 @@ public class CubeHTableUtil {
             Preconditions.checkArgument(admin.isTableAvailable(tableName), "table " + tableName
+ " created, but is not available due to some reasons");
             logger.info("create hbase table " + tableName + " done.");
         } finally {
-            admin.close();
+            IOUtils.closeQuietly(admin);
         }
 
     }
@@ -119,7 +120,7 @@ public class CubeHTableUtil {
                 admin.deleteTable(tableName);
             }
         } finally {
-            admin.close();
+            IOUtils.closeQuietly(admin);
         }
     }
 
@@ -146,7 +147,7 @@ public class CubeHTableUtil {
             Preconditions.checkArgument(admin.isTableAvailable(tableName), "table " + tableName
+ " created, but is not available due to some reasons");
             logger.info("create hbase table " + tableName + " done.");
         } finally {
-            admin.close();
+            IOUtils.closeQuietly(admin);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/DeprecatedGCStep.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/DeprecatedGCStep.java
b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/DeprecatedGCStep.java
index 7aecd7e..21f1c00 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/DeprecatedGCStep.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/steps/DeprecatedGCStep.java
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -122,12 +123,7 @@ public class DeprecatedGCStep extends AbstractExecutable {
                 }
 
             } finally {
-                if (admin != null)
-                    try {
-                        admin.close();
-                    } catch (IOException e) {
-                        logger.error(e.getLocalizedMessage());
-                    }
+                IOUtils.closeQuietly(admin);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HBaseRegionSizeCalculator.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HBaseRegionSizeCalculator.java
b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HBaseRegionSizeCalculator.java
index 346c3a2..937b65f 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HBaseRegionSizeCalculator.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HBaseRegionSizeCalculator.java
@@ -28,6 +28,7 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeSet;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.ClusterStatus;
 import org.apache.hadoop.hbase.HRegionInfo;
@@ -104,7 +105,7 @@ public class HBaseRegionSizeCalculator {
                 }
             }
         } finally {
-            hBaseAdmin.close();
+            IOUtils.closeQuietly(hBaseAdmin);
         }
 
     }

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HbaseStreamingInput.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HbaseStreamingInput.java
b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HbaseStreamingInput.java
index f30f2c9..e26c8e8 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HbaseStreamingInput.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/HbaseStreamingInput.java
@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Random;
 import java.util.concurrent.Semaphore;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.hbase.Cell;
 import org.apache.hadoop.hbase.HColumnDescriptor;
 import org.apache.hadoop.hbase.HTableDescriptor;
@@ -79,8 +80,8 @@ public class HbaseStreamingInput {
 
             logger.info("HTable '" + tableName + "' created");
         } finally {
-            conn.close();
-            hadmin.close();
+            IOUtils.closeQuietly(conn);
+            IOUtils.closeQuietly(hadmin);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/kylin/blob/31b80905/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/PingHBaseCLI.java
----------------------------------------------------------------------
diff --git a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/PingHBaseCLI.java
b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/PingHBaseCLI.java
index 58ef7cb..81b79cb 100644
--- a/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/PingHBaseCLI.java
+++ b/storage-hbase/src/main/java/org/apache/kylin/storage/hbase/util/PingHBaseCLI.java
@@ -20,6 +20,7 @@ package org.apache.kylin.storage.hbase.util;
 
 import java.io.IOException;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.client.HConnection;
 import org.apache.hadoop.hbase.client.HConnectionManager;
@@ -73,15 +74,9 @@ public class PingHBaseCLI {
                     break;
             }
         } finally {
-            if (scanner != null) {
-                scanner.close();
-            }
-            if (table != null) {
-                table.close();
-            }
-            if (conn != null) {
-                conn.close();
-            }
+            IOUtils.closeQuietly(scanner);
+            IOUtils.closeQuietly(table);
+            IOUtils.closeQuietly(conn);
         }
 
     }


Mime
View raw message