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

This commit is contained in:
Akira Ajisaka 2016-05-08 15:49:13 -07:00
parent 2750fb900f
commit 411fb4b2b7
2 changed files with 36 additions and 5 deletions

View File

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

View File

@ -28,6 +28,7 @@
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.Map;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -308,4 +309,33 @@ public void testOfflineEditsViewerHelpMessage() throws Throwable {
IOUtils.closeStream(out); 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));
}
}
}
} }