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.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;
30  
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.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.metrics2.MetricsExecutor;
36  
37  import com.google.common.collect.Sets;
38  
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 = new ConcurrentHashMap<>();
47  
48    public MetricsTableWrapperAggregateImpl(final HRegionServer regionServer) {
49      this.regionServer = regionServer;
50      this.period = regionServer.conf.getLong(HConstants.REGIONSERVER_METRICS_PERIOD,
51        HConstants.DEFAULT_REGIONSERVER_METRICS_PERIOD) + 1000;
52      this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
53      this.runnable = new TableMetricsWrapperRunnable();
54      this.tableMetricsUpdateTask = this.executor.scheduleWithFixedDelay(this.runnable, period, this.period,
55        TimeUnit.MILLISECONDS);
56    }
57  
58    public class TableMetricsWrapperRunnable implements Runnable {
59  
60      @Override
61      public void run() {
62        Map<TableName, MetricsTableValues> localMetricsTableMap = new HashMap<>();
63  
64        for (Region r : regionServer.getOnlineRegionsLocalContext()) {
65          TableName tbl= r.getTableDesc().getTableName();
66          MetricsTableValues metricsTable = localMetricsTableMap.get(tbl);
67          if (metricsTable == null) {
68            metricsTable = new MetricsTableValues();
69            localMetricsTableMap.put(tbl, metricsTable);
70          }
71          long tempStorefilesSize = 0;
72          for (Store store : r.getStores()) {
73            tempStorefilesSize += store.getStorefilesSize();
74          }
75          metricsTable.setMemstoresSize(metricsTable.getMemstoresSize() + r.getMemstoreSize());
76          metricsTable.setStoreFilesSize(metricsTable.getStoreFilesSize() + tempStorefilesSize);
77          metricsTable.setTableSize(metricsTable.getMemstoresSize() + metricsTable.getStoreFilesSize());
78          metricsTable.setReadRequestsCount(metricsTable.getReadRequestsCount() + r.getReadRequestsCount());
79          metricsTable.setWriteRequestsCount(metricsTable.getWriteRequestsCount() + r.getWriteRequestsCount());
80          metricsTable.setTotalRequestsCount(metricsTable.getReadRequestsCount() + metricsTable.getWriteRequestsCount());
81        }
82  
83        for(Map.Entry<TableName, MetricsTableValues> entry : localMetricsTableMap.entrySet()) {
84          TableName tbl = entry.getKey();
85          if (metricsTableMap.get(tbl) == null) {
86            MetricsTableSource tableSource = CompatibilitySingletonFactory
87                .getInstance(MetricsRegionServerSourceFactory.class).createTable(tbl.getNameAsString(),
88                  MetricsTableWrapperAggregateImpl.this);
89            CompatibilitySingletonFactory
90            .getInstance(MetricsRegionServerSourceFactory.class).getTableAggregate()
91            .register(tbl.getNameAsString(), tableSource);
92          }
93          metricsTableMap.put(entry.getKey(), entry.getValue());
94        }
95        Set<TableName> existingTableNames = Sets.newHashSet(metricsTableMap.keySet());
96        existingTableNames.removeAll(localMetricsTableMap.keySet());
97        MetricsTableAggregateSource agg = CompatibilitySingletonFactory
98            .getInstance(MetricsRegionServerSourceFactory.class).getTableAggregate();
99        for (TableName table : existingTableNames) {
100         agg.deregister(table.getNameAsString());
101         if (metricsTableMap.get(table) != null) {
102           metricsTableMap.remove(table);
103         }
104       }
105     }
106   }
107 
108   @Override
109   public long getReadRequestsCount(String table) {
110     MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
111     if (metricsTable == null)
112       return 0;
113     else
114       return metricsTable.getReadRequestsCount();
115   }
116 
117   @Override
118   public long getWriteRequestsCount(String table) {
119     MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
120     if (metricsTable == null)
121       return 0;
122     else
123       return metricsTable.getWriteRequestsCount();
124   }
125 
126   @Override
127   public long getTotalRequestsCount(String table) {
128     MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
129     if (metricsTable == null)
130       return 0;
131     else
132       return metricsTable.getTotalRequestsCount();
133   }
134 
135   @Override
136   public long getMemstoresSize(String table) {
137     MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
138     if (metricsTable == null)
139       return 0;
140     else
141       return metricsTable.getMemstoresSize();
142   }
143 
144   @Override
145   public long getStoreFilesSize(String table) {
146     MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
147     if (metricsTable == null)
148       return 0;
149     else
150       return metricsTable.getStoreFilesSize();
151   }
152 
153   @Override
154   public long getTableSize(String table) {
155     MetricsTableValues metricsTable = metricsTableMap.get(TableName.valueOf(table));
156     if (metricsTable == null)
157       return 0;
158     else
159       return metricsTable.getTableSize();
160   }
161 
162   @Override
163   public void close() throws IOException {
164     tableMetricsUpdateTask.cancel(true);
165   }
166 
167   private static class MetricsTableValues {
168 
169     private long totalRequestsCount;
170     private long readRequestsCount;
171     private long writeRequestsCount;
172     private long memstoresSize;
173     private long storeFilesSize;
174     private long tableSize;
175 
176     public long getTotalRequestsCount() {
177       return totalRequestsCount;
178     }
179 
180     public void setTotalRequestsCount(long totalRequestsCount) {
181       this.totalRequestsCount = totalRequestsCount;
182     }
183 
184     public long getReadRequestsCount() {
185       return readRequestsCount;
186     }
187 
188     public void setReadRequestsCount(long readRequestsCount) {
189       this.readRequestsCount = readRequestsCount;
190     }
191 
192     public long getWriteRequestsCount() {
193       return writeRequestsCount;
194     }
195 
196     public void setWriteRequestsCount(long writeRequestsCount) {
197       this.writeRequestsCount = writeRequestsCount;
198     }
199 
200     public long getMemstoresSize() {
201       return memstoresSize;
202     }
203 
204     public void setMemstoresSize(long memstoresSize) {
205       this.memstoresSize = memstoresSize;
206     }
207 
208     public long getStoreFilesSize() {
209       return storeFilesSize;
210     }
211 
212     public void setStoreFilesSize(long storeFilesSize) {
213       this.storeFilesSize = storeFilesSize;
214     }
215 
216     public long getTableSize() {
217       return tableSize;
218     }
219 
220     public void setTableSize(long tableSize) {
221       this.tableSize = tableSize;
222     }
223   }
224 
225 }