HDFS-9934. ReverseXML oiv processor should bail out if the XML file's layoutVersion doesn't match oiv's (cmccabe)

This commit is contained in:
Colin Patrick Mccabe 2016-03-10 13:41:06 -08:00
parent 500875dfcc
commit bd49354c6d
2 changed files with 45 additions and 0 deletions

View File

@ -68,6 +68,7 @@ import org.apache.hadoop.hdfs.server.namenode.FsImageProto.INodeSection;
import org.apache.hadoop.hdfs.server.namenode.FsImageProto.NameSystemSection; import org.apache.hadoop.hdfs.server.namenode.FsImageProto.NameSystemSection;
import org.apache.hadoop.hdfs.server.namenode.FsImageProto.SecretManagerSection; import org.apache.hadoop.hdfs.server.namenode.FsImageProto.SecretManagerSection;
import org.apache.hadoop.hdfs.server.namenode.FsImageProto.SnapshotDiffSection.DiffEntry; import org.apache.hadoop.hdfs.server.namenode.FsImageProto.SnapshotDiffSection.DiffEntry;
import org.apache.hadoop.hdfs.server.namenode.NameNodeLayoutVersion;
import org.apache.hadoop.hdfs.util.MD5FileUtils; import org.apache.hadoop.hdfs.util.MD5FileUtils;
import org.apache.hadoop.hdfs.util.XMLUtils; import org.apache.hadoop.hdfs.util.XMLUtils;
import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.IOUtils;
@ -1493,6 +1494,16 @@ class OfflineImageReconstructor {
throw new IOException("The <version> section doesn't contain " + throw new IOException("The <version> section doesn't contain " +
"the layoutVersion."); "the layoutVersion.");
} }
if (layoutVersion.intValue() !=
NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION) {
throw new IOException("Layout version mismatch. This oiv tool " +
"handles layout version " +
NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION + ", but the " +
"XML file has <layoutVersion> " + layoutVersion + ". Please " +
"either re-generate the XML file with the proper layout version, " +
"or manually edit the XML file to be usable with this version " +
"of the oiv tool.");
}
fileSummaryBld.setOndiskVersion(onDiskVersion); fileSummaryBld.setOndiskVersion(onDiskVersion);
fileSummaryBld.setLayoutVersion(layoutVersion); fileSummaryBld.setLayoutVersion(layoutVersion);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {

View File

@ -29,6 +29,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.io.StringReader; import java.io.StringReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -64,6 +65,7 @@ import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction; import org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction;
import org.apache.hadoop.hdfs.server.namenode.FSImageTestUtil; import org.apache.hadoop.hdfs.server.namenode.FSImageTestUtil;
import org.apache.hadoop.hdfs.server.namenode.NameNodeLayoutVersion;
import org.apache.hadoop.hdfs.web.WebHdfsFileSystem; import org.apache.hadoop.hdfs.web.WebHdfsFileSystem;
import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.net.NetUtils;
@ -499,4 +501,36 @@ public class TestOfflineImageViewer {
Assert.assertEquals("", Assert.assertEquals("",
GenericTestUtils.getFilesDiff(reverseImageXml, reverseImage2Xml)); GenericTestUtils.getFilesDiff(reverseImageXml, reverseImage2Xml));
} }
/**
* Tests that the ReverseXML processor doesn't accept XML files with the wrong
* layoutVersion.
*/
@Test
public void testReverseXmlWrongLayoutVersion() throws Throwable {
File imageWrongVersion = new File(tempDir, "imageWrongVersion.xml");
PrintWriter writer = new PrintWriter(imageWrongVersion, "UTF-8");
try {
writer.println("<?xml version=\"1.0\"?>");
writer.println("<fsimage>");
writer.println("<version>");
writer.println(String.format("<layoutVersion>%d</layoutVersion>",
NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION + 1));
writer.println("<onDiskVersion>1</onDiskVersion>");
writer.println("<oivRevision>" +
"545bbef596c06af1c3c8dca1ce29096a64608478</oivRevision>");
writer.println("</version>");
writer.println("</fsimage>");
} finally {
writer.close();
}
try {
OfflineImageReconstructor.run(imageWrongVersion.getAbsolutePath(),
imageWrongVersion.getAbsolutePath() + ".out");
Assert.fail("Expected OfflineImageReconstructor to fail with " +
"version mismatch.");
} catch (Throwable t) {
GenericTestUtils.assertExceptionContains("Layout version mismatch.", t);
}
}
} }