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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertTrue
;
23 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
24 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
25 import org
.junit
.ClassRule
;
26 import org
.junit
.Test
;
27 import org
.junit
.experimental
.categories
.Category
;
29 import org
.apache
.hbase
.thirdparty
.com
.google
.protobuf
.ByteString
;
31 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.ClusterStatusProtos
;
32 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.HBaseProtos
;
34 @Category({ MiscTests
.class, SmallTests
.class })
35 public class TestServerMetrics
{
38 public static final HBaseClassTestRule CLASS_RULE
=
39 HBaseClassTestRule
.forClass(TestServerMetrics
.class);
42 public void testRegionLoadAggregation() {
43 ServerMetrics metrics
= ServerMetricsBuilder
.toServerMetrics(
44 ServerName
.valueOf("localhost,1,1"), createServerLoadProto());
46 metrics
.getRegionMetrics().values().stream().mapToInt(v
-> v
.getStoreCount()).sum());
48 metrics
.getRegionMetrics().values().stream().mapToInt(v
-> v
.getStoreFileCount()).sum());
49 assertEquals(129, metrics
.getRegionMetrics().values().stream()
50 .mapToDouble(v
-> v
.getUncompressedStoreFileSize().get(Size
.Unit
.MEGABYTE
)).sum(), 0);
51 assertEquals(504, metrics
.getRegionMetrics().values().stream()
52 .mapToDouble(v
-> v
.getStoreFileRootLevelIndexSize().get(Size
.Unit
.KILOBYTE
)).sum(), 0);
53 assertEquals(820, metrics
.getRegionMetrics().values().stream()
54 .mapToDouble(v
-> v
.getStoreFileSize().get(Size
.Unit
.MEGABYTE
)).sum(), 0);
55 assertEquals(82, metrics
.getRegionMetrics().values().stream()
56 .mapToDouble(v
-> v
.getStoreFileIndexSize().get(Size
.Unit
.KILOBYTE
)).sum(), 0);
57 assertEquals(((long) Integer
.MAX_VALUE
) * 2,
58 metrics
.getRegionMetrics().values().stream().mapToLong(v
-> v
.getReadRequestCount()).sum());
60 metrics
.getRegionMetrics().values().stream().mapToLong(v
-> v
.getCpRequestCount()).sum());
62 metrics
.getRegionMetrics().values().stream().mapToLong(v
-> v
.getFilteredReadRequestCount())
67 public void testToString() {
68 ServerMetrics metrics
= ServerMetricsBuilder
.toServerMetrics(
69 ServerName
.valueOf("localhost,1,1"), createServerLoadProto());
70 String slToString
= metrics
.toString();
71 assertTrue(slToString
.contains("numberOfStores=13"));
72 assertTrue(slToString
.contains("numberOfStorefiles=114"));
73 assertTrue(slToString
.contains("storefileUncompressedSizeMB=129"));
74 assertTrue(slToString
.contains("storefileSizeMB=820"));
75 assertTrue(slToString
.contains("rootIndexSizeKB=504"));
76 assertTrue(slToString
.contains("coprocessors=[]"));
77 assertTrue(slToString
.contains("filteredReadRequestsCount=300"));
81 public void testRegionLoadWrapAroundAggregation() {
82 ServerMetrics metrics
= ServerMetricsBuilder
.toServerMetrics(
83 ServerName
.valueOf("localhost,1,1"), createServerLoadProto());
84 long totalCount
= ((long) Integer
.MAX_VALUE
) * 2;
85 assertEquals(totalCount
,
86 metrics
.getRegionMetrics().values().stream().mapToLong(v
-> v
.getReadRequestCount()).sum());
87 assertEquals(totalCount
,
88 metrics
.getRegionMetrics().values().stream().mapToLong(v
-> v
.getWriteRequestCount())
92 private ClusterStatusProtos
.ServerLoad
createServerLoadProto() {
93 HBaseProtos
.RegionSpecifier rSpecOne
= HBaseProtos
.RegionSpecifier
.newBuilder()
94 .setType(HBaseProtos
.RegionSpecifier
.RegionSpecifierType
.ENCODED_REGION_NAME
)
95 .setValue(ByteString
.copyFromUtf8("ASDFGQWERT")).build();
96 HBaseProtos
.RegionSpecifier rSpecTwo
= HBaseProtos
.RegionSpecifier
.newBuilder()
97 .setType(HBaseProtos
.RegionSpecifier
.RegionSpecifierType
.ENCODED_REGION_NAME
)
98 .setValue(ByteString
.copyFromUtf8("QWERTYUIOP")).build();
100 ClusterStatusProtos
.RegionLoad rlOne
=
101 ClusterStatusProtos
.RegionLoad
.newBuilder().setRegionSpecifier(rSpecOne
).setStores(10)
102 .setStorefiles(101).setStoreUncompressedSizeMB(106).setStorefileSizeMB(520)
103 .setFilteredReadRequestsCount(100).setStorefileIndexSizeKB(42).setRootIndexSizeKB(201)
104 .setReadRequestsCount(Integer
.MAX_VALUE
).setWriteRequestsCount(Integer
.MAX_VALUE
)
106 ClusterStatusProtos
.RegionLoad rlTwo
=
107 ClusterStatusProtos
.RegionLoad
.newBuilder().setRegionSpecifier(rSpecTwo
).setStores(3)
108 .setStorefiles(13).setStoreUncompressedSizeMB(23).setStorefileSizeMB(300)
109 .setFilteredReadRequestsCount(200).setStorefileIndexSizeKB(40).setRootIndexSizeKB(303)
110 .setReadRequestsCount(Integer
.MAX_VALUE
).setWriteRequestsCount(Integer
.MAX_VALUE
)
111 .setCpRequestsCount(100)
114 ClusterStatusProtos
.ServerLoad sl
=
115 ClusterStatusProtos
.ServerLoad
.newBuilder().addRegionLoads(rlOne
).
116 addRegionLoads(rlTwo
).build();