commit f835ed0ebec8a1f972ed2e1ff5dbc0afa3e2d661 Author: Andrew Sherman Date: Tue Mar 6 11:29:09 2018 -0800 HIVE-18877: HiveSchemaTool.validateSchemaTables() should wrap a SQLException when rethrowing If schematool is run with the -verbose flag then it will print a stack trace for an exception that occurs. If a SQLException is caught during HiveSchemaTool.validateSchemaTables() then a HiveMetaException is rethrown containing the text of the SQLException. If we instead throw a HiveMetaException that wraps the SQLException then the stacktrace will help with diagnosis of issues where the SQLException contains a generic error text. diff --git beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java index 07282891355df1d84af9b3048a4a3a5a39a3cf12..040724ae3aa47dfe91e0884c7ce96cf7dc03cb24 100644 --- beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java +++ beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java @@ -750,7 +750,7 @@ boolean validateSchemaTables(Connection conn) throws HiveMetaException { LOG.debug("Found table " + table + " in HMS dbstore"); } } catch (SQLException e) { - throw new HiveMetaException("Failed to retrieve schema tables from Hive Metastore DB," + e.getMessage()); + throw new HiveMetaException("Failed to retrieve schema tables from Hive Metastore DB", e); } finally { if (rs != null) { try { diff --git itests/hive-unit/pom.xml itests/hive-unit/pom.xml index 626bbfbebcf62a6af5b433267466c9552d413b88..6923ee53900e935abedd555f8077950fc8bc59fb 100644 --- itests/hive-unit/pom.xml +++ itests/hive-unit/pom.xml @@ -468,6 +468,11 @@ ${plexus.version} test + + commons-dbcp + commons-dbcp + ${commons-dbcp.version} + diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java index d92a50c904d245cf4d3c6a1ca314a47c6b50fed6..ac93abcad303088421a40d6f1da16f7174efc243 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java @@ -27,10 +27,13 @@ import java.io.PrintStream; import java.net.URI; import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; import java.util.Random; import junit.framework.TestCase; +import org.apache.commons.dbcp.DelegatingConnection; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.hive.conf.HiveConf; @@ -153,7 +156,24 @@ public void testValidateSchemaTables() throws Exception { schemaTool.runBeeLine(scriptFile.getPath()); isValid = schemaTool.validateSchemaTables(conn); assertTrue(isValid); - } + + // Check that an exception from getMetaData() is reported correctly + try { + // Make a Connection object that will throw an exception + BadMetaDataConnection bad = new BadMetaDataConnection(conn); + schemaTool.validateSchemaTables(bad); + fail("did not get expected exception"); + } catch (HiveMetaException hme) { + String message = hme.getMessage(); + assertTrue("Bad HiveMetaException message :" + message, + message.contains("Failed to retrieve schema tables from Hive Metastore DB")); + Throwable cause = hme.getCause(); + assertNotNull("HiveMetaException did not contain a cause", cause); + String causeMessage = cause.getMessage(); + assertTrue("Bad SQLException message: " + causeMessage, causeMessage.contains( + BadMetaDataConnection.FAILURE_TEXT)); + } + } /* * Test the validation of incorrect NULL values in the tables @@ -759,4 +779,20 @@ private void createTestHiveTableSchemas() throws IOException { File scriptFile = generateTestScript(scripts); schemaTool.runBeeLine(scriptFile.getPath()); } + + /** + * A mock Connection class that throws an exception out of getMetaData(). + */ + class BadMetaDataConnection extends DelegatingConnection { + static final String FAILURE_TEXT = "fault injected"; + + BadMetaDataConnection(Connection connection) { + super(connection); + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + throw new SQLException(FAILURE_TEXT); + } + } }