Skip to content

Commit 3ed442c

Browse files
committed
HDFS-16192: ViewDistributedFileSystem#rename wrongly using src in the place of dst.
1 parent 16e6030 commit 3ed442c

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,4 +1872,19 @@ public static FileContext write(final FileContext fileContext,
18721872
final Path path, final CharSequence charseq) throws IOException {
18731873
return write(fileContext, path, charseq, StandardCharsets.UTF_8);
18741874
}
1875+
1876+
@InterfaceAudience.LimitedPrivate({"ViewDistributedFileSystem"})
1877+
@InterfaceStability.Unstable
1878+
/**
1879+
* Used in ViewDistributedFileSystem rename API to get access to the protected
1880+
* API of FileSystem interface. Even though Rename with options API
1881+
* deprecated, we are still using as part of trash. If any filesystem provided
1882+
* implementation to this protected FileSystem API, we can't invoke it with
1883+
* out casting to the specific filesystem. This util method is proposed to get
1884+
* the access to FileSystem#rename with options.
1885+
*/
1886+
public static void rename(FileSystem srcFs, Path src, Path dst,
1887+
final Options.Rename... options) throws IOException {
1888+
srcFs.rename(src, dst, options);
1889+
}
18751890
}

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/ViewDistributedFileSystem.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.hadoop.fs.FileEncryptionInfo;
3434
import org.apache.hadoop.fs.FileStatus;
3535
import org.apache.hadoop.fs.FileSystem;
36+
import org.apache.hadoop.fs.FileUtil;
3637
import org.apache.hadoop.fs.FsServerDefaults;
3738
import org.apache.hadoop.fs.FsStatus;
3839
import org.apache.hadoop.fs.LocatedFileStatus;
@@ -537,14 +538,11 @@ public void rename(Path src, Path dst, final Options.Rename... options)
537538
return;
538539
}
539540

540-
// TODO: revisit
541541
ViewFileSystemOverloadScheme.MountPathInfo<FileSystem> mountSrcPathInfo =
542542
this.vfs.getMountPathInfo(src, getConf());
543-
checkDFS(mountSrcPathInfo.getTargetFs(), "rename");
544543

545544
ViewFileSystemOverloadScheme.MountPathInfo<FileSystem> mountDstPathInfo =
546-
this.vfs.getMountPathInfo(src, getConf());
547-
checkDFS(mountDstPathInfo.getTargetFs(), "rename");
545+
this.vfs.getMountPathInfo(dst, getConf());
548546

549547
//Check both in same cluster.
550548
if (!mountSrcPathInfo.getTargetFs().getUri()
@@ -553,9 +551,9 @@ public void rename(Path src, Path dst, final Options.Rename... options)
553551
"Can't rename across file systems.");
554552
}
555553

556-
((DistributedFileSystem) mountSrcPathInfo.getTargetFs())
557-
.rename(mountSrcPathInfo.getPathOnTarget(),
558-
mountDstPathInfo.getPathOnTarget(), options);
554+
FileUtil.rename(mountSrcPathInfo.getTargetFs(),
555+
mountSrcPathInfo.getPathOnTarget(), mountDstPathInfo.getPathOnTarget(),
556+
options);
559557
}
560558

561559
@Override

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestViewDistributedFileSystem.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import org.apache.hadoop.conf.Configuration;
2121
import org.apache.hadoop.fs.CommonConfigurationKeys;
2222
import org.apache.hadoop.fs.FileSystem;
23+
import org.apache.hadoop.fs.Options;
2324
import org.apache.hadoop.fs.Path;
2425
import org.apache.hadoop.fs.PathHandle;
2526
import org.apache.hadoop.fs.viewfs.ConfigUtil;
2627
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
2728
import org.apache.hadoop.test.Whitebox;
29+
import org.junit.Assert;
2830
import org.junit.Test;
2931

3032
import java.io.IOException;
@@ -89,4 +91,29 @@ public void testEmptyDelegationToken() throws IOException {
8991
}
9092
}
9193
}
94+
95+
@Test
96+
public void testRenameWithOptions() throws IOException {
97+
Configuration conf = getTestConfiguration();
98+
MiniDFSCluster cluster = null;
99+
try {
100+
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
101+
URI defaultUri =
102+
URI.create(conf.get(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY));
103+
conf.set("fs.viewfs.mounttable." + defaultUri.getHost() + ".linkFallback",
104+
defaultUri.toString());
105+
conf.setLong(CommonConfigurationKeys.FS_TRASH_INTERVAL_KEY, 30000);
106+
try (ViewDistributedFileSystem fileSystem = (ViewDistributedFileSystem) FileSystem.get(conf)) {
107+
final Path testDir = new Path("/test");
108+
final Path renameDir = new Path("/testRename");
109+
fileSystem.mkdirs(testDir);
110+
fileSystem.rename(testDir, renameDir, Options.Rename.TO_TRASH);
111+
Assert.assertTrue(fileSystem.exists(renameDir));
112+
}
113+
} finally {
114+
if (cluster != null) {
115+
cluster.shutdown();
116+
}
117+
}
118+
}
92119
}

0 commit comments

Comments
 (0)