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 acef599..11928cc 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -244,13 +244,22 @@ public TTransport getTransport(TTransport trans) { // right now they come from jpox.properties private Warehouse wh; // hdfs warehouse - private final ThreadLocal threadLocalMS = + + private static ThreadLocal threadLocalMS = new ThreadLocal() { - @Override - protected synchronized RawStore initialValue() { - return null; - } - }; + @Override + protected synchronized RawStore initialValue() { + return null; + } + }; + + public static RawStore getRawStore() { + return threadLocalMS.get(); + } + + public static void setRawStore(RawStore rawStore) { + threadLocalMS.set(rawStore); + } private final ThreadLocal threadLocalTxn = new ThreadLocal() { @Override diff --git a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java index de54ca1..719ab3a 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java @@ -31,6 +31,8 @@ import java.util.concurrent.RejectedExecutionException; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.RawStore; import org.apache.hadoop.hive.metastore.api.FieldSchema; import org.apache.hadoop.hive.metastore.api.Schema; import org.apache.hadoop.hive.ql.CommandNeedRetryException; @@ -171,25 +173,29 @@ public void run() throws HiveSQLException { if (!shouldRunAsync()) { runInternal(opConfig); } else { + // We'll set all ThreadLocals in the background thread from the foreground (handler) thread final SessionState parentSessionState = SessionState.get(); - // current Hive object needs to be set in aysnc thread in case of remote metastore. - // The metastore client in Hive is associated with right user + // ThreadLocal Hive object needs to be set in background thread. + // The metastore client in Hive is associated with right user. final Hive sessionHive = getCurrentHive(); - // current UGI will get used by metastore when metsatore is in embedded mode - // so this needs to get passed to the new async thread + // Current UGI will get used by metastore when metsatore is in embedded mode + // So this needs to get passed to the new background thread final UserGroupInformation currentUGI = getCurrentUGI(opConfig); + // In the case where HiveServer2 uses an embedded metastore, we need to set the + // ThreadLocal RawStore instance in the background thread to be the same as the foreground thread. + final RawStore sessionRawStore = getCurrentRawStore(); // Runnable impl to call runInternal asynchronously, // from a different thread Runnable backgroundOperation = new Runnable() { - @Override public void run() { PrivilegedExceptionAction doAsAction = new PrivilegedExceptionAction() { @Override public Object run() throws HiveSQLException { - - // Storing the current Hive object necessary when doAs is enabled + // Set the ThreadLocal RawStore + HiveMetaStore.HMSHandler.setRawStore(sessionRawStore); + // Storing the ThreadLocal Hive object necessary when doAs is enabled // User information is part of the metastore client member in Hive Hive.set(sessionHive); SessionState.setCurrentSessionState(parentSessionState); @@ -223,6 +229,12 @@ public Object run() throws HiveSQLException { } } + /** + * Returns the current UGI on the stack + * @param opConfig + * @return + * @throws HiveSQLException + */ private UserGroupInformation getCurrentUGI(HiveConf opConfig) throws HiveSQLException { try { return ShimLoader.getHadoopShims().getUGIForConf(opConfig); @@ -231,11 +243,29 @@ private UserGroupInformation getCurrentUGI(HiveConf opConfig) throws HiveSQLExce } } + /** + * Returns the ThreadLocal Hive for the current thread + * @return + * @throws HiveSQLException + */ private Hive getCurrentHive() throws HiveSQLException { try { return Hive.get(); } catch (HiveException e) { - throw new HiveSQLException("Failed to get current Hive object", e); + throw new HiveSQLException("Failed to get ThreadLocal Hive object", e); + } + } + + /** + * Returns the ThreadLocal RawStore for the current thread + * @return + * @throws HiveSQLException + */ + private RawStore getCurrentRawStore() throws HiveSQLException { + try { + return HiveMetaStore.HMSHandler.getRawStore(); + } catch (Exception e) { + throw new HiveSQLException("Failed to get the ThreadLocal RawStore", e); } }