Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.hadoop.fs.Options.ChecksumOpt;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.security.AccessControlException;
import org.apache.hadoop.util.CheckedBiFunction;
import org.apache.hadoop.util.DataChecksum;
import org.apache.hadoop.util.Progressable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -457,17 +458,35 @@ public boolean setReplication(Path src, short replication)
@Override
public void renameInternal(Path src, Path dst)
throws IOException, UnresolvedLinkException {
renameInternal(src, dst, (s, d) -> getMyFs().rename(s, d));
}

@Override
public void renameInternal(Path src, Path dst, boolean overwrite)
throws AccessControlException, FileAlreadyExistsException,
FileNotFoundException, ParentNotDirectoryException,
UnresolvedLinkException, IOException {
Options.Rename renameOpt = Options.Rename.NONE;
if (overwrite) {
renameOpt = Options.Rename.OVERWRITE;
}
final Options.Rename opt = renameOpt;
renameInternal(src, dst, (s, d) -> getMyFs().rename(s, d, opt));
Copy link

@skonto skonto Aug 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be wrong to delegate to myFs like here:


Just curious. What is the concrete class of the AbastractFileSystem field?

Copy link
Contributor Author

@HeartSaVioR HeartSaVioR Sep 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChecksumFs should deal with checksum file by itself (others even don't have to know the existence of checksum - once all operations are pass through ChecksumFs's override methods): that's why the method should be override. Just delegating rename would have same result, checksum file leak.

What is the concrete class of the AbastractFileSystem field?

If the source file is from local filesystem, I think it's RawLocalFs. It could be DelegateToFileSystem, but it will redirect to RawLocalFs eventually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this, its the more elegant functional API. Which is nice for hadoop 3+. But I fear its probably going to lose all that elegance on branch-2 (assuming you do want a backport). If you do, then simply copying the existing renameInternal to one with a new signature is going to be the simplest

}

private void renameInternal(Path src, Path dst,
CheckedBiFunction<Path, Path, IOException> renameFn) throws IOException {
if (isDirectory(src)) {
getMyFs().rename(src, dst);
renameFn.apply(src, dst);
} else {
getMyFs().rename(src, dst);
renameFn.apply(src, dst);

Path checkFile = getChecksumFile(src);
if (exists(checkFile)) { //try to rename checksum
if (isDirectory(dst)) {
getMyFs().rename(checkFile, dst);
renameFn.apply(checkFile, dst);
} else {
getMyFs().rename(checkFile, getChecksumFile(dst));
renameFn.apply(checkFile, getChecksumFile(dst));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.util;

import java.io.IOException;

/**
* Defines a functional interface having two inputs which throws IOException.
*/
@FunctionalInterface
public interface CheckedBiFunction<LEFT, RIGHT, THROWABLE extends IOException> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cute! I never knew you could do that with templates and exceptions!

  1. can you put into org.apache.hadoop.fs.impl where the other internal-for-fs-only lambda stuff is going.
  2. be advised that for backports to branch 2 we will have to make things compile on Java 8. Mostly this is just using the IDE to convert things to callables. Doesn't mean they shouldn't be used, only that once you get sufficiently advanced things become unbackportable. This patch looks fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually CheckedBiFunction is already available in other place (HDDS) - I feel these interfaces/classes would be better to be moved into common module when Hadoop could forget about JDK7.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, they should. I'm doing in new code that I know isn't going to be backportable into jdk7, not now we have to worry about jdk11

void apply(LEFT left, RIGHT right) throws THROWABLE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.fs;

import java.io.IOException;
import java.util.EnumSet;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.local.LocalFs;
import org.apache.hadoop.fs.permission.FsPermission;
import static org.apache.hadoop.fs.CreateFlag.*;
import org.apache.hadoop.test.GenericTestUtils;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
* This class tests the functionality of ChecksumFs.
*/
public class TestChecksumFs {
private Configuration conf;
private Path testRootDirPath;
private FileContext fc;

@Before
public void setUp() throws Exception {
conf = getTestConfiguration();
fc = FileContext.getFileContext(conf);
testRootDirPath = new Path(GenericTestUtils.getRandomizedTestDir()
.getAbsolutePath());
mkdirs(testRootDirPath);
}

public void tearDown() throws Exception {
fc.delete(testRootDirPath, true);
}

@Test
public void testRenameFileToFile() throws Exception {
Path srcPath = new Path(testRootDirPath, "testRenameSrc");
Path dstPath = new Path(testRootDirPath, "testRenameDst");
verifyRename(srcPath, dstPath, false);
}

@Test
public void testRenameFileToFileWithOverwrite() throws Exception {
Path srcPath = new Path(testRootDirPath, "testRenameSrc");
Path dstPath = new Path(testRootDirPath, "testRenameDst");
verifyRename(srcPath, dstPath, true);
}

@Test
public void testRenameFileIntoDirFile() throws Exception {
Path srcPath = new Path(testRootDirPath, "testRenameSrc");
Path dstPath = new Path(testRootDirPath, "testRenameDir/testRenameDst");
mkdirs(dstPath);
verifyRename(srcPath, dstPath, false);
}

@Test
public void testRenameFileIntoDirFileWithOverwrite() throws Exception {
Path srcPath = new Path(testRootDirPath, "testRenameSrc");
Path dstPath = new Path(testRootDirPath, "testRenameDir/testRenameDst");
mkdirs(dstPath);
verifyRename(srcPath, dstPath, true);
}

private void verifyRename(Path srcPath, Path dstPath,
boolean overwrite) throws Exception {
AbstractFileSystem fs = fc.getDefaultFileSystem();
assertTrue(fs instanceof LocalFs);
ChecksumFs checksumFs = (ChecksumFs) fs;

fs.delete(srcPath, true);
fs.delete(dstPath, true);

Options.Rename renameOpt = Options.Rename.NONE;
if (overwrite) {
renameOpt = Options.Rename.OVERWRITE;
createTestFile(checksumFs, dstPath, 2);
}

// ensure file + checksum are moved
createTestFile(checksumFs, srcPath, 1);
assertTrue(fc.util().exists(checksumFs.getChecksumFile(srcPath)));
checksumFs.rename(srcPath, dstPath, renameOpt);
assertTrue(fc.util().exists(checksumFs.getChecksumFile(dstPath)));
try (FSDataInputStream is = fs.open(dstPath)) {
assertEquals(1, is.readInt());
}
}

private static Configuration getTestConfiguration() {
Configuration conf = new Configuration(false);
conf.set("fs.defaultFS", "file:///");
conf.setClass("fs.AbstractFileSystem.file.impl",
org.apache.hadoop.fs.local.LocalFs.class,
org.apache.hadoop.fs.AbstractFileSystem.class);
return conf;
}

private void createTestFile(ChecksumFs fs, Path path, int content)
throws IOException {
try (FSDataOutputStream fout = fs.create(path,
EnumSet.of(CREATE, OVERWRITE),
Options.CreateOpts.perms(FsPermission.getDefault()))) {
fout.writeInt(content);
}
}

private void mkdirs(Path dirPath) throws IOException {
fc.mkdir(dirPath, FileContext.DEFAULT_PERM, true);
}
}