ソースを参照

HDFS-7257. Add the time of last HA state transition to NN's /jmx page. Contributed by Charles Lamb.

Haohui Mai 10 年 前
コミット
670879ef41

+ 3 - 0
hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

@@ -297,6 +297,9 @@ Release 2.7.0 - UNRELEASED
 
     HDFS-7222. Expose DataNode network errors as a metric. (Charles Lamb via wang)
 
+    HDFS-7257. Add the time of last HA state transition to NN's /jmx page.
+    (Charles Lamb via wheat9)
+
   OPTIMIZATIONS
 
   BUG FIXES

+ 5 - 0
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java

@@ -1619,6 +1619,11 @@ public class NameNode implements NameNodeStatusMXBean {
     return UserGroupInformation.isSecurityEnabled();
   }
 
+  @Override // NameNodeStatusMXBean
+  public long getLastHATransitionTime() {
+    return state.getLastHATransitionTime();
+  }
+
   /**
    * Shutdown the NN immediately in an ungraceful way. Used when it would be
    * unsafe for the NN to continue operating, e.g. during a failed HA state

+ 7 - 0
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeStatusMXBean.java

@@ -54,4 +54,11 @@ public interface NameNodeStatusMXBean {
    * @return true, if security is enabled.
    */
   public boolean isSecurityEnabled();
+
+  /**
+   * Gets the most recent HA transition time in milliseconds from the epoch.
+   *
+   * @return the most recent HA transition time in milliseconds from the epoch.
+   */
+  public long getLastHATransitionTime();
 }

+ 16 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ha/HAState.java

@@ -21,8 +21,8 @@ import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
 import org.apache.hadoop.ha.ServiceFailedException;
 import org.apache.hadoop.hdfs.server.namenode.NameNode.OperationCategory;
-import org.apache.hadoop.hdfs.server.namenode.UnsupportedActionException;
 import org.apache.hadoop.ipc.StandbyException;
+import org.apache.hadoop.util.Time;
 
 /**
  * Namenode base state to implement state machine pattern.
@@ -30,6 +30,7 @@ import org.apache.hadoop.ipc.StandbyException;
 @InterfaceAudience.Private
 abstract public class HAState {
   protected final HAServiceState state;
+  private long lastHATransitionTime;
 
   /**
    * Constructor
@@ -61,11 +62,25 @@ abstract public class HAState {
       exitState(context);
       context.setState(s);
       s.enterState(context);
+      s.updateLastHATransitionTime();
     } finally {
       context.writeUnlock();
     }
   }
 
+  /**
+   * Gets the most recent HA transition time in milliseconds from the epoch.
+   *
+   * @return the most recent HA transition time in milliseconds from the epoch.
+   */
+  public long getLastHATransitionTime() {
+    return lastHATransitionTime;
+  }
+
+  private void updateLastHATransitionTime() {
+    lastHATransitionTime = Time.now();
+  }
+
   /**
    * Method to be overridden by subclasses to prepare to enter a state.
    * This method is called <em>without</em> the context being locked,

+ 13 - 0
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestHAMetrics.java

@@ -30,6 +30,10 @@ import org.apache.hadoop.hdfs.server.namenode.FSNamesystem;
 import org.apache.hadoop.io.IOUtils;
 import org.junit.Test;
 
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
@@ -62,6 +66,12 @@ public class TestHAMetrics {
       assertTrue(0 < nn1.getMillisSinceLastLoadedEdits());
 
       cluster.transitionToActive(0);
+      final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+      final ObjectName mxbeanName =
+          new ObjectName("Hadoop:service=NameNode,name=NameNodeStatus");
+      final Long ltt1 =
+          (Long) mbs.getAttribute(mxbeanName, "LastHATransitionTime");
+      assertTrue("lastHATransitionTime should be > 0", ltt1 > 0);
       
       assertEquals("active", nn0.getHAState());
       assertEquals(0, nn0.getMillisSinceLastLoadedEdits());
@@ -69,6 +79,9 @@ public class TestHAMetrics {
       assertTrue(0 < nn1.getMillisSinceLastLoadedEdits());
       
       cluster.transitionToStandby(0);
+      final Long ltt2 =
+          (Long) mbs.getAttribute(mxbeanName, "LastHATransitionTime");
+      assertTrue("lastHATransitionTime should be > " + ltt1, ltt2 > ltt1);
       cluster.transitionToActive(1);
       
       assertEquals("standby", nn0.getHAState());