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