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