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    public static final String UNKNOWN = "unknown";
37  
38    private final HRegion region;
39    private ScheduledExecutorService executor;
40    private Runnable runnable;
41    private long numStoreFiles;
42    private long memstoreSize;
43    private long storeFileSize;
44  
45    private ScheduledFuture<?> regionMetricsUpdateTask;
46  
47    public MetricsRegionWrapperImpl(HRegion region) {
48      this.region = region;
49      this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
50      this.runnable = new HRegionMetricsWrapperRunnable();
51      this.regionMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, PERIOD,
52        PERIOD, TimeUnit.SECONDS);
53    }
54  
55    @Override
56    public String getTableName() {
57      HTableDescriptor tableDesc = this.region.getTableDesc();
58      if (tableDesc == null) {
59        return UNKNOWN;
60      }
61      return tableDesc.getTableName().getQualifierAsString();
62    }
63  
64    @Override
65    public String getNamespace() {
66      HTableDescriptor tableDesc = this.region.getTableDesc();
67      if (tableDesc == null) {
68        return UNKNOWN;
69      }
70      return tableDesc.getTableName().getNamespaceAsString();
71    }
72  
73  
74    @Override
75    public String getRegionName() {
76      HRegionInfo regionInfo = this.region.getRegionInfo();
77      if (regionInfo == null) {
78        return UNKNOWN;
79      }
80      return regionInfo.getEncodedName();
81    }
82  
83    @Override
84    public long getNumStores() {
85      Map<byte[],Store> stores = this.region.stores;
86      if (stores == null) {
87        return 0;
88      }
89      return stores.size();
90    }
91  
92    @Override
93    public long getNumStoreFiles() {
94      return numStoreFiles;
95    }
96  
97    @Override
98    public long getMemstoreSize() {
99      return memstoreSize;
100   }
101 
102   @Override
103   public long getStoreFileSize() {
104     return storeFileSize;
105   }
106 
107   @Override
108   public long getReadRequestCount() {
109     return this.region.getReadRequestsCount();
110   }
111 
112   @Override
113   public long getWriteRequestCount() {
114     return this.region.getWriteRequestsCount();
115   }
116 
117   @Override
118   public long getNumFilesCompacted() {
119     return this.region.compactionNumFilesCompacted.get();
120   }
121 
122   @Override
123   public long getNumBytesCompacted() {
124     return this.region.compactionNumBytesCompacted.get();
125   }
126 
127   @Override
128   public long getNumCompactionsCompleted() {
129     return this.region.compactionsFinished.get();
130   }
131 
132   public class HRegionMetricsWrapperRunnable implements Runnable {
133 
134     @Override
135     public void run() {
136       long tempNumStoreFiles = 0;
137       long tempMemstoreSize = 0;
138       long tempStoreFileSize = 0;
139 
140       if (region.stores != null) {
141         for (Store store : region.stores.values()) {
142           tempNumStoreFiles += store.getStorefilesCount();
143           tempMemstoreSize += store.getMemStoreSize();
144           tempStoreFileSize += store.getStorefilesSize();
145         }
146       }
147 
148       numStoreFiles = tempNumStoreFiles;
149       memstoreSize = tempMemstoreSize;
150       storeFileSize = tempStoreFileSize;
151     }
152   }
153 
154   @Override
155   public void close() throws IOException {
156     regionMetricsUpdateTask.cancel(true);
157   }
158 
159 }