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.hbase.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 boolean equals(Object obj) {
165     if (obj == this) return true;
166     if (!(obj instanceof MetricsRegionSourceImpl)) return false;
167     return compareTo((MetricsRegionSourceImpl)obj) == 0;
168   }
169 
170   void snapshot(MetricsRecordBuilder mrb, boolean ignored) {
171     if (closed) return;
172 
173     mrb.addGauge(
174         Interns.info(regionNamePrefix + MetricsRegionServerSource.STORE_COUNT,
175             MetricsRegionServerSource.STORE_COUNT_DESC),
176         this.regionWrapper.getNumStores());
177     mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.STOREFILE_COUNT,
178         MetricsRegionServerSource.STOREFILE_COUNT_DESC),
179         this.regionWrapper.getNumStoreFiles());
180     mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.MEMSTORE_SIZE,
181         MetricsRegionServerSource.MEMSTORE_SIZE_DESC),
182         this.regionWrapper.getMemstoreSize());
183     mrb.addGauge(Interns.info(regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
184         MetricsRegionServerSource.STOREFILE_SIZE_DESC),
185         this.regionWrapper.getStoreFileSize());
186     mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.COMPACTIONS_COMPLETED_COUNT,
187         MetricsRegionSource.COMPACTIONS_COMPLETED_DESC),
188         this.regionWrapper.getNumCompactionsCompleted());
189     mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.NUM_BYTES_COMPACTED_COUNT,
190         MetricsRegionSource.NUM_BYTES_COMPACTED_DESC),
191         this.regionWrapper.getNumBytesCompacted());
192     mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.NUM_FILES_COMPACTED_COUNT,
193         MetricsRegionSource.NUM_FILES_COMPACTED_DESC),
194         this.regionWrapper.getNumFilesCompacted());
195     for (Map.Entry<String, DescriptiveStatistics> entry : this.regionWrapper
196         .getCoprocessorExecutionStatistics()
197         .entrySet()) {
198       DescriptiveStatistics ds = entry.getValue();
199       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
200           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
201         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "Min: "), ds.getMin() / 1000);
202       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
203           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
204         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "Mean: "), ds.getMean() / 1000);
205       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
206           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
207         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "Max: "), ds.getMax() / 1000);
208       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
209           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
210         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "90th percentile: "), ds
211           .getPercentile(90d) / 1000);
212       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
213           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
214         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "95th percentile: "), ds
215           .getPercentile(95d) / 1000);
216       mrb.addGauge(Interns.info(regionNamePrefix + " " + entry.getKey() + " "
217           + MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS,
218         MetricsRegionSource.COPROCESSOR_EXECUTION_STATISTICS_DESC + "99th percentile: "), ds
219           .getPercentile(99d) / 1000);
220     }
221 
222   }
223 }