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; 019 020import static org.hamcrest.CoreMatchers.is; 021import static org.junit.Assert.assertThat; 022import static org.junit.Assert.fail; 023 024import java.text.ParseException; 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030import org.apache.commons.lang3.time.FastDateFormat; 031import org.apache.hadoop.hbase.ClusterMetrics; 032import org.apache.hadoop.hbase.ClusterMetricsBuilder; 033import org.apache.hadoop.hbase.RegionMetrics; 034import org.apache.hadoop.hbase.RegionMetricsBuilder; 035import org.apache.hadoop.hbase.ServerMetrics; 036import org.apache.hadoop.hbase.ServerMetricsBuilder; 037import org.apache.hadoop.hbase.ServerName; 038import org.apache.hadoop.hbase.Size; 039import org.apache.hadoop.hbase.TableName; 040import org.apache.hadoop.hbase.client.RegionInfoBuilder; 041import org.apache.hadoop.hbase.hbtop.field.Field; 042import org.apache.hadoop.hbase.hbtop.screen.top.Summary; 043import org.apache.hadoop.hbase.master.RegionState; 044import org.apache.hadoop.hbase.util.Bytes; 045 046 047public final class TestUtils { 048 049 private TestUtils() { 050 } 051 052 public static ClusterMetrics createDummyClusterMetrics() { 053 Map<ServerName, ServerMetrics> serverMetricsMap = new HashMap<>(); 054 055 // host1 056 List<RegionMetrics> regionMetricsList = new ArrayList<>(); 057 regionMetricsList.add(createRegionMetrics( 058 "table1,,1.00000000000000000000000000000000.", 059 100, 50, 100, 060 new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1, 061 new Size(100, Size.Unit.MEGABYTE), 0.1f, 100, 100, "2019-07-22 00:00:00")); 062 regionMetricsList.add(createRegionMetrics( 063 "table2,1,2.00000000000000000000000000000001.", 064 200, 100, 200, 065 new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2, 066 new Size(200, Size.Unit.MEGABYTE), 0.2f, 50, 200, "2019-07-22 00:00:01")); 067 regionMetricsList.add(createRegionMetrics( 068 "namespace:table3,,3_0001.00000000000000000000000000000002.", 069 300, 150, 300, 070 new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3, 071 new Size(300, Size.Unit.MEGABYTE), 0.3f, 100, 300, "2019-07-22 00:00:02")); 072 073 ServerName host1 = ServerName.valueOf("host1.apache.com", 1000, 1); 074 serverMetricsMap.put(host1, createServerMetrics(host1, 100, 075 new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 100, 076 regionMetricsList)); 077 078 // host2 079 regionMetricsList.clear(); 080 regionMetricsList.add(createRegionMetrics( 081 "table1,1,4.00000000000000000000000000000003.", 082 100, 50, 100, 083 new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1, 084 new Size(100, Size.Unit.MEGABYTE), 0.4f, 50, 100, "2019-07-22 00:00:03")); 085 regionMetricsList.add(createRegionMetrics( 086 "table2,,5.00000000000000000000000000000004.", 087 200, 100, 200, 088 new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2, 089 new Size(200, Size.Unit.MEGABYTE), 0.5f, 150, 200, "2019-07-22 00:00:04")); 090 regionMetricsList.add(createRegionMetrics( 091 "namespace:table3,,6.00000000000000000000000000000005.", 092 300, 150, 300, 093 new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3, 094 new Size(300, Size.Unit.MEGABYTE), 0.6f, 200, 300, "2019-07-22 00:00:05")); 095 096 ServerName host2 = ServerName.valueOf("host2.apache.com", 1001, 2); 097 serverMetricsMap.put(host2, createServerMetrics(host2, 200, 098 new Size(16, Size.Unit.GIGABYTE), new Size(32, Size.Unit.GIGABYTE), 200, 099 regionMetricsList)); 100 101 ServerName host3 = ServerName.valueOf("host3.apache.com", 1002, 3); 102 return ClusterMetricsBuilder.newBuilder() 103 .setHBaseVersion("3.0.0-SNAPSHOT") 104 .setClusterId("01234567-89ab-cdef-0123-456789abcdef") 105 .setLiveServerMetrics(serverMetricsMap) 106 .setDeadServerNames(Collections.singletonList(host3)) 107 .setRegionsInTransition(Collections.singletonList( 108 new RegionState(RegionInfoBuilder.newBuilder(TableName.valueOf("table4")) 109 .setStartKey(new byte [0]) 110 .setEndKey(new byte [0]) 111 .setOffline(true) 112 .setReplicaId(0) 113 .setRegionId(0) 114 .setSplit(false) 115 .build(), 116 RegionState.State.OFFLINE, host3))) 117 .build(); 118 } 119 120 private static RegionMetrics createRegionMetrics(String regionName, long readRequestCount, 121 long filteredReadRequestCount, long writeRequestCount, Size storeFileSize, 122 Size uncompressedStoreFileSize, int storeFileCount, Size memStoreSize, float locality, 123 long compactedCellCount, long compactingCellCount, String lastMajorCompactionTime) { 124 125 FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); 126 try { 127 return RegionMetricsBuilder.newBuilder(Bytes.toBytes(regionName)) 128 .setReadRequestCount(readRequestCount) 129 .setFilteredReadRequestCount(filteredReadRequestCount) 130 .setWriteRequestCount(writeRequestCount).setStoreFileSize(storeFileSize) 131 .setUncompressedStoreFileSize(uncompressedStoreFileSize).setStoreFileCount(storeFileCount) 132 .setMemStoreSize(memStoreSize).setDataLocality(locality) 133 .setCompactedCellCount(compactedCellCount).setCompactingCellCount(compactingCellCount) 134 .setLastMajorCompactionTimestamp(df.parse(lastMajorCompactionTime).getTime()).build(); 135 } catch (ParseException e) { 136 throw new IllegalArgumentException(e); 137 } 138 } 139 140 private static ServerMetrics createServerMetrics(ServerName serverName, long reportTimestamp, 141 Size usedHeapSize, Size maxHeapSize, long requestCountPerSecond, 142 List<RegionMetrics> regionMetricsList) { 143 144 return ServerMetricsBuilder.newBuilder(serverName) 145 .setReportTimestamp(reportTimestamp) 146 .setUsedHeapSize(usedHeapSize) 147 .setMaxHeapSize(maxHeapSize) 148 .setRequestCountPerSecond(requestCountPerSecond) 149 .setRegionMetrics(regionMetricsList).build(); 150 } 151 152 public static void assertRecordsInRegionMode(List<Record> records) { 153 assertThat(records.size(), is(6)); 154 155 for (Record record : records) { 156 switch (record.get(Field.REGION_NAME).asString()) { 157 case "table1,,1.00000000000000000000000000000000.": 158 assertRecordInRegionMode(record, "default", "1", "", "table1", 159 "00000000000000000000000000000000", "host1:1000", "host1.apache.com,1000,1",0L, 160 0L, 0L, 0L, new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1, 161 new Size(100, Size.Unit.MEGABYTE), 0.1f, "", 100L, 100L, 100f, 162 "2019-07-22 00:00:00"); 163 break; 164 165 case "table1,1,4.00000000000000000000000000000003.": 166 assertRecordInRegionMode(record, "default", "4", "", "table1", 167 "00000000000000000000000000000003", "host2:1001", "host2.apache.com,1001,2",0L, 168 0L, 0L, 0L, new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1, 169 new Size(100, Size.Unit.MEGABYTE), 0.4f, "1", 100L, 50L, 50f, 170 "2019-07-22 00:00:03"); 171 break; 172 173 case "table2,,5.00000000000000000000000000000004.": 174 assertRecordInRegionMode(record, "default", "5", "", "table2", 175 "00000000000000000000000000000004", "host2:1001", "host2.apache.com,1001,2",0L, 176 0L, 0L, 0L, new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2, 177 new Size(200, Size.Unit.MEGABYTE), 0.5f, "", 200L, 150L, 75f, 178 "2019-07-22 00:00:04"); 179 break; 180 181 case "table2,1,2.00000000000000000000000000000001.": 182 assertRecordInRegionMode(record, "default", "2", "", "table2", 183 "00000000000000000000000000000001", "host1:1000", "host1.apache.com,1000,1",0L, 184 0L, 0L, 0L, new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2, 185 new Size(200, Size.Unit.MEGABYTE), 0.2f, "1", 200L, 50L, 25f, 186 "2019-07-22 00:00:01"); 187 break; 188 189 case "namespace:table3,,6.00000000000000000000000000000005.": 190 assertRecordInRegionMode(record, "namespace", "6", "", "table3", 191 "00000000000000000000000000000005", "host2:1001", "host2.apache.com,1001,2",0L, 192 0L, 0L, 0L, new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3, 193 new Size(300, Size.Unit.MEGABYTE), 0.6f, "", 300L, 200L, 66.66667f, 194 "2019-07-22 00:00:05"); 195 break; 196 197 case "namespace:table3,,3_0001.00000000000000000000000000000002.": 198 assertRecordInRegionMode(record, "namespace", "3", "1", "table3", 199 "00000000000000000000000000000002", "host1:1000", "host1.apache.com,1000,1",0L, 200 0L, 0L, 0L, new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3, 201 new Size(300, Size.Unit.MEGABYTE), 0.3f, "", 300L, 100L, 33.333336f, 202 "2019-07-22 00:00:02"); 203 break; 204 205 default: 206 fail(); 207 } 208 } 209 } 210 211 private static void assertRecordInRegionMode(Record record, String namespace, String startCode, 212 String replicaId, String table, String region, String regionServer, String longRegionServer, 213 long requestCountPerSecond, long readRequestCountPerSecond, 214 long filteredReadRequestCountPerSecond, long writeCountRequestPerSecond, 215 Size storeFileSize, Size uncompressedStoreFileSize, int numStoreFiles, 216 Size memStoreSize, float Locality, String startKey, long compactingCellCount, 217 long compactedCellCount, float compactionProgress, String lastMajorCompactionTime) { 218 assertThat(record.size(), is(22)); 219 assertThat(record.get(Field.NAMESPACE).asString(), is(namespace)); 220 assertThat(record.get(Field.START_CODE).asString(), is(startCode)); 221 assertThat(record.get(Field.REPLICA_ID).asString(), is(replicaId)); 222 assertThat(record.get(Field.TABLE).asString(), is(table)); 223 assertThat(record.get(Field.REGION).asString(), is(region)); 224 assertThat(record.get(Field.REGION_SERVER).asString(), is(regionServer)); 225 assertThat(record.get(Field.LONG_REGION_SERVER).asString(), is(longRegionServer)); 226 assertThat(record.get(Field.REQUEST_COUNT_PER_SECOND).asLong(), 227 is(requestCountPerSecond)); 228 assertThat(record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong(), 229 is(readRequestCountPerSecond)); 230 assertThat(record.get(Field.FILTERED_READ_REQUEST_COUNT_PER_SECOND).asLong(), 231 is(filteredReadRequestCountPerSecond)); 232 assertThat(record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong(), 233 is(writeCountRequestPerSecond)); 234 assertThat(record.get(Field.STORE_FILE_SIZE).asSize(), is(storeFileSize)); 235 assertThat(record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize(), 236 is(uncompressedStoreFileSize)); 237 assertThat(record.get(Field.NUM_STORE_FILES).asInt(), is(numStoreFiles)); 238 assertThat(record.get(Field.MEM_STORE_SIZE).asSize(), is(memStoreSize)); 239 assertThat(record.get(Field.LOCALITY).asFloat(), is(Locality)); 240 assertThat(record.get(Field.START_KEY).asString(), is(startKey)); 241 assertThat(record.get(Field.COMPACTING_CELL_COUNT).asLong(), is(compactingCellCount)); 242 assertThat(record.get(Field.COMPACTED_CELL_COUNT).asLong(), is(compactedCellCount)); 243 assertThat(record.get(Field.COMPACTION_PROGRESS).asFloat(), is(compactionProgress)); 244 assertThat(record.get(Field.LAST_MAJOR_COMPACTION_TIME).asString(), 245 is(lastMajorCompactionTime)); 246 } 247 248 public static void assertRecordsInNamespaceMode(List<Record> records) { 249 assertThat(records.size(), is(2)); 250 251 for (Record record : records) { 252 switch (record.get(Field.NAMESPACE).asString()) { 253 case "default": 254 assertRecordInNamespaceMode(record, 0L, 0L, 0L, 0L, new Size(600, Size.Unit.MEGABYTE), 255 new Size(1200, Size.Unit.MEGABYTE), 6, new Size(600, Size.Unit.MEGABYTE), 4); 256 break; 257 258 case "namespace": 259 assertRecordInNamespaceMode(record, 0L, 0L, 0L, 0L, new Size(600, Size.Unit.MEGABYTE), 260 new Size(1200, Size.Unit.MEGABYTE), 6, new Size(600, Size.Unit.MEGABYTE), 2); 261 break; 262 263 default: 264 fail(); 265 } 266 } 267 } 268 269 private static void assertRecordInNamespaceMode(Record record, long requestCountPerSecond, 270 long readRequestCountPerSecond, long filteredReadRequestCountPerSecond, 271 long writeCountRequestPerSecond, Size storeFileSize, Size uncompressedStoreFileSize, 272 int numStoreFiles, Size memStoreSize, int regionCount) { 273 assertThat(record.size(), is(10)); 274 assertThat(record.get(Field.REQUEST_COUNT_PER_SECOND).asLong(), 275 is(requestCountPerSecond)); 276 assertThat(record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong(), 277 is(readRequestCountPerSecond)); 278 assertThat(record.get(Field.FILTERED_READ_REQUEST_COUNT_PER_SECOND).asLong(), 279 is(filteredReadRequestCountPerSecond)); 280 assertThat(record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong(), 281 is(writeCountRequestPerSecond)); 282 assertThat(record.get(Field.STORE_FILE_SIZE).asSize(), is(storeFileSize)); 283 assertThat(record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize(), 284 is(uncompressedStoreFileSize)); 285 assertThat(record.get(Field.NUM_STORE_FILES).asInt(), is(numStoreFiles)); 286 assertThat(record.get(Field.MEM_STORE_SIZE).asSize(), is(memStoreSize)); 287 assertThat(record.get(Field.REGION_COUNT).asInt(), is(regionCount)); 288 } 289 290 public static void assertRecordsInTableMode(List<Record> records) { 291 assertThat(records.size(), is(3)); 292 293 for (Record record : records) { 294 String tableName = String.format("%s:%s", record.get(Field.NAMESPACE).asString(), 295 record.get(Field.TABLE).asString()); 296 297 switch (tableName) { 298 case "default:table1": 299 assertRecordInTableMode(record, 0L, 0L, 0L, 0L, new Size(200, Size.Unit.MEGABYTE), 300 new Size(400, Size.Unit.MEGABYTE), 2, new Size(200, Size.Unit.MEGABYTE), 2); 301 break; 302 303 case "default:table2": 304 assertRecordInTableMode(record, 0L, 0L, 0L, 0L, new Size(400, Size.Unit.MEGABYTE), 305 new Size(800, Size.Unit.MEGABYTE), 4, new Size(400, Size.Unit.MEGABYTE), 2); 306 break; 307 308 case "namespace:table3": 309 assertRecordInTableMode(record, 0L, 0L, 0L, 0L, new Size(600, Size.Unit.MEGABYTE), 310 new Size(1200, Size.Unit.MEGABYTE), 6, new Size(600, Size.Unit.MEGABYTE), 2); 311 break; 312 313 default: 314 fail(); 315 } 316 } 317 } 318 319 private static void assertRecordInTableMode(Record record, long requestCountPerSecond, 320 long readRequestCountPerSecond, long filteredReadRequestCountPerSecond, 321 long writeCountRequestPerSecond, Size storeFileSize, Size uncompressedStoreFileSize, 322 int numStoreFiles, Size memStoreSize, int regionCount) { 323 assertThat(record.size(), is(11)); 324 assertThat(record.get(Field.REQUEST_COUNT_PER_SECOND).asLong(), 325 is(requestCountPerSecond)); 326 assertThat(record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong(), 327 is(readRequestCountPerSecond)); 328 assertThat(record.get(Field.FILTERED_READ_REQUEST_COUNT_PER_SECOND).asLong(), 329 is(filteredReadRequestCountPerSecond)); 330 assertThat(record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong(), 331 is(writeCountRequestPerSecond)); 332 assertThat(record.get(Field.STORE_FILE_SIZE).asSize(), is(storeFileSize)); 333 assertThat(record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize(), 334 is(uncompressedStoreFileSize)); 335 assertThat(record.get(Field.NUM_STORE_FILES).asInt(), is(numStoreFiles)); 336 assertThat(record.get(Field.MEM_STORE_SIZE).asSize(), is(memStoreSize)); 337 assertThat(record.get(Field.REGION_COUNT).asInt(), is(regionCount)); 338 } 339 340 public static void assertRecordsInRegionServerMode(List<Record> records) { 341 assertThat(records.size(), is(2)); 342 343 for (Record record : records) { 344 switch (record.get(Field.REGION_SERVER).asString()) { 345 case "host1:1000": 346 assertRecordInRegionServerMode(record, "host1.apache.com,1000,1", 0L, 0L, 0L, 0L, 347 new Size(600, Size.Unit.MEGABYTE), new Size(1200, Size.Unit.MEGABYTE), 6, 348 new Size(600, Size.Unit.MEGABYTE), 3, new Size(100, Size.Unit.MEGABYTE), 349 new Size(200, Size.Unit.MEGABYTE)); 350 break; 351 352 case "host2:1001": 353 assertRecordInRegionServerMode(record, "host2.apache.com,1001,2", 0L, 0L, 0L, 0L, 354 new Size(600, Size.Unit.MEGABYTE), new Size(1200, Size.Unit.MEGABYTE), 6, 355 new Size(600, Size.Unit.MEGABYTE), 3, new Size(16, Size.Unit.GIGABYTE), 356 new Size(32, Size.Unit.GIGABYTE)); 357 break; 358 359 default: 360 fail(); 361 } 362 } 363 } 364 365 private static void assertRecordInRegionServerMode(Record record, String longRegionServer, 366 long requestCountPerSecond, long readRequestCountPerSecond, 367 long filteredReadRequestCountPerSecond, long writeCountRequestPerSecond, 368 Size storeFileSize, Size uncompressedStoreFileSize, int numStoreFiles, 369 Size memStoreSize, int regionCount, Size usedHeapSize, Size maxHeapSize) { 370 assertThat(record.size(), is(13)); 371 assertThat(record.get(Field.LONG_REGION_SERVER).asString(), 372 is(longRegionServer)); 373 assertThat(record.get(Field.REQUEST_COUNT_PER_SECOND).asLong(), 374 is(requestCountPerSecond)); 375 assertThat(record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong(), 376 is(readRequestCountPerSecond)); 377 assertThat(record.get(Field.FILTERED_READ_REQUEST_COUNT_PER_SECOND).asLong(), 378 is(filteredReadRequestCountPerSecond)); 379 assertThat(record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong(), 380 is(writeCountRequestPerSecond)); 381 assertThat(record.get(Field.STORE_FILE_SIZE).asSize(), is(storeFileSize)); 382 assertThat(record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize(), 383 is(uncompressedStoreFileSize)); 384 assertThat(record.get(Field.NUM_STORE_FILES).asInt(), is(numStoreFiles)); 385 assertThat(record.get(Field.MEM_STORE_SIZE).asSize(), is(memStoreSize)); 386 assertThat(record.get(Field.REGION_COUNT).asInt(), is(regionCount)); 387 assertThat(record.get(Field.USED_HEAP_SIZE).asSize(), is(usedHeapSize)); 388 assertThat(record.get(Field.MAX_HEAP_SIZE).asSize(), is(maxHeapSize)); 389 } 390 391 public static void assertSummary(Summary summary) { 392 assertThat(summary.getVersion(), is("3.0.0-SNAPSHOT")); 393 assertThat(summary.getClusterId(), is("01234567-89ab-cdef-0123-456789abcdef")); 394 assertThat(summary.getServers(), is(3)); 395 assertThat(summary.getLiveServers(), is(2)); 396 assertThat(summary.getDeadServers(), is(1)); 397 assertThat(summary.getRegionCount(), is(6)); 398 assertThat(summary.getRitCount(), is(1)); 399 assertThat(summary.getAverageLoad(), is(3.0)); 400 assertThat(summary.getAggregateRequestPerSecond(), is(300L)); 401 } 402}