commit 21ab3a05d9e0e49c05a4650c6e3fdfb95cd79418 Author: Bharath Krishna Date: Tue Sep 18 23:44:33 2018 -0700 HIVE-8446 : Failing to delete data after dropping a table or database should result in error diff --git standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java index ba82a9327cf18e8d55ebddcd774786d3d72f753a..4e6c07dfccff26a73232bc8a92199ee556480cc1 100644 --- standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -1444,6 +1444,7 @@ private void drop_database_core(RawStore ms, String catName, throws NoSuchObjectException, InvalidOperationException, MetaException, IOException, InvalidObjectException, InvalidInputException { boolean success = false; + boolean deleteDataSuccess = true; Database db = null; List tablePaths = new ArrayList<>(); List partitionPaths = new ArrayList<>(); @@ -1593,10 +1594,10 @@ private void drop_database_core(RawStore ms, String catName, ms.rollbackTransaction(); } else if (deleteData) { // Delete the data in the partitions which have other locations - deletePartitionData(partitionPaths, false, db); + deleteDataSuccess = deletePartitionData(partitionPaths, false, db); // Delete the data in the tables which have other locations for (Path tablePath : tablePaths) { - deleteTableData(tablePath, false, db); + deleteDataSuccess &= deleteTableData(tablePath, false, db); } // Delete the data in the database try { @@ -1604,8 +1605,9 @@ private void drop_database_core(RawStore ms, String catName, } catch (Exception e) { LOG.error("Failed to delete database directory: " + db.getLocationUri() + " " + e.getMessage()); + deleteDataSuccess = false; } - // it is not a terrible thing even if the data is not deleted + // it is not a terrible thing even if the data is not deleted but throw exception to indicate this to client } if (!listeners.isEmpty()) { @@ -1616,6 +1618,9 @@ private void drop_database_core(RawStore ms, String catName, transactionalListenerResponses, ms); } } + if (!deleteDataSuccess) { + throw new MetaException("Drop Database: Metadata deleted but data was not deleted successfully."); + } } @Override @@ -2451,6 +2456,7 @@ private boolean drop_table_core(final RawStore ms, final String catName, final S throws NoSuchObjectException, MetaException, IOException, InvalidObjectException, InvalidInputException { boolean success = false; + boolean deleteDataSuccess = true; boolean tableDataShouldBeDeleted = false; Path tblPath = null; List partPaths = null; @@ -2512,10 +2518,11 @@ private boolean drop_table_core(final RawStore ms, final String catName, final S } else if (tableDataShouldBeDeleted) { // Data needs deletion. Check if trash may be skipped. // Delete the data in the partitions which have other locations - deletePartitionData(partPaths, ifPurge, db); + deleteDataSuccess = deletePartitionData(partPaths, ifPurge, db); // Delete the data in the table - deleteTableData(tblPath, ifPurge, db); - // ok even if the data is not deleted + deleteDataSuccess &= deleteTableData(tblPath, ifPurge, db); + + // ok even if the data is not deleted but throw exception at the end to indicate this to client } if (!listeners.isEmpty()) { @@ -2526,6 +2533,9 @@ private boolean drop_table_core(final RawStore ms, final String catName, final S transactionalListenerResponses, ms); } } + if (!deleteDataSuccess) { + throw new MetaException("Drop Table: Metadata deleted but data was not deleted successfully."); + } return success; } @@ -2545,7 +2555,7 @@ private boolean checkTableDataShouldBeDeleted(Table tbl, boolean deleteData) { * data from warehouse * @param db database the table belongs to */ - private void deleteTableData(Path tablePath, boolean ifPurge, Database db) { + private boolean deleteTableData(Path tablePath, boolean ifPurge, Database db) { if (tablePath != null) { try { @@ -2553,8 +2563,10 @@ private void deleteTableData(Path tablePath, boolean ifPurge, Database db) { } catch (Exception e) { LOG.error("Failed to delete table directory: " + tablePath + " " + e.getMessage()); + return false; } } + return true; } /** @@ -2566,7 +2578,8 @@ private void deleteTableData(Path tablePath, boolean ifPurge, Database db) { * removing data from warehouse * @param db database the partition belongs to */ - private void deletePartitionData(List partPaths, boolean ifPurge, Database db) { + private boolean deletePartitionData(List partPaths, boolean ifPurge, Database db) { + boolean deleteSuccess = true; if (partPaths != null && !partPaths.isEmpty()) { for (Path partPath : partPaths) { try { @@ -2574,9 +2587,11 @@ private void deletePartitionData(List partPaths, boolean ifPurge, Database } catch (Exception e) { LOG.error("Failed to delete partition directory: " + partPath + " " + e.getMessage()); + deleteSuccess = false; } } } + return deleteSuccess; } /**