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.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.hadoop.hbase.ClusterStatus;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.hbtop.Record;
30  import org.apache.hadoop.hbase.hbtop.RecordFilter;
31  import org.apache.hadoop.hbase.hbtop.field.Field;
32  import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
33  
34  /**
35   * Implementation for {@link ModeStrategy} for Namespace Mode.
36   */
37  @InterfaceAudience.Private
38  public final class NamespaceModeStrategy implements ModeStrategy {
39  
40    private final List<FieldInfo> fieldInfos = Arrays.asList(
41      new FieldInfo(Field.NAMESPACE, 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    NamespaceModeStrategy(){
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<String, Record> retMap = new HashMap<>();
87      for (Record record : records) {
88        String namespace = record.get(Field.NAMESPACE).asString();
89        if (retMap.containsKey(namespace)) {
90          retMap.put(namespace, retMap.get(namespace).combine(record));
91        } else {
92          retMap.put(namespace, record);
93        }
94      }
95      return new ArrayList<>(retMap.values());
96    }
97  
98    @Override
99    public DrillDownInfo drillDown(Record selectedRecord) {
100     List<RecordFilter> initialFilters =
101       Collections.singletonList(RecordFilter.newBuilder(Field.NAMESPACE)
102         .doubleEquals(selectedRecord.get(Field.NAMESPACE)));
103     return new DrillDownInfo(Mode.TABLE, initialFilters);
104   }
105 }