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  package org.apache.hadoop.hbase.hbtop.mode;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import org.apache.hadoop.hbase.ClusterStatus;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.hbtop.Record;
29  import org.apache.hadoop.hbase.hbtop.RecordFilter;
30  import org.apache.hadoop.hbase.hbtop.field.Field;
31  import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
32  
33  /**
34   * Implementation for {@link ModeStrategy} for Table Mode.
35   */
36  @InterfaceAudience.Private
37  public final class TableModeStrategy implements ModeStrategy {
38  
39    private final List<FieldInfo> fieldInfos = Arrays.asList(
40      new FieldInfo(Field.NAMESPACE, 0, true),
41      new FieldInfo(Field.TABLE, 0, true),
42      new FieldInfo(Field.REGION_COUNT, 7, true),
43      new FieldInfo(Field.REQUEST_COUNT_PER_SECOND, 10, true),
44      new FieldInfo(Field.READ_REQUEST_COUNT_PER_SECOND, 10, true),
45      new FieldInfo(Field.WRITE_REQUEST_COUNT_PER_SECOND, 10, true),
46      new FieldInfo(Field.STORE_FILE_SIZE, 13, true),
47      new FieldInfo(Field.UNCOMPRESSED_STORE_FILE_SIZE, 15, false),
48      new FieldInfo(Field.NUM_STORE_FILES, 7, true),
49      new FieldInfo(Field.MEM_STORE_SIZE, 11, true)
50    );
51  
52    private final RegionModeStrategy regionModeStrategy = new RegionModeStrategy();
53  
54    TableModeStrategy() {
55    }
56  
57    @Override
58    public List<FieldInfo> getFieldInfos() {
59      return fieldInfos;
60    }
61  
62    @Override
63    public Field getDefaultSortField() {
64      return Field.REQUEST_COUNT_PER_SECOND;
65    }
66  
67    @Override
68    public List<Record> getRecords(ClusterStatus clusterStatus) {
69      // Get records from RegionModeStrategy and add REGION_COUNT field
70      List<Record> records = new ArrayList<>();
71      for (Record record : regionModeStrategy.getRecords(clusterStatus)) {
72        List<Record.Entry> entries = new ArrayList<>();
73        for (FieldInfo fieldInfo : fieldInfos) {
74          if (record.containsKey(fieldInfo.getField())) {
75            entries.add(Record.entry(fieldInfo.getField(),
76              record.get(fieldInfo.getField())));
77          }
78        }
79  
80        // Add REGION_COUNT field
81        records.add(Record.builder().putAll(Record.ofEntries(entries))
82          .put(Field.REGION_COUNT, 1).build());
83      }
84  
85      // Aggregation by NAMESPACE field
86      Map<TableName, Record> retMap = new HashMap<>();
87      for (Record record : records) {
88        String namespace = record.get(Field.NAMESPACE).asString();
89        String table = record.get(Field.TABLE).asString();
90        TableName tableName = TableName.valueOf(namespace, table);
91  
92        if (retMap.containsKey(tableName)) {
93          retMap.put(tableName, retMap.get(tableName).combine(record));
94        } else {
95          retMap.put(tableName, record);
96        }
97      }
98      return new ArrayList<>(retMap.values());
99    }
100 
101   @Override
102   public DrillDownInfo drillDown(Record selectedRecord) {
103     List<RecordFilter> initialFilters = Arrays.asList(
104       RecordFilter.newBuilder(Field.NAMESPACE).doubleEquals(selectedRecord.get(Field.NAMESPACE)),
105       RecordFilter.newBuilder(Field.TABLE).doubleEquals(selectedRecord.get(Field.TABLE)));
106     return new DrillDownInfo(Mode.REGION, initialFilters);
107   }
108 }