001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.regionserver;
020
021import java.io.Closeable;
022import java.io.IOException;
023import java.util.HashMap;
024import java.util.Map;
025import java.util.OptionalDouble;
026import java.util.OptionalLong;
027import java.util.concurrent.ScheduledExecutorService;
028import java.util.concurrent.ScheduledFuture;
029import java.util.concurrent.TimeUnit;
030
031import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.TableDescriptor;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.apache.hadoop.metrics2.MetricsExecutor;
036import org.apache.yetus.audience.InterfaceAudience;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040@InterfaceAudience.Private
041public class MetricsRegionWrapperImpl implements MetricsRegionWrapper, Closeable {
042
043  private static final Logger LOG = LoggerFactory.getLogger(MetricsRegionWrapperImpl.class);
044
045  public static final int PERIOD = 45;
046  public static final String UNKNOWN = "unknown";
047
048  private final HRegion region;
049  private ScheduledExecutorService executor;
050  private Runnable runnable;
051  private long numStoreFiles;
052  private long storeRefCount;
053  private long maxCompactedStoreFileRefCount;
054  private long memstoreSize;
055  private long storeFileSize;
056  private long maxStoreFileAge;
057  private long minStoreFileAge;
058  private long avgStoreFileAge;
059  private long numReferenceFiles;
060  private long maxFlushQueueSize;
061  private long maxCompactionQueueSize;
062  private Map<String, Long> readsOnlyFromMemstore;
063  private Map<String, Long> mixedReadsOnStore;
064
065  private ScheduledFuture<?> regionMetricsUpdateTask;
066
067  public MetricsRegionWrapperImpl(HRegion region) {
068    this.region = region;
069    this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
070    this.runnable = new HRegionMetricsWrapperRunnable();
071    this.regionMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, PERIOD,
072      PERIOD, TimeUnit.SECONDS);
073  }
074
075  @Override
076  public String getTableName() {
077    TableDescriptor tableDesc = this.region.getTableDescriptor();
078    if (tableDesc == null) {
079      return UNKNOWN;
080    }
081    return tableDesc.getTableName().getQualifierAsString();
082  }
083
084  @Override
085  public String getNamespace() {
086    TableDescriptor tableDesc = this.region.getTableDescriptor();
087    if (tableDesc == null) {
088      return UNKNOWN;
089    }
090    return tableDesc.getTableName().getNamespaceAsString();
091  }
092
093
094  @Override
095  public String getRegionName() {
096    RegionInfo regionInfo = this.region.getRegionInfo();
097    if (regionInfo == null) {
098      return UNKNOWN;
099    }
100    return regionInfo.getEncodedName();
101  }
102
103  @Override
104  public long getNumStores() {
105    Map<byte[], HStore> stores = this.region.stores;
106    if (stores == null) {
107      return 0;
108    }
109    return stores.size();
110  }
111
112  @Override
113  public long getNumStoreFiles() {
114    return numStoreFiles;
115  }
116
117  @Override
118  public long getMemStoreSize() {
119    return memstoreSize;
120  }
121
122  @Override
123  public long getStoreFileSize() {
124    return storeFileSize;
125  }
126
127  @Override
128  public long getStoreRefCount() {
129    return storeRefCount;
130  }
131
132  @Override
133  public long getMaxCompactedStoreFileRefCount() {
134    return maxCompactedStoreFileRefCount;
135  }
136
137  @Override
138  public long getReadRequestCount() {
139    return this.region.getReadRequestsCount();
140  }
141
142  @Override
143  public long getCpRequestCount() {
144    return this.region.getCpRequestsCount();
145  }
146
147  @Override
148  public long getFilteredReadRequestCount() {
149    return this.region.getFilteredReadRequestsCount();
150  }
151
152  @Override
153  public long getWriteRequestCount() {
154    return this.region.getWriteRequestsCount();
155  }
156
157  @Override
158  public long getNumFilesCompacted() {
159    return this.region.compactionNumFilesCompacted.sum();
160  }
161
162  @Override
163  public long getNumBytesCompacted() {
164    return this.region.compactionNumBytesCompacted.sum();
165  }
166
167  @Override
168  public long getNumCompactionsCompleted() {
169    return this.region.compactionsFinished.sum();
170  }
171
172  @Override
173  public long getLastMajorCompactionAge() {
174    long lastMajorCompactionTs = 0L;
175    try {
176      lastMajorCompactionTs = this.region.getOldestHfileTs(true);
177    } catch (IOException ioe) {
178      LOG.error("Could not load HFile info ", ioe);
179    }
180    long now = EnvironmentEdgeManager.currentTime();
181    return now - lastMajorCompactionTs;
182  }
183
184  @Override
185  public long getTotalRequestCount() {
186    return getReadRequestCount() + getWriteRequestCount();
187  }
188
189  @Override
190  public long getNumCompactionsFailed() {
191    return this.region.compactionsFailed.sum();
192  }
193
194  @Override
195  public long getNumCompactionsQueued() {
196    return this.region.compactionsQueued.sum();
197  }
198
199  @Override
200  public long getNumFlushesQueued() {
201    return this.region.flushesQueued.sum();
202  }
203
204  @Override
205  public long getMaxCompactionQueueSize() {
206    return maxCompactionQueueSize;
207  }
208
209  @Override
210  public long getMaxFlushQueueSize() {
211    return maxFlushQueueSize;
212  }
213
214  @Override
215  public long getMaxStoreFileAge() {
216    return maxStoreFileAge;
217  }
218
219  @Override
220  public long getMinStoreFileAge() {
221    return minStoreFileAge;
222  }
223
224  @Override
225  public long getAvgStoreFileAge() {
226    return avgStoreFileAge;
227  }
228
229  @Override
230  public long getNumReferenceFiles() {
231    return numReferenceFiles;
232  }
233
234  @Override
235  public int getRegionHashCode() {
236    return this.region.hashCode();
237  }
238
239  @Override
240  public Map<String, Long> getMemstoreOnlyRowReadsCount() {
241    return readsOnlyFromMemstore;
242  }
243
244  @Override
245  public Map<String, Long> getMixedRowReadsCount() {
246    return mixedReadsOnStore;
247  }
248
249  public class HRegionMetricsWrapperRunnable implements Runnable {
250
251    @Override
252    public void run() {
253      long tempNumStoreFiles = 0;
254      int tempStoreRefCount = 0;
255      int tempMaxCompactedStoreFileRefCount = 0;
256      long tempMemstoreSize = 0;
257      long tempStoreFileSize = 0;
258      long tempMaxStoreFileAge = 0;
259      long tempMinStoreFileAge = Long.MAX_VALUE;
260      long tempNumReferenceFiles = 0;
261      long tempMaxCompactionQueueSize = 0;
262      long tempMaxFlushQueueSize = 0;
263      long avgAgeNumerator = 0;
264      long numHFiles = 0;
265      if (region.stores != null) {
266        for (HStore store : region.stores.values()) {
267          tempNumStoreFiles += store.getStorefilesCount();
268          int currentStoreRefCount = store.getStoreRefCount();
269          tempStoreRefCount += currentStoreRefCount;
270          int currentMaxCompactedStoreFileRefCount = store.getMaxCompactedStoreFileRefCount();
271          tempMaxCompactedStoreFileRefCount = Math.max(tempMaxCompactedStoreFileRefCount,
272            currentMaxCompactedStoreFileRefCount);
273          tempMemstoreSize += store.getMemStoreSize().getDataSize();
274          tempStoreFileSize += store.getStorefilesSize();
275          OptionalLong storeMaxStoreFileAge = store.getMaxStoreFileAge();
276          if (storeMaxStoreFileAge.isPresent() &&
277              storeMaxStoreFileAge.getAsLong() > tempMaxStoreFileAge) {
278            tempMaxStoreFileAge = storeMaxStoreFileAge.getAsLong();
279          }
280
281          OptionalLong storeMinStoreFileAge = store.getMinStoreFileAge();
282          if (storeMinStoreFileAge.isPresent() &&
283              storeMinStoreFileAge.getAsLong() < tempMinStoreFileAge) {
284            tempMinStoreFileAge = storeMinStoreFileAge.getAsLong();
285          }
286
287          long storeHFiles = store.getNumHFiles();
288          numHFiles += storeHFiles;
289          tempNumReferenceFiles += store.getNumReferenceFiles();
290
291          OptionalDouble storeAvgStoreFileAge = store.getAvgStoreFileAge();
292          if (storeAvgStoreFileAge.isPresent()) {
293            avgAgeNumerator += (long) storeAvgStoreFileAge.getAsDouble() * storeHFiles;
294          }
295          if(mixedReadsOnStore == null) {
296            mixedReadsOnStore = new HashMap<String, Long>();
297          }
298          Long tempVal = mixedReadsOnStore.get(store.getColumnFamilyName());
299          if (tempVal == null) {
300            tempVal = 0L;
301          } else {
302            tempVal += store.getMixedRowReadsCount();
303          }
304          mixedReadsOnStore.put(store.getColumnFamilyName(), tempVal);
305          if (readsOnlyFromMemstore == null) {
306            readsOnlyFromMemstore = new HashMap<String, Long>();
307          }
308          tempVal = readsOnlyFromMemstore.get(store.getColumnFamilyName());
309          if (tempVal == null) {
310            tempVal = 0L;
311          } else {
312            tempVal += store.getMemstoreOnlyRowReadsCount();
313          }
314          readsOnlyFromMemstore.put(store.getColumnFamilyName(), tempVal);
315        }
316      }
317
318      numStoreFiles = tempNumStoreFiles;
319      storeRefCount = tempStoreRefCount;
320      maxCompactedStoreFileRefCount = tempMaxCompactedStoreFileRefCount;
321      memstoreSize = tempMemstoreSize;
322      storeFileSize = tempStoreFileSize;
323      maxStoreFileAge = tempMaxStoreFileAge;
324      if (tempMinStoreFileAge != Long.MAX_VALUE) {
325        minStoreFileAge = tempMinStoreFileAge;
326      }
327
328      if (numHFiles != 0) {
329        avgStoreFileAge = avgAgeNumerator / numHFiles;
330      }
331
332      numReferenceFiles = tempNumReferenceFiles;
333      tempMaxCompactionQueueSize = getNumCompactionsQueued();
334      tempMaxFlushQueueSize = getNumFlushesQueued();
335      if (tempMaxCompactionQueueSize > maxCompactionQueueSize) {
336        maxCompactionQueueSize = tempMaxCompactionQueueSize;
337      }
338      if (tempMaxFlushQueueSize > maxFlushQueueSize) {
339        maxFlushQueueSize = tempMaxFlushQueueSize;
340      }
341    }
342  }
343
344  @Override
345  public void close() throws IOException {
346    regionMetricsUpdateTask.cancel(true);
347  }
348
349  /**
350   * Get the replica id of this region.
351   */
352  @Override
353  public int getReplicaId() {
354    return region.getRegionInfo().getReplicaId();
355  }
356
357}