From 58e2605afa05b0e00e91f04cc65dc3aabdbfb45e Mon Sep 17 00:00:00 2001 From: Alexander Pivovarov Date: Mon, 26 Aug 2013 13:49:33 -0700 Subject: [PATCH] remove HiveConf and Configuration from thread-local after conection to hiveserver is closed --- .../org/apache/hadoop/hive/metastore/HiveMetaStore.java | 7 ++++++- ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java | 2 +- .../org/apache/hadoop/hive/ql/session/SessionState.java | 6 ++++++ .../java/org/apache/hadoop/hive/service/HiveServer.java | 9 ++++++--- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java index 54eea7d0bf87..af7a71e81c50 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -163,7 +163,7 @@ protected synchronized RawStore initialValue() { // Thread local configuration is needed as many threads could make changes // to the conf using the connection hook - private final ThreadLocal threadLocalConf = + private static final ThreadLocal threadLocalConf = new ThreadLocal() { @Override protected synchronized Configuration initialValue() { @@ -319,6 +319,11 @@ private Configuration getConf() { return conf; } + public static void cleanThreadLocal() { + threadLocalConf.remove(); + LOG.debug("removed current Configuration instance from thread-local var"); + } + /** * Get a cached RawStore. * diff --git a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java index 05316202c02f..2ec919c3c566 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java @@ -63,7 +63,6 @@ import org.apache.hadoop.hive.metastore.api.HiveObjectRef; import org.apache.hadoop.hive.metastore.api.HiveObjectType; import org.apache.hadoop.hive.metastore.api.Index; -import org.apache.hadoop.hive.metastore.api.InvalidObjectException; import org.apache.hadoop.hive.metastore.api.InvalidOperationException; import org.apache.hadoop.hive.metastore.api.MetaException; import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; @@ -178,6 +177,7 @@ public static Hive get() throws HiveException { public static void closeCurrent() { hiveDB.remove(); + LOG.debug("removed current metastore.Hive instance from thread-local var"); } /** diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index 8edfef864c3a..c7ccd23b5b6e 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -748,4 +748,10 @@ public void addLocalMapRedErrors(String id, List localMapRedErrors) { public void setLocalMapRedErrors(Map> localMapRedErrors) { this.localMapRedErrors = localMapRedErrors; } + + public static void cleanThreadLocal() { + tss.remove(); + Log LOG = LogFactory.getLog(SessionState.class.getName()); + LOG.debug("removed current SessionState instance from thread-local var"); + } } diff --git a/service/src/java/org/apache/hadoop/hive/service/HiveServer.java b/service/src/java/org/apache/hadoop/hive/service/HiveServer.java index 5e190aa5e5d5..50ff7bbb80b1 100644 --- a/service/src/java/org/apache/hadoop/hive/service/HiveServer.java +++ b/service/src/java/org/apache/hadoop/hive/service/HiveServer.java @@ -34,9 +34,9 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hive.common.ServerUtils; import org.apache.hadoop.hive.common.LogUtils; import org.apache.hadoop.hive.common.LogUtils.LogInitializationException; +import org.apache.hadoop.hive.common.ServerUtils; import org.apache.hadoop.hive.common.cli.CommonCliOptions; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.HiveMetaStore; @@ -44,6 +44,7 @@ import org.apache.hadoop.hive.metastore.api.Schema; import org.apache.hadoop.hive.ql.CommandNeedRetryException; import org.apache.hadoop.hive.ql.Driver; +import org.apache.hadoop.hive.ql.metadata.Hive; import org.apache.hadoop.hive.ql.plan.api.QueryPlan; import org.apache.hadoop.hive.ql.processors.CommandProcessor; import org.apache.hadoop.hive.ql.processors.CommandProcessorFactory; @@ -61,8 +62,7 @@ import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportFactory; -import org.apache.hadoop.fs.FileSystem; -import org.apache.hadoop.fs.Path; + import com.facebook.fb303.fb_status; /** @@ -227,6 +227,9 @@ public void clean() { if (session.getTmpOutputFile() != null) { session.getTmpOutputFile().delete(); } + SessionState.cleanThreadLocal(); + Hive.closeCurrent(); + HiveMetaStore.HMSHandler.cleanThreadLocal(); pipeIn = null; }