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
;
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
;
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
{
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
);
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()) {
86 Math
.max(mt
.maxStoreFileAge
, store
.getMaxStoreFileAge().getAsLong());
88 if (store
.getMinStoreFileAge().isPresent()) {
90 Math
.min(mt
.minStoreFileAge
, store
.getMinStoreFileAge().getAsLong());
92 if (store
.getAvgStoreFileAge().isPresent()) {
93 mt
.totalStoreFileAge
=
94 (long) store
.getAvgStoreFileAge().getAsDouble() * store
.getStorefilesCount();
97 tempKey
= tbl
.getNameAsString() + HASH
+ familyName
;
98 Long tempVal
= mt
.perStoreMemstoreOnlyReadCount
.get(tempKey
);
99 if (tempVal
== null) {
102 memstoreReadCount
= store
.getMemstoreOnlyRowReadsCount() + tempVal
;
103 tempVal
= mt
.perStoreMixedReadCount
.get(tempKey
);
104 if (tempVal
== null) {
107 mixedReadCount
= store
.getMixedRowReadsCount() + tempVal
;
108 // accumulate the count
109 mt
.perStoreMemstoreOnlyReadCount
.put(tempKey
, memstoreReadCount
);
110 mt
.perStoreMixedReadCount
.put(tempKey
, mixedReadCount
);
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)
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
);
146 public long getReadRequestCount(String table
) {
147 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
148 if (metricsTable
== null) {
151 return metricsTable
.readRequestCount
;
156 public Map
<String
, Long
> getMemstoreOnlyRowReadsCount(String table
) {
157 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
158 if (metricsTable
== null) {
161 return metricsTable
.perStoreMemstoreOnlyReadCount
;
166 public Map
<String
, Long
> getMixedRowReadsCount(String table
) {
167 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
168 if (metricsTable
== null) {
171 return metricsTable
.perStoreMixedReadCount
;
176 public long getCpRequestsCount(String table
) {
177 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
178 if (metricsTable
== null) {
181 return metricsTable
.cpRequestCount
;
185 public long getFilteredReadRequestCount(String table
) {
186 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
187 if (metricsTable
== null) {
190 return metricsTable
.filteredReadRequestCount
;
194 public long getWriteRequestCount(String table
) {
195 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
196 if (metricsTable
== null) {
199 return metricsTable
.writeRequestCount
;
204 public long getTotalRequestsCount(String table
) {
205 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
206 if (metricsTable
== null) {
209 return metricsTable
.readRequestCount
+ metricsTable
.writeRequestCount
;
214 public long getMemStoreSize(String table
) {
215 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
216 if (metricsTable
== null) {
219 return metricsTable
.memstoreSize
;
224 public long getStoreFileSize(String table
) {
225 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
226 if (metricsTable
== null) {
229 return metricsTable
.storeFileSize
;
234 public long getTableSize(String table
) {
235 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
236 if (metricsTable
== null) {
239 return metricsTable
.memstoreSize
+ metricsTable
.storeFileSize
;
243 public long getNumRegions(String table
) {
244 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
245 if (metricsTable
== null) {
248 return metricsTable
.regionCount
;
252 public long getNumStores(String table
) {
253 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
254 if (metricsTable
== null) {
257 return metricsTable
.storeCount
;
261 public long getNumStoreFiles(String table
) {
262 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
263 if (metricsTable
== null) {
266 return metricsTable
.storeFileCount
;
270 public long getMaxStoreFileAge(String table
) {
271 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
272 if (metricsTable
== null) {
275 return metricsTable
.maxStoreFileAge
;
279 public long getMinStoreFileAge(String table
) {
280 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
281 if (metricsTable
== null) {
284 return metricsTable
.minStoreFileAge
== Long
.MAX_VALUE ?
0 : metricsTable
.minStoreFileAge
;
288 public long getAvgStoreFileAge(String table
) {
289 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
290 if (metricsTable
== null) {
294 return metricsTable
.storeFileCount
== 0
296 : (metricsTable
.totalStoreFileAge
/ metricsTable
.storeFileCount
);
300 public long getNumReferenceFiles(String table
) {
301 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
302 if (metricsTable
== null) {
305 return metricsTable
.referenceFileCount
;
309 public long getAvgRegionSize(String table
) {
310 MetricsTableValues metricsTable
= metricsTableMap
.get(TableName
.valueOf(table
));
311 if (metricsTable
== null) {
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) {
324 return metricsTable
.cpRequestCount
;
328 public void close() throws IOException
{
329 tableMetricsUpdateTask
.cancel(true);
332 private static class MetricsTableValues
{
333 long readRequestCount
;
334 long filteredReadRequestCount
;
335 long writeRequestCount
;
341 long maxStoreFileAge
;
342 long minStoreFileAge
= Long
.MAX_VALUE
;
343 long totalStoreFileAge
;
344 long referenceFileCount
;
346 Map
<String
, Long
> perStoreMemstoreOnlyReadCount
= new HashMap
<>();
347 Map
<String
, Long
> perStoreMixedReadCount
= new HashMap
<>();