Skip to content

Commit

Permalink
HDFS-9110. Use Files.walkFileTree in NNUpgradeUtil#doPreUpgrade for b…
Browse files Browse the repository at this point in the history
…etter efficiency. Contributed by Charlie Helin.
  • Loading branch information
umbrant committed Oct 9, 2015
1 parent de8efc6 commit 357b1fd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
3 changes: 3 additions & 0 deletions hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt
Expand Up @@ -1503,6 +1503,9 @@ Release 2.8.0 - UNRELEASED
HDFS-9181. Better handling of exceptions thrown during upgrade shutdown.
(Wei-Chiu Chuang via Yongjun Zhang)

HDFS-9110. Use Files.walkFileTree in NNUpgradeUtil#doPreUpgrade for
better efficiency. (Charlie Helin via wang)

OPTIMIZATIONS

HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than
Expand Down
Expand Up @@ -18,10 +18,14 @@
package org.apache.hadoop.hdfs.server.namenode;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.util.List;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -31,7 +35,6 @@
import org.apache.hadoop.hdfs.server.common.StorageInfo;

import com.google.common.base.Preconditions;
import org.apache.hadoop.io.IOUtils;

public abstract class NNUpgradeUtil {

Expand Down Expand Up @@ -116,21 +119,30 @@ static void doPreUpgrade(Configuration conf, StorageDirectory sd)
// rename current to tmp
renameCurToTmp(sd);

final File curDir = sd.getCurrentDir();
final File tmpDir = sd.getPreviousTmp();
List<String> fileNameList = IOUtils.listDirectory(tmpDir, new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return dir.equals(tmpDir)
&& name.startsWith(NNStorage.NameNodeFile.EDITS.getName());
}
});

for (String s : fileNameList) {
File prevFile = new File(tmpDir, s);
File newFile = new File(curDir, prevFile.getName());
Files.createLink(newFile.toPath(), prevFile.toPath());
}
final Path curDir = sd.getCurrentDir().toPath();
final Path tmpDir = sd.getPreviousTmp().toPath();

Files.walkFileTree(tmpDir,
/* do not follow links */ Collections.<FileVisitOption>emptySet(),
1, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {

String name = file.getFileName().toString();

if (Files.isRegularFile(file)
&& name.startsWith(NNStorage.NameNodeFile.EDITS.getName())) {

Path newFile = curDir.resolve(name);
Files.createLink(newFile, file);
}

return super.visitFile(file, attrs);
}
}
);
}

/**
Expand Down

0 comments on commit 357b1fd

Please sign in to comment.