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.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
35  import org.apache.hadoop.metrics2.MetricsExecutor;
36  
37  @InterfaceAudience.Private
38  public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable {
39  
40    private static final Log LOG = LogFactory.getLog(MetricsRegionWrapperImpl.class);
41  
42    public static final int PERIOD = 45;
43    public static final String UNKNOWN = "unknown";
44  
45    private final HRegion region;
46    private ScheduledExecutorService executor;
47    private Runnable runnable;
48    private long numStoreFiles;
49    private long storeRefCount;
50    private long memstoreSize;
51    private long storeFileSize;
52    private long maxStoreFileAge;
53    private long minStoreFileAge;
54    private long avgStoreFileAge;
55    private long numReferenceFiles;
56    private long maxFlushQueueSize;
57    private long maxCompactionQueueSize;
58    private int maxCompactedStoreFileRefCount;
59  
60    private ScheduledFuture<?> regionMetricsUpdateTask;
61  
62    public MetricsRegionWrapperImpl(HRegion region) {
63      this.region = region;
64      this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
65      this.runnable = new HRegionMetricsWrapperRunnable();
66      this.regionMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, PERIOD,
67        PERIOD, TimeUnit.SECONDS);
68    }
69  
70    @Override
71    public String getTableName() {
72      HTableDescriptor tableDesc = this.region.getTableDesc();
73      if (tableDesc == null) {
74        return UNKNOWN;
75      }
76      return tableDesc.getTableName().getQualifierAsString();
77    }
78  
79    @Override
80    public String getNamespace() {
81      HTableDescriptor tableDesc = this.region.getTableDesc();
82      if (tableDesc == null) {
83        return UNKNOWN;
84      }
85      return tableDesc.getTableName().getNamespaceAsString();
86    }
87  
88  
89    @Override
90    public String getRegionName() {
91      HRegionInfo regionInfo = this.region.getRegionInfo();
92      if (regionInfo == null) {
93        return UNKNOWN;
94      }
95      return regionInfo.getEncodedName();
96    }
97  
98    @Override
99    public long getNumStores() {
100     Map<byte[],Store> stores = this.region.stores;
101     if (stores == null) {
102       return 0;
103     }
104     return stores.size();
105   }
106 
107   @Override
108   public long getNumStoreFiles() {
109     return numStoreFiles;
110   }
111 
112   @Override
113   public long getMemstoreSize() {
114     return memstoreSize;
115   }
116 
117   @Override
118   public long getStoreFileSize() {
119     return storeFileSize;
120   }
121 
122   @Override
123   public long getStoreRefCount() {
124     return storeRefCount;
125   }
126 
127   @Override
128   public int getMaxCompactedStoreFileRefCount() {
129     return maxCompactedStoreFileRefCount;
130   }
131 
132   @Override
133   public long getReadRequestCount() {
134     return this.region.getReadRequestsCount();
135   }
136 
137   @Override
138   public long getWriteRequestCount() {
139     return this.region.getWriteRequestsCount();
140   }
141 
142   @Override
143   public long getNumFilesCompacted() {
144     return this.region.compactionNumFilesCompacted.get();
145   }
146 
147   @Override
148   public long getNumBytesCompacted() {
149     return this.region.compactionNumBytesCompacted.get();
150   }
151 
152   @Override
153   public long getNumCompactionsCompleted() {
154     return this.region.compactionsFinished.get();
155   }
156 
157   @Override
158   public long getLastMajorCompactionAge() {
159     long lastMajorCompactionTs = 0L;
160     try {
161       lastMajorCompactionTs = this.region.getOldestHfileTs(true);
162     } catch (IOException ioe) {
163       LOG.error("Could not load HFile info ", ioe);
164     }
165     long now = EnvironmentEdgeManager.currentTime();
166     return now - lastMajorCompactionTs;
167   }
168 
169   @Override
170   public long getNumCompactionsFailed() {
171     return this.region.compactionsFailed.get();
172   }
173 
174   @Override
175   public long getNumCompactionsQueued() {
176     return this.region.compactionsQueued.get();
177   }
178 
179   @Override
180   public long getNumFlushesQueued() {
181     return this.region.flushesQueued.get();
182   }
183 
184   @Override
185   public long getMaxCompactionQueueSize() {
186     return maxCompactionQueueSize;
187   }
188 
189   @Override
190   public long getMaxFlushQueueSize() {
191     return maxFlushQueueSize;
192   }
193 
194   @Override
195   public long getMaxStoreFileAge() {
196     return maxStoreFileAge;
197   }
198 
199   @Override
200   public long getMinStoreFileAge() {
201     return minStoreFileAge;
202   }
203 
204   @Override
205   public long getAvgStoreFileAge() {
206     return avgStoreFileAge;
207   }
208 
209   @Override
210   public long getNumReferenceFiles() {
211     return numReferenceFiles;
212   }
213 
214   @Override
215   public int getRegionHashCode() {
216     return this.region.hashCode();
217   }
218 
219   public class HRegionMetricsWrapperRunnable implements Runnable {
220 
221     @Override
222     public void run() {
223       long tempNumStoreFiles = 0;
224       int tempStoreRefCount = 0;
225       int tempMaxCompactedStoreFileRefCount = 0;
226       long tempMemstoreSize = 0;
227       long tempStoreFileSize = 0;
228       long tempMaxStoreFileAge = 0;
229       long tempMinStoreFileAge = Long.MAX_VALUE;
230       long tempNumReferenceFiles = 0;
231       long tempMaxCompactionQueueSize = 0;
232       long tempMaxFlushQueueSize = 0;
233 
234       long avgAgeNumerator = 0;
235       long numHFiles = 0;
236       if (region.stores != null) {
237         for (Store store : region.stores.values()) {
238           tempNumStoreFiles += store.getStorefilesCount();
239           tempMemstoreSize += store.getMemStoreSize();
240           tempStoreFileSize += store.getStorefilesSize();
241 
242           long storeMaxStoreFileAge = store.getMaxStoreFileAge();
243           tempMaxStoreFileAge = (storeMaxStoreFileAge > tempMaxStoreFileAge) ?
244             storeMaxStoreFileAge : tempMaxStoreFileAge;
245 
246           long storeMinStoreFileAge = store.getMinStoreFileAge();
247           tempMinStoreFileAge = (storeMinStoreFileAge < tempMinStoreFileAge) ?
248             storeMinStoreFileAge : tempMinStoreFileAge;
249 
250           long storeHFiles = store.getNumHFiles();
251           avgAgeNumerator += store.getAvgStoreFileAge() * storeHFiles;
252           numHFiles += storeHFiles;
253           tempNumReferenceFiles += store.getNumReferenceFiles();
254 
255           if (store instanceof HStore) {
256             // Cast here to avoid interface changes to Store
257             HStore hStore = ((HStore) store);
258             tempStoreRefCount += hStore.getStoreRefCount();
259             int currentMaxCompactedStoreFileRefCount = hStore.getMaxCompactedStoreFileRefCount();
260             tempMaxCompactedStoreFileRefCount = Math.max(tempMaxCompactedStoreFileRefCount,
261               currentMaxCompactedStoreFileRefCount);
262           }
263         }
264       }
265 
266       numStoreFiles = tempNumStoreFiles;
267       storeRefCount = tempStoreRefCount;
268       maxCompactedStoreFileRefCount = tempMaxCompactedStoreFileRefCount;
269       memstoreSize = tempMemstoreSize;
270       storeFileSize = tempStoreFileSize;
271       maxStoreFileAge = tempMaxStoreFileAge;
272       if (tempMinStoreFileAge != Long.MAX_VALUE) {
273         minStoreFileAge = tempMinStoreFileAge;
274       }
275 
276       if (numHFiles != 0) {
277         avgStoreFileAge = avgAgeNumerator / numHFiles;
278       }
279 
280       numReferenceFiles = tempNumReferenceFiles;
281       tempMaxCompactionQueueSize = getNumCompactionsQueued();
282       tempMaxFlushQueueSize = getNumFlushesQueued();
283       if (tempMaxCompactionQueueSize > maxCompactionQueueSize) {
284         maxCompactionQueueSize = tempMaxCompactionQueueSize;
285       }
286       if (tempMaxFlushQueueSize > maxFlushQueueSize) {
287         maxFlushQueueSize = tempMaxFlushQueueSize;
288       }
289     }
290   }
291 
292   @Override
293   public void close() throws IOException {
294     regionMetricsUpdateTask.cancel(true);
295   }
296 
297   /**
298    * Get the replica id of this region.
299    */
300   @Override
301   public int getReplicaId() {
302     return region.getRegionInfo().getReplicaId();
303   }
304 
305 }