Skip to content

Commit f75c18b

Browse files
authored
HIVE-29117: Refactor deleteDir and moveToTrash (#6006)
* HIVE-29117: Refactor deleteDir and moveToTrash * fix compile and remove unnecessary MetaStoreFS interface * return void in deleteDir() * fix sonar reports warnnings * Revert "return void in deleteDir()" This reverts commit 7ad708b.
1 parent b115c85 commit f75c18b

File tree

18 files changed

+75
-221
lines changed

18 files changed

+75
-221
lines changed

common/src/java/org/apache/hadoop/hive/common/FileUtils.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.apache.hadoop.fs.Path;
5858
import org.apache.hadoop.fs.PathFilter;
5959
import org.apache.hadoop.fs.RemoteIterator;
60-
import org.apache.hadoop.fs.Trash;
6160
import org.apache.hadoop.fs.permission.FsAction;
6261
import org.apache.hadoop.fs.FSDataOutputStream;
6362
import org.apache.hadoop.fs.PathExistsException;
@@ -963,41 +962,6 @@ public static boolean distCpWithSnapshot(String oldSnapshot, String newSnapshot,
963962
return copied;
964963
}
965964

966-
/**
967-
* Move a particular file or directory to the trash.
968-
* @param fs FileSystem to use
969-
* @param f path of file or directory to move to trash.
970-
* @param conf
971-
* @return true if move successful
972-
* @throws IOException
973-
*/
974-
public static boolean moveToTrash(FileSystem fs, Path f, Configuration conf, boolean purge)
975-
throws IOException {
976-
LOG.debug("deleting " + f);
977-
boolean result = false;
978-
try {
979-
if(purge) {
980-
LOG.debug("purge is set to true. Not moving to Trash " + f);
981-
} else {
982-
result = Trash.moveToAppropriateTrash(fs, f, conf);
983-
if (result) {
984-
LOG.trace("Moved to trash: " + f);
985-
return true;
986-
}
987-
}
988-
} catch (IOException ioe) {
989-
// for whatever failure reason including that trash has lower encryption zone
990-
// retry with force delete
991-
LOG.warn(ioe.getMessage() + "; Force to delete it.");
992-
}
993-
994-
result = fs.delete(f, true);
995-
if (!result) {
996-
LOG.error("Failed to delete " + f);
997-
}
998-
return result;
999-
}
1000-
1001965
public static boolean rename(FileSystem fs, Path sourcePath,
1002966
Path destPath, Configuration conf) throws IOException {
1003967
LOG.info("Renaming " + sourcePath + " to " + destPath);

common/src/java/org/apache/hadoop/hive/conf/HiveConf.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,11 +1736,6 @@ public static enum ConfVars {
17361736
CLI_PROMPT("hive.cli.prompt", "hive",
17371737
"Command line prompt configuration value. Other hiveconf can be used in this configuration value. \n" +
17381738
"Variable substitution will only be invoked at the Hive CLI startup."),
1739-
/**
1740-
* @deprecated Use MetastoreConf.FS_HANDLER_CLS
1741-
*/
1742-
@Deprecated
1743-
HIVE_METASTORE_FS_HANDLER_CLS("hive.metastore.fs.handler.class", "org.apache.hadoop.hive.metastore.HiveMetaStoreFsImpl", ""),
17441739

17451740
// Things we log in the jobconf
17461741

ql/src/java/org/apache/hadoop/hive/ql/ddl/table/storage/archive/AlterTableArchiveUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static Path getInterMediateDir(Path dir, Configuration conf, ConfVars suffixConf
9797
static void deleteDir(Path dir, boolean shouldEnableCm, Configuration conf) throws HiveException {
9898
try {
9999
Warehouse wh = new Warehouse(conf);
100-
wh.deleteDir(dir, true, false, shouldEnableCm);
100+
wh.deleteDir(dir, false, shouldEnableCm);
101101
} catch (MetaException e) {
102102
throw new HiveException(e);
103103
}

ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5948,13 +5948,13 @@ public static boolean trashFiles(final FileSystem fs, final FileStatus[] statuse
59485948
final SessionState parentSession = SessionState.get();
59495949
for (final FileStatus status : statuses) {
59505950
if (null == pool) {
5951-
result &= FileUtils.moveToTrash(fs, status.getPath(), conf, purge);
5951+
result &= org.apache.hadoop.hive.metastore.utils.FileUtils.deleteDir(fs, status.getPath(), purge, conf);
59525952
} else {
59535953
futures.add(pool.submit(new Callable<Boolean>() {
59545954
@Override
59555955
public Boolean call() throws Exception {
59565956
SessionState.setCurrentSessionState(parentSession);
5957-
return FileUtils.moveToTrash(fs, status.getPath(), conf, purge);
5957+
return org.apache.hadoop.hive.metastore.utils.FileUtils.deleteDir(fs, status.getPath(), purge, conf);
59585958
}
59595959
}));
59605960
}

ql/src/java/org/apache/hadoop/hive/ql/metadata/SessionHiveMetaStoreClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ private void truncateTempTable(Table table) throws TException {
10621062
HdfsUtils.HadoopFileStatus status = HdfsUtils.HadoopFileStatus.createInstance(conf, fs, location);
10631063
FileStatus targetStatus = fs.getFileStatus(location);
10641064
String targetGroup = targetStatus == null ? null : targetStatus.getGroup();
1065-
FileUtils.moveToTrash(fs, location, conf, isSkipTrash);
1065+
org.apache.hadoop.hive.metastore.utils.FileUtils.deleteDir(fs, location, isSkipTrash, conf);
10661066
fs.mkdirs(location);
10671067
HdfsUtils.setFullFileStatus(conf, status, targetGroup, fs, location, false);
10681068
} else {
@@ -1124,7 +1124,7 @@ private void dropTempTable(Table table, boolean deleteData,
11241124
// Delete table data
11251125
if (deleteData && !isExternalTable(table)) {
11261126
try {
1127-
getWh().deleteDir(tablePath, true, ifPurge, false);
1127+
getWh().deleteDir(tablePath, ifPurge, false);
11281128
} catch (Exception err) {
11291129
LOG.error("Failed to delete temp table directory: " + tablePath, err);
11301130
// Forgive error
@@ -2215,7 +2215,7 @@ private boolean deletePartitionLocation(Partition partition, boolean purgeData)
22152215
Path path = getWh().getDnsPath(new Path(location));
22162216
try {
22172217
do {
2218-
if (!getWh().deleteDir(path, true, purgeData, false)) {
2218+
if (!getWh().deleteDir(path, purgeData, false)) {
22192219
throw new MetaException("Unable to delete partition at " + location);
22202220
}
22212221
path = path.getParent();

ql/src/java/org/apache/hadoop/hive/ql/parse/MetaDataExportListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void export_meta_data(PreDropTableEvent tableEvent) throws MetaException
9090
EximUtil.createExportDump(fs, outFile, mTbl, null, null,
9191
new HiveConf(conf, MetaDataExportListener.class));
9292
if (moveMetadataToTrash == true) {
93-
wh.deleteDir(metaPath, true, false, false);
93+
wh.deleteDir(metaPath, false, false);
9494
}
9595
} catch (IOException | SemanticException e) {
9696
throw new MetaException(e.getMessage());

ql/src/java/org/apache/hadoop/hive/ql/txn/compactor/FSRemover.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private List<Path> removeFiles(CleanupRequest cr)
122122
if (needCmRecycle) {
123123
replChangeManager.recycle(dead, ReplChangeManager.RecycleType.MOVE, cr.isPurge());
124124
}
125-
if (FileUtils.moveToTrash(fs, dead, conf, cr.isPurge())) {
125+
if (FileUtils.deleteDir(fs, dead, cr.isPurge(), conf)) {
126126
deleted.add(dead);
127127
}
128128
}

ql/src/test/org/apache/hadoop/hive/ql/metadata/TestSessionHiveMetastoreClientExchangePartitionsTempTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ private Table createNonTempTable(String dbName, String tableName, List<FieldSche
356356
}
357357

358358
private void cleanTempTableDir(Table table) throws MetaException {
359-
wh.deleteDir(new Path(table.getSd().getLocation()), true, false, false);
359+
wh.deleteDir(new Path(table.getSd().getLocation()), false, false);
360360
}
361361

362362
}

standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreFS.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/Warehouse.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
4141
import org.apache.hadoop.hive.metastore.utils.FileUtils;
4242
import org.apache.hadoop.hive.metastore.utils.HdfsUtils;
43-
import org.apache.hadoop.hive.metastore.utils.JavaUtils;
4443
import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
4544
import org.slf4j.Logger;
4645
import org.slf4j.LoggerFactory;
@@ -57,7 +56,6 @@
5756
import org.apache.hadoop.hive.metastore.api.Partition;
5857
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
5958
import org.apache.hadoop.hive.metastore.api.Table;
60-
import org.apache.hadoop.util.ReflectionUtils;
6159

6260
import static org.apache.hadoop.hive.common.AcidConstants.SOFT_DELETE_TABLE_PATTERN;
6361
import static org.apache.hadoop.hive.common.AcidConstants.SOFT_DELETE_PATH_SUFFIX;
@@ -83,7 +81,6 @@ public class Warehouse {
8381

8482
public static final Logger LOG = LoggerFactory.getLogger("hive.metastore.warehouse");
8583

86-
private MetaStoreFS fsHandler = null;
8784
private boolean storageAuthCheck = false;
8885
private ReplChangeManager cm = null;
8986

@@ -95,27 +92,11 @@ public Warehouse(Configuration conf) throws MetaException {
9592
+ " is not set in the config or blank");
9693
}
9794
whRootExternalString = MetastoreConf.getVar(conf, ConfVars.WAREHOUSE_EXTERNAL);
98-
fsHandler = getMetaStoreFsHandler(conf);
9995
cm = ReplChangeManager.getInstance(conf);
10096
storageAuthCheck = MetastoreConf.getBoolVar(conf, ConfVars.AUTHORIZATION_STORAGE_AUTH_CHECKS);
10197
isTenantBasedStorage = MetastoreConf.getBoolVar(conf, ConfVars.ALLOW_TENANT_BASED_STORAGE);
10298
}
10399

104-
private MetaStoreFS getMetaStoreFsHandler(Configuration conf)
105-
throws MetaException {
106-
String handlerClassStr = MetastoreConf.getVar(conf, ConfVars.FS_HANDLER_CLS);
107-
try {
108-
Class<? extends MetaStoreFS> handlerClass = (Class<? extends MetaStoreFS>) Class
109-
.forName(handlerClassStr, true, JavaUtils.getClassLoader());
110-
MetaStoreFS handler = ReflectionUtils.newInstance(handlerClass, conf);
111-
return handler;
112-
} catch (ClassNotFoundException e) {
113-
throw new MetaException("Error in loading MetaStoreFS handler."
114-
+ e.getMessage());
115-
}
116-
}
117-
118-
119100
/**
120101
* Helper functions to convert IOException to MetaException
121102
*/
@@ -451,15 +432,15 @@ void addToChangeManagement(Path file) throws MetaException {
451432
}
452433
}
453434

454-
public boolean deleteDir(Path f, boolean recursive, Database db) throws MetaException {
455-
return deleteDir(f, recursive, false, db);
435+
public boolean deleteDir(Path f, Database db) throws MetaException {
436+
return deleteDir(f, false, db);
456437
}
457438

458-
public boolean deleteDir(Path f, boolean recursive, boolean ifPurge, Database db) throws MetaException {
459-
return deleteDir(f, recursive, ifPurge, ReplChangeManager.isSourceOfReplication(db));
439+
public boolean deleteDir(Path f, boolean ifPurge, Database db) throws MetaException {
440+
return deleteDir(f, ifPurge, ReplChangeManager.isSourceOfReplication(db));
460441
}
461442

462-
public boolean deleteDir(Path f, boolean recursive, boolean ifPurge, boolean needCmRecycle) throws MetaException {
443+
public boolean deleteDir(Path f, boolean ifPurge, boolean needCmRecycle) throws MetaException {
463444
if (needCmRecycle) {
464445
try {
465446
cm.recycle(f, RecycleType.MOVE, ifPurge);
@@ -472,7 +453,13 @@ public boolean deleteDir(Path f, boolean recursive, boolean ifPurge, boolean nee
472453
LOG.warn("Har path {} is not supported to delete, skipping it.", f);
473454
return true;
474455
}
475-
return fsHandler.deleteDir(fs, f, recursive, ifPurge, conf);
456+
boolean delete = false;
457+
try {
458+
delete = FileUtils.deleteDir(fs, f, ifPurge, conf);
459+
} catch (IOException e) {
460+
MetaStoreUtils.throwMetaException(e);
461+
}
462+
return delete;
476463
}
477464

478465
public void recycleDirToCmPath(Path f, boolean ifPurge) throws MetaException {

0 commit comments

Comments
 (0)