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 getWriteRequestCount() {
116     return this.region.getWriteRequestsCount();
117   }
118 
119   @Override
120   public long getNumFilesCompacted() {
121     return this.region.compactionNumFilesCompacted.get();
122   }
123 
124   @Override
125   public long getNumBytesCompacted() {
126     return this.region.compactionNumBytesCompacted.get();
127   }
128 
129   @Override
130   public long getNumCompactionsCompleted() {
131     return this.region.compactionsFinished.get();
132   }
133 
134   @Override
135   public int getRegionHashCode() {
136     return this.region.hashCode();
137   }
138 
139   public class HRegionMetricsWrapperRunnable implements Runnable {
140 
141     @Override
142     public void run() {
143       long tempNumStoreFiles = 0;
144       long tempMemstoreSize = 0;
145       long tempStoreFileSize = 0;
146 
147       if (region.stores != null) {
148         for (Store store : region.stores.values()) {
149           tempNumStoreFiles += store.getStorefilesCount();
150           tempMemstoreSize += store.getMemStoreSize();
151           tempStoreFileSize += store.getStorefilesSize();
152         }
153       }
154 
155       numStoreFiles = tempNumStoreFiles;
156       memstoreSize = tempMemstoreSize;
157       storeFileSize = tempStoreFileSize;
158     }
159   }
160 
161   @Override
162   public void close() throws IOException {
163     regionMetricsUpdateTask.cancel(true);
164   }
165 
166   /**
167    * Get the replica id of this region.
168    */
169   @Override
170   public int getReplicaId() {
171     return region.getRegionInfo().getReplicaId();
172   }
173 
174 }