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  import org.apache.hadoop.hbase.ClusterStatus;
27  import org.apache.hadoop.hbase.ServerLoad;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.hbtop.Record;
31  import org.apache.hadoop.hbase.hbtop.RecordFilter;
32  import org.apache.hadoop.hbase.hbtop.field.Field;
33  import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
34  import org.apache.hadoop.hbase.hbtop.field.Size;
35  import org.apache.hadoop.hbase.hbtop.field.Size.Unit;
36  
37  /**
38   * Implementation for {@link ModeStrategy} for RegionServer Mode.
39   */
40  @InterfaceAudience.Private
41  public final class RegionServerModeStrategy implements ModeStrategy {
42  
43    private final List<FieldInfo> fieldInfos = Arrays.asList(
44      new FieldInfo(Field.REGION_SERVER, 0, true),
45      new FieldInfo(Field.LONG_REGION_SERVER, 0, false),
46      new FieldInfo(Field.REGION_COUNT, 7, true),
47      new FieldInfo(Field.REQUEST_COUNT_PER_SECOND, 10, true),
48      new FieldInfo(Field.READ_REQUEST_COUNT_PER_SECOND, 10, true),
49      new FieldInfo(Field.WRITE_REQUEST_COUNT_PER_SECOND, 10, true),
50      new FieldInfo(Field.STORE_FILE_SIZE, 13, true),
51      new FieldInfo(Field.UNCOMPRESSED_STORE_FILE_SIZE, 15, false),
52      new FieldInfo(Field.NUM_STORE_FILES, 7, true),
53      new FieldInfo(Field.MEM_STORE_SIZE, 11, true),
54      new FieldInfo(Field.USED_HEAP_SIZE, 11, true),
55      new FieldInfo(Field.MAX_HEAP_SIZE, 11, true)
56    );
57  
58    private final RegionModeStrategy regionModeStrategy = new RegionModeStrategy();
59  
60    RegionServerModeStrategy(){
61    }
62  
63    @Override
64    public List<FieldInfo> getFieldInfos() {
65      return fieldInfos;
66    }
67  
68    @Override
69    public Field getDefaultSortField() {
70      return Field.REQUEST_COUNT_PER_SECOND;
71    }
72  
73    @Override
74    public List<Record> getRecords(ClusterStatus clusterStatus) {
75      // Get records from RegionModeStrategy and add REGION_COUNT field
76      List<Record> records = new ArrayList<>();
77      for (Record record : regionModeStrategy.getRecords(clusterStatus)) {
78        List<Record.Entry> entries = new ArrayList<>();
79        for (FieldInfo fieldInfo : fieldInfos) {
80          if (record.containsKey(fieldInfo.getField())) {
81            entries.add(Record.entry(fieldInfo.getField(),
82              record.get(fieldInfo.getField())));
83          }
84        }
85  
86        // Add REGION_COUNT field
87        records.add(Record.builder().putAll(Record.ofEntries(entries))
88          .put(Field.REGION_COUNT, 1).build());
89      }
90  
91      // Aggregation by NAMESPACE field
92      Map<String, Record> retMap = new HashMap<>();
93      for (Record record : records) {
94        String regionServer = record.get(Field.LONG_REGION_SERVER).asString();
95        if (retMap.containsKey(regionServer)) {
96          retMap.put(regionServer, retMap.get(regionServer).combine(record));
97        } else {
98          retMap.put(regionServer, record);
99        }
100     }
101 
102     // Add USED_HEAP_SIZE field and MAX_HEAP_SIZE field
103     for (ServerName sn : clusterStatus.getServers()) {
104       Record record = retMap.get(sn.getServerName());
105       if (record == null) {
106         continue;
107       }
108       ServerLoad sl = clusterStatus.getLoad(sn);
109       Record newRecord = Record.builder().putAll(record)
110         .put(Field.USED_HEAP_SIZE, new Size(sl.getUsedHeapMB(), Unit.MEGABYTE))
111         .put(Field.MAX_HEAP_SIZE, new Size(sl.getMaxHeapMB(), Unit.MEGABYTE)).build();
112       retMap.put(sn.getServerName(), newRecord);
113     }
114     return new ArrayList<>(retMap.values());
115   }
116 
117   @Override
118   public DrillDownInfo drillDown(Record selectedRecord) {
119     List<RecordFilter> initialFilters = Collections.singletonList(RecordFilter
120       .newBuilder(Field.REGION_SERVER)
121       .doubleEquals(selectedRecord.get(Field.REGION_SERVER)));
122     return new DrillDownInfo(Mode.REGION, initialFilters);
123   }
124 }