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