Skip to content

Commit 5d6280b

Browse files
author
Xing Lin
committed
HADOOP-18444 Add Support for localized trash for ViewFileSystem in Trash.moveToAppropriateTrash
Signed-off-by: Xing Lin <[email protected]>
1 parent be4c638 commit 5d6280b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Trash.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import org.apache.hadoop.classification.InterfaceStability;
2424
import org.apache.hadoop.conf.Configuration;
2525
import org.apache.hadoop.conf.Configured;
26+
import org.apache.hadoop.fs.viewfs.ViewFileSystem;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
29+
import static org.apache.hadoop.fs.viewfs.Constants.*;
2830

2931
/**
3032
* Provides a trash facility which supports pluggable Trash policies.
@@ -94,6 +96,27 @@ public static boolean moveToAppropriateTrash(FileSystem fs, Path p,
9496
LOG.warn("Failed to get server trash configuration", e);
9597
throw new IOException("Failed to get server trash configuration", e);
9698
}
99+
100+
// Directly use viewfs if localized trash is enabled
101+
if (fs instanceof ViewFileSystem &&
102+
conf.getBoolean(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT,
103+
CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT_DEFAULT)) {
104+
// Save the original config in savedValue for localized trash config.
105+
String savedValue = fs.getConf().get(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT);
106+
fs.getConf().setBoolean(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT, true);
107+
Trash trash = new Trash(fs, conf);
108+
boolean res = trash.moveToTrash(p);
109+
110+
// Restore the original value of localized trash config
111+
if (savedValue != null) {
112+
fs.getConf().set(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT, savedValue);
113+
} else {
114+
fs.getConf().unset(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT);
115+
}
116+
117+
return res;
118+
}
119+
97120
Trash trash = new Trash(fullyResolvedFs, conf);
98121
return trash.moveToTrash(fullyResolvedPath);
99122
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/TestViewFsTrash.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@
1717
*/
1818
package org.apache.hadoop.fs.viewfs;
1919

20+
import java.io.DataOutputStream;
21+
import java.io.IOException;
2022
import org.apache.hadoop.conf.Configuration;
2123
import org.apache.hadoop.fs.FileSystem;
2224
import org.apache.hadoop.fs.FileSystemTestHelper;
2325
import org.apache.hadoop.fs.FsConstants;
26+
import org.apache.hadoop.fs.LocalFileSystem;
2427
import org.apache.hadoop.fs.Path;
2528
import org.apache.hadoop.fs.TestTrash;
29+
import org.apache.hadoop.fs.Trash;
30+
import org.apache.hadoop.fs.TrashPolicyDefault;
2631
import org.junit.After;
2732
import org.junit.Before;
2833
import org.junit.Test;
34+
import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.*;
35+
import static org.apache.hadoop.fs.viewfs.Constants.*;
36+
import static org.junit.Assert.*;
2937

3038
public class TestViewFsTrash {
3139
FileSystem fsTarget; // the target file system - the mount will point here
@@ -65,5 +73,39 @@ public void testTrash() throws Exception {
6573
TestTrash.trashShell(conf, fileSystemTestHelper.getTestRootPath(fsView),
6674
fsView, new Path(fileSystemTestHelper.getTestRootPath(fsView), ".Trash/Current"));
6775
}
68-
76+
77+
@Test
78+
public void testLocalizedTrashInMoveToAppropriateTrash() throws IOException {
79+
Configuration conf2 = new Configuration(conf);
80+
81+
// Enable moveToTrash and add a mount point for /data
82+
conf2.setLong(FS_TRASH_INTERVAL_KEY, 1);
83+
ConfigUtil.addLink(conf2, "/data", new Path(fileSystemTestHelper.getAbsoluteTestRootPath(fsTarget), "data").toUri());
84+
85+
// Default case. file should be moved to fsTarget.getTrashRoot()/resolvedPath
86+
conf2.setBoolean(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT, false);
87+
FileSystem fsView2 = FileSystem.get(conf2);
88+
Path f = new Path("/data/testfile.txt");
89+
DataOutputStream out = fsView2.create(f);
90+
out.writeBytes("testfile.txt:" + f);
91+
out.close();
92+
Path resolvedFile = fsView2.resolvePath(f);
93+
94+
Trash.moveToAppropriateTrash(fsView2, f, conf2);
95+
Trash trash = new Trash(fsTarget, conf2);
96+
Path movedPath = Path.mergePaths(trash.getCurrentTrashDir(f), resolvedFile);
97+
assertTrue("File not in trash", fsTarget.exists(movedPath));
98+
99+
// Turn on localized trash. File should be moved to viewfs:/data/.Trash/{user}/Current.
100+
conf2.setBoolean(CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT, true);
101+
fsView2 = FileSystem.get(conf2);
102+
out = fsView2.create(f);
103+
out.writeBytes("testfile.txt:" + f);
104+
out.close();
105+
106+
Trash.moveToAppropriateTrash(fsView2, f, conf2);
107+
trash = new Trash(fsView2, conf2);
108+
movedPath = Path.mergePaths(trash.getCurrentTrashDir(f), f);
109+
assertTrue("File not in localized trash", fsView2.exists(movedPath));
110+
}
69111
}

0 commit comments

Comments
 (0)