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