From 1586941a3acc78eece020fb2da2e89756bd0938f Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Fri, 1 Jun 2012 02:40:19 +0000 Subject: [PATCH] HDFS-3487. offlineimageviewer should give byte offset information when it encounters an exception. Contributed by Colin Patrick McCabe git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1344973 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 +++ .../ImageLoaderCurrent.java | 13 ++++++---- .../OfflineImageViewer.java | 24 +++++++++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index dbb4b40a0fd..9447f7e9ac6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -141,6 +141,9 @@ Release 2.0.1-alpha - UNRELEASED HDFS-3486. offlineimageviewer can't read fsimage files that contain persistent delegation tokens. (Colin Patrick McCabe via eli) + HDFS-3487. offlineimageviewer should give byte offset information + when it encounters an exception. (Colin Patrick McCabe via eli) + Release 2.0.0-alpha - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/ImageLoaderCurrent.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/ImageLoaderCurrent.java index 3a6ce3ef1a2..2aade9eb14b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/ImageLoaderCurrent.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/ImageLoaderCurrent.java @@ -145,6 +145,7 @@ class ImageLoaderCurrent implements ImageLoader { @Override public void loadImage(DataInputStream in, ImageVisitor v, boolean skipBlocks) throws IOException { + boolean done = false; try { v.start(); v.visitEnclosingElement(ImageElement.FS_IMAGE); @@ -189,11 +190,13 @@ class ImageLoaderCurrent implements ImageLoader { } v.leaveEnclosingElement(); // FSImage - v.finish(); - } catch(IOException e) { - // Tell the visitor to clean up, then re-throw the exception - v.finishAbnormally(); - throw e; + done = true; + } finally { + if (done) { + v.finish(); + } else { + v.finishAbnormally(); + } } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageViewer.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageViewer.java index 1d3208d47cc..f3c5cbbb792 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageViewer.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/OfflineImageViewer.java @@ -30,8 +30,12 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.hadoop.classification.InterfaceAudience; +import org.apache.hadoop.io.IOUtils; +import org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.PositionTrackingInputStream; /** * OfflineImageViewer to dump the contents of an Hadoop image file to XML @@ -40,6 +44,8 @@ import org.apache.hadoop.classification.InterfaceAudience; */ @InterfaceAudience.Private public class OfflineImageViewer { + public static final Log LOG = LogFactory.getLog(OfflineImageViewer.class); + private final static String usage = "Usage: bin/hdfs oiv [OPTIONS] -i INPUTFILE -o OUTPUTFILE\n" + "Offline Image Viewer\n" + @@ -112,24 +118,28 @@ public class OfflineImageViewer { */ public void go() throws IOException { DataInputStream in = null; - + PositionTrackingInputStream tracker = null; + ImageLoader fsip = null; + boolean done = false; try { - in = new DataInputStream(new BufferedInputStream( + tracker = new PositionTrackingInputStream(new BufferedInputStream( new FileInputStream(new File(inputFile)))); + in = new DataInputStream(tracker); int imageVersionFile = findImageVersion(in); - ImageLoader fsip = - ImageLoader.LoaderFactory.getLoader(imageVersionFile); + fsip = ImageLoader.LoaderFactory.getLoader(imageVersionFile); if(fsip == null) throw new IOException("No image processor to read version " + imageVersionFile + " is available."); - fsip.loadImage(in, processor, skipBlocks); - + done = true; } finally { - if(in != null) in.close(); + if (!done) { + LOG.error("image loading failed at offset " + tracker.getPos()); + } + IOUtils.cleanup(LOG, in, tracker); } }