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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.testclassification.MiscTests;
024import org.apache.hadoop.hbase.testclassification.SmallTests;
025import org.junit.ClassRule;
026import org.junit.Test;
027import org.junit.experimental.categories.Category;
028
029import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
030
031import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
033
034@Category({ MiscTests.class, SmallTests.class })
035public class TestServerMetrics {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestServerMetrics.class);
040
041  @Test
042  public void testRegionLoadAggregation() {
043    ServerMetrics metrics = ServerMetricsBuilder.toServerMetrics(
044        ServerName.valueOf("localhost,1,1"), createServerLoadProto());
045    assertEquals(13,
046        metrics.getRegionMetrics().values().stream().mapToInt(v -> v.getStoreCount()).sum());
047    assertEquals(114,
048        metrics.getRegionMetrics().values().stream().mapToInt(v -> v.getStoreFileCount()).sum());
049    assertEquals(129, metrics.getRegionMetrics().values().stream()
050        .mapToDouble(v -> v.getUncompressedStoreFileSize().get(Size.Unit.MEGABYTE)).sum(), 0);
051    assertEquals(504, metrics.getRegionMetrics().values().stream()
052        .mapToDouble(v -> v.getStoreFileRootLevelIndexSize().get(Size.Unit.KILOBYTE)).sum(), 0);
053    assertEquals(820, metrics.getRegionMetrics().values().stream()
054        .mapToDouble(v -> v.getStoreFileSize().get(Size.Unit.MEGABYTE)).sum(), 0);
055    assertEquals(82, metrics.getRegionMetrics().values().stream()
056        .mapToDouble(v -> v.getStoreFileIndexSize().get(Size.Unit.KILOBYTE)).sum(), 0);
057    assertEquals(((long) Integer.MAX_VALUE) * 2,
058        metrics.getRegionMetrics().values().stream().mapToLong(v -> v.getReadRequestCount()).sum());
059    assertEquals(300,
060        metrics.getRegionMetrics().values().stream().mapToLong(v -> v.getFilteredReadRequestCount())
061            .sum());
062  }
063
064  @Test
065  public void testToString() {
066    ServerMetrics metrics = ServerMetricsBuilder.toServerMetrics(
067        ServerName.valueOf("localhost,1,1"), createServerLoadProto());
068    String slToString = metrics.toString();
069    assertTrue(slToString.contains("numberOfStores=13"));
070    assertTrue(slToString.contains("numberOfStorefiles=114"));
071    assertTrue(slToString.contains("storefileUncompressedSizeMB=129"));
072    assertTrue(slToString.contains("storefileSizeMB=820"));
073    assertTrue(slToString.contains("rootIndexSizeKB=504"));
074    assertTrue(slToString.contains("coprocessors=[]"));
075    assertTrue(slToString.contains("filteredReadRequestsCount=300"));
076  }
077
078  @Test
079  public void testRegionLoadWrapAroundAggregation() {
080    ServerMetrics metrics = ServerMetricsBuilder.toServerMetrics(
081        ServerName.valueOf("localhost,1,1"), createServerLoadProto());
082    long totalCount = ((long) Integer.MAX_VALUE) * 2;
083    assertEquals(totalCount,
084        metrics.getRegionMetrics().values().stream().mapToLong(v -> v.getReadRequestCount()).sum());
085    assertEquals(totalCount,
086        metrics.getRegionMetrics().values().stream().mapToLong(v -> v.getWriteRequestCount())
087            .sum());
088  }
089
090  private ClusterStatusProtos.ServerLoad createServerLoadProto() {
091    HBaseProtos.RegionSpecifier rSpecOne = HBaseProtos.RegionSpecifier.newBuilder()
092        .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME)
093        .setValue(ByteString.copyFromUtf8("ASDFGQWERT")).build();
094    HBaseProtos.RegionSpecifier rSpecTwo = HBaseProtos.RegionSpecifier.newBuilder()
095        .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME)
096        .setValue(ByteString.copyFromUtf8("QWERTYUIOP")).build();
097
098    ClusterStatusProtos.RegionLoad rlOne =
099        ClusterStatusProtos.RegionLoad.newBuilder().setRegionSpecifier(rSpecOne).setStores(10)
100            .setStorefiles(101).setStoreUncompressedSizeMB(106).setStorefileSizeMB(520)
101            .setFilteredReadRequestsCount(100).setStorefileIndexSizeKB(42).setRootIndexSizeKB(201)
102            .setReadRequestsCount(Integer.MAX_VALUE).setWriteRequestsCount(Integer.MAX_VALUE)
103            .build();
104    ClusterStatusProtos.RegionLoad rlTwo =
105        ClusterStatusProtos.RegionLoad.newBuilder().setRegionSpecifier(rSpecTwo).setStores(3)
106            .setStorefiles(13).setStoreUncompressedSizeMB(23).setStorefileSizeMB(300)
107            .setFilteredReadRequestsCount(200).setStorefileIndexSizeKB(40).setRootIndexSizeKB(303)
108            .setReadRequestsCount(Integer.MAX_VALUE).setWriteRequestsCount(Integer.MAX_VALUE)
109            .build();
110
111    ClusterStatusProtos.ServerLoad sl =
112        ClusterStatusProtos.ServerLoad.newBuilder().addRegionLoads(rlOne).
113            addRegionLoads(rlTwo).build();
114    return sl;
115  }
116
117}