You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by se...@apache.org on 2014/11/04 01:32:56 UTC

svn commit: r1636480 - in /hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore: MetaStoreDirectSql.java ObjectStore.java

Author: sershe
Date: Tue Nov  4 00:32:56 2014
New Revision: 1636480

URL: http://svn.apache.org/r1636480
Log:
HIVE-8714 : getDatabase reports direct SQL error when database is missing (Sergey Shelukhin, reviewed by Ashutosh Chauhan and Sushanth Sowmyan)

Modified:
    hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
    hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java

Modified: hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
URL: http://svn.apache.org/viewvc/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java?rev=1636480&r1=1636479&r2=1636480&view=diff
==============================================================================
--- hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java (original)
+++ hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java Tue Nov  4 00:32:56 2014
@@ -48,7 +48,6 @@ import org.apache.hadoop.hive.metastore.
 import org.apache.hadoop.hive.metastore.api.Database;
 import org.apache.hadoop.hive.metastore.api.FieldSchema;
 import org.apache.hadoop.hive.metastore.api.MetaException;
-import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
 import org.apache.hadoop.hive.metastore.api.Order;
 import org.apache.hadoop.hive.metastore.api.Partition;
 import org.apache.hadoop.hive.metastore.api.PrincipalType;
@@ -151,18 +150,17 @@ class MetaStoreDirectSql {
    * here - for eg., for MySQL, we signal that we want to use ANSI SQL quoting behaviour
    */
   private void doDbSpecificInitializationsBeforeQuery() throws MetaException {
-    if (isMySql){
-      try {
-        assert pm.currentTransaction().isActive(); // must be inside tx together with queries
-        trySetAnsiQuotesForMysql();
-      } catch (SQLException sqlEx) {
-        throw new MetaException("Error setting ansi quotes: " + sqlEx.getMessage());
-      }
+    if (!isMySql) return;
+    try {
+      assert pm.currentTransaction().isActive(); // must be inside tx together with queries
+      trySetAnsiQuotesForMysql();
+    } catch (SQLException sqlEx) {
+      throw new MetaException("Error setting ansi quotes: " + sqlEx.getMessage());
     }
   }
 
   /**
-   * MySQL, by default, doesn't recognize ANSI quotes which need to have for Postgres.
+   * MySQL, by default, doesn't recognize ANSI quotes which we need to have for Postgres.
    * Try to set the ANSI quotes mode on for the session. Due to connection pooling, needs
    * to be called in the same transaction as the actual queries.
    */
@@ -194,18 +192,20 @@ class MetaStoreDirectSql {
       Object[] params = new Object[] { dbName };
       queryDbSelector = pm.newQuery("javax.jdo.query.SQL", queryTextDbSelector);
 
-      LOG.debug("getDatabase:query instantiated : " + queryTextDbSelector + " with param ["+params[0]+"]");
+      if (LOG.isTraceEnabled()) {
+        LOG.trace("getDatabase:query instantiated : " + queryTextDbSelector
+            + " with param [" + params[0] + "]");
+      }
 
+      @SuppressWarnings("unchecked")
       List<Object[]> sqlResult = (List<Object[]>)queryDbSelector.executeWithArray(params);
       if ((sqlResult == null) || sqlResult.isEmpty()) {
-        LOG.debug("getDatabase:queryDbSelector ran, returned no/empty results, returning NoSuchObjectException");
-        throw new MetaException("There is no database named " + dbName);
+        return null;
       }
 
       assert(sqlResult.size() == 1);
-      if (sqlResult.get(0) == null){
-        LOG.debug("getDatabase:queryDbSelector ran, returned results, but the result entry was null, returning NoSuchObjectException");
-        throw new MetaException("There is no database named " + dbName);
+      if (sqlResult.get(0) == null) {
+        return null;
       }
 
       Object[] dbline = sqlResult.get(0);
@@ -215,25 +215,28 @@ class MetaStoreDirectSql {
           + " FROM \"DATABASE_PARAMS\" "
           + " WHERE \"DB_ID\" = ? "
           + " AND \"PARAM_KEY\" IS NOT NULL";
-      Object[] params2 = new Object[] { dbid };
-      queryDbParams = pm.newQuery("javax.jdo.query.SQL",queryTextDbParams);
-      LOG.debug("getDatabase:query2 instantiated : " + queryTextDbParams + " with param ["+params2[0]+"]");
+      params[0] = dbid;
+      queryDbParams = pm.newQuery("javax.jdo.query.SQL", queryTextDbParams);
+      if (LOG.isTraceEnabled()) {
+        LOG.trace("getDatabase:query2 instantiated : " + queryTextDbParams
+            + " with param [" + params[0] + "]");
+      }
 
       Map<String,String> dbParams = new HashMap<String,String>();
-      List<Object[]> sqlResult2 = ensureList(queryDbParams.executeWithArray(params2));
-      if (!sqlResult2.isEmpty()){
-        for (Object[] line : sqlResult2){
+      List<Object[]> sqlResult2 = ensureList(queryDbParams.executeWithArray(params));
+      if (!sqlResult2.isEmpty()) {
+        for (Object[] line : sqlResult2) {
           dbParams.put(extractSqlString(line[0]),extractSqlString(line[1]));
         }
       }
-      LOG.debug("getDatabase: instantiating db object to return");
       Database db = new Database();
       db.setName(extractSqlString(dbline[1]));
       db.setLocationUri(extractSqlString(dbline[2]));
       db.setDescription(extractSqlString(dbline[3]));
       db.setOwnerName(extractSqlString(dbline[4]));
       String type = extractSqlString(dbline[5]);
-      db.setOwnerType((null == type || type.trim().isEmpty()) ? null : PrincipalType.valueOf(type));
+      db.setOwnerType(
+          (null == type || type.trim().isEmpty()) ? null : PrincipalType.valueOf(type));
       db.setParameters(dbParams);
       if (LOG.isDebugEnabled()){
         LOG.debug("getDatabase: directsql returning db " + db.getName()

Modified: hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java
URL: http://svn.apache.org/viewvc/hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java?rev=1636480&r1=1636479&r2=1636480&view=diff
==============================================================================
--- hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java (original)
+++ hive/trunk/metastore/src/java/org/apache/hadoop/hive/metastore/ObjectStore.java Tue Nov  4 00:32:56 2014
@@ -521,17 +521,22 @@ public class ObjectStore implements RawS
 
   @Override
   public Database getDatabase(String name) throws NoSuchObjectException {
+    MetaException ex = null;
+    Database db = null;
     try {
-      return getDatabaseInternal(name);
+      db = getDatabaseInternal(name);
     } catch (MetaException e) {
       // Signature restriction to NSOE, and NSOE being a flat exception prevents us from
       // setting the cause of the NSOE as the MetaException. We should not lose the info
       // we got here, but it's very likely that the MetaException is irrelevant and is
       // actually an NSOE message, so we should log it and throw an NSOE with the msg.
-      LOG.warn("Got a MetaException trying to call getDatabase("
-          +name+"), returning NoSuchObjectException", e);
-      throw new NoSuchObjectException(e.getMessage());
+      ex = e;
     }
+    if (db == null) {
+      LOG.warn("Failed to get database " + name +", returning NoSuchObjectException", ex);
+      throw new NoSuchObjectException(name + (ex == null ? "" : (": " + ex.getMessage())));
+    }
+    return db;
   }
 
   public Database getDatabaseInternal(String name) throws MetaException, NoSuchObjectException {
@@ -2375,7 +2380,7 @@ public class ObjectStore implements RawS
     }
 
     private void handleDirectSqlError(Exception ex) throws MetaException, NoSuchObjectException {
-      LOG.error("Direct SQL failed" + (allowJdo ? ", falling back to ORM" : ""), ex);
+      LOG.warn("Direct SQL failed" + (allowJdo ? ", falling back to ORM" : ""), ex);
       if (!allowJdo) {
         if (ex instanceof MetaException) {
           throw (MetaException)ex;