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