1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.hbtop;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Maps;
25 import com.google.protobuf.ByteString;
26 import java.text.ParseException;
27 import java.util.List;
28 import java.util.Map;
29 import org.apache.commons.lang3.time.FastDateFormat;
30 import org.apache.hadoop.hbase.ClusterStatus;
31 import org.apache.hadoop.hbase.HRegionInfo;
32 import org.apache.hadoop.hbase.ServerLoad;
33 import org.apache.hadoop.hbase.ServerName;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.hbtop.field.Field;
36 import org.apache.hadoop.hbase.hbtop.field.Size;
37 import org.apache.hadoop.hbase.hbtop.field.Size.Unit;
38 import org.apache.hadoop.hbase.hbtop.screen.top.Summary;
39 import org.apache.hadoop.hbase.master.RegionState;
40 import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
41 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
42
43 public final class TestUtils {
44
45 static final String HBASE_VERSION = "1.5.0-SNAPSHOT";
46 static final String CLUSTER_UUID = "01234567-89ab-cdef-0123-456789abcdef";
47
48 private TestUtils() { }
49
50 public static ClusterStatus createDummyClusterStatus() {
51 Map<ServerName,ServerLoad> serverLoads = Maps.newHashMap();
52 List<ServerName> deadServers = Lists.newArrayList();
53 Map<String,RegionState> rit = Maps.newHashMap();
54
55 ServerName host1 = ServerName.valueOf("host1.apache.com", 1000, 1);
56
57 serverLoads.put(host1,
58 createServerLoad(100,
59 new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 100,
60 Lists.newArrayList(
61 createRegionLoad("table1,,1.00000000000000000000000000000000.", 100, 100,
62 new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1,
63 new Size(100, Size.Unit.MEGABYTE), 0.1f, 100, 100, "2019-07-22 00:00:00"),
64 createRegionLoad("table2,1,2.00000000000000000000000000000001.", 200, 200,
65 new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2,
66 new Size(200, Size.Unit.MEGABYTE), 0.2f, 50, 200, "2019-07-22 00:00:01"),
67 createRegionLoad(
68 "namespace:table3,,3_0001.00000000000000000000000000000002.", 300, 300,
69 new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3,
70 new Size(300, Size.Unit.MEGABYTE), 0.3f, 100, 300, "2019-07-22 00:00:02"))));
71
72 ServerName host2 = ServerName.valueOf("host2.apache.com", 1001, 2);
73
74 serverLoads.put(host2,
75 createServerLoad(200,
76 new Size(16, Size.Unit.GIGABYTE), new Size(32, Size.Unit.GIGABYTE), 200,
77 Lists.newArrayList(
78 createRegionLoad("table1,1,4.00000000000000000000000000000003.", 100, 100,
79 new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1,
80 new Size(100, Size.Unit.MEGABYTE), 0.4f, 50, 100, "2019-07-22 00:00:03"),
81 createRegionLoad("table2,,5.00000000000000000000000000000004.", 200, 200,
82 new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2,
83 new Size(200, Size.Unit.MEGABYTE), 0.5f, 150, 200, "2019-07-22 00:00:04"),
84 createRegionLoad("namespace:table3,,6.00000000000000000000000000000005.", 300, 300,
85 new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3,
86 new Size(300, Size.Unit.MEGABYTE), 0.6f, 200, 300, "2019-07-22 00:00:05"))));
87
88 ServerName host3 = ServerName.valueOf("host3.apache.com", 1002, 3);
89
90 deadServers.add(host3);
91
92 rit.put("table4", new RegionState(new HRegionInfo(0, TableName.valueOf("table4"), 0),
93 RegionState.State.OFFLINE, host3));
94
95 return new ClusterStatus(HBASE_VERSION, CLUSTER_UUID, serverLoads, deadServers, null, null,
96 rit, new String[0], true);
97 }
98
99 private static ClusterStatusProtos.RegionLoad createRegionLoad(String regionName,
100 long readRequestCount, long writeRequestCount, Size storeFileSize,
101 Size uncompressedStoreFileSize, int storeFileCount, Size memStoreSize, float locality,
102 long compactedCellCount, long compactingCellCount, String lastMajorCompactionTime) {
103 FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
104 try {
105 return ClusterStatusProtos.RegionLoad.newBuilder()
106 .setRegionSpecifier(HBaseProtos.RegionSpecifier.newBuilder()
107 .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME)
108 .setValue(ByteString.copyFromUtf8(regionName)).build())
109 .setReadRequestsCount(readRequestCount)
110 .setWriteRequestsCount(writeRequestCount)
111 .setStorefileSizeMB((int)storeFileSize.get(Unit.MEGABYTE))
112 .setStoreUncompressedSizeMB((int)uncompressedStoreFileSize.get(Unit.MEGABYTE))
113 .setStorefiles(storeFileCount)
114 .setMemstoreSizeMB((int)memStoreSize.get(Unit.MEGABYTE))
115 .setDataLocality(locality)
116 .setCurrentCompactedKVs(compactedCellCount)
117 .setTotalCompactingKVs(compactingCellCount)
118 .setLastMajorCompactionTs(df.parse(lastMajorCompactionTime).getTime())
119 .build();
120 } catch (ParseException e) {
121 throw new IllegalArgumentException(e);
122 }
123 }
124
125 private static ServerLoad createServerLoad(long reportTimestamp,
126 Size usedHeapSize, Size maxHeapSize, long requestCountPerSecond,
127 List<ClusterStatusProtos.RegionLoad> regionLoads) {
128 return new ServerLoad(ClusterStatusProtos.ServerLoad.newBuilder()
129 .setReportStartTime(reportTimestamp)
130 .setReportEndTime(reportTimestamp)
131 .setUsedHeapMB((int)usedHeapSize.get(Unit.MEGABYTE))
132 .setMaxHeapMB((int)maxHeapSize.get(Unit.MEGABYTE))
133 .setNumberOfRequests(requestCountPerSecond)
134 .addAllRegionLoads(regionLoads)
135 .build());
136 }
137
138 public static void assertRecordsInRegionMode(List<Record> records) {
139 assertEquals(6, records.size());
140
141 for (Record record : records) {
142 switch (record.get(Field.REGION_NAME).asString()) {
143 case "table1,,1.00000000000000000000000000000000.":
144 assertRecordInRegionMode(record, "default", "1", "", "table1",
145 "00000000000000000000000000000000", "host1:1000", "host1.apache.com,1000,1",
146 0L, 0L, 0L, new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1,
147 new Size(100, Size.Unit.MEGABYTE), 0.1f, "", 100L, 100L, 100f,
148 "2019-07-22 00:00:00");
149 break;
150
151 case "table1,1,4.00000000000000000000000000000003.":
152 assertRecordInRegionMode(record, "default", "4", "", "table1",
153 "00000000000000000000000000000003", "host2:1001", "host2.apache.com,1001,2",
154 0L, 0L, 0L, new Size(100, Size.Unit.MEGABYTE), new Size(200, Size.Unit.MEGABYTE), 1,
155 new Size(100, Size.Unit.MEGABYTE), 0.4f, "1", 100L, 50L, 50f,
156 "2019-07-22 00:00:03");
157 break;
158
159 case "table2,,5.00000000000000000000000000000004.":
160 assertRecordInRegionMode(record, "default", "5", "", "table2",
161 "00000000000000000000000000000004", "host2:1001", "host2.apache.com,1001,2",
162 0L, 0L, 0L, new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2,
163 new Size(200, Size.Unit.MEGABYTE), 0.5f, "", 200L, 150L, 75f,
164 "2019-07-22 00:00:04");
165 break;
166
167 case "table2,1,2.00000000000000000000000000000001.":
168 assertRecordInRegionMode(record, "default", "2", "", "table2",
169 "00000000000000000000000000000001", "host1:1000", "host1.apache.com,1000,1",
170 0L, 0L, 0L, new Size(200, Size.Unit.MEGABYTE), new Size(400, Size.Unit.MEGABYTE), 2,
171 new Size(200, Size.Unit.MEGABYTE), 0.2f, "1", 200L, 50L, 25f,
172 "2019-07-22 00:00:01");
173 break;
174
175 case "namespace:table3,,6.00000000000000000000000000000005.":
176 assertRecordInRegionMode(record, "namespace", "6", "", "table3",
177 "00000000000000000000000000000005", "host2:1001", "host2.apache.com,1001,2",
178 0L, 0L, 0L, new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3,
179 new Size(300, Size.Unit.MEGABYTE), 0.6f, "", 300L, 200L, 66.66667f,
180 "2019-07-22 00:00:05");
181 break;
182
183 case "namespace:table3,,3_0001.00000000000000000000000000000002.":
184 assertRecordInRegionMode(record, "namespace", "3", "1", "table3",
185 "00000000000000000000000000000002", "host1:1000", "host1.apache.com,1000,1",
186 0L, 0L, 0L, new Size(300, Size.Unit.MEGABYTE), new Size(600, Size.Unit.MEGABYTE), 3,
187 new Size(300, Size.Unit.MEGABYTE), 0.3f, "", 300L, 100L, 33.333336f,
188 "2019-07-22 00:00:02");
189 break;
190
191 default:
192 fail();
193 }
194 }
195 }
196
197 private static void assertRecordInRegionMode(Record record, String namespace, String startCode,
198 String replicaId, String table, String region, String regionServer, String longRegionServer,
199 long requestCountPerSecond, long readRequestCountPerSecond, long writeCountRequestPerSecond,
200 Size storeFileSize, Size uncompressedStoreFileSize, int numStoreFiles,
201 Size memStoreSize, float locality, String startKey, long compactingCellCount,
202 long compactedCellCount, float compactionProgress, String lastMajorCompactionTime) {
203 assertEquals(21, record.size());
204 assertEquals(namespace, record.get(Field.NAMESPACE).asString());
205 assertEquals(startCode, record.get(Field.START_CODE).asString());
206 assertEquals(replicaId, record.get(Field.REPLICA_ID).asString());
207 assertEquals(table, record.get(Field.TABLE).asString());
208 assertEquals(region, record.get(Field.REGION).asString());
209 assertEquals(regionServer, record.get(Field.REGION_SERVER).asString());
210 assertEquals(longRegionServer, record.get(Field.LONG_REGION_SERVER).asString());
211 assertEquals(requestCountPerSecond, record.get(Field.REQUEST_COUNT_PER_SECOND).asLong());
212 assertEquals(readRequestCountPerSecond,
213 record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong());
214 assertEquals(writeCountRequestPerSecond,
215 record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong());
216 assertEquals(storeFileSize, record.get(Field.STORE_FILE_SIZE).asSize());
217 assertEquals(uncompressedStoreFileSize,
218 record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize());
219 assertEquals(numStoreFiles, record.get(Field.NUM_STORE_FILES).asInt());
220 assertEquals(record.get(Field.MEM_STORE_SIZE).asSize(), memStoreSize);
221 assertEquals(locality, record.get(Field.LOCALITY).asFloat(), 0.001);
222 assertEquals(startKey, record.get(Field.START_KEY).asString());
223 assertEquals(compactingCellCount, record.get(Field.COMPACTING_CELL_COUNT).asLong());
224 assertEquals(compactedCellCount, record.get(Field.COMPACTED_CELL_COUNT).asLong());
225 assertEquals(compactionProgress, record.get(Field.COMPACTION_PROGRESS).asFloat(), 0.001);
226 assertEquals(lastMajorCompactionTime,
227 record.get(Field.LAST_MAJOR_COMPACTION_TIME).asString());
228 }
229
230 public static void assertRecordsInNamespaceMode(List<Record> records) {
231 assertEquals(2, records.size());
232
233 for (Record record : records) {
234 switch (record.get(Field.NAMESPACE).asString()) {
235 case "default":
236 assertRecordInNamespaceMode(record, 0L, 0L, 0L, new Size(600, Size.Unit.MEGABYTE),
237 new Size(1200, Size.Unit.MEGABYTE), 6, new Size(600, Size.Unit.MEGABYTE), 4);
238 break;
239
240 case "namespace":
241 assertRecordInNamespaceMode(record, 0L, 0L, 0L, new Size(600, Size.Unit.MEGABYTE),
242 new Size(1200, Size.Unit.MEGABYTE), 6, new Size(600, Size.Unit.MEGABYTE), 2);
243 break;
244
245 default:
246 fail();
247 }
248 }
249 }
250
251 private static void assertRecordInNamespaceMode(Record record, long requestCountPerSecond,
252 long readRequestCountPerSecond, long writeCountRequestPerSecond, Size storeFileSize,
253 Size uncompressedStoreFileSize, int numStoreFiles, Size memStoreSize, int regionCount) {
254 assertEquals(9, record.size());
255 assertEquals(requestCountPerSecond, record.get(Field.REQUEST_COUNT_PER_SECOND).asLong());
256 assertEquals(readRequestCountPerSecond,
257 record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong());
258 assertEquals(writeCountRequestPerSecond,
259 record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong());
260 assertEquals(storeFileSize, record.get(Field.STORE_FILE_SIZE).asSize());
261 assertEquals(uncompressedStoreFileSize,
262 record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize());
263 assertEquals(numStoreFiles, record.get(Field.NUM_STORE_FILES).asInt());
264 assertEquals(memStoreSize, record.get(Field.MEM_STORE_SIZE).asSize());
265 assertEquals(regionCount, record.get(Field.REGION_COUNT).asInt());
266 }
267
268 public static void assertRecordsInTableMode(List<Record> records) {
269 assertEquals(3, records.size());
270
271 for (Record record : records) {
272 String tableName = String.format("%s:%s", record.get(Field.NAMESPACE).asString(),
273 record.get(Field.TABLE).asString());
274
275 switch (tableName) {
276 case "default:table1":
277 assertRecordInTableMode(record, 0L, 0L, 0L, new Size(200, Size.Unit.MEGABYTE),
278 new Size(400, Size.Unit.MEGABYTE), 2, new Size(200, Size.Unit.MEGABYTE), 2);
279 break;
280
281 case "default:table2":
282 assertRecordInTableMode(record, 0L, 0L, 0L, new Size(400, Size.Unit.MEGABYTE),
283 new Size(800, Size.Unit.MEGABYTE), 4, new Size(400, Size.Unit.MEGABYTE), 2);
284 break;
285
286 case "namespace:table3":
287 assertRecordInTableMode(record, 0L, 0L, 0L, new Size(600, Size.Unit.MEGABYTE),
288 new Size(1200, Size.Unit.MEGABYTE), 6, new Size(600, Size.Unit.MEGABYTE), 2);
289 break;
290
291 default:
292 fail();
293 }
294 }
295 }
296
297 private static void assertRecordInTableMode(Record record, long requestCountPerSecond,
298 long readRequestCountPerSecond, long writeCountRequestPerSecond, Size storeFileSize,
299 Size uncompressedStoreFileSize, int numStoreFiles, Size memStoreSize, int regionCount) {
300 assertEquals(10, record.size());
301 assertEquals(requestCountPerSecond, record.get(Field.REQUEST_COUNT_PER_SECOND).asLong());
302 assertEquals(readRequestCountPerSecond,
303 record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong());
304 assertEquals(writeCountRequestPerSecond,
305 record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong());
306 assertEquals(storeFileSize, record.get(Field.STORE_FILE_SIZE).asSize());
307 assertEquals(uncompressedStoreFileSize,
308 record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize());
309 assertEquals(numStoreFiles, record.get(Field.NUM_STORE_FILES).asInt());
310 assertEquals(memStoreSize, record.get(Field.MEM_STORE_SIZE).asSize());
311 assertEquals(regionCount, record.get(Field.REGION_COUNT).asInt());
312 }
313
314 public static void assertRecordsInRegionServerMode(List<Record> records) {
315 assertEquals(2, records.size());
316
317 for (Record record : records) {
318 switch (record.get(Field.REGION_SERVER).asString()) {
319 case "host1:1000":
320 assertRecordInRegionServerMode(record, "host1.apache.com,1000,1", 0L, 0L, 0L,
321 new Size(600, Size.Unit.MEGABYTE), new Size(1200, Size.Unit.MEGABYTE), 6,
322 new Size(600, Size.Unit.MEGABYTE), 3, new Size(100, Size.Unit.MEGABYTE),
323 new Size(200, Size.Unit.MEGABYTE));
324 break;
325
326 case "host2:1001":
327 assertRecordInRegionServerMode(record, "host2.apache.com,1001,2", 0L, 0L, 0L,
328 new Size(600, Size.Unit.MEGABYTE), new Size(1200, Size.Unit.MEGABYTE), 6,
329 new Size(600, Size.Unit.MEGABYTE), 3, new Size(16, Size.Unit.GIGABYTE),
330 new Size(32, Size.Unit.GIGABYTE));
331 break;
332
333 default:
334 fail();
335 }
336 }
337 }
338
339 private static void assertRecordInRegionServerMode(Record record, String longRegionServer,
340 long requestCountPerSecond, long readRequestCountPerSecond, long writeCountRequestPerSecond,
341 Size storeFileSize, Size uncompressedStoreFileSize, int numStoreFiles,
342 Size memStoreSize, int regionCount, Size usedHeapSize, Size maxHeapSize) {
343 assertEquals(12, record.size());
344 assertEquals(longRegionServer, record.get(Field.LONG_REGION_SERVER).asString());
345 assertEquals(requestCountPerSecond, record.get(Field.REQUEST_COUNT_PER_SECOND).asLong());
346 assertEquals(readRequestCountPerSecond,
347 record.get(Field.READ_REQUEST_COUNT_PER_SECOND).asLong());
348 assertEquals(writeCountRequestPerSecond,
349 record.get(Field.WRITE_REQUEST_COUNT_PER_SECOND).asLong());
350 assertEquals(storeFileSize, record.get(Field.STORE_FILE_SIZE).asSize());
351 assertEquals(uncompressedStoreFileSize,
352 record.get(Field.UNCOMPRESSED_STORE_FILE_SIZE).asSize());
353 assertEquals(numStoreFiles, record.get(Field.NUM_STORE_FILES).asInt());
354 assertEquals(memStoreSize, record.get(Field.MEM_STORE_SIZE).asSize());
355 assertEquals(regionCount, record.get(Field.REGION_COUNT).asInt());
356 assertEquals(usedHeapSize, record.get(Field.USED_HEAP_SIZE).asSize());
357 assertEquals(maxHeapSize, record.get(Field.MAX_HEAP_SIZE).asSize());
358 }
359
360 public static void assertSummary(Summary summary) {
361 assertEquals(HBASE_VERSION, summary.getVersion());
362 assertEquals(CLUSTER_UUID, summary.getClusterId());
363 assertEquals(3, summary.getServers());
364 assertEquals(2, summary.getLiveServers());
365 assertEquals(1, summary.getDeadServers());
366 assertEquals(6, summary.getRegionCount());
367 assertEquals(1, summary.getRitCount());
368 assertEquals(3.0, summary.getAverageLoad(), 0.001);
369 assertEquals(300L, summary.getAggregateRequestPerSecond());
370 }
371 }