View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
21  import org.apache.hadoop.hbase.TableName;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.classification.InterfaceStability;
24  import org.apache.hadoop.hbase.metrics.MetricRegistries;
25  import org.apache.hadoop.hbase.metrics.MetricRegistry;
26  import org.apache.hadoop.hbase.metrics.Timer;
27  import org.apache.hadoop.conf.Configuration;
28  
29  import com.google.common.annotations.VisibleForTesting;
30  
31  /**
32   * <p>
33   * This class is for maintaining the various regionserver statistics
34   * and publishing them through the metrics interfaces.
35   * </p>
36   * This class has a number of metrics variables that are publicly accessible;
37   * these variables (objects) have methods to update their values.
38   */
39  @InterfaceStability.Evolving
40  @InterfaceAudience.Private
41  public class MetricsRegionServer {
42    public static final String RS_ENABLE_TABLE_METRICS_KEY =
43        "hbase.regionserver.enable.table.latencies";
44    public static final boolean RS_ENABLE_TABLE_METRICS_DEFAULT = true;
45  
46    private MetricsRegionServerSource serverSource;
47    private MetricsRegionServerWrapper regionServerWrapper;
48    private RegionServerTableMetrics tableMetrics;
49  
50    private MetricRegistry metricRegistry;
51    private Timer bulkLoadTimer;
52  
53    public MetricsRegionServer(MetricsRegionServerWrapper regionServerWrapper, Configuration conf) {
54      this(regionServerWrapper,
55          CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
56              .createServer(regionServerWrapper),
57          createTableMetrics(conf));
58  
59      // Create hbase-metrics module based metrics. The registry should already be registered by the
60      // MetricsRegionServerSource
61      metricRegistry = MetricRegistries.global().get(serverSource.getMetricRegistryInfo()).get();
62  
63      // create and use metrics from the new hbase-metrics based registry.
64      bulkLoadTimer = metricRegistry.timer("Bulkload");
65    }
66  
67    MetricsRegionServer(MetricsRegionServerWrapper regionServerWrapper,
68                        MetricsRegionServerSource serverSource,
69                        RegionServerTableMetrics tableMetrics) {
70      this.regionServerWrapper = regionServerWrapper;
71      this.serverSource = serverSource;
72      this.tableMetrics = tableMetrics;
73    }
74  
75    /**
76     * Creates an instance of {@link RegionServerTableMetrics} only if the feature is enabled.
77     */
78    static RegionServerTableMetrics createTableMetrics(Configuration conf) {
79      if (conf.getBoolean(RS_ENABLE_TABLE_METRICS_KEY, RS_ENABLE_TABLE_METRICS_DEFAULT)) {
80        return new RegionServerTableMetrics();
81      }
82      return null;
83    }
84  
85    @VisibleForTesting
86    public MetricsRegionServerSource getMetricsSource() {
87      return serverSource;
88    }
89  
90    public MetricsRegionServerWrapper getRegionServerWrapper() {
91      return regionServerWrapper;
92    }
93  
94    public void updatePutBatch(TableName tn, long t) {
95      if (tableMetrics != null && tn != null) {
96        tableMetrics.updatePut(tn, t);
97      }
98      if (t > 1000) {
99        serverSource.incrSlowPut();
100     }
101     serverSource.updatePutBatch(t);
102   }
103 
104   public void updatePut(TableName tn, long t) {
105     if (tableMetrics != null && tn != null) {
106       tableMetrics.updatePut(tn, t);
107     }
108     serverSource.updatePut(t);
109   }
110 
111   public void updateDelete(TableName tn, long t) {
112     if (tableMetrics != null && tn != null) {
113       tableMetrics.updateDelete(tn, t);
114     }
115     serverSource.updateDelete(t);
116   }
117 
118   public void updateDeleteBatch(TableName tn, long t) {
119     if (tableMetrics != null && tn != null) {
120       tableMetrics.updateDelete(tn, t);
121     }
122     if (t > 1000) {
123       serverSource.incrSlowDelete();
124     }
125     serverSource.updateDeleteBatch(t);
126   }
127 
128   public void updateCheckAndDelete(long t) {
129     serverSource.updateCheckAndDelete(t);
130   }
131 
132   public void updateCheckAndPut(long t) {
133     serverSource.updateCheckAndPut(t);
134   }
135 
136   public void updateGet(TableName tn, long t) {
137     if (tableMetrics != null && tn != null) {
138       tableMetrics.updateGet(tn, t);
139     }
140     if (t > 1000) {
141       serverSource.incrSlowGet();
142     }
143     serverSource.updateGet(t);
144   }
145 
146   public void updateIncrement(TableName tn, long t) {
147     if (tableMetrics != null && tn != null) {
148       tableMetrics.updateIncrement(tn, t);
149     }
150     if (t > 1000) {
151       serverSource.incrSlowIncrement();
152     }
153     serverSource.updateIncrement(t);
154   }
155 
156   public void updateAppend(TableName tn, long t) {
157     if (tableMetrics != null && tn != null) {
158       tableMetrics.updateAppend(tn, t);
159     }
160     if (t > 1000) {
161       serverSource.incrSlowAppend();
162     }
163     serverSource.updateAppend(t);
164   }
165 
166   public void updateReplay(long t){
167     serverSource.updateReplay(t);
168   }
169 
170   public void updateScanSize(TableName tn, long scanSize){
171     if (tableMetrics != null && tn != null) {
172       tableMetrics.updateScanSize(tn, scanSize);
173     }
174     serverSource.updateScanSize(scanSize);
175   }
176 
177   public void updateScanTime(TableName tn, long t) {
178     if (tableMetrics != null && tn != null) {
179       tableMetrics.updateScanTime(tn, t);
180     }
181     serverSource.updateScanTime(t);
182   }
183 
184   public void updateSplitTime(long t) {
185     serverSource.updateSplitTime(t);
186   }
187 
188   public void incrSplitRequest() {
189     serverSource.incrSplitRequest();
190   }
191 
192   public void incrSplitSuccess() {
193     serverSource.incrSplitSuccess();
194   }
195 
196   public void updateFlush(long t, long memstoreSize, long fileSize) {
197     serverSource.updateFlushTime(t);
198     serverSource.updateFlushMemstoreSize(memstoreSize);
199     serverSource.updateFlushOutputSize(fileSize);
200   }
201 
202   public void updateCompaction(boolean isMajor, long t, int inputFileCount, int outputFileCount,
203       long inputBytes, long outputBytes) {
204     serverSource.updateCompactionTime(isMajor, t);
205     serverSource.updateCompactionInputFileCount(isMajor, inputFileCount);
206     serverSource.updateCompactionOutputFileCount(isMajor, outputFileCount);
207     serverSource.updateCompactionInputSize(isMajor, inputBytes);
208     serverSource.updateCompactionOutputSize(isMajor, outputBytes);
209   }
210 
211   public void updateBulkLoad(long millis) {
212     this.bulkLoadTimer.updateMillis(millis);
213   }
214 }