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 org.apache.hadoop.hbase.ClusterMetrics;
027import org.apache.hadoop.hbase.ServerMetrics;
028import org.apache.hadoop.hbase.hbtop.Record;
029import org.apache.hadoop.hbase.hbtop.RecordFilter;
030import org.apache.hadoop.hbase.hbtop.field.Field;
031import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
032import org.apache.yetus.audience.InterfaceAudience;
033
034/**
035 * Implementation for {@link ModeStrategy} for RegionServer Mode.
036 */
037@InterfaceAudience.Private
038public final class RegionServerModeStrategy implements ModeStrategy {
039
040  private final List<FieldInfo> fieldInfos =
041    Arrays.asList(new FieldInfo(Field.REGION_SERVER, 0, true),
042      new FieldInfo(Field.LONG_REGION_SERVER, 0, false), new FieldInfo(Field.REGION_COUNT, 7, true),
043      new FieldInfo(Field.REQUEST_COUNT_PER_SECOND, 10, true),
044      new FieldInfo(Field.READ_REQUEST_COUNT_PER_SECOND, 10, true),
045      new FieldInfo(Field.FILTERED_READ_REQUEST_COUNT_PER_SECOND, 8, true),
046      new FieldInfo(Field.WRITE_REQUEST_COUNT_PER_SECOND, 10, true),
047      new FieldInfo(Field.STORE_FILE_SIZE, 13, true),
048      new FieldInfo(Field.UNCOMPRESSED_STORE_FILE_SIZE, 15, false),
049      new FieldInfo(Field.NUM_STORE_FILES, 7, true), new FieldInfo(Field.MEM_STORE_SIZE, 11, true),
050      new FieldInfo(Field.USED_HEAP_SIZE, 11, true), new FieldInfo(Field.MAX_HEAP_SIZE, 11, true));
051
052  private final RegionModeStrategy regionModeStrategy = new RegionModeStrategy();
053
054  RegionServerModeStrategy() {
055  }
056
057  @Override
058  public List<FieldInfo> getFieldInfos() {
059    return fieldInfos;
060  }
061
062  @Override
063  public Field getDefaultSortField() {
064    return Field.REQUEST_COUNT_PER_SECOND;
065  }
066
067  @Override
068  public List<Record> getRecords(ClusterMetrics clusterMetrics,
069    List<RecordFilter> pushDownFilters) {
070    // Get records from RegionModeStrategy and add REGION_COUNT field
071    List<Record> records = regionModeStrategy.selectModeFieldsAndAddCountField(fieldInfos,
072      regionModeStrategy.getRecords(clusterMetrics, pushDownFilters), Field.REGION_COUNT);
073    // Aggregation by LONG_REGION_SERVER field
074    Map<String, Record> retMap =
075      ModeStrategyUtils.aggregateRecords(records, Field.LONG_REGION_SERVER).stream()
076        .collect(Collectors.toMap(r -> r.get(Field.LONG_REGION_SERVER).asString(), r -> r));
077
078    // Add USED_HEAP_SIZE field and MAX_HEAP_SIZE field
079    for (ServerMetrics sm : clusterMetrics.getLiveServerMetrics().values()) {
080      Record record = retMap.get(sm.getServerName().getServerName());
081      if (record == null) {
082        continue;
083      }
084
085      Record newRecord =
086        Record.builder().putAll(record).put(Field.USED_HEAP_SIZE, sm.getUsedHeapSize())
087          .put(Field.MAX_HEAP_SIZE, sm.getMaxHeapSize()).build();
088
089      retMap.put(sm.getServerName().getServerName(), newRecord);
090    }
091
092    return new ArrayList<>(retMap.values());
093  }
094
095  @Override
096  public DrillDownInfo drillDown(Record selectedRecord) {
097    List<RecordFilter> initialFilters = Collections.singletonList(RecordFilter
098      .newBuilder(Field.REGION_SERVER).doubleEquals(selectedRecord.get(Field.REGION_SERVER)));
099    return new DrillDownInfo(Mode.REGION, initialFilters);
100  }
101}