Skip to content

Commit 57c3b0e

Browse files
committed
HDFS-15463. Support validation over a fsimag directory.
1 parent 8eaffd4 commit 57c3b0e

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FsImageValidation.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* {@link org.apache.hadoop.hdfs.tools.offlineImageViewer.OfflineImageViewer}.
4848
*/
4949
public class FsImageValidation {
50-
static final String FS_IMAGE_FILE = "FS_IMAGE_FILE";
50+
static final String FS_IMAGE = "FS_IMAGE";
5151

5252
static HdfsConfiguration newHdfsConfiguration() {
5353
final HdfsConfiguration conf = new HdfsConfiguration();
@@ -61,19 +61,19 @@ static FsImageValidation newInstance(String... args) {
6161
final String f = Cli.parse(args);
6262
if (f == null) {
6363
throw new HadoopIllegalArgumentException(
64-
FS_IMAGE_FILE + " is not specified.");
64+
FS_IMAGE + " is not specified.");
6565
}
6666
return new FsImageValidation(new File(f));
6767
}
6868

69-
private final File fsImageFile;
69+
private final File fsImage;
7070

71-
FsImageValidation(File fsImageFile) {
72-
this.fsImageFile = fsImageFile;
71+
FsImageValidation(File fsImage) {
72+
this.fsImage = fsImage;
7373
}
7474

7575
int checkINodeReference() throws Exception {
76-
Cli.println("Check INodeReference for %d: %d", FS_IMAGE_FILE, fsImageFile);
76+
Cli.println("Check INodeReference for %s", fsImage);
7777

7878
final TimerTask checkProgress = new TimerTask() {
7979
@Override
@@ -83,29 +83,39 @@ public void run() {
8383
Cli.println("%s Progress: %.1f%%", Phase.LOADING_FSIMAGE, 100*percent);
8484
}
8585
};
86-
final Timer t = new Timer();
87-
t.scheduleAtFixedRate(checkProgress, 0, 60_000);
8886

8987
final HdfsConfiguration conf = newHdfsConfiguration();
90-
final FSImage fsImage = new FSImage(conf);
91-
final FSNamesystem namesystem = new FSNamesystem(conf, fsImage, false);
9288

93-
final NamespaceInfo namespaceInfo = NNStorage.newNamespaceInfo();
94-
namespaceInfo.clusterID = "cluster0";
95-
fsImage.getStorage().setStorageInfo(namespaceInfo);
96-
97-
final FSImageFormat.LoaderDelegator loader
98-
= FSImageFormat.newLoader(conf, namesystem);
9989
INodeReferenceValidation.start();
100-
namesystem.writeLock();
101-
namesystem.getFSDirectory().writeLock();
102-
try {
103-
loader.load(fsImageFile, false);
104-
} finally {
105-
namesystem.getFSDirectory().writeUnlock();
106-
namesystem.writeUnlock();
107-
t.cancel();
90+
final Timer t = new Timer();
91+
t.scheduleAtFixedRate(checkProgress, 0, 60_000);
92+
if (fsImage.isDirectory()) {
93+
Cli.println("Loading %s as a directory.", fsImage);
94+
final String dir = fsImage.getCanonicalPath();
95+
conf.set(DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY, dir);
96+
conf.set(DFSConfigKeys.DFS_NAMENODE_EDITS_DIR_KEY, dir);
97+
FSNamesystem.loadFromDisk(conf);
98+
} else {
99+
Cli.println("Loading %s as a file.", fsImage);
100+
final FSImage fsImage = new FSImage(conf);
101+
final FSNamesystem namesystem = new FSNamesystem(conf, fsImage, false);
102+
103+
final NamespaceInfo namespaceInfo = NNStorage.newNamespaceInfo();
104+
namespaceInfo.clusterID = "cluster0";
105+
fsImage.getStorage().setStorageInfo(namespaceInfo);
106+
107+
final FSImageFormat.LoaderDelegator loader
108+
= FSImageFormat.newLoader(conf, namesystem);
109+
namesystem.writeLock();
110+
namesystem.getFSDirectory().writeLock();
111+
try {
112+
loader.load(this.fsImage, false);
113+
} finally {
114+
namesystem.getFSDirectory().writeUnlock();
115+
namesystem.writeUnlock();
116+
}
108117
}
118+
t.cancel();
109119
return INodeReferenceValidation.end();
110120
}
111121

@@ -115,10 +125,9 @@ static class Cli extends Configured implements Tool {
115125
static {
116126
final String clazz = FsImageValidation.class.getSimpleName();
117127
COMMAND = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
118-
USAGE = "Usage: hdfs " + COMMAND + " <" + FS_IMAGE_FILE + ">";
128+
USAGE = "Usage: hdfs " + COMMAND + " <" + FS_IMAGE + ">";
119129
}
120130

121-
122131
@Override
123132
public int run(String[] args) throws Exception {
124133
final FsImageValidation validation = FsImageValidation.newInstance(args);
@@ -130,9 +139,9 @@ public int run(String[] args) throws Exception {
130139
static String parse(String... args) {
131140
final String f;
132141
if (args == null || args.length == 0) {
133-
f = System.getenv().get(FS_IMAGE_FILE);
142+
f = System.getenv().get(FS_IMAGE);
134143
if (f != null) {
135-
println("Environment variable %s = %s", FS_IMAGE_FILE, f);
144+
println("Environment variable %s = %s", FS_IMAGE, f);
136145
}
137146
} else if (args.length == 1) {
138147
f = args[0];
@@ -141,7 +150,7 @@ static String parse(String... args) {
141150
"args = " + Arrays.toString(args));
142151
}
143152

144-
println("%s = %s", FS_IMAGE_FILE, f);
153+
println("%s = %s", FS_IMAGE, f);
145154
return f;
146155
}
147156

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestFsImageValidation.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.hadoop.HadoopIllegalArgumentException;
2121
import org.apache.hadoop.test.GenericTestUtils;
22+
import org.junit.Assert;
2223
import org.junit.Test;
2324
import org.slf4j.event.Level;
2425

@@ -37,10 +38,11 @@ public class TestFsImageValidation {
3738
public void testINodeReference() throws Exception {
3839
try {
3940
final FsImageValidation validation = FsImageValidation.newInstance();
40-
validation.checkINodeReference();
41+
final int errorCount = validation.checkINodeReference();
42+
Assert.assertEquals("Error Count: " + errorCount, 0, errorCount);
4143
} catch (HadoopIllegalArgumentException e) {
4244
FsImageValidation.Cli.printError("The environment variable "
43-
+ FsImageValidation.FS_IMAGE_FILE + " is not set.", e);
45+
+ FsImageValidation.FS_IMAGE + " is not set.", e);
4446
}
4547
}
4648
}

0 commit comments

Comments
 (0)