Skip to content

Commit 6ba682c

Browse files
committed
HIVE-29117: Refactor deleteDir and moveToTrash
1 parent dcb1b9e commit 6ba682c

4 files changed

Lines changed: 58 additions & 70 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ public interface MetaStoreFS {
3333
*
3434
* @param f
3535
* @param ifPurge
36-
* @param recursive
3736
* @return true on success
3837
* @throws MetaException
3938
*/
40-
public boolean deleteDir(FileSystem fs, Path f, boolean recursive,
39+
public boolean deleteDir(FileSystem fs, Path f,
4140
boolean ifPurge, Configuration conf) throws MetaException;
4241

4342
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public boolean deleteDir(Path f, boolean recursive, boolean ifPurge, boolean nee
472472
LOG.warn("Har path {} is not supported to delete, skipping it.", f);
473473
return true;
474474
}
475-
return fsHandler.deleteDir(fs, f, recursive, ifPurge, conf);
475+
return fsHandler.deleteDir(fs, f, ifPurge, conf);
476476
}
477477

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

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

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@
3535
import org.apache.hadoop.fs.PathFilter;
3636
import org.apache.hadoop.fs.RemoteIterator;
3737
import org.apache.hadoop.fs.Trash;
38-
import org.apache.hadoop.hdfs.DistributedFileSystem;
39-
import org.apache.hadoop.hdfs.protocol.SnapshotException;
4038
import org.apache.hadoop.hive.metastore.api.MetaException;
4139
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
4240
import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
43-
import org.apache.hadoop.ipc.RemoteException;
4441
import org.slf4j.Logger;
4542
import org.slf4j.LoggerFactory;
4643

@@ -77,72 +74,19 @@ public boolean accept(Path p) {
7774
* @param f path of file or directory to move to trash.
7875
* @param conf configuration object
7976
* @return true if move successful
80-
* @throws IOException
8177
*/
82-
public static boolean moveToTrash(FileSystem fs, Path f, Configuration conf, boolean purge)
83-
throws IOException {
84-
LOG.debug("deleting " + f);
85-
boolean result;
78+
public static boolean moveToTrash(FileSystem fs, Path f, Configuration conf) {
79+
LOG.debug("moving " + f + " to trash");
8680
try {
87-
if (!fs.exists(f)) {
88-
LOG.warn("The path to moveToTrash does not exist: " + f);
81+
boolean result = Trash.moveToAppropriateTrash(fs, f, conf);
82+
if (result) {
83+
LOG.trace("Moved to trash: " + f);
8984
return true;
9085
}
91-
if (purge) {
92-
LOG.debug("purge is set to true. Not moving to Trash " + f);
93-
} else {
94-
result = Trash.moveToAppropriateTrash(fs, f, conf);
95-
if (result) {
96-
LOG.trace("Moved to trash: " + f);
97-
return true;
98-
}
99-
}
10086
} catch (IOException ioe) {
101-
// for whatever failure reason including that trash has lower encryption zone
102-
// retry with force delete
103-
LOG.warn(ioe.getMessage() + "; Force to delete it.");
104-
}
105-
106-
try {
107-
result = fs.delete(f, true);
108-
109-
} catch (RemoteException | SnapshotException se) {
110-
// If this is snapshot exception or the cause is snapshot replication from HDFS, could be the case where the
111-
// snapshots were created by replication, so in that case attempt to delete the replication related snapshots,
112-
// if the exists and then re attempt delete.
113-
if (se instanceof SnapshotException || se.getCause() instanceof SnapshotException || se.getMessage()
114-
.contains("Snapshot"))
115-
deleteReplRelatedSnapshots(fs, f);
116-
// retry delete after attempting to delete replication related snapshots
117-
result = fs.delete(f, true);
118-
}
119-
if (!result) {
120-
LOG.error("Failed to delete " + f);
121-
}
122-
return result;
123-
}
124-
125-
/**
126-
* Attempts to delete the replication related snapshots
127-
* @param fs the filesystem
128-
* @param path path where the snapshots are supposed to exists.
129-
*/
130-
private static void deleteReplRelatedSnapshots(FileSystem fs, Path path) {
131-
try {
132-
DistributedFileSystem dfs = (DistributedFileSystem) fs;
133-
// List the snapshot directory.
134-
FileStatus[] listing = fs.listStatus(new Path(path, ".snapshot"));
135-
for (FileStatus elem : listing) {
136-
// if the snapshot name has replication related suffix, then delete that snapshot.
137-
if (elem.getPath().getName().endsWith("replOld") || elem.getPath().getName().endsWith("replNew")) {
138-
dfs.deleteSnapshot(path, elem.getPath().getName());
139-
}
140-
}
141-
} catch (Exception ioe) {
142-
// Ignore since this method is used as part of purge which actually ignores all exception, if the directory can
143-
// not be deleted, so preserve the same behaviour.
144-
LOG.warn("Couldn't clean up replication related snapshots", ioe);
87+
LOG.warn("Failed to move path to trash: " + f, ioe);
14588
}
89+
return false;
14690
}
14791

14892
/**

standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStoreFsImpl.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020

2121
import org.apache.hadoop.hive.metastore.utils.FileUtils;
2222
import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils;
23+
import org.apache.hadoop.ipc.RemoteException;
2324
import org.slf4j.Logger;
2425
import org.slf4j.LoggerFactory;
2526
import org.apache.hadoop.conf.Configuration;
27+
import org.apache.hadoop.fs.FileStatus;
2628
import org.apache.hadoop.fs.FileSystem;
2729
import org.apache.hadoop.fs.Path;
30+
import org.apache.hadoop.hdfs.DistributedFileSystem;
31+
import org.apache.hadoop.hdfs.protocol.SnapshotException;
2832
import org.apache.hadoop.hive.metastore.api.MetaException;
2933

3034
public class HiveMetaStoreFsImpl implements MetaStoreFS {
@@ -33,15 +37,56 @@ public class HiveMetaStoreFsImpl implements MetaStoreFS {
3337
.getLogger("hive.metastore.hivemetastoreFsimpl");
3438

3539
@Override
36-
public boolean deleteDir(FileSystem fs, Path f, boolean recursive,
37-
boolean ifPurge, Configuration conf) throws MetaException {
40+
public boolean deleteDir(FileSystem fs, Path f, boolean ifPurge, Configuration conf)
41+
throws MetaException {
3842
try {
39-
if (FileUtils.moveToTrash(fs, f, conf, ifPurge)) {
43+
if (!fs.exists(f)) {
44+
LOG.warn("The path to delete does not exist: " + f);
4045
return true;
4146
}
47+
if (!ifPurge && FileUtils.moveToTrash(fs, f, conf)) {
48+
return true;
49+
}
50+
try {
51+
// for whatever failure reason including that trash has lower encryption zone
52+
// retry with force delete
53+
fs.delete(f, true);
54+
} catch (RemoteException | SnapshotException se) {
55+
// If this is snapshot exception or the cause is snapshot replication from HDFS, could be the case where the
56+
// snapshots were created by replication, so in that case attempt to delete the replication related snapshots,
57+
// if the exists and then re attempt delete.
58+
if (se instanceof SnapshotException || se.getCause() instanceof SnapshotException || se.getMessage()
59+
.contains("Snapshot"))
60+
deleteReplRelatedSnapshots(fs, f);
61+
// retry delete after attempting to delete replication related snapshots
62+
fs.delete(f, true);
63+
}
4264
} catch (Exception e) {
4365
MetaStoreUtils.throwMetaException(e);
4466
}
45-
throw new MetaException("Unable to delete directory: " + f);
67+
return true;
68+
}
69+
70+
/**
71+
* Attempts to delete the replication related snapshots
72+
* @param fs the filesystem
73+
* @param path path where the snapshots are supposed to exists.
74+
*/
75+
private static void deleteReplRelatedSnapshots(FileSystem fs, Path path) {
76+
try {
77+
DistributedFileSystem dfs = (DistributedFileSystem) fs;
78+
// List the snapshot directory.
79+
FileStatus[] listing = fs.listStatus(new Path(path, ".snapshot"));
80+
for (FileStatus elem : listing) {
81+
// if the snapshot name has replication related suffix, then delete that snapshot.
82+
if (elem.getPath().getName().endsWith("replOld") || elem.getPath().getName().endsWith("replNew")) {
83+
dfs.deleteSnapshot(path, elem.getPath().getName());
84+
}
85+
}
86+
} catch (Exception ioe) {
87+
// Ignore since this method is used as part of purge which actually ignores all exception, if the directory can
88+
// not be deleted, so preserve the same behaviour.
89+
LOG.warn("Couldn't clean up replication related snapshots", ioe);
90+
}
4691
}
4792
}

0 commit comments

Comments
 (0)