diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index 5b8e6de..6a32e53 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -321,9 +321,9 @@ public IMetaStoreClient getMetaStoreClient() throws HiveSQLException { try { return Hive.get(getHiveConf()).getMSC(); } catch (HiveException e) { - throw new HiveSQLException("Failed to get metastore connection", e); + throw new HiveSQLException("Failed to get metastore connection: " + e, e); } catch (MetaException e) { - throw new HiveSQLException("Failed to get metastore connection", e); + throw new HiveSQLException("Failed to get metastore connection: " + e, e); } } diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java index a29e5d1..e9a76f5 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImplwithUGI.java @@ -24,9 +24,10 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.MetaException; import org.apache.hadoop.hive.ql.metadata.Hive; import org.apache.hadoop.hive.ql.metadata.HiveException; -import org.apache.hadoop.hive.shims.ShimLoader; import org.apache.hadoop.hive.shims.Utils; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hive.service.auth.HiveAuthFactory; @@ -42,7 +43,7 @@ public static final String HS2TOKEN = "HiveServer2ImpersonationToken"; private UserGroupInformation sessionUgi = null; - private String delegationTokenStr = null; + private String hmsDelegationTokenStr = null; private Hive sessionHive = null; private HiveSession proxySession = null; static final Log LOG = LogFactory.getLog(HiveSessionImplwithUGI.class); @@ -84,7 +85,7 @@ public UserGroupInformation getSessionUgi() { } public String getDelegationToken () { - return this.delegationTokenStr; + return this.hmsDelegationTokenStr; } @Override @@ -126,30 +127,55 @@ public void close() throws HiveSQLException { * @throws HiveException * @throws IOException */ - private void setDelegationToken(String delegationTokenStr) throws HiveSQLException { - this.delegationTokenStr = delegationTokenStr; - if (delegationTokenStr != null) { + private void setDelegationToken(String hmsDelegationTokenStr) throws HiveSQLException { + this.hmsDelegationTokenStr = hmsDelegationTokenStr; + if (hmsDelegationTokenStr != null) { getHiveConf().set("hive.metastore.token.signature", HS2TOKEN); try { - Utils.setTokenStr(sessionUgi, delegationTokenStr, HS2TOKEN); + Utils.setTokenStr(sessionUgi, hmsDelegationTokenStr, HS2TOKEN); } catch (IOException e) { - throw new HiveSQLException("Couldn't setup delegation token in the ugi", e); + throw new HiveSQLException("Couldn't setup delegation token in the ugi: " + e, e); } } } // If the session has a delegation token obtained from the metastore, then cancel it private void cancelDelegationToken() throws HiveSQLException { - if (delegationTokenStr != null) { + if (hmsDelegationTokenStr != null) { try { - Hive.get(getHiveConf()).cancelDelegationToken(delegationTokenStr); + Hive.get(getHiveConf()).cancelDelegationToken(hmsDelegationTokenStr); } catch (HiveException e) { - throw new HiveSQLException("Couldn't cancel delegation token", e); + throw new HiveSQLException("Couldn't cancel delegation token: " + e, e); } // close the metastore connection created with this delegation token Hive.closeCurrent(); } } + @Override + public IMetaStoreClient getMetaStoreClient() throws HiveSQLException { + return getMetaStoreClient(true); + } + + private IMetaStoreClient getMetaStoreClient(boolean retryInCaseOfTokenExpiration) throws HiveSQLException { + try { + return Hive.get(getHiveConf()).getMSC(); + } catch (HiveException e) { + throw new HiveSQLException("Failed to get metastore connection: " + e, e); + } catch(MetaException e1) { + if (hmsDelegationTokenStr != null && retryInCaseOfTokenExpiration) { + LOG.info("Retrying failed metastore connection: " + e1, e1); + Hive.closeCurrent(); + try { + setDelegationToken(Hive.get(getHiveConf()).getDelegationToken(getUsername(), getUsername())); + } catch (HiveException e2) { + throw new HiveSQLException("Error connect metastore to setup impersonation: " + e2, e2); + } + return getMetaStoreClient(false); + } else { + throw new HiveSQLException("Failed to get metastore connection: " + e1, e1); + } + } + } @Override protected HiveSession getSession() {