Explorar o código

HDFS-10337. OfflineEditsViewer stats option should print 0 instead of null for the count of operations. Contributed by Yiqun Lin.

Akira Ajisaka %!s(int64=9) %!d(string=hai) anos
pai
achega
411fb4b2b7

+ 6 - 5
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineEditsViewer/StatisticsEditsVisitor.java

@@ -107,16 +107,17 @@ public class StatisticsEditsVisitor implements OfflineEditsVisitor {
    * @return statistics in in string format, suitable for printing
    */
   public String getStatisticsString() {
-    StringBuffer sb = new StringBuffer();
+    StringBuilder sb = new StringBuilder();
     sb.append(String.format(
         "    %-30.30s      : %d%n",
         "VERSION", version));
     for(FSEditLogOpCodes opCode : FSEditLogOpCodes.values()) {
+      Long count = opCodeCount.get(opCode);
       sb.append(String.format(
-        "    %-30.30s (%3d): %d%n",
-        opCode.toString(),
-        opCode.getOpCode(),
-        opCodeCount.get(opCode)));
+          "    %-30.30s (%3d): %d%n",
+          opCode.toString(),
+          opCode.getOpCode(),
+          count == null ? Long.valueOf(0L) : count));
     }
     return sb.toString();
   }

+ 30 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/tools/offlineEditsViewer/TestOfflineEditsViewer.java

@@ -28,6 +28,7 @@ import java.io.IOException;
 import java.io.PrintStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
+import java.util.Map;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.logging.Log;
@@ -308,4 +309,33 @@ public class TestOfflineEditsViewer {
       IOUtils.closeStream(out);
     }
   }
+
+  @Test
+  public void testStatisticsStrWithNullOpCodeCount() throws IOException {
+    String editFilename = nnHelper.generateEdits();
+    String outFilename = editFilename + ".stats";
+    FileOutputStream fout = new FileOutputStream(outFilename);
+    StatisticsEditsVisitor visitor = new StatisticsEditsVisitor(fout);
+    OfflineEditsViewer oev = new OfflineEditsViewer();
+
+    String statisticsStr = null;
+    if (oev.go(editFilename, outFilename, "stats", new Flags(), visitor) == 0) {
+      statisticsStr = visitor.getStatisticsString();
+    }
+    Assert.assertNotNull(statisticsStr);
+
+    String str;
+    Long count;
+    Map<FSEditLogOpCodes, Long> opCodeCount = visitor.getStatistics();
+    for (FSEditLogOpCodes opCode : FSEditLogOpCodes.values()) {
+      count = opCodeCount.get(opCode);
+      // Verify the str when the opCode's count is null
+      if (count == null) {
+        str =
+            String.format("    %-30.30s (%3d): %d%n", opCode.toString(),
+                opCode.getOpCode(), Long.valueOf(0L));
+        assertTrue(statisticsStr.contains(str));
+      }
+    }
+  }
 }