001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.mode;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026import java.util.stream.Stream;
027import org.apache.hadoop.hbase.ClusterMetrics;
028import org.apache.hadoop.hbase.ServerMetrics;
029import org.apache.hadoop.hbase.hbtop.Record;
030import org.apache.hadoop.hbase.hbtop.RecordFilter;
031import org.apache.hadoop.hbase.hbtop.field.Field;
032import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
033import org.apache.yetus.audience.InterfaceAudience;
034
035
036/**
037 * Implementation for {@link ModeStrategy} for RegionServer Mode.
038 */
039@InterfaceAudience.Private
040public final class RegionServerModeStrategy implements ModeStrategy {
041
042  private final List<FieldInfo> fieldInfos = Arrays.asList(
043    new FieldInfo(Field.REGION_SERVER, 0, true),
044    new FieldInfo(Field.LONG_REGION_SERVER, 0, false),
045    new FieldInfo(Field.REGION_COUNT, 7, true),
046    new FieldInfo(Field.REQUEST_COUNT_PER_SECOND, 10, true),
047    new FieldInfo(Field.READ_REQUEST_COUNT_PER_SECOND, 10, true),
048    new FieldInfo(Field.FILTERED_READ_REQUEST_COUNT_PER_SECOND, 8, true),
049    new FieldInfo(Field.WRITE_REQUEST_COUNT_PER_SECOND, 10, true),
050    new FieldInfo(Field.STORE_FILE_SIZE, 13, true),
051    new FieldInfo(Field.UNCOMPRESSED_STORE_FILE_SIZE, 15, false),
052    new FieldInfo(Field.NUM_STORE_FILES, 7, true),
053    new FieldInfo(Field.MEM_STORE_SIZE, 11, true),
054    new FieldInfo(Field.USED_HEAP_SIZE, 11, true),
055    new FieldInfo(Field.MAX_HEAP_SIZE, 11, true)
056  );
057
058  private final RegionModeStrategy regionModeStrategy = new RegionModeStrategy();
059
060  RegionServerModeStrategy(){
061  }
062
063  @Override
064  public List<FieldInfo> getFieldInfos() {
065    return fieldInfos;
066  }
067
068  @Override
069  public Field getDefaultSortField() {
070    return Field.REQUEST_COUNT_PER_SECOND;
071  }
072
073  @Override
074  public List<Record> getRecords(ClusterMetrics clusterMetrics) {
075    // Get records from RegionModeStrategy and add REGION_COUNT field
076    List<Record> records = regionModeStrategy.getRecords(clusterMetrics).stream()
077      .map(record ->
078        Record.ofEntries(fieldInfos.stream()
079          .filter(fi -> record.containsKey(fi.getField()))
080          .map(fi -> Record.entry(fi.getField(), record.get(fi.getField())))))
081      .map(record -> Record.builder().putAll(record).put(Field.REGION_COUNT, 1).build())
082      .collect(Collectors.toList());
083
084    // Aggregation by LONG_REGION_SERVER field
085    Map<String, Record> retMap = records.stream()
086      .collect(Collectors.groupingBy(r -> r.get(Field.LONG_REGION_SERVER).asString()))
087      .entrySet().stream()
088      .flatMap(
089        e -> e.getValue().stream()
090          .reduce(Record::combine)
091          .map(Stream::of)
092          .orElse(Stream.empty()))
093      .collect(Collectors.toMap(r -> r.get(Field.LONG_REGION_SERVER).asString(), r -> r));
094
095    // Add USED_HEAP_SIZE field and MAX_HEAP_SIZE field
096    for (ServerMetrics sm : clusterMetrics.getLiveServerMetrics().values()) {
097      Record record = retMap.get(sm.getServerName().getServerName());
098      if (record == null) {
099        continue;
100      }
101
102      Record newRecord = Record.builder().putAll(record)
103        .put(Field.USED_HEAP_SIZE, sm.getUsedHeapSize())
104        .put(Field.MAX_HEAP_SIZE, sm.getMaxHeapSize()).build();
105
106      retMap.put(sm.getServerName().getServerName(), newRecord);
107    }
108
109    return new ArrayList<>(retMap.values());
110  }
111
112  @Override
113  public DrillDownInfo drillDown(Record selectedRecord) {
114    List<RecordFilter> initialFilters = Collections.singletonList(RecordFilter
115      .newBuilder(Field.REGION_SERVER)
116      .doubleEquals(selectedRecord.get(Field.REGION_SERVER)));
117    return new DrillDownInfo(Mode.REGION, initialFilters);
118  }
119}