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 com.google.common.annotations.VisibleForTesting;
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
25  import org.apache.hadoop.hbase.TableName;
26  
27  /**
28   * This class is for maintaining the various regionserver statistics
29   * and publishing them through the metrics interfaces.
30   * <p/>
31   * This class has a number of metrics variables that are publicly accessible;
32   * these variables (objects) have methods to update their values.
33   */
34  @InterfaceStability.Evolving
35  @InterfaceAudience.Private
36  public class MetricsRegionServer {
37    public static final String RS_ENABLE_TABLE_METRICS_KEY =
38        "hbase.regionserver.enable.table.latencies";
39    public static final boolean RS_ENABLE_TABLE_METRICS_DEFAULT = true;
40  
41    private final MetricsRegionServerSource serverSource;
42    private final MetricsRegionServerWrapper regionServerWrapper;
43    private final MetricsTable metricsTable;
44    private final RegionServerTableMetrics tableMetrics;
45  
46    private final MetricsUserAggregate userAggregate;
47  
48    public MetricsRegionServer(Configuration conf, MetricsRegionServerWrapper regionServerWrapper,
49                               MetricsTable metricsTable) {
50      this(conf, regionServerWrapper,
51          CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
52              .createServer(regionServerWrapper), metricsTable, createTableMetrics(conf));
53  
54    }
55  
56    MetricsRegionServer(Configuration conf, MetricsRegionServerWrapper regionServerWrapper,
57                        MetricsRegionServerSource serverSource, MetricsTable metricsTable,
58                        RegionServerTableMetrics tableMetrics) {
59      this.regionServerWrapper = regionServerWrapper;
60      this.serverSource = serverSource;
61      this.userAggregate = new MetricsUserAggregate(conf);
62      this.metricsTable = metricsTable;
63      this.tableMetrics = tableMetrics;
64    }
65  
66    /**
67     * Creates an instance of {@link RegionServerTableMetrics} only if the feature is enabled.
68     */
69    static RegionServerTableMetrics createTableMetrics(Configuration conf) {
70      if (conf.getBoolean(RS_ENABLE_TABLE_METRICS_KEY, RS_ENABLE_TABLE_METRICS_DEFAULT)) {
71        return new RegionServerTableMetrics();
72      }
73      return null;
74    }
75  
76    // for unit-test usage
77    public MetricsRegionServerSource getMetricsSource() {
78      return serverSource;
79    }
80  
81    @VisibleForTesting
82    public MetricsUserAggregate getMetricsUserAggregate() {
83      return userAggregate;
84    }
85  
86    public MetricsRegionServerWrapper getRegionServerWrapper() {
87      return regionServerWrapper;
88    }
89  
90    public void updatePut(TableName tn, long t) {
91      if (tableMetrics != null && tn != null) {
92        tableMetrics.updatePut(tn, t);
93      }
94      if (t > 1000) {
95        serverSource.incrSlowPut();
96      }
97      serverSource.updatePut(t);
98      userAggregate.updatePut(t);
99    }
100 
101   public void updateDelete(TableName tn, long t) {
102     if (tableMetrics != null && tn != null) {
103       tableMetrics.updateDelete(tn, t);
104     }
105     if (t > 1000) {
106       serverSource.incrSlowDelete();
107     }
108     serverSource.updateDelete(t);
109     userAggregate.updateDelete(t);
110   }
111 
112   public void updateGet(TableName tn, long t) {
113     if (tableMetrics != null && tn != null) {
114       tableMetrics.updateGet(tn, t);
115     }
116     if (t > 1000) {
117       serverSource.incrSlowGet();
118     }
119     serverSource.updateGet(t);
120     userAggregate.updateGet(t);
121   }
122 
123   public void updateIncrement(TableName tn, long t) {
124     if (tableMetrics != null && tn != null) {
125       tableMetrics.updateIncrement(tn, t);
126     }
127     if (t > 1000) {
128       serverSource.incrSlowIncrement();
129     }
130     serverSource.updateIncrement(t);
131     userAggregate.updateIncrement(t);
132   }
133 
134   public void updateAppend(TableName tn, long t) {
135     if (tableMetrics != null && tn != null) {
136       tableMetrics.updateAppend(tn, t);
137     }
138     if (t > 1000) {
139       serverSource.incrSlowAppend();
140     }
141     serverSource.updateAppend(t);
142     userAggregate.updateAppend(t);
143   }
144 
145   public void updateReplay(long t){
146     serverSource.updateReplay(t);
147     userAggregate.updateReplay(t);
148   }
149 
150   public void updateScanSize(TableName tn, long scanSize){
151     if (tableMetrics != null && tn != null) {
152       tableMetrics.updateScanSize(tn, scanSize);
153     }
154     serverSource.updateScanSize(scanSize);
155   }
156 
157   public void updateScanTime(TableName tn, long t) {
158     if (tableMetrics != null && tn != null) {
159       tableMetrics.updateScanTime(tn, t);
160     }
161     serverSource.updateScanTime(t);
162     userAggregate.updateScanTime(t);
163   }
164 
165   public void updateSplitTime(long t) {
166     serverSource.updateSplitTime(t);
167   }
168 
169   public void incrSplitRequest() {
170     serverSource.incrSplitRequest();
171   }
172 
173   public void incrSplitSuccess() {
174     serverSource.incrSplitSuccess();
175   }
176 
177   public void updateFlush(String table, long t, long memstoreSize, long fileSize) {
178     serverSource.updateFlushTime(t);
179     serverSource.updateFlushMemstoreSize(memstoreSize);
180     serverSource.updateFlushOutputSize(fileSize);
181 
182     if (table != null) {
183       metricsTable.updateFlushTime(table, memstoreSize);
184       metricsTable.updateFlushMemstoreSize(table, memstoreSize);
185       metricsTable.updateFlushOutputSize(table, fileSize);
186     }
187   }
188 
189   public void updateCompaction(String table, boolean isMajor, long t, int inputFileCount,
190       int outputFileCount, long inputBytes, long outputBytes) {
191     serverSource.updateCompactionTime(isMajor, t);
192     serverSource.updateCompactionInputFileCount(isMajor, inputFileCount);
193     serverSource.updateCompactionOutputFileCount(isMajor, outputFileCount);
194     serverSource.updateCompactionInputSize(isMajor, inputBytes);
195     serverSource.updateCompactionOutputSize(isMajor, outputBytes);
196 
197     if (table != null) {
198       metricsTable.updateCompactionTime(table, isMajor, t);
199       metricsTable.updateCompactionInputFileCount(table, isMajor, inputFileCount);
200       metricsTable.updateCompactionOutputFileCount(table, isMajor, outputFileCount);
201       metricsTable.updateCompactionInputSize(table, isMajor, inputBytes);
202       metricsTable.updateCompactionOutputSize(table, isMajor, outputBytes);
203     }
204   }
205 }