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.MetricMutableStat;
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 MetricMutableStat regionGet;
50    private MetricMutableStat 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() +
60          " " +
61          regionWrapper.getRegionName());
62  
63      registry = agg.getMetricsRegistry();
64  
65      regionNamePrefix = "table." + regionWrapper.getTableName() + "."
66          + "region." + regionWrapper.getRegionName() + ".";
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.newStat(regionGetKey, "", OPS_SAMPLE_NAME, SIZE_VALUE_NAME);
85  
86      regionScanNextKey = regionNamePrefix + MetricsRegionServerSource.SCAN_NEXT_KEY;
87      regionScanNext = registry.newStat(regionScanNextKey, "", OPS_SAMPLE_NAME, SIZE_VALUE_NAME);
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   void snapshot(MetricsRecordBuilder mrb, boolean ignored) {
162     if (closed) return;
163 
164     mrb.addGauge(regionNamePrefix + MetricsRegionServerSource.STORE_COUNT,
165         MetricsRegionServerSource.STORE_COUNT_DESC,
166         this.regionWrapper.getNumStores());
167     mrb.addGauge(regionNamePrefix + MetricsRegionServerSource.STOREFILE_COUNT,
168         MetricsRegionServerSource.STOREFILE_COUNT_DESC,
169         this.regionWrapper.getNumStoreFiles());
170     mrb.addGauge(regionNamePrefix + MetricsRegionServerSource.MEMSTORE_SIZE,
171         MetricsRegionServerSource.MEMSTORE_SIZE_DESC,
172         this.regionWrapper.getMemstoreSize());
173     mrb.addGauge(regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
174         MetricsRegionServerSource.STOREFILE_SIZE_DESC,
175         this.regionWrapper.getStoreFileSize());
176     mrb.addCounter(regionNamePrefix + MetricsRegionServerSource.READ_REQUEST_COUNT,
177         MetricsRegionServerSource.READ_REQUEST_COUNT_DESC,
178         this.regionWrapper.getReadRequestCount());
179     mrb.addCounter(regionNamePrefix + MetricsRegionServerSource.WRITE_REQUEST_COUNT,
180         MetricsRegionServerSource.WRITE_REQUEST_COUNT_DESC,
181         this.regionWrapper.getWriteRequestCount());
182 
183   }
184 }