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 getNumCompactionsFailed() {
145     return this.region.compactionsFailed.get();
146   }
147 
148   @Override
149   public long getMaxStoreFileAge() {
150     return maxStoreFileAge;
151   }
152 
153   @Override
154   public long getMinStoreFileAge() {
155     return minStoreFileAge;
156   }
157 
158   @Override
159   public long getAvgStoreFileAge() {
160     return avgStoreFileAge;
161   }
162 
163   @Override
164   public long getNumReferenceFiles() {
165     return numReferenceFiles;
166   }
167 
168   @Override
169   public int getRegionHashCode() {
170     return this.region.hashCode();
171   }
172
173   public class HRegionMetricsWrapperRunnable implements Runnable {
174
175     @Override
176     public void run() {
177       long tempNumStoreFiles = 0;
178       long tempMemstoreSize = 0;
179       long tempStoreFileSize = 0;
180       long tempMaxStoreFileAge = 0;
181       long tempMinStoreFileAge = Long.MAX_VALUE;
182       long tempNumReferenceFiles = 0;
183
184       long avgAgeNumerator = 0;
185       long numHFiles = 0;
186       if (region.stores != null) {
187         for (Store store : region.stores.values()) {
188           tempNumStoreFiles += store.getStorefilesCount();
189           tempMemstoreSize += store.getMemStoreSize();
190           tempStoreFileSize += store.getStorefilesSize();
191
192           long storeMaxStoreFileAge = store.getMaxStoreFileAge();
193           tempMaxStoreFileAge = (storeMaxStoreFileAge > tempMaxStoreFileAge) ?
194             storeMaxStoreFileAge : tempMaxStoreFileAge;
195
196           long storeMinStoreFileAge = store.getMinStoreFileAge();
197           tempMinStoreFileAge = (storeMinStoreFileAge < tempMinStoreFileAge) ?
198             storeMinStoreFileAge : tempMinStoreFileAge;
199
200           long storeHFiles = store.getNumHFiles();
201           avgAgeNumerator += store.getAvgStoreFileAge() * storeHFiles;
202           numHFiles += storeHFiles;
203           tempNumReferenceFiles += store.getNumReferenceFiles();
204         }
205       }
206
207       numStoreFiles = tempNumStoreFiles;
208       memstoreSize = tempMemstoreSize;
209       storeFileSize = tempStoreFileSize;
210       maxStoreFileAge = tempMaxStoreFileAge;
211       if (tempMinStoreFileAge != Long.MAX_VALUE) {
212         minStoreFileAge = tempMinStoreFileAge;
213       }
214
215       if (numHFiles != 0) {
216         avgStoreFileAge = avgAgeNumerator / numHFiles;
217       }
218
219       numReferenceFiles = tempNumReferenceFiles;
220     }
221   }
222 
223   @Override
224   public void close() throws IOException {
225     regionMetricsUpdateTask.cancel(true);
226   }
227
228   /**
229    * Get the replica id of this region.
230    */
231   @Override
232   public int getReplicaId() {
233     return region.getRegionInfo().getReplicaId();
234   }
235
236 }