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  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.util.Map;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
28  import org.apache.hadoop.metrics2.impl.JmxCacheBuster;
29  import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
30  import org.apache.hadoop.metrics2.lib.Interns;
31  import org.apache.hadoop.metrics2.lib.MutableCounterLong;
32  import org.apache.hadoop.metrics2.lib.MutableHistogram;
33  
34  @InterfaceAudience.Private
35  public class MetricsRegionSourceImpl implements MetricsRegionSource {
36  
37    private final MetricsRegionWrapper regionWrapper;
38  
39  
40    private boolean closed = false;
41    private MetricsRegionAggregateSourceImpl agg;
42    private DynamicMetricsRegistry registry;
43    private static final Log LOG = LogFactory.getLog(MetricsRegionSourceImpl.class);
44  
45    private String regionNamePrefix;
46    private String regionPutKey;
47    private String regionDeleteKey;
48    private String regionGetKey;
49    private String regionIncrementKey;
50    private String regionAppendKey;
51    private String regionScanNextKey;
52    private MutableCounterLong regionPut;
53    private MutableCounterLong regionDelete;
54  
55    private MutableCounterLong regionIncrement;
56    private MutableCounterLong regionAppend;
57  
58    private MutableHistogram regionGet;
59    private MutableHistogram regionScanNext;
60  
61    public MetricsRegionSourceImpl(MetricsRegionWrapper regionWrapper,
62                                   MetricsRegionAggregateSourceImpl aggregate) {
63      this.regionWrapper = regionWrapper;
64      agg = aggregate;
65      agg.register(this);
66  
67      LOG.debug("Creating new MetricsRegionSourceImpl for table " +
68          regionWrapper.getTableName() + " " + regionWrapper.getRegionName());
69  
70      registry = agg.getMetricsRegistry();
71  
72      regionNamePrefix = "Namespace_" + regionWrapper.getNamespace() +
73          "_table_" + regionWrapper.getTableName() +
74          "_region_" + regionWrapper.getRegionName()  +
75          "_metric_";
76  
77      String suffix = "Count";
78  
79      regionPutKey = regionNamePrefix + MetricsRegionServerSource.MUTATE_KEY + suffix;
80      regionPut = registry.getLongCounter(regionPutKey, 0l);
81  
82      regionDeleteKey = regionNamePrefix + MetricsRegionServerSource.DELETE_KEY + suffix;
83      regionDelete = registry.getLongCounter(regionDeleteKey, 0l);
84  
85      regionIncrementKey = regionNamePrefix + MetricsRegionServerSource.INCREMENT_KEY + suffix;
86      regionIncrement = registry.getLongCounter(regionIncrementKey, 0l);
87  
88      regionAppendKey = regionNamePrefix + MetricsRegionServerSource.APPEND_KEY + suffix;
89      regionAppend = registry.getLongCounter(regionAppendKey, 0l);
90  
91      regionGetKey = regionNamePrefix + MetricsRegionServerSource.GET_KEY;
92      regionGet = registry.newHistogram(regionGetKey);
93  
94      regionScanNextKey = regionNamePrefix + MetricsRegionServerSource.SCAN_NEXT_KEY;
95      regionScanNext = registry.newHistogram(regionScanNextKey);
96    }
97  
98    @Override
99    public void close() {
100     closed = true;
101     agg.deregister(this);
102 
103     LOG.trace("Removing region Metrics: " + regionWrapper.getRegionName());
104     registry.removeMetric(regionPutKey);
105     registry.removeMetric(regionDeleteKey);
106 
107     registry.removeMetric(regionIncrementKey);
108 
109     registry.removeMetric(regionAppendKey);
110 
111     registry.removeMetric(regionGetKey);
112     registry.removeMetric(regionScanNextKey);
113 
114     JmxCacheBuster.clearJmxCache();
115   }
116 
117   @Override
118   public void updatePut() {
119     regionPut.incr();
120   }
121 
122   @Override
123   public void updateDelete() {
124     regionDelete.incr();
125   }
126 
127   @Override
128   public void updateGet(long getSize) {
129     regionGet.add(getSize);
130   }
131 
132   @Override
133   public void updateScan(long scanSize) {
134     regionScanNext.add(scanSize);
135   }
136 
137   @Override
138   public void updateIncrement() {
139     regionIncrement.incr();
140   }
141 
142   @Override
143   public void updateAppend() {
144     regionAppend.incr();
145   }
146 
147   @Override
148   public MetricsRegionAggregateSource getAggregateSource() {
149     return agg;
150   }
151 
152   @Override
153   public int compareTo(MetricsRegionSource source) {
154 
155     if (!(source instanceof MetricsRegionSourceImpl))
156       return -1;
157 
158     MetricsRegionSourceImpl impl = (MetricsRegionSourceImpl) source;
159     return this.regionWrapper.getRegionName()
160         .compareTo(impl.regionWrapper.getRegionName());
161   }
162 
163   @Override
164   public int hashCode() {
165     return this.regionWrapper.getRegionName().hashCode();
166   }
167 
168   @Override
169   public boolean equals(Object obj) {
170     if (obj == this) return true;
171     if (!(obj instanceof MetricsRegionSourceImpl)) return false;
172     return compareTo((MetricsRegionSourceImpl)obj) == 0;
173   }
174 
175   void snapshot(MetricsRecordBuilder mrb, boolean ignored) {
176     if (closed) return;
177 
178     mrb.addGauge(
179         Interns.info(regionNamePrefix + MetricsRegionServerSource.STORE_COUNT,
180             MetricsRegionServerSource.STORE_COUNT_DESC),
181         this.regionWrapper.getNumStores());
182     mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.STOREFILE_COUNT,
183         MetricsRegionServerSource.STOREFILE_COUNT_DESC),
184         this.regionWrapper.getNumStoreFiles());
185     mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.MEMSTORE_SIZE,
186         MetricsRegionServerSource.MEMSTORE_SIZE_DESC),
187         this.regionWrapper.getMemstoreSize());
188     mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
189         MetricsRegionServerSource.STOREFILE_SIZE_DESC),
190         this.regionWrapper.getStoreFileSize());
191     mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.COMPACTIONS_COMPLETED_COUNT,
192         MetricsRegionSource.COMPACTIONS_COMPLETED_DESC),
193         this.regionWrapper.getNumCompactionsCompleted());
194     mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.NUM_BYTES_COMPACTED_COUNT,
195         MetricsRegionSource.NUM_BYTES_COMPACTED_DESC),
196         this.regionWrapper.getNumBytesCompacted());
197     mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.NUM_FILES_COMPACTED_COUNT,
198         MetricsRegionSource.NUM_FILES_COMPACTED_DESC),
199         this.regionWrapper.getNumFilesCompacted());
200     for (Map.Entry<String, DescriptiveStatistics> entry : this.regionWrapper
201         .getCoprocessorExecutionStatistics()
202         .entrySet()) {
203       DescriptiveStatistics ds = entry.getValue();
204       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
205           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
206         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "Min: "), ds.getMin() / 1000);
207       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
208           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
209         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "Mean: "), ds.getMean() / 1000);
210       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
211           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
212         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "Max: "), ds.getMax() / 1000);
213       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
214           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
215         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "90th percentile: "), ds
216           .getPercentile(90d) / 1000);
217       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
218           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
219         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "95th percentile: "), ds
220           .getPercentile(95d) / 1000);
221       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
222           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
223         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "99th percentile: "), ds
224           .getPercentile(99d) / 1000);
225     }
226 
227   }
228 }