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