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