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 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;
27  
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;
33  
34  @InterfaceAudience.Private
35  public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable {
36  
37    public static final int PERIOD = 45;
38    public static final String UNKNOWN = "unknown";
39  
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;
50  
51    private ScheduledFuture<?> regionMetricsUpdateTask;
52  
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);
59    }
60  
61    @Override
62    public String getTableName() {
63      HTableDescriptor tableDesc = this.region.getTableDesc();
64      if (tableDesc == null) {
65        return UNKNOWN;
66      }
67      return tableDesc.getTableName().getQualifierAsString();
68    }
69  
70    @Override
71    public String getNamespace() {
72      HTableDescriptor tableDesc = this.region.getTableDesc();
73      if (tableDesc == null) {
74        return UNKNOWN;
75      }
76      return tableDesc.getTableName().getNamespaceAsString();
77    }
78  
79  
80    @Override
81    public String getRegionName() {
82      HRegionInfo regionInfo = this.region.getRegionInfo();
83      if (regionInfo == null) {
84        return UNKNOWN;
85      }
86      return regionInfo.getEncodedName();
87    }
88  
89    @Override
90    public long getNumStores() {
91      Map<byte[],Store> stores = this.region.stores;
92      if (stores == null) {
93        return 0;
94      }
95      return stores.size();
96    }
97  
98    @Override
99    public long getNumStoreFiles() {
100     return numStoreFiles;
101   }
102 
103   @Override
104   public long getMemstoreSize() {
105     return memstoreSize;
106   }
107 
108   @Override
109   public long getStoreFileSize() {
110     return storeFileSize;
111   }
112 
113   @Override
114   public long getReadRequestCount() {
115     return this.region.getReadRequestsCount();
116   }
117 
118   @Override
119   public long getFilteredReadRequestCount() {
120     return this.region.getFilteredReadRequestsCount();
121   }
122 
123   @Override
124   public long getWriteRequestCount() {
125     return this.region.getWriteRequestsCount();
126   }
127 
128   @Override
129   public long getNumFilesCompacted() {
130     return this.region.compactionNumFilesCompacted.get();
131   }
132 
133   @Override
134   public long getNumBytesCompacted() {
135     return this.region.compactionNumBytesCompacted.get();
136   }
137 
138   @Override
139   public long getNumCompactionsCompleted() {
140     return this.region.compactionsFinished.get();
141   }
142 
143   @Override
144   public long getMaxStoreFileAge() {
145     return maxStoreFileAge;
146   }
147 
148   @Override
149   public long getMinStoreFileAge() {
150     return minStoreFileAge;
151   }
152 
153   @Override
154   public long getAvgStoreFileAge() {
155     return avgStoreFileAge;
156   }
157 
158   @Override
159   public long getNumReferenceFiles() {
160     return numReferenceFiles;
161   }
162 
163   @Override
164   public int getRegionHashCode() {
165     return this.region.hashCode();
166   }
167 
168   public class HRegionMetricsWrapperRunnable implements Runnable {
169 
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;
178 
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();
186 
187           long storeMaxStoreFileAge = store.getMaxStoreFileAge();
188           tempMaxStoreFileAge = (storeMaxStoreFileAge > tempMaxStoreFileAge) ?
189             storeMaxStoreFileAge : tempMaxStoreFileAge;
190 
191           long storeMinStoreFileAge = store.getMinStoreFileAge();
192           tempMinStoreFileAge = (storeMinStoreFileAge < tempMinStoreFileAge) ?
193             storeMinStoreFileAge : tempMinStoreFileAge;
194 
195           long storeHFiles = store.getNumHFiles();
196           avgAgeNumerator += store.getAvgStoreFileAge() * storeHFiles;
197           numHFiles += storeHFiles;
198           tempNumReferenceFiles += store.getNumReferenceFiles();
199         }
200       }
201 
202       numStoreFiles = tempNumStoreFiles;
203       memstoreSize = tempMemstoreSize;
204       storeFileSize = tempStoreFileSize;
205       maxStoreFileAge = tempMaxStoreFileAge;
206       if (tempMinStoreFileAge != Long.MAX_VALUE) {
207         minStoreFileAge = tempMinStoreFileAge;
208       }
209 
210       if (numHFiles != 0) {
211         avgStoreFileAge = avgAgeNumerator / numHFiles;
212       }
213 
214       numReferenceFiles = tempNumReferenceFiles;
215     }
216   }
217 
218   @Override
219   public void close() throws IOException {
220     regionMetricsUpdateTask.cancel(true);
221   }
222 
223   /**
224    * Get the replica id of this region.
225    */
226   @Override
227   public int getReplicaId() {
228     return region.getRegionInfo().getReplicaId();
229   }
230 
231 }