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.concurrent.atomic.AtomicBoolean;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.metrics.Interns;
27  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
28  import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
29  import org.apache.hadoop.metrics2.lib.MutableFastCounter;
30  
31  @InterfaceAudience.Private
32  public class MetricsRegionSourceImpl implements MetricsRegionSource {
33  
34    private static final Log LOG = LogFactory.getLog(MetricsRegionSourceImpl.class);
35  
36    private AtomicBoolean closed = new AtomicBoolean(false);
37  
38    // Non-final so that we can null out the wrapper
39    // This is just paranoia. We really really don't want to
40    // leak a whole region by way of keeping the
41    // regionWrapper around too long.
42    private MetricsRegionWrapper regionWrapper;
43  
44    private final MetricsRegionAggregateSourceImpl agg;
45    private final DynamicMetricsRegistry registry;
46  
47    private final String regionNamePrefix;
48    private final String regionPutKey;
49    private final String regionDeleteKey;
50    private final String regionGetKey;
51    private final String regionIncrementKey;
52    private final String regionAppendKey;
53    private final String regionScanKey;
54  
55    /*
56     * Implementation note: Do not put histograms per region. With hundreds of regions in a server
57     * histograms allocate too many counters. See HBASE-17016.
58     */
59    private final MutableFastCounter regionPut;
60    private final MutableFastCounter regionDelete;
61    private final MutableFastCounter regionIncrement;
62    private final MutableFastCounter regionAppend;
63    private final MutableFastCounter regionGet;
64    private final MutableFastCounter regionScan;
65  
66    private final int hashCode;
67  
68    public MetricsRegionSourceImpl(MetricsRegionWrapper regionWrapper,
69                                   MetricsRegionAggregateSourceImpl aggregate) {
70      this.regionWrapper = regionWrapper;
71      agg = aggregate;
72      hashCode = regionWrapper.getRegionHashCode();
73      agg.register(this);
74  
75      LOG.debug("Creating new MetricsRegionSourceImpl for table " +
76          regionWrapper.getTableName() + " " + regionWrapper.getRegionName());
77  
78      registry = agg.getMetricsRegistry();
79  
80      regionNamePrefix = "Namespace_" + regionWrapper.getNamespace() +
81          "_table_" + regionWrapper.getTableName() +
82          "_region_" + regionWrapper.getRegionName()  +
83          "_metric_";
84  
85      String suffix = "Count";
86  
87      regionPutKey = regionNamePrefix + MetricsRegionServerSource.PUT_KEY + suffix;
88      regionPut = registry.getCounter(regionPutKey, 0L);
89  
90      regionDeleteKey = regionNamePrefix + MetricsRegionServerSource.DELETE_KEY + suffix;
91      regionDelete = registry.getCounter(regionDeleteKey, 0L);
92  
93      regionIncrementKey = regionNamePrefix + MetricsRegionServerSource.INCREMENT_KEY + suffix;
94      regionIncrement = registry.getCounter(regionIncrementKey, 0L);
95  
96      regionAppendKey = regionNamePrefix + MetricsRegionServerSource.APPEND_KEY + suffix;
97      regionAppend = registry.getCounter(regionAppendKey, 0L);
98  
99      regionGetKey = regionNamePrefix + MetricsRegionServerSource.GET_KEY + suffix;
100     regionGet = registry.getCounter(regionGetKey, 0L);
101 
102     regionScanKey = regionNamePrefix + MetricsRegionServerSource.SCAN_KEY + suffix;
103     regionScan = registry.getCounter(regionScanKey, 0L);
104   }
105 
106   @Override
107   public void close() {
108     boolean wasClosed = closed.getAndSet(true);
109 
110     // Has someone else already closed this for us?
111     if (wasClosed) {
112       return;
113     }
114 
115     // Before removing the metrics remove this region from the aggregate region bean.
116     // This should mean that it's unlikely that snapshot and close happen at the same time.
117     agg.deregister(this);
118 
119     // While it's un-likely that snapshot and close happen at the same time it's still possible.
120     // So grab the lock to ensure that all calls to snapshot are done before we remove the metrics
121     synchronized (this) {
122       if (LOG.isTraceEnabled()) {
123         LOG.trace("Removing region Metrics: " + regionWrapper.getRegionName());
124       }
125 
126       registry.removeMetric(regionPutKey);
127       registry.removeMetric(regionDeleteKey);
128       registry.removeMetric(regionIncrementKey);
129       registry.removeMetric(regionAppendKey);
130       registry.removeMetric(regionGetKey);
131       registry.removeMetric(regionScanKey);
132 
133       regionWrapper = null;
134     }
135   }
136 
137   @Override
138   public void updatePut() {
139     regionPut.incr();
140   }
141 
142   @Override
143   public void updateDelete() {
144     regionDelete.incr();
145   }
146 
147   @Override
148   public void updateGet(long mills) {
149     regionGet.incr();
150   }
151 
152   @Override
153   public void updateScanTime(long mills) {
154     regionScan.incr();
155   }
156 
157   @Override
158   public void updateIncrement() {
159     regionIncrement.incr();
160   }
161 
162   @Override
163   public void updateAppend() {
164     regionAppend.incr();
165   }
166 
167   @Override
168   public MetricsRegionAggregateSource getAggregateSource() {
169     return agg;
170   }
171 
172   @Override
173   public int compareTo(MetricsRegionSource source) {
174     if (!(source instanceof MetricsRegionSourceImpl)) {
175       return -1;
176     }
177 
178     MetricsRegionSourceImpl impl = (MetricsRegionSourceImpl) source;
179     if (impl == null) {
180       return -1;
181     }
182 
183     return Long.compare(hashCode, impl.hashCode);
184   }
185 
186   void snapshot(MetricsRecordBuilder mrb, boolean ignored) {
187 
188     // If there is a close that started be double extra sure
189     // that we're not getting any locks and not putting data
190     // into the metrics that should be removed. So early out
191     // before even getting the lock.
192     if (closed.get()) {
193       return;
194     }
195 
196     // Grab the read
197     // This ensures that removes of the metrics
198     // can't happen while we are putting them back in.
199     synchronized (this) {
200 
201       // It's possible that a close happened between checking
202       // the closed variable and getting the lock.
203       if (closed.get()) {
204         return;
205       }
206 
207       mrb.addGauge(
208           Interns.info(
209               regionNamePrefix + MetricsRegionServerSource.STORE_COUNT,
210               MetricsRegionServerSource.STORE_COUNT_DESC),
211           this.regionWrapper.getNumStores());
212       mrb.addGauge(Interns.info(
213               regionNamePrefix + MetricsRegionServerSource.STOREFILE_COUNT,
214               MetricsRegionServerSource.STOREFILE_COUNT_DESC),
215           this.regionWrapper.getNumStoreFiles());
216       mrb.addGauge(Interns.info(
217               regionNamePrefix + MetricsRegionServerSource.STORE_REF_COUNT,
218               MetricsRegionServerSource.STORE_REF_COUNT),
219           this.regionWrapper.getStoreRefCount());
220       mrb.addGauge(Interns.info(
221         regionNamePrefix + MetricsRegionServerSource.MAX_COMPACTED_STORE_FILE_REF_COUNT,
222         MetricsRegionServerSource.MAX_COMPACTED_STORE_FILE_REF_COUNT),
223         this.regionWrapper.getMaxCompactedStoreFileRefCount()
224       );
225       mrb.addGauge(Interns.info(
226               regionNamePrefix + MetricsRegionServerSource.MEMSTORE_SIZE,
227               MetricsRegionServerSource.MEMSTORE_SIZE_DESC),
228           this.regionWrapper.getMemstoreSize());
229       mrb.addGauge(Interns.info(
230         regionNamePrefix + MetricsRegionServerSource.MAX_STORE_FILE_AGE,
231         MetricsRegionServerSource.MAX_STORE_FILE_AGE_DESC),
232         this.regionWrapper.getMaxStoreFileAge());
233       mrb.addGauge(Interns.info(
234         regionNamePrefix + MetricsRegionServerSource.MIN_STORE_FILE_AGE,
235         MetricsRegionServerSource.MIN_STORE_FILE_AGE_DESC),
236         this.regionWrapper.getMinStoreFileAge());
237       mrb.addGauge(Interns.info(
238         regionNamePrefix + MetricsRegionServerSource.AVG_STORE_FILE_AGE,
239         MetricsRegionServerSource.AVG_STORE_FILE_AGE_DESC),
240         this.regionWrapper.getAvgStoreFileAge());
241       mrb.addGauge(Interns.info(
242         regionNamePrefix + MetricsRegionServerSource.NUM_REFERENCE_FILES,
243         MetricsRegionServerSource.NUM_REFERENCE_FILES_DESC),
244         this.regionWrapper.getNumReferenceFiles());
245       mrb.addGauge(Interns.info(
246               regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
247               MetricsRegionServerSource.STOREFILE_SIZE_DESC),
248           this.regionWrapper.getStoreFileSize());
249       mrb.addCounter(Interns.info(
250               regionNamePrefix + MetricsRegionSource.COMPACTIONS_COMPLETED_COUNT,
251               MetricsRegionSource.COMPACTIONS_COMPLETED_DESC),
252           this.regionWrapper.getNumCompactionsCompleted());
253       mrb.addCounter(Interns.info(
254           regionNamePrefix + MetricsRegionSource.COMPACTIONS_FAILED_COUNT,
255           MetricsRegionSource.COMPACTIONS_FAILED_DESC),
256           this.regionWrapper.getNumCompactionsFailed());
257       mrb.addCounter(Interns.info(
258               regionNamePrefix + MetricsRegionSource.LAST_MAJOR_COMPACTION_AGE,
259               MetricsRegionSource.LAST_MAJOR_COMPACTION_DESC),
260           this.regionWrapper.getLastMajorCompactionAge());
261       mrb.addCounter(Interns.info(
262               regionNamePrefix + MetricsRegionSource.NUM_BYTES_COMPACTED_COUNT,
263               MetricsRegionSource.NUM_BYTES_COMPACTED_DESC),
264           this.regionWrapper.getNumBytesCompacted());
265       mrb.addCounter(Interns.info(
266               regionNamePrefix + MetricsRegionSource.NUM_FILES_COMPACTED_COUNT,
267               MetricsRegionSource.NUM_FILES_COMPACTED_DESC),
268           this.regionWrapper.getNumFilesCompacted());
269       mrb.addCounter(Interns.info(
270               regionNamePrefix + MetricsRegionServerSource.READ_REQUEST_COUNT,
271               MetricsRegionServerSource.READ_REQUEST_COUNT_DESC),
272           this.regionWrapper.getReadRequestCount());
273       mrb.addCounter(Interns.info(
274               regionNamePrefix + MetricsRegionServerSource.WRITE_REQUEST_COUNT,
275               MetricsRegionServerSource.WRITE_REQUEST_COUNT_DESC),
276           this.regionWrapper.getWriteRequestCount());
277       mrb.addCounter(Interns.info(
278               regionNamePrefix + MetricsRegionSource.REPLICA_ID,
279               MetricsRegionSource.REPLICA_ID_DESC),
280           this.regionWrapper.getReplicaId());
281       mrb.addCounter(Interns.info(
282               regionNamePrefix + MetricsRegionSource.COMPACTIONS_QUEUED_COUNT,
283               MetricsRegionSource.COMPACTIONS_QUEUED_DESC),
284           this.regionWrapper.getNumCompactionsQueued());
285       mrb.addCounter(Interns.info(
286               regionNamePrefix + MetricsRegionSource.FLUSHES_QUEUED_COUNT,
287               MetricsRegionSource.FLUSHES_QUEUED_DESC),
288           this.regionWrapper.getNumFlushesQueued());
289       mrb.addCounter(Interns.info(
290               regionNamePrefix + MetricsRegionSource.MAX_COMPACTION_QUEUE_SIZE,
291               MetricsRegionSource.MAX_COMPACTION_QUEUE_DESC),
292           this.regionWrapper.getMaxCompactionQueueSize());
293       mrb.addCounter(Interns.info(
294               regionNamePrefix + MetricsRegionSource.MAX_FLUSH_QUEUE_SIZE,
295               MetricsRegionSource.MAX_FLUSH_QUEUE_DESC),
296           this.regionWrapper.getMaxFlushQueueSize());
297     }
298   }
299 
300   @Override
301   public int hashCode() {
302     return hashCode;
303   }
304 
305   @Override
306   public boolean equals(Object obj) {
307     return obj == this ||
308         (obj instanceof MetricsRegionSourceImpl && compareTo((MetricsRegionSourceImpl) obj) == 0);
309   }
310 }