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