HBASE-16012 Major compaction can't work due to obsolete scanner read point in RegionS...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / MetricsRegionWrapperImpl.java
blobf9e01cd7a6ae748da9bf9438f40465f195f55320
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.hadoop.hbase.regionserver;
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.util.Map;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.metrics2.MetricsExecutor;
34 @InterfaceAudience.Private
35 public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable {
37 public static final int PERIOD = 45;
38 public static final String UNKNOWN = "unknown";
40 private final HRegion region;
41 private ScheduledExecutorService executor;
42 private Runnable runnable;
43 private long numStoreFiles;
44 private long memstoreSize;
45 private long storeFileSize;
46 private long maxStoreFileAge;
47 private long minStoreFileAge;
48 private long avgStoreFileAge;
49 private long numReferenceFiles;
51 private ScheduledFuture<?> regionMetricsUpdateTask;
53 public MetricsRegionWrapperImpl(HRegion region) {
54 this.region = region;
55 this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
56 this.runnable = new HRegionMetricsWrapperRunnable();
57 this.regionMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, PERIOD,
58 PERIOD, TimeUnit.SECONDS);
61 @Override
62 public String getTableName() {
63 HTableDescriptor tableDesc = this.region.getTableDesc();
64 if (tableDesc == null) {
65 return UNKNOWN;
67 return tableDesc.getTableName().getQualifierAsString();
70 @Override
71 public String getNamespace() {
72 HTableDescriptor tableDesc = this.region.getTableDesc();
73 if (tableDesc == null) {
74 return UNKNOWN;
76 return tableDesc.getTableName().getNamespaceAsString();
80 @Override
81 public String getRegionName() {
82 HRegionInfo regionInfo = this.region.getRegionInfo();
83 if (regionInfo == null) {
84 return UNKNOWN;
86 return regionInfo.getEncodedName();
89 @Override
90 public long getNumStores() {
91 Map<byte[],Store> stores = this.region.stores;
92 if (stores == null) {
93 return 0;
95 return stores.size();
98 @Override
99 public long getNumStoreFiles() {
100 return numStoreFiles;
103 @Override
104 public long getMemstoreSize() {
105 return memstoreSize;
108 @Override
109 public long getStoreFileSize() {
110 return storeFileSize;
113 @Override
114 public long getReadRequestCount() {
115 return this.region.getReadRequestsCount();
118 @Override
119 public long getFilteredReadRequestCount() {
120 return this.region.getFilteredReadRequestsCount();
123 @Override
124 public long getWriteRequestCount() {
125 return this.region.getWriteRequestsCount();
128 @Override
129 public long getNumFilesCompacted() {
130 return this.region.compactionNumFilesCompacted.get();
133 @Override
134 public long getNumBytesCompacted() {
135 return this.region.compactionNumBytesCompacted.get();
138 @Override
139 public long getNumCompactionsCompleted() {
140 return this.region.compactionsFinished.get();
143 @Override
144 public long getMaxStoreFileAge() {
145 return maxStoreFileAge;
148 @Override
149 public long getMinStoreFileAge() {
150 return minStoreFileAge;
153 @Override
154 public long getAvgStoreFileAge() {
155 return avgStoreFileAge;
158 @Override
159 public long getNumReferenceFiles() {
160 return numReferenceFiles;
163 @Override
164 public int getRegionHashCode() {
165 return this.region.hashCode();
168 public class HRegionMetricsWrapperRunnable implements Runnable {
170 @Override
171 public void run() {
172 long tempNumStoreFiles = 0;
173 long tempMemstoreSize = 0;
174 long tempStoreFileSize = 0;
175 long tempMaxStoreFileAge = 0;
176 long tempMinStoreFileAge = Long.MAX_VALUE;
177 long tempNumReferenceFiles = 0;
179 long avgAgeNumerator = 0;
180 long numHFiles = 0;
181 if (region.stores != null) {
182 for (Store store : region.stores.values()) {
183 tempNumStoreFiles += store.getStorefilesCount();
184 tempMemstoreSize += store.getMemStoreSize();
185 tempStoreFileSize += store.getStorefilesSize();
187 long storeMaxStoreFileAge = store.getMaxStoreFileAge();
188 tempMaxStoreFileAge = (storeMaxStoreFileAge > tempMaxStoreFileAge) ?
189 storeMaxStoreFileAge : tempMaxStoreFileAge;
191 long storeMinStoreFileAge = store.getMinStoreFileAge();
192 tempMinStoreFileAge = (storeMinStoreFileAge < tempMinStoreFileAge) ?
193 storeMinStoreFileAge : tempMinStoreFileAge;
195 long storeHFiles = store.getNumHFiles();
196 avgAgeNumerator += store.getAvgStoreFileAge() * storeHFiles;
197 numHFiles += storeHFiles;
198 tempNumReferenceFiles += store.getNumReferenceFiles();
202 numStoreFiles = tempNumStoreFiles;
203 memstoreSize = tempMemstoreSize;
204 storeFileSize = tempStoreFileSize;
205 maxStoreFileAge = tempMaxStoreFileAge;
206 if (tempMinStoreFileAge != Long.MAX_VALUE) {
207 minStoreFileAge = tempMinStoreFileAge;
210 if (numHFiles != 0) {
211 avgStoreFileAge = avgAgeNumerator / numHFiles;
214 numReferenceFiles = tempNumReferenceFiles;
218 @Override
219 public void close() throws IOException {
220 regionMetricsUpdateTask.cancel(true);
224 * Get the replica id of this region.
226 @Override
227 public int getReplicaId() {
228 return region.getRegionInfo().getReplicaId();