diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java
index 7d0a4924f13..65abc6f41cb 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageReconstructor.java
@@ -1353,9 +1353,11 @@ class OfflineImageReconstructor {
if (sd == null) {
break;
}
- Long dir = sd.removeChildLong(SNAPSHOT_SECTION_DIR);
- sd.verifyNoRemainingKeys("
");
- bld.addSnapshottableDir(dir);
+ Long dir;
+ while ((dir = sd.removeChildLong(SNAPSHOT_SECTION_DIR)) != null) {
+ // Add all snapshottable directories, one by one
+ bld.addSnapshottableDir(dir);
+ }
}
header.verifyNoRemainingKeys("SnapshotSection");
bld.build().writeDelimitedTo(out);
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java
index 2fdba752416..8a3e3d0990b 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/TestOfflineImageViewer.java
@@ -251,6 +251,16 @@ public class TestOfflineImageViewer {
hdfs.truncate(file1, 1);
writtenFiles.put(file1.toString(), hdfs.getFileStatus(file1));
+ // HDFS-14148: Create a second snapshot-enabled directory. This can cause
+ // TestOfflineImageViewer#testReverseXmlRoundTrip to fail before the patch
+ final Path snapshotDir2 = new Path("/snapshotDir2");
+ hdfs.mkdirs(snapshotDir2);
+ // Simply enable snapshot on it, no need to create one
+ hdfs.allowSnapshot(snapshotDir2);
+ dirCount++;
+ writtenFiles.put(snapshotDir2.toString(),
+ hdfs.getFileStatus(snapshotDir2));
+
// Set XAttrs so the fsimage contains XAttr ops
final Path xattr = new Path("/xattr");
hdfs.mkdirs(xattr);