This is an automated email from the ASF dual-hosted git repository.

reidchan pushed a commit to branch branch-2
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2 by this push:
     new 0c5b6df  HBASE-22975 Add read and write QPS metrics at server level 
and table level
0c5b6df is described below

commit 0c5b6df52e8c973d7993294d50028399ad66c1af
Author: zbq.dean <zbq.d...@gmail.com>
AuthorDate: Mon Sep 23 14:18:29 2019 +0800

    HBASE-22975 Add read and write QPS metrics at server level and table level
    
    Signed-off-by: Reid Chan <reidc...@apache.org>
---
 .../hbase/regionserver/MetricsTableQueryMeter.java |  53 +++++++++++
 .../regionserver/MetricsTableLatenciesImpl.java    |  13 +++
 .../regionserver/MetricsTableQueryMeterImpl.java   | 102 +++++++++++++++++++++
 .../apache/hadoop/hbase/regionserver/HRegion.java  |  16 ++++
 .../hbase/regionserver/MetricsRegionServer.java    |  34 +++++++
 .../regionserver/RegionServerTableMetrics.java     |  20 ++++
 6 files changed, 238 insertions(+)

diff --git 
a/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableQueryMeter.java
 
b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableQueryMeter.java
new file mode 100644
index 0000000..d0085ff
--- /dev/null
+++ 
b/hbase-hadoop-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableQueryMeter.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.regionserver;
+
+import org.apache.hadoop.hbase.TableName;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * Query Per Second for each table in a RegionServer.
+ */
+@InterfaceAudience.Private
+public interface MetricsTableQueryMeter {
+
+  /**
+   * Update table read QPS
+   * @param tableName The table the metric is for
+   * @param count Number of occurrences to record
+   */
+  void updateTableReadQueryMeter(TableName tableName, long count);
+
+  /**
+   * Update table read QPS
+   * @param tableName The table the metric is for
+   */
+  void updateTableReadQueryMeter(TableName tableName);
+
+  /**
+   * Update table write QPS
+   * @param tableName The table the metric is for
+   * @param count Number of occurrences to record
+   */
+  void updateTableWriteQueryMeter(TableName tableName, long count);
+
+  /**
+   * Update table write QPS
+   * @param tableName The table the metric is for
+   */
+  void updateTableWriteQueryMeter(TableName tableName);
+}
diff --git 
a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableLatenciesImpl.java
 
b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableLatenciesImpl.java
index 980388f..5a3f3b9 100644
--- 
a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableLatenciesImpl.java
+++ 
b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableLatenciesImpl.java
@@ -20,6 +20,8 @@ import java.util.HashMap;
 import org.apache.hadoop.hbase.TableName;
 import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
 import org.apache.hadoop.metrics2.MetricHistogram;
+import org.apache.hadoop.metrics2.MetricsCollector;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
 import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
 import org.apache.yetus.audience.InterfaceAudience;
 
@@ -171,4 +173,15 @@ public class MetricsTableLatenciesImpl extends 
BaseSourceImpl implements Metrics
   public void updateScanTime(String tableName, long t) {
     getOrCreateTableHistogram(tableName).updateScanTime(t);
   }
+
+  @Override
+  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
+    MetricsRecordBuilder mrb = metricsCollector.addRecord(metricsName);
+    // source is registered in supers constructor, sometimes called before the 
whole initialization.
+    metricsRegistry.snapshot(mrb, all);
+    if (metricsAdapter != null) {
+      // snapshot MetricRegistry as well
+      metricsAdapter.snapshotAllMetrics(registry, mrb);
+    }
+  }
 }
diff --git 
a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableQueryMeterImpl.java
 
