From a0a976cee9f07f2af8cf182a72b196a2bf25973e Mon Sep 17 00:00:00 2001
From: Tsz-wo Sze <szetszwo@apache.org>
Date: Sat, 18 Apr 2009 00:03:28 +0000
Subject: [PATCH] HADOOP-5650. Fix safemode messages in the Namenode log.  Contributed by Suresh Srinivas

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@766182 13f79535-47bb-0310-9956-ffa450edef68
---
 .../hadoop/hdfs/server/namenode/FSNamesystem.java  |   48 +++++++++++---------
 1 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/hdfs/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java b/src/hdfs/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java
index c8e2dc0..ca5f2ff 100644
--- a/src/hdfs/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java
+++ b/src/hdfs/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java
@@ -3996,6 +3996,8 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
     int blockTotal; 
     /** Number of safe blocks. */
     private int blockSafe;
+    /** Number of blocks needed to satisfy safe mode threshold condition */
+    private int blockThreshold;
     /** time of the last status printout */
     private long lastStatusReport = 0;
       
@@ -4115,15 +4117,7 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
      * if DFS is empty or {@link #threshold} == 0
      */
     boolean needEnter() {
-      return getSafeBlockRatio() < threshold;
-    }
-      
-    /**
-     * Ratio of the number of safe blocks to the total number of blocks 
-     * to be compared with the threshold.
-     */
-    private float getSafeBlockRatio() {
-      return (blockTotal == 0 ? 1 : (float)blockSafe/blockTotal);
+      return threshold != 0 && blockSafe < blockThreshold;
     }
       
     /**
@@ -4156,7 +4150,8 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
      * Set total number of blocks.
      */
     synchronized void setBlockTotal(int total) {
-      this.blockTotal = total; 
+      this.blockTotal = total;
+      this.blockThreshold = (int) (blockTotal * threshold);
       checkMode();
     }
       
@@ -4200,9 +4195,9 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
      * A tip on how safe mode is to be turned off: manually or automatically.
      */
     String getTurnOffTip() {
-      String leaveMsg = "Safe mode will be turned off automatically";
       if(reached < 0)
         return "Safe mode is OFF.";
+      String leaveMsg = "Safe mode will be turned off automatically";
       if(isManual()) {
         if(getDistributedUpgradeState())
           return leaveMsg + " upon completion of " + 
@@ -4212,15 +4207,24 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
       }
       if(blockTotal < 0)
         return leaveMsg + ".";
-      String safeBlockRatioMsg = 
-        String.format("The ratio of reported blocks %.4f has " +
-          (reached == 0 ? "not " : "") + "reached the threshold %.4f. ",
-          getSafeBlockRatio(), threshold) + leaveMsg;
-      if(reached == 0 || isManual())  // threshold is not reached or manual
-        return safeBlockRatioMsg + ".";
+      
+      String msg = null;
+      if (reached == 0) {
+        msg = String.format("The reported blocks %d needs additional %d"
+            + " blocks to reach the threshold %.4f of total blocks %d. %s",
+            blockSafe, (blockThreshold - blockSafe), threshold, blockTotal,
+            leaveMsg);
+      } else {
+        msg = String.format("The reported blocks %d has reached the threshold"
+            + " %.4f of total blocks %d. %s", blockSafe, threshold, 
+            blockTotal, leaveMsg);
+      }
+      if(reached == 0 || isManual()) {  // threshold is not reached or manual       
+        return msg + ".";
+      }
       // extension period is in progress
-      return safeBlockRatioMsg + " in " 
-            + Math.abs(reached + extension - now())/1000 + " seconds.";
+      return msg + " in " + Math.abs(reached + extension - now()) / 1000
+          + " seconds.";
     }
 
     /**
@@ -4238,9 +4242,9 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
      * Returns printable state of the class.
      */
     public String toString() {
-      String resText = "Current safe block ratio = " 
-        + getSafeBlockRatio() 
-        + ". Target threshold = " + threshold
+      String resText = "Current safe blocks = " 
+        + blockSafe 
+        + ". Target blocks = " + blockThreshold + " for threshold = %" + threshold
         + ". Minimal replication = " + safeReplication + ".";
       if (reached > 0) 
         resText += " Threshold was reached " + new Date(reached) + ".";
-- 
1.6.0.4

