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 edu.umd.cs.findbugs.annotations.Nullable;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import org.apache.commons.lang3.time.FastDateFormat;
28  import org.apache.hadoop.hbase.ClusterStatus;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.RegionLoad;
31  import org.apache.hadoop.hbase.ServerLoad;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.hbtop.Record;
36  import org.apache.hadoop.hbase.hbtop.field.Field;
37  import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
38  import org.apache.hadoop.hbase.hbtop.field.Size;
39  import org.apache.hadoop.hbase.hbtop.field.Size.Unit;
40  import org.apache.hadoop.hbase.util.Bytes;
41  
42  /**
43   * Implementation for {@link ModeStrategy} for Region Mode.
44   */
45  @InterfaceAudience.Private
46  public final class RegionModeStrategy implements ModeStrategy {
47  
48    private final List<FieldInfo> fieldInfos = Arrays.asList(
49      new FieldInfo(Field.REGION_NAME, 0, false),
50      new FieldInfo(Field.NAMESPACE, 0, true),
51      new FieldInfo(Field.TABLE, 0,  true),
52      new FieldInfo(Field.START_CODE, 13, false),
53      new FieldInfo(Field.REPLICA_ID, 5, false),
54      new FieldInfo(Field.REGION, 32, true),
55      new FieldInfo(Field.REGION_SERVER, 0, true),
56      new FieldInfo(Field.LONG_REGION_SERVER, 0, false),
57      new FieldInfo(Field.REQUEST_COUNT_PER_SECOND, 8, true),
58      new FieldInfo(Field.READ_REQUEST_COUNT_PER_SECOND, 8, true),
59      new FieldInfo(Field.WRITE_REQUEST_COUNT_PER_SECOND, 8, true),
60      new FieldInfo(Field.STORE_FILE_SIZE, 10, true),
61      new FieldInfo(Field.UNCOMPRESSED_STORE_FILE_SIZE, 12, false),
62      new FieldInfo(Field.NUM_STORE_FILES,4, true),
63      new FieldInfo(Field.MEM_STORE_SIZE, 8, true),
64      new FieldInfo(Field.LOCALITY, 8, true),
65      new FieldInfo(Field.START_KEY, 0, false),
66      new FieldInfo(Field.COMPACTING_CELL_COUNT, 12, false),
67      new FieldInfo(Field.COMPACTED_CELL_COUNT, 12, false),
68      new FieldInfo(Field.COMPACTION_PROGRESS, 7, false),
69      new FieldInfo(Field.LAST_MAJOR_COMPACTION_TIME, 19, false)
70    );
71  
72    private final Map<String, RequestCountPerSecond> requestCountPerSecondMap = new HashMap<>();
73  
74    RegionModeStrategy() {
75    }
76  
77    @Override
78    public List<FieldInfo> getFieldInfos() {
79      return fieldInfos;
80    }
81  
82    @Override
83    public Field getDefaultSortField() {
84      return Field.REQUEST_COUNT_PER_SECOND;
85    }
86  
87    @Override
88    public List<Record> getRecords(ClusterStatus clusterStatus) {
89      List<Record> ret = new ArrayList<>();
90      for (ServerName sn: clusterStatus.getServers()) {
91        ServerLoad sl = clusterStatus.getLoad(sn);
92        long lastReportTimestamp = sl.obtainServerLoadPB().getReportEndTime();
93        for (RegionLoad rl: sl.getRegionsLoad().values()) {
94          ret.add(createRecord(sn, rl, lastReportTimestamp));
95        }
96      }
97      return ret;
98    }
99  
100   private Record createRecord(ServerName sn, RegionLoad regionLoad, long lastReportTimestamp) {
101     Record.Builder builder = Record.builder();
102 
103     String regionName = regionLoad.getNameAsString();
104     builder.put(Field.REGION_NAME, regionName);
105 
106     String namespaceName = "";
107     String tableName = "";
108     String region = "";
109     String startKey = "";
110     String startCode = "";
111     String replicaId = "";
112     try {
113       byte[][] elements = HRegionInfo.parseRegionName(regionLoad.getName());
114       TableName tn = TableName.valueOf(elements[0]);
115       namespaceName = tn.getNamespaceAsString();
116       tableName = tn.getQualifierAsString();
117       startKey = Bytes.toStringBinary(elements[1]);
118       startCode = Bytes.toString(elements[2]);
119       replicaId = elements.length == 4 ?
120         Integer.valueOf(Bytes.toString(elements[3])).toString() : "";
121       region = HRegionInfo.encodeRegionName(regionLoad.getName());
122     } catch (IOException ignored) {
123     }
124 
125     builder.put(Field.NAMESPACE, namespaceName);
126     builder.put(Field.TABLE, tableName);
127     builder.put(Field.START_CODE, startCode);
128     builder.put(Field.REPLICA_ID, replicaId);
129     builder.put(Field.REGION, region);
130     builder.put(Field.START_KEY, startKey);
131     builder.put(Field.REGION_SERVER, sn.toShortString());
132     builder.put(Field.LONG_REGION_SERVER, sn.getServerName());
133 
134     RequestCountPerSecond requestCountPerSecond = requestCountPerSecondMap.get(regionName);
135     if (requestCountPerSecond == null) {
136       requestCountPerSecond = new RequestCountPerSecond();
137       requestCountPerSecondMap.put(regionName, requestCountPerSecond);
138     }
139     requestCountPerSecond.refresh(lastReportTimestamp, regionLoad.getReadRequestsCount(),
140       regionLoad.getWriteRequestsCount());
141 
142     builder.put(Field.READ_REQUEST_COUNT_PER_SECOND,
143       requestCountPerSecond.getReadRequestCountPerSecond());
144     builder.put(Field.WRITE_REQUEST_COUNT_PER_SECOND,
145       requestCountPerSecond.getWriteRequestCountPerSecond());
146     builder.put(Field.REQUEST_COUNT_PER_SECOND,
147       requestCountPerSecond.getRequestCountPerSecond());
148 
149     builder.put(Field.STORE_FILE_SIZE, new Size(regionLoad.getStorefileSizeMB(), Unit.MEGABYTE));
150     builder.put(Field.UNCOMPRESSED_STORE_FILE_SIZE,
151       new Size(regionLoad.getStoreUncompressedSizeMB(), Unit.MEGABYTE));
152     builder.put(Field.NUM_STORE_FILES, regionLoad.getStorefiles());
153     builder.put(Field.MEM_STORE_SIZE, new Size(regionLoad.getMemStoreSizeMB(), Unit.MEGABYTE));
154     builder.put(Field.LOCALITY, regionLoad.getDataLocality());
155 
156     long compactingCellCount = regionLoad.getTotalCompactingKVs();
157     long compactedCellCount = regionLoad.getCurrentCompactedKVs();
158     float compactionProgress = 0;
159     if  (compactedCellCount > 0) {
160       compactionProgress = 100 * ((float) compactedCellCount / compactingCellCount);
161     }
162 
163     builder.put(Field.COMPACTING_CELL_COUNT, compactingCellCount);
164     builder.put(Field.COMPACTED_CELL_COUNT, compactedCellCount);
165     builder.put(Field.COMPACTION_PROGRESS, compactionProgress);
166 
167     FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
168     long lastMajorCompactionTimestamp = regionLoad.getLastMajorCompactionTs();
169 
170     builder.put(Field.LAST_MAJOR_COMPACTION_TIME,
171       lastMajorCompactionTimestamp == 0 ? "" : df.format(lastMajorCompactionTimestamp));
172 
173     return builder.build();
174   }
175 
176   @Nullable
177   @Override
178   public DrillDownInfo drillDown(Record selectedRecord) {
179     // do nothing
180     return null;
181   }
182 }