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
044      .toServerMetrics(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, metrics.getRegionMetrics().values().stream()
060      .mapToLong(v -> v.getFilteredReadRequestCount()).sum());
061    assertEquals(2, metrics.getRegionMetrics().values().stream()
062      .mapToLong(v -> (long) v.getCurrentRegionCachedRatio()).count());
063    assertEquals(150, metrics.getRegionMetrics().values().stream()
064      .mapToDouble(v -> v.getRegionSizeMB().get(Size.Unit.MEGABYTE)).sum(), 0);
065  }
066
067  @Test
068  public void testToString() {
069    ServerMetrics metrics = ServerMetricsBuilder
070      .toServerMetrics(ServerName.valueOf("localhost,1,1"), createServerLoadProto());
071    String slToString = metrics.toString();
072    assertTrue(slToString.contains("numberOfStores=13"));
073    assertTrue(slToString.contains("numberOfStorefiles=114"));
074    assertTrue(slToString.contains("storefileUncompressedSizeMB=129"));
075    assertTrue(slToString.contains("storefileSizeMB=820"));
076    assertTrue(slToString.contains("rootIndexSizeKB=504"));
077    assertTrue(slToString.contains("coprocessors=[]"));
078    assertTrue(slToString.contains("filteredReadRequestsCount=300"));
079  }
080
081  @Test
082  public void testRegionLoadWrapAroundAggregation() {
083    ServerMetrics metrics = ServerMetricsBuilder
084      .toServerMetrics(ServerName.valueOf("localhost,1,1"), createServerLoadProto());
085    long totalCount = ((long) Integer.MAX_VALUE) * 2;
086    assertEquals(totalCount,
087      metrics.getRegionMetrics().values().stream().mapToLong(v -> v.getReadRequestCount()).sum());
088    assertEquals(totalCount,
089      metrics.getRegionMetrics().values().stream().mapToLong(v -> v.getWriteRequestCount()).sum());
090  }
091
092  private ClusterStatusProtos.ServerLoad createServerLoadProto() {
093    HBaseProtos.RegionSpecifier rSpecOne = HBaseProtos.RegionSpecifier.newBuilder()
094      .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME)
095      .setValue(ByteString.copyFromUtf8("ASDFGQWERT")).build();
096    HBaseProtos.RegionSpecifier rSpecTwo = HBaseProtos.RegionSpecifier.newBuilder()
097      .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME)
098      .setValue(ByteString.copyFromUtf8("QWERTYUIOP")).build();
099
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)
105        .setRegionSizeMB(100).setCurrentRegionCachedRatio(0.9f).build();
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        .setRegionSizeMB(50).setCurrentRegionCachedRatio(1.0f).build();
112
113    ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
114      .addRegionLoads(rlOne).addRegionLoads(rlTwo).build();
115    return sl;
116  }
117
118}