b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableQueryMeterImpl.java
new file mode 100644
index 0000000..438310f
--- /dev/null
+++ 
b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsTableQueryMeterImpl.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.regionserver;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.metrics.Meter;
+import org.apache.hadoop.hbase.metrics.MetricRegistry;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * Implementation of {@link MetricsTableQueryMeter} to track query per second 
for each table in
+ * a RegionServer.
+ */
+@InterfaceAudience.Private
+public class MetricsTableQueryMeterImpl implements MetricsTableQueryMeter {
+  private final Map<TableName, TableMeters> metersByTable = new 
ConcurrentHashMap<>();
+  private final MetricRegistry metricRegistry;
+
+  private final static String TABLE_READ_QUERY_PER_SECOND = 
"tableReadQueryPerSecond";
+  private final static String TABLE_WRITE_QUERY_PER_SECOND = 
"tableWriteQueryPerSecond";
+
+  public MetricsTableQueryMeterImpl(MetricRegistry metricRegistry) {
+    this.metricRegistry = metricRegistry;
+  }
+
+  private static class TableMeters {
+    final Meter tableReadQueryMeter;
+    final Meter tableWriteQueryMeter;
+
+    TableMeters(MetricRegistry metricRegistry, TableName tableName) {
+      this.tableReadQueryMeter = 
metricRegistry.meter(qualifyMetricsName(tableName,
+        TABLE_READ_QUERY_PER_SECOND));
+      this.tableWriteQueryMeter =
+        metricRegistry.meter(qualifyMetricsName(tableName, 
TABLE_WRITE_QUERY_PER_SECOND));
+    }
+
+    public void updateTableReadQueryMeter(long count) {
+      tableReadQueryMeter.mark(count);
+    }
+
+    public void updateTableReadQueryMeter() {
+      tableReadQueryMeter.mark();
+    }
+
+    public void updateTableWriteQueryMeter(long count) {
+      tableWriteQueryMeter.mark(count);
+    }
+
+    public void updateTableWriteQueryMeter() {
+      tableWriteQueryMeter.mark();
+    }
+  }
+
+  private static String qualifyMetricsName(TableName tableName, String metric) 
{
+    StringBuilder sb = new StringBuilder();
+    sb.append("Namespace_").append(tableName.getNamespaceAsString());
+    sb.append("_table_").append(tableName.getQualifierAsString());
+    sb.append("_metric_").append(metric);
+    return sb.toString();
+  }
+
+  private TableMeters getOrCreateTableMeter(TableName tableName) {
+    return metersByTable.computeIfAbsent(tableName, tbn -> new 
TableMeters(metricRegistry, tbn));
+  }
+
+  @Override
+  public void updateTableReadQueryMeter(TableName tableName, long count) {
+    getOrCreateTableMeter(tableName).updateTableReadQueryMeter(count);
+  }
+
+  @Override
+  public void updateTableReadQueryMeter(TableName tableName) {
+    getOrCreateTableMeter(tableName).updateTableReadQueryMeter();
+  }
+
+  @Override
+  public void updateTableWriteQueryMeter(TableName tableName, long count) {
+    getOrCreateTableMeter(tableName).updateTableWriteQueryMeter(count);
+  }
+
+  @Override
+  public void updateTableWriteQueryMeter(TableName tableName) {
+    getOrCreateTableMeter(tableName).updateTableWriteQueryMeter();
+  }
+}
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
index f601418..13cc13c 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/HRegion.java
@@ -4045,6 +4045,10 @@ public class HRegion implements HeapSize, 
PropagatingConfigurationObserver, Regi
         requestFlushIfNeeded();
       }
     } finally {
+      if (rsServices != null && rsServices.getMetrics() != null) {
+        rsServices.getMetrics().updateWriteQueryMeter(this.htableDescriptor.
+          getTableName(), batchOp.size());
+      }
       batchOp.closeRegionOperation();
     }
     return batchOp.retCodeDetails;
@@ -6561,6 +6565,9 @@ public class HRegion implements HeapSize, 
PropagatingConfigurationObserver, Regi
       if (!outResults.isEmpty()) {
         readRequestsCount.increment();
       }
+      if (rsServices != null && rsServices.getMetrics() != null) {
+        
rsServices.getMetrics().updateReadQueryMeter(getRegionInfo().getTable());
+      }
 
       // If the size limit was reached it means a partial Result is being 
returned. Returning a
       // partial Result means that we should not reset the filters; filters 
should only be reset in
@@ -7688,6 +7695,11 @@ public class HRegion implements HeapSize, 
PropagatingConfigurationObserver, Regi
 
           // STEP 11. Release row lock(s)
           releaseRowLocks(acquiredRowLocks);
+
+          if (rsServices != null && rsServices.getMetrics() != null) {
+            
rsServices.getMetrics().updateWriteQueryMeter(this.htableDescriptor.
+              getTableName(), mutations.size());
+          }
         }
         success = true;
       } finally {
@@ -7843,6 +7855,10 @@ public class HRegion implements HeapSize, 
PropagatingConfigurationObserver, Regi
           rsServices.getNonceManager().addMvccToOperationContext(nonceGroup, 
nonce,
             writeEntry.getWriteNumber());
         }
+        if (rsServices != null && rsServices.getMetrics() != null) {
+          rsServices.getMetrics().updateWriteQueryMeter(this.htableDescriptor.
+            getTableName());
+        }
         writeEntry = null;
       } finally {
         this.updatesLock.readLock().unlock();
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServer.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServer.java
index 3396549..f3f68f2 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServer.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServer.java
@@ -19,6 +19,7 @@ package org.apache.hadoop.hbase.regionserver;
 
 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
 import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.metrics.Meter;
 import org.apache.hadoop.hbase.metrics.MetricRegistries;
 import org.apache.hadoop.hbase.metrics.MetricRegistry;
 import org.apache.hadoop.hbase.metrics.Timer;
@@ -50,6 +51,8 @@ public class MetricsRegionServer {
 
   private MetricRegistry metricRegistry;
   private Timer bulkLoadTimer;
+  private Meter serverReadQueryMeter;
+  private Meter serverWriteQueryMeter;
 
   public MetricsRegionServer(MetricsRegionServerWrapper regionServerWrapper, 
Configuration conf,
       MetricsTable metricsTable) {
@@ -65,6 +68,9 @@ public class MetricsRegionServer {
 
     // create and use metrics from the new hbase-metrics based registry.
     bulkLoadTimer = metricRegistry.timer("Bulkload");
+
+    serverReadQueryMeter = metricRegistry.meter("ServerReadQueryPerSecond");
+    serverWriteQueryMeter = metricRegistry.meter("ServerWriteQueryPerSecond");
   }
 
   MetricsRegionServer(MetricsRegionServerWrapper regionServerWrapper,
@@ -231,4 +237,32 @@ public class MetricsRegionServer {
   public void updateBulkLoad(long millis) {
     this.bulkLoadTimer.updateMillis(millis);
   }
+
+  public void updateReadQueryMeter(TableName tn, long count) {
+    if (tableMetrics != null && tn != null) {
+      tableMetrics.updateTableReadQueryMeter(tn, count);
+    }
+    this.serverReadQueryMeter.mark(count);
+  }
+
+  public void updateReadQueryMeter(TableName tn) {
+    if (tableMetrics != null && tn != null) {
+      tableMetrics.updateTableReadQueryMeter(tn);
+    }
+    this.serverReadQueryMeter.mark();
+  }
+
+  public void updateWriteQueryMeter(TableName tn, long count) {
+    if (tableMetrics != null && tn != null) {
+      tableMetrics.updateTableWriteQueryMeter(tn, count);
+    }
+    this.serverWriteQueryMeter.mark(count);
+  }
+
+  public void updateWriteQueryMeter(TableName tn) {
+    if (tableMetrics != null && tn != null) {
+      tableMetrics.updateTableWriteQueryMeter(tn);
+    }
+    this.serverWriteQueryMeter.mark();
+  }
 }
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerTableMetrics.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerTableMetrics.java
index 652a062..ec6c049 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerTableMetrics.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerTableMetrics.java
@@ -18,6 +18,7 @@ package org.apache.hadoop.hbase.regionserver;
 
 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
 import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.metrics.MetricRegistries;
 import org.apache.yetus.audience.InterfaceAudience;
 
 /**
@@ -28,9 +29,12 @@ import org.apache.yetus.audience.InterfaceAudience;
 public class RegionServerTableMetrics {
 
   private final MetricsTableLatencies latencies;
+  private final MetricsTableQueryMeter queryMeter;
 
   public RegionServerTableMetrics() {
     latencies = 
CompatibilitySingletonFactory.getInstance(MetricsTableLatencies.class);
+    queryMeter = new MetricsTableQueryMeterImpl(MetricRegistries.global().
+      get(((MetricsTableLatenciesImpl) 
latencies).getMetricRegistryInfo()).get());
   }
 
   public void updatePut(TableName table, long time) {
@@ -68,4 +72,20 @@ public class RegionServerTableMetrics {
   public void updateScanSize(TableName table, long size) {
     latencies.updateScanSize(table.getNameAsString(), size);
   }
+
+  public void updateTableReadQueryMeter(TableName table, long count) {
+    queryMeter.updateTableReadQueryMeter(table, count);
+  }
+
+  public void updateTableReadQueryMeter(TableName table) {
+    queryMeter.updateTableReadQueryMeter(table);
+  }
+
+  public void updateTableWriteQueryMeter(TableName table, long count) {
+    queryMeter.updateTableWriteQueryMeter(table, count);
+  }
+
+  public void updateTableWriteQueryMeter(TableName table) {
+    queryMeter.updateTableWriteQueryMeter(table);
+  }
 }

Reply via email to