View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.regionserver;
18  
19  import java.util.HashMap;
20  
21  import org.apache.hadoop.hbase.TableName;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
24  import org.apache.hadoop.metrics2.MetricHistogram;
25  import org.apache.hadoop.metrics2.MetricsCollector;
26  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
27  import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
28  
29  /**
30   * Implementation of {@link MetricsTableLatencies} to track latencies for one table in a
31   * RegionServer.
32   */
33  @InterfaceAudience.Private
34  public class MetricsTableLatenciesImpl extends BaseSourceImpl implements MetricsTableLatencies {
35  
36    private final HashMap<TableName,TableHistograms> histogramsByTable = new HashMap<>();
37  
38    public static class TableHistograms {
39      final MetricHistogram getTimeHisto;
40      final MetricHistogram incrementTimeHisto;
41      final MetricHistogram appendTimeHisto;
42      final MetricHistogram putTimeHisto;
43      final MetricHistogram putBatchTimeHisto;
44      final MetricHistogram deleteTimeHisto;
45      final MetricHistogram deleteBatchTimeHisto;
46      final MetricHistogram scanTimeHisto;
47      final MetricHistogram scanSizeHisto;
48      final MetricHistogram checkAndDeleteTimeHisto;
49      final MetricHistogram checkAndPutTimeHisto;
50  
51      TableHistograms(DynamicMetricsRegistry registry, TableName tn) {
52        getTimeHisto = registry.newTimeHistogram(qualifyMetricsName(tn, GET_TIME));
53        incrementTimeHisto = registry.newTimeHistogram(
54            qualifyMetricsName(tn, INCREMENT_TIME));
55        appendTimeHisto = registry.newTimeHistogram(qualifyMetricsName(tn, APPEND_TIME));
56        putTimeHisto = registry.newTimeHistogram(qualifyMetricsName(tn, PUT_TIME));
57        putBatchTimeHisto = registry.newTimeHistogram(qualifyMetricsName(tn, PUT_BATCH_TIME));
58        deleteTimeHisto = registry.newTimeHistogram(qualifyMetricsName(tn, DELETE_TIME));
59        deleteBatchTimeHisto = registry.newTimeHistogram(
60            qualifyMetricsName(tn, DELETE_BATCH_TIME));
61        scanTimeHisto = registry.newTimeHistogram(qualifyMetricsName(tn, SCAN_TIME));
62        scanSizeHisto = registry.newSizeHistogram(qualifyMetricsName(tn, SCAN_SIZE));
63        checkAndDeleteTimeHisto =
64            registry.newTimeHistogram(qualifyMetricsName(tn, CHECK_AND_DELETE_TIME));
65        checkAndPutTimeHisto =
66            registry.newTimeHistogram(qualifyMetricsName(tn, CHECK_AND_PUT_TIME));
67      }
68  
69      public void updatePut(long time) {
70        putTimeHisto.add(time);
71      }
72  
73      public void updatePutBatch(long time) {
74        putBatchTimeHisto.add(time);
75      }
76  
77      public void updateDelete(long t) {
78        deleteTimeHisto.add(t);
79      }
80  
81      public void updateDeleteBatch(long t) {
82        deleteBatchTimeHisto.add(t);
83      }
84  
85      public void updateGet(long t) {
86        getTimeHisto.add(t);
87      }
88  
89      public void updateIncrement(long t) {
90        incrementTimeHisto.add(t);
91      }
92  
93      public void updateAppend(long t) {
94        appendTimeHisto.add(t);
95      }
96  
97      public void updateScanSize(long scanSize) {
98        scanSizeHisto.add(scanSize);
99      }
100 
101     public void updateScanTime(long t) {
102       scanTimeHisto.add(t);
103     }
104 
105     public void updateCheckAndDeleteTime(long t) {
106       checkAndDeleteTimeHisto.add(t);
107     }
108 
109     public void updateCheckAndPutTime(long t) {
110       checkAndPutTimeHisto.add(t);
111     }
112 
113   }
114 
115   public static String qualifyMetricsName(TableName tableName, String metric) {
116     StringBuilder sb = new StringBuilder();
117     sb.append("Namespace_").append(tableName.getNamespaceAsString());
118     sb.append("_table_").append(tableName.getQualifierAsString());
119     sb.append("_metric_").append(metric);
120     return sb.toString();
121   }
122 
123   public TableHistograms getOrCreateTableHistogram(String tableName) {
124     // TODO Java8's ConcurrentHashMap#computeIfAbsent would be stellar instead
125     final TableName tn = TableName.valueOf(tableName);
126     TableHistograms latency = histogramsByTable.get(tn);
127     if (latency == null) {
128       latency = new TableHistograms(getMetricsRegistry(), tn);
129       histogramsByTable.put(tn, latency);
130     }
131     return latency;
132   }
133 
134   public MetricsTableLatenciesImpl() {
135     this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
136   }
137 
138   public MetricsTableLatenciesImpl(String metricsName, String metricsDescription,
139       String metricsContext, String metricsJmxContext) {
140     super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
141   }
142 
143   @Override
144   public void updatePut(String tableName, long t) {
145     getOrCreateTableHistogram(tableName).updatePut(t);
146   }
147 
148   @Override
149   public void updatePutBatch(String tableName, long t) {
150     getOrCreateTableHistogram(tableName).updatePutBatch(t);
151   }
152 
153   @Override
154   public void updateDelete(String tableName, long t) {
155     getOrCreateTableHistogram(tableName).updateDelete(t);
156   }
157 
158   @Override
159   public void updateDeleteBatch(String tableName, long t) {
160     getOrCreateTableHistogram(tableName).updateDeleteBatch(t);
161   }
162 
163   @Override
164   public void updateGet(String tableName, long t) {
165     getOrCreateTableHistogram(tableName).updateGet(t);
166   }
167 
168   @Override
169   public void updateIncrement(String tableName, long t) {
170     getOrCreateTableHistogram(tableName).updateIncrement(t);
171   }
172 
173   @Override
174   public void updateAppend(String tableName, long t) {
175     getOrCreateTableHistogram(tableName).updateAppend(t);
176   }
177 
178   @Override
179   public void updateScanSize(String tableName, long scanSize) {
180     getOrCreateTableHistogram(tableName).updateScanSize(scanSize);
181   }
182 
183   @Override
184   public void updateScanTime(String tableName, long t) {
185     getOrCreateTableHistogram(tableName).updateScanTime(t);
186   }
187 
188   @Override
189   public void updateCheckAndDelete(String tableName, long time) {
190     getOrCreateTableHistogram(tableName).updateCheckAndDeleteTime(time);
191   }
192 
193   @Override
194   public void updateCheckAndPut(String tableName, long time) {
195     getOrCreateTableHistogram(tableName).updateCheckAndPutTime(time);
196   }
197 
198   @Override
199   public void getMetrics(MetricsCollector metricsCollector, boolean all) {
200     MetricsRecordBuilder mrb = metricsCollector.addRecord(metricsName);
201     // source is registered in supers constructor, sometimes called before the whole initialization.
202     metricsRegistry.snapshot(mrb, all);
203     if (metricsAdapter != null) {
204       // snapshot MetricRegistry as well
205       metricsAdapter.snapshotAllMetrics(registry, mrb);
206     }
207   }
208 }