From 980fab9168670f4c9f2fb596ea28e7613bb39d7b Mon Sep 17 00:00:00 2001 From: singer-bin Date: Sun, 6 Feb 2022 13:05:57 +0800 Subject: [PATCH] =?UTF-8?q?HDFS-16437=20ReverseXML=20processor=20doesn't?= =?UTF-8?q?=20accept=20XML=20files=20without=20the=20=E2=80=A6=20(#3926)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 125e3b616040b4f98956aa946cc51e99f7d596c2) Change-Id: I03e4f2af17f0e4a8245c9c2c8ea1cb2cb41f777a --- .../OfflineImageReconstructor.java | 4 ++ .../TestOfflineImageViewer.java | 42 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) 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 9ad4b090649..203bcc13284 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 @@ -1761,6 +1761,10 @@ class OfflineImageReconstructor { XMLEvent ev = expectTag("[section header]", true); if (ev.getEventType() == XMLStreamConstants.END_ELEMENT) { if (ev.asEndElement().getName().getLocalPart().equals("fsimage")) { + if(unprocessedSections.size() == 1 && unprocessedSections.contains + (SnapshotDiffSectionProcessor.NAME)){ + break; + } throw new IOException("FSImage XML ended prematurely, without " + "including section(s) " + StringUtils.join(", ", unprocessedSections)); 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 7bf3bfc1f8e..8980e18b68e 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 @@ -1122,17 +1122,17 @@ public class TestOfflineImageViewer { LOG.info("Creating reverseImage.xml=" + reverseImageXml.getAbsolutePath() + ", reverseImage=" + reverseImage.getAbsolutePath() + ", reverseImage2Xml=" + reverseImage2Xml.getAbsolutePath()); - if (OfflineImageViewerPB.run(new String[] { "-p", "XML", + if (OfflineImageViewerPB.run(new String[] {"-p", "XML", "-i", originalFsimage.getAbsolutePath(), "-o", reverseImageXml.getAbsolutePath() }) != 0) { throw new IOException("oiv returned failure creating first XML file."); } - if (OfflineImageViewerPB.run(new String[] { "-p", "ReverseXML", + if (OfflineImageViewerPB.run(new String[] {"-p", "ReverseXML", "-i", reverseImageXml.getAbsolutePath(), "-o", reverseImage.getAbsolutePath() }) != 0) { throw new IOException("oiv returned failure recreating fsimage file."); } - if (OfflineImageViewerPB.run(new String[] { "-p", "XML", + if (OfflineImageViewerPB.run(new String[] {"-p", "XML", "-i", reverseImage.getAbsolutePath(), "-o", reverseImage2Xml.getAbsolutePath() }) != 0) { throw new IOException("oiv returned failure creating second " + @@ -1141,7 +1141,7 @@ public class TestOfflineImageViewer { // The XML file we wrote based on the re-created fsimage should be the // same as the one we dumped from the original fsimage. Assert.assertEquals("", - GenericTestUtils.getFilesDiff(reverseImageXml, reverseImage2Xml)); + GenericTestUtils.getFilesDiff(reverseImageXml, reverseImage2Xml)); } /** @@ -1176,6 +1176,40 @@ public class TestOfflineImageViewer { } } + /** + * Tests that the ReverseXML processor doesn't accept XML files without the SnapshotDiffSection. + */ + @Test + public void testReverseXmlWithoutSnapshotDiffSection() throws Throwable { + File imageWSDS = new File(tempDir, "imageWithoutSnapshotDiffSection.xml"); + try(PrintWriter writer = new PrintWriter(imageWSDS, "UTF-8")) { + writer.println(""); + writer.println(""); + writer.println(""); + writer.println("-66"); + writer.println("1"); + writer.println("545bbef596c06af1c3c8dca1ce29096a64608478"); + writer.println(""); + writer.println(""); + writer.println(""); + writer.println("914880" + + ""); + writer.println("9035" + + "00" + + ""); + writer.println(""); + writer.println("00" + + ""); + writer.println("326384987"); + writer.println("10" + + "0"); + writer.println(""); + writer.println(""); + } + OfflineImageReconstructor.run(imageWSDS.getAbsolutePath(), + imageWSDS.getAbsolutePath() + ".out"); + } + @Test public void testFileDistributionCalculatorForException() throws Exception { File fsimageFile = null;