HBASE-26286: Add support for specifying store file tracker when restoring or cloning...
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / MetricsTableWrapperAggregateImpl.java
blob77130b8da4cc8c77b0df397073fe346f931f8265
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.hadoop.hbase.regionserver;
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.ScheduledFuture;
29 import java.util.concurrent.TimeUnit;
31 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.yetus.audience.InterfaceAudience;
35 import org.apache.hadoop.metrics2.MetricsExecutor;
37 import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
39 @InterfaceAudience.Private
40 public class MetricsTableWrapperAggregateImpl implements MetricsTableWrapperAggregate, Closeable {
41 private final HRegionServer regionServer;
42 private ScheduledExecutorService executor;
43 private Runnable runnable;
44 private long period;
45 private ScheduledFuture<?> tableMetricsUpdateTask;
46 private ConcurrentHashMap<TableName, MetricsTableValues> metricsTableMap
47 = new ConcurrentHashMap<>();
49 public MetricsTableWrapperAggregateImpl(final HRegionServer regionServer) {
50 this.regionServer = regionServer;
51 this.period = regionServer.getConfiguration().getLong(HConstants.REGIONSERVER_METRICS_PERIOD,
52 HConstants.DEFAULT_REGIONSERVER_METRICS_PERIOD) + 1000;
53 this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
54 this.runnable = new TableMetricsWrapperRunnable();
55 this.tableMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, period,
56 period, TimeUnit.MILLISECONDS);
59 public class TableMetricsWrapperRunnable implements Runnable {
61 @Override
62 public void run() {
63 Map<TableName, MetricsTableValues> localMetricsTableMap = new HashMap<>();
64 for (Region r : regionServer.getOnlineRegionsLocalContext()) {
65 TableName tbl = r.getTableDescriptor().getTableName();
66 MetricsTableValues mt = localMetricsTableMap.get(tbl);
67 if (mt == null) {
68 mt = new MetricsTableValues();
69 localMetricsTableMap.put(tbl, mt);
71 long memstoreReadCount = 0L;
72 long mixedReadCount = 0L;
73 String tempKey = null;
74 if (r.getStores() != null) {
75 String familyName = null;
76 for (Store store : r.getStores()) {
77 familyName = store.getColumnFamilyName();
79 mt.storeFileCount += store.getStorefilesCount();
80 mt.memstoreSize += (store.getMemStoreSize().getDataSize()
81 + store.getMemStoreSize().getHeapSize() + store.getMemStoreSize().getOffHeapSize());
82 mt.storeFileSize += store.getStorefilesSize();
83 mt.referenceFileCount += store.getNumReferenceFiles();
84 if (store.getMaxStoreFileAge().isPresent()) {
85 mt.maxStoreFileAge =
86 Math.max(mt.maxStoreFileAge, store.getMaxStoreFileAge().getAsLong());
88 if (store.getMinStoreFileAge().isPresent()) {
89 mt.minStoreFileAge =
90 Math.min(mt.minStoreFileAge, store.getMinStoreFileAge().getAsLong());
92 if (store.getAvgStoreFileAge().isPresent()) {
93 mt.totalStoreFileAge =
94 (long) store.getAvgStoreFileAge().getAsDouble() * store.getStorefilesCount();
96 mt.storeCount += 1;
97 tempKey = tbl.getNameAsString() + HASH + familyName;
98 Long tempVal = mt.perStoreMemstoreOnlyReadCount.get(tempKey);
99 if (tempVal == null) {
100 tempVal = 0L;
102 memstoreReadCount = store.getMemstoreOnlyRowReadsCount() + tempVal;
103 tempVal = mt.perStoreMixedReadCount.get(tempKey);
104 if (tempVal == null) {
105 tempVal = 0L;
107 mixedReadCount = store.getMixedRowReadsCount() + tempVal;
108 // accumulate the count
109 mt.perStoreMemstoreOnlyReadCount.put(tempKey, memstoreReadCount);
110 mt.perStoreMixedReadCount.put(tempKey, mixedReadCount);
113 mt.regionCount += 1;
115 mt.readRequestCount += r.getReadRequestsCount();
116 mt.filteredReadRequestCount += r.getFilteredReadRequestsCount();
117 mt.writeRequestCount += r.getWriteRequestsCount();
121 for (Map.Entry<TableName, MetricsTableValues> entry : localMetricsTableMap.entrySet()) {
122 TableName tbl = entry.getKey();
123 if (metricsTableMap.get(tbl) == null) {
124 // this will add the Wrapper to the list of TableMetrics
125 CompatibilitySingletonFactory
126 .getInstance(MetricsRegionServerSourceFactory.class)
127 .getTableAggregate()
128 .getOrCreateTableSource(tbl.getNameAsString(), MetricsTableWrapperAggregateImpl.this);
130 metricsTableMap.put(entry.getKey(), entry.getValue());
132 Set<TableName> existingTableNames = Sets.newHashSet(metricsTableMap.keySet());
133 existingTableNames.removeAll(localMetricsTableMap.keySet());
134 MetricsTableAggregateSource agg = CompatibilitySingletonFactory
135 .getInstance(MetricsRegionServerSourceFactory.class).getTableAggregate();
136 for (TableName table : existingTableNames) {
137 agg.deleteTableSource(table.getNameAsString());
138 if (metricsTableMap.get(table) != null) {
139 metricsTableMap.remove(table);
145 @Override
146 public long getReadRequestCount(String table) {
147 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
148 if (metricsTable == null) {
149 return 0;
150 } else {
151 return metricsTable.readRequestCount;
155 @Override
156 public Map<String, Long> getMemstoreOnlyRowReadsCount(String table) {
157 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
158 if (metricsTable == null) {
159 return null;
160 } else {
161 return metricsTable.perStoreMemstoreOnlyReadCount;
165 @Override
166 public Map<String, Long> getMixedRowReadsCount(String table) {
167 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
168 if (metricsTable == null) {
169 return null;
170 } else {
171 return metricsTable.perStoreMixedReadCount;
175 @Override
176 public long getCpRequestsCount(String table) {
177 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
178 if (metricsTable == null) {
179 return 0;
180 } else {
181 return metricsTable.cpRequestCount;
185 public long getFilteredReadRequestCount(String table) {
186 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
187 if (metricsTable == null) {
188 return 0;
190 return metricsTable.filteredReadRequestCount;
193 @Override
194 public long getWriteRequestCount(String table) {
195 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
196 if (metricsTable == null) {
197 return 0;
198 } else {
199 return metricsTable.writeRequestCount;
203 @Override
204 public long getTotalRequestsCount(String table) {
205 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
206 if (metricsTable == null) {
207 return 0;
208 } else {
209 return metricsTable.readRequestCount + metricsTable.writeRequestCount;
213 @Override
214 public long getMemStoreSize(String table) {
215 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
216 if (metricsTable == null) {
217 return 0;
218 } else {
219 return metricsTable.memstoreSize;
223 @Override
224 public long getStoreFileSize(String table) {
225 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
226 if (metricsTable == null) {
227 return 0;
228 } else {
229 return metricsTable.storeFileSize;
233 @Override
234 public long getTableSize(String table) {
235 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
236 if (metricsTable == null) {
237 return 0;
238 } else {
239 return metricsTable.memstoreSize + metricsTable.storeFileSize;
243 public long getNumRegions(String table) {
244 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
245 if (metricsTable == null) {
246 return 0;
248 return metricsTable.regionCount;
251 @Override
252 public long getNumStores(String table) {
253 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
254 if (metricsTable == null) {
255 return 0;
257 return metricsTable.storeCount;
260 @Override
261 public long getNumStoreFiles(String table) {
262 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
263 if (metricsTable == null) {
264 return 0;
266 return metricsTable.storeFileCount;
269 @Override
270 public long getMaxStoreFileAge(String table) {
271 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
272 if (metricsTable == null) {
273 return 0;
275 return metricsTable.maxStoreFileAge;
278 @Override
279 public long getMinStoreFileAge(String table) {
280 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
281 if (metricsTable == null) {
282 return 0;
284 return metricsTable.minStoreFileAge == Long.MAX_VALUE ? 0 : metricsTable.minStoreFileAge;
287 @Override
288 public long getAvgStoreFileAge(String table) {
289 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
290 if (metricsTable == null) {
291 return 0;
294 return metricsTable.storeFileCount == 0
296 : (metricsTable.totalStoreFileAge / metricsTable.storeFileCount);
299 @Override
300 public long getNumReferenceFiles(String table) {
301 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
302 if (metricsTable == null) {
303 return 0;
305 return metricsTable.referenceFileCount;
308 @Override
309 public long getAvgRegionSize(String table) {
310 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
311 if (metricsTable == null) {
312 return 0;
314 return metricsTable.regionCount == 0
316 : (metricsTable.memstoreSize + metricsTable.storeFileSize) / metricsTable.regionCount;
319 public long getCpRequestCount(String table) {
320 MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
321 if (metricsTable == null) {
322 return 0;
324 return metricsTable.cpRequestCount;
327 @Override
328 public void close() throws IOException {
329 tableMetricsUpdateTask.cancel(true);
332 private static class MetricsTableValues {
333 long readRequestCount;
334 long filteredReadRequestCount;
335 long writeRequestCount;
336 long memstoreSize;
337 long regionCount;
338 long storeCount;
339 long storeFileCount;
340 long storeFileSize;
341 long maxStoreFileAge;
342 long minStoreFileAge = Long.MAX_VALUE;
343 long totalStoreFileAge;
344 long referenceFileCount;
345 long cpRequestCount;
346 Map<String, Long> perStoreMemstoreOnlyReadCount = new HashMap<>();
347 Map<String, Long> perStoreMixedReadCount = new HashMap<>();