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.metrics2.MetricsRecordBuilder;
27  import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
28  import org.apache.hadoop.metrics2.lib.Interns;
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.MUTATE_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.MEMSTORE_SIZE,
218               MetricsRegionServerSource.MEMSTORE_SIZE_DESC),
219           this.regionWrapper.getMemstoreSize());
220       mrb.addGauge(Interns.info(
221         regionNamePrefix + MetricsRegionServerSource.MAX_STORE_FILE_AGE,
222         MetricsRegionServerSource.MAX_STORE_FILE_AGE_DESC),
223         this.regionWrapper.getMaxStoreFileAge());
224       mrb.addGauge(Interns.info(
225         regionNamePrefix + MetricsRegionServerSource.MIN_STORE_FILE_AGE,
226         MetricsRegionServerSource.MIN_STORE_FILE_AGE_DESC),
227         this.regionWrapper.getMinStoreFileAge());
228       mrb.addGauge(Interns.info(
229         regionNamePrefix + MetricsRegionServerSource.AVG_STORE_FILE_AGE,
230         MetricsRegionServerSource.AVG_STORE_FILE_AGE_DESC),
231         this.regionWrapper.getAvgStoreFileAge());
232       mrb.addGauge(Interns.info(
233         regionNamePrefix + MetricsRegionServerSource.NUM_REFERENCE_FILES,
234         MetricsRegionServerSource.NUM_REFERENCE_FILES_DESC),
235         this.regionWrapper.getNumReferenceFiles());
236       mrb.addGauge(Interns.info(
237               regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
238               MetricsRegionServerSource.STOREFILE_SIZE_DESC),
239           this.regionWrapper.getStoreFileSize());
240       mrb.addCounter(Interns.info(
241               regionNamePrefix + MetricsRegionSource.COMPACTIONS_COMPLETED_COUNT,
242               MetricsRegionSource.COMPACTIONS_COMPLETED_DESC),
243           this.regionWrapper.getNumCompactionsCompleted());
244       mrb.addCounter(Interns.info(
245               regionNamePrefix + MetricsRegionSource.NUM_BYTES_COMPACTED_COUNT,
246               MetricsRegionSource.NUM_BYTES_COMPACTED_DESC),
247           this.regionWrapper.getNumBytesCompacted());
248       mrb.addCounter(Interns.info(
249               regionNamePrefix + MetricsRegionSource.NUM_FILES_COMPACTED_COUNT,
250               MetricsRegionSource.NUM_FILES_COMPACTED_DESC),
251           this.regionWrapper.getNumFilesCompacted());
252       mrb.addCounter(Interns.info(
253               regionNamePrefix + MetricsRegionServerSource.READ_REQUEST_COUNT,
254               MetricsRegionServerSource.READ_REQUEST_COUNT_DESC),
255           this.regionWrapper.getReadRequestCount());
256       mrb.addCounter(Interns.info(
257               regionNamePrefix + MetricsRegionServerSource.WRITE_REQUEST_COUNT,
258               MetricsRegionServerSource.WRITE_REQUEST_COUNT_DESC),
259           this.regionWrapper.getWriteRequestCount());
260       mrb.addCounter(Interns.info(
261               regionNamePrefix + MetricsRegionServerSource.TOTAL_REQUEST_COUNT,
262               MetricsRegionServerSource.TOTAL_REQUEST_COUNT_DESC),
263           this.regionWrapper.getTotalRequestCount());
264       mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.REPLICA_ID,
265               MetricsRegionSource.REPLICA_ID_DESC),
266           this.regionWrapper.getReplicaId());
267     }
268   }
269 
270   @Override
271   public int hashCode() {
272     return hashCode;
273   }
274 
275   @Override
276   public boolean equals(Object obj) {
277     return obj == this ||
278         (obj instanceof MetricsRegionSourceImpl && compareTo((MetricsRegionSourceImpl) obj) == 0);
279   }
280 }