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.hadoop.hbase.CompatibilitySingletonFactory;
22  import org.apache.hadoop.hbase.HRegionInfo;
23  import org.apache.hadoop.hbase.HTableDescriptor;
24  import org.apache.hadoop.metrics2.MetricsExecutor;
25  
26  import java.io.Closeable;
27  import java.io.IOException;
28  import java.util.Map;
29  import java.util.concurrent.ScheduledExecutorService;
30  import java.util.concurrent.ScheduledFuture;
31  import java.util.concurrent.TimeUnit;
32  
33  public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable {
34  
35    public static final int PERIOD = 45;
36  
37    private final HRegion region;
38    private ScheduledExecutorService executor;
39    private Runnable runnable;
40    private long numStoreFiles;
41    private long memstoreSize;
42    private long storeFileSize;
43  
44    private ScheduledFuture<?> regionMetricsUpdateTask;
45  
46    public MetricsRegionWrapperImpl(HRegion region) {
47      this.region = region;
48      this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
49      this.runnable = new HRegionMetricsWrapperRunnable();
50      this.regionMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, PERIOD,
51        PERIOD, TimeUnit.SECONDS);
52    }
53  
54    @Override
55    public String getTableName() {
56      HTableDescriptor tableDesc = this.region.getTableDesc();
57      if (tableDesc == null) {
58        return "";
59      }
60      return tableDesc.getTableName().getNameAsString();
61    }
62  
63    @Override
64    public String getRegionName() {
65      HRegionInfo regionInfo = this.region.getRegionInfo();
66      if (regionInfo == null) {
67        return "";
68      }
69      return regionInfo.getEncodedName();
70    }
71  
72    @Override
73    public long getNumStores() {
74      Map<byte[],Store> stores = this.region.stores;
75      if (stores == null) {
76        return 0;
77      }
78      return stores.size();
79    }
80  
81    @Override
82    public long getNumStoreFiles() {
83      return numStoreFiles;
84    }
85  
86    @Override
87    public long getMemstoreSize() {
88      return memstoreSize;
89    }
90  
91    @Override
92    public long getStoreFileSize() {
93      return storeFileSize;
94    }
95  
96    @Override
97    public long getReadRequestCount() {
98      return this.region.getReadRequestsCount();
99    }
100 
101   @Override
102   public long getWriteRequestCount() {
103     return this.region.getWriteRequestsCount();
104   }
105 
106   public class HRegionMetricsWrapperRunnable implements Runnable {
107 
108     @Override
109     public void run() {
110       long tempNumStoreFiles = 0;
111       long tempMemstoreSize = 0;
112       long tempStoreFileSize = 0;
113 
114       if (region.stores != null) {
115         for (Store store : region.stores.values()) {
116           tempNumStoreFiles += store.getStorefilesCount();
117           tempMemstoreSize += store.getMemStoreSize();
118           tempStoreFileSize += store.getStorefilesSize();
119         }
120       }
121 
122       numStoreFiles = tempNumStoreFiles;
123       memstoreSize = tempMemstoreSize;
124       storeFileSize = tempStoreFileSize;
125     }
126   }
127 
128   @Override
129   public void close() throws IOException {
130     regionMetricsUpdateTask.cancel(true);
131   }
132 
133 }