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