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
;
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
) {
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
);
62 public String
getTableName() {
63 HTableDescriptor tableDesc
= this.region
.getTableDesc();
64 if (tableDesc
== null) {
67 return tableDesc
.getTableName().getQualifierAsString();
71 public String
getNamespace() {
72 HTableDescriptor tableDesc
= this.region
.getTableDesc();
73 if (tableDesc
== null) {
76 return tableDesc
.getTableName().getNamespaceAsString();
81 public String
getRegionName() {
82 HRegionInfo regionInfo
= this.region
.getRegionInfo();
83 if (regionInfo
== null) {
86 return regionInfo
.getEncodedName();
90 public long getNumStores() {
91 Map
<byte[],Store
> stores
= this.region
.stores
;
99 public long getNumStoreFiles() {
100 return numStoreFiles
;
104 public long getMemstoreSize() {
109 public long getStoreFileSize() {
110 return storeFileSize
;
114 public long getReadRequestCount() {
115 return this.region
.getReadRequestsCount();
119 public long getFilteredReadRequestCount() {
120 return this.region
.getFilteredReadRequestsCount();
124 public long getWriteRequestCount() {
125 return this.region
.getWriteRequestsCount();
129 public long getNumFilesCompacted() {
130 return this.region
.compactionNumFilesCompacted
.get();
134 public long getNumBytesCompacted() {
135 return this.region
.compactionNumBytesCompacted
.get();
139 public long getNumCompactionsCompleted() {
140 return this.region
.compactionsFinished
.get();
144 public long getMaxStoreFileAge() {
145 return maxStoreFileAge
;
149 public long getMinStoreFileAge() {
150 return minStoreFileAge
;
154 public long getAvgStoreFileAge() {
155 return avgStoreFileAge
;
159 public long getNumReferenceFiles() {
160 return numReferenceFiles
;
164 public int getRegionHashCode() {
165 return this.region
.hashCode();
168 public class HRegionMetricsWrapperRunnable
implements Runnable
{
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;
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
;
219 public void close() throws IOException
{
220 regionMetricsUpdateTask
.cancel(true);
224 * Get the replica id of this region.
227 public int getReplicaId() {
228 return region
.getRegionInfo().getReplicaId();