HBASE-17205 Add a metric for the duration of region in transition

Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/682dd57c
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/682dd57c
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/682dd57c

Branch: refs/heads/branch-1
Commit: 682dd57cd63f3ab0786d8bcc31a38ffd400f81b1
Parents: 9da0d5d
Author: Guanghao Zhang <zghao...@gmail.com>
Authored: Thu Dec 1 09:42:43 2016 +0800
Committer: Matteo Bertozzi <matteo.berto...@cloudera.com>
Committed: Thu Dec 1 10:32:24 2016 -0800

----------------------------------------------------------------------
 .../apache/hadoop/hbase/master/RegionState.java | 21 +++++++++++++++++++
 .../master/MetricsAssignmentManagerSource.java  |  3 +++
 .../MetricsAssignmentManagerSourceImpl.java     |  7 +++++++
 .../hbase/master/MetricsAssignmentManager.java  |  8 +++++++
 .../hadoop/hbase/master/RegionStates.java       | 22 +++++++++++++++-----
 .../hadoop/hbase/master/TestRegionStates.java   |  2 +-
 6 files changed, 57 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/682dd57c/hbase-client/src/main/java/org/apache/hadoop/hbase/master/RegionState.java
----------------------------------------------------------------------
diff --git 
a/hbase-client/src/main/java/org/apache/hadoop/hbase/master/RegionState.java 
b/hbase-client/src/main/java/org/apache/hadoop/hbase/master/RegionState.java
index 40fe38a..eae8c78 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/master/RegionState.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/master/RegionState.java
@@ -180,6 +180,8 @@ public class RegionState {
 
   private volatile ServerName serverName;
   private volatile State state;
+  // The duration of region in transition
+  private long ritDuration;
 
   public RegionState() {
     this.stamp = new AtomicLong(System.currentTimeMillis());
@@ -196,10 +198,16 @@ public class RegionState {
 
   public RegionState(HRegionInfo region,
       State state, long stamp, ServerName serverName) {
+    this(region, state, stamp, serverName, 0);
+  }
+
+  public RegionState(HRegionInfo region, State state, long stamp, ServerName 
serverName,
+      long ritDuration) {
     this.hri = region;
     this.state = state;
     this.stamp = new AtomicLong(stamp);
     this.serverName = serverName;
+    this.ritDuration = ritDuration;
   }
 
   public void updateTimestampToNow() {
@@ -222,6 +230,19 @@ public class RegionState {
     return serverName;
   }
 
+  public long getRitDuration() {
+    return ritDuration;
+  }
+
+  /**
+   * Update the duration of region in transition
+   * @param previousStamp previous RegionState's timestamp
+   */
+  @InterfaceAudience.Private
+  void updateRitDuration(long previousStamp) {
+    this.ritDuration += (this.stamp.get() - previousStamp);
+  }
+
   public boolean isClosing() {
     return state == State.CLOSING;
   }

http://git-wip-us.apache.org/repos/asf/hbase/blob/682dd57c/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSource.java
----------------------------------------------------------------------
diff --git 
a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSource.java
 
b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSource.java
index 92fd111..08b4ac5 100644
--- 
a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSource.java
+++ 
b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSource.java
@@ -45,6 +45,7 @@ public interface MetricsAssignmentManagerSource extends 
BaseSource {
   String RIT_COUNT_NAME = "ritCount";
   String RIT_COUNT_OVER_THRESHOLD_NAME = "ritCountOverThreshold";
   String RIT_OLDEST_AGE_NAME = "ritOldestAge";
+  String RIT_DURATION_NAME = "ritDuration";
   String ASSIGN_TIME_NAME = "assign";
   String BULK_ASSIGN_TIME_NAME = "bulkAssign";
 
@@ -72,4 +73,6 @@ public interface MetricsAssignmentManagerSource extends 
BaseSource {
    * @param age age of the oldest RIT.
    */
   void setRITOldestAge(long age);
+
+  void updateRitDuration(long duration);
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/682dd57c/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSourceImpl.java
----------------------------------------------------------------------
diff --git 
a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSourceImpl.java
 
b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSourceImpl.java
index a2192d2..ab504f5 100644
--- 
a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSourceImpl.java
+++ 
b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManagerSourceImpl.java
@@ -31,6 +31,7 @@ public class MetricsAssignmentManagerSourceImpl
   private MutableGaugeLong ritGauge;
   private MutableGaugeLong ritCountOverThresholdGauge;
   private MutableGaugeLong ritOldestAgeGauge;
+  private MetricHistogram ritDurationHisto;
   private MetricHistogram assignTimeHisto;
   private MetricHistogram bulkAssignTimeHisto;
 
@@ -50,6 +51,7 @@ public class MetricsAssignmentManagerSourceImpl
     ritOldestAgeGauge = metricsRegistry.newGauge(RIT_OLDEST_AGE_NAME, "", 0l);
     assignTimeHisto = metricsRegistry.newTimeHistogram(ASSIGN_TIME_NAME);
     bulkAssignTimeHisto = 
metricsRegistry.newTimeHistogram(BULK_ASSIGN_TIME_NAME);
+    ritDurationHisto = metricsRegistry.newTimeHistogram(RIT_DURATION_NAME);
   }
 
   @Override
@@ -73,4 +75,9 @@ public class MetricsAssignmentManagerSourceImpl
   public void setRITOldestAge(long ritCount) {
     ritOldestAgeGauge.set(ritCount);
   }
+
+  @Override
+  public void updateRitDuration(long duration) {
+    ritDurationHisto.add(duration);
+  }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/682dd57c/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManager.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManager.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManager.java
index b89d2da..40e79ae 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManager.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MetricsAssignmentManager.java
@@ -64,4 +64,12 @@ public class MetricsAssignmentManager {
   public void updateRITOldestAge(long timestamp) {
     assignmentManagerSource.setRITOldestAge(timestamp);
   }
+
+  /**
+   * update the duration metrics of region is transition
+   * @param duration
+   */
+  public void updateRitDuration(long duration) {
+    assignmentManagerSource.updateRitDuration(duration);
+  }
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/682dd57c/hbase-server/src/main/java/org/apache/hadoop/hbase/master/RegionStates.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/RegionStates.java 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/RegionStates.java
index 181227d..d80784e 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/RegionStates.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/RegionStates.java
@@ -39,7 +39,6 @@ import org.apache.hadoop.hbase.HRegionInfo;
 import org.apache.hadoop.hbase.HTableDescriptor;
 import org.apache.hadoop.hbase.MetaTableAccessor;
 import org.apache.hadoop.hbase.RegionTransition;
-import org.apache.hadoop.hbase.Server;
 import org.apache.hadoop.hbase.ServerLoad;
 import org.apache.hadoop.hbase.ServerName;
 import org.apache.hadoop.hbase.TableName;
@@ -169,13 +168,13 @@ public class RegionStates {
   private final TableStateManager tableStateManager;
   private final RegionStateStore regionStateStore;
   private final ServerManager serverManager;
-  private final Server server;
+  private final MasterServices server;
 
   // The maximum time to keep a log split info in region states map
   static final String LOG_SPLIT_TIME = 
"hbase.master.maximum.logsplit.keeptime";
   static final long DEFAULT_LOG_SPLIT_TIME = 7200000L; // 2 hours
 
-  RegionStates(final Server master, final TableStateManager tableStateManager,
+  RegionStates(final MasterServices master, final TableStateManager 
tableStateManager,
       final ServerManager serverManager, final RegionStateStore 
regionStateStore) {
     this.tableStateManager = tableStateManager;
     this.regionStateStore = regionStateStore;
@@ -493,7 +492,15 @@ public class RegionStates {
     updateRegionState(hri, State.OPEN, serverName, openSeqNum);
 
     synchronized (this) {
-      regionsInTransition.remove(encodedName);
+      RegionState regionState = regionsInTransition.remove(encodedName);
+      // When region is online and remove from regionsInTransition,
+      // update the RIT duration to assignment manager metrics
+      if (regionState != null && this.server.getAssignmentManager() != null) {
+        long ritDuration = System.currentTimeMillis() - regionState.getStamp()
+            + regionState.getRitDuration();
+        this.server.getAssignmentManager().getAssignmentManagerMetrics()
+            .updateRitDuration(ritDuration);
+      }
       ServerName oldServerName = regionAssignments.put(hri, serverName);
       if (!serverName.equals(oldServerName)) {
         if (LOG.isDebugEnabled()) {
@@ -1155,7 +1162,12 @@ public class RegionStates {
     }
 
     synchronized (this) {
-      regionsInTransition.put(encodedName, regionState);
+      RegionState oldRegionState = regionsInTransition.put(encodedName, 
regionState);
+      // When region transform old region state to new region state,
+      // accumulate the RIT duration to new region state.
+      if (oldRegionState != null) {
+        regionState.updateRitDuration(oldRegionState.getStamp());
+      }
       putRegionState(regionState);
 
       // For these states, region should be properly closed.

http://git-wip-us.apache.org/repos/asf/hbase/blob/682dd57c/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestRegionStates.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestRegionStates.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestRegionStates.java
index 20cbd62..0410294 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestRegionStates.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestRegionStates.java
@@ -43,7 +43,7 @@ public class TestRegionStates {
 
   @Test
   public void testWeDontReturnDrainingServersForOurBalancePlans() throws 
Exception {
-    Server server = mock(Server.class);
+    MasterServices server = mock(MasterServices.class);
     when(server.getServerName()).thenReturn(ServerName.valueOf("master,1,1"));
     Configuration configuration = mock(Configuration.class);
     when(server.getConfiguration()).thenReturn(configuration);

Reply via email to