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.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.hadoop.hbase.testclassification.MiscTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
031
032import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
034
035@Category({ MiscTests.class, SmallTests.class })
036public class TestServerLoad {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestServerLoad.class);
041
042  @Test
043  public void testRegionLoadAggregation() {
044    ServerLoad sl = new ServerLoad(ServerName.valueOf("localhost,1,1"), createServerLoadProto());
045    assertEquals(13, sl.getStores());
046    assertEquals(114, sl.getStorefiles());
047    assertEquals(129, sl.getStoreUncompressedSizeMB());
048    assertEquals(504, sl.getRootIndexSizeKB());
049    assertEquals(820, sl.getStorefileSizeMB());
050    assertEquals(82, sl.getStorefileIndexSizeKB());
051    assertEquals(((long) Integer.MAX_VALUE) * 2, sl.getReadRequestsCount());
052    assertEquals(300, sl.getFilteredReadRequestsCount());
053  }
054
055  @Test
056  public void testToString() {
057    ServerLoad sl = new ServerLoad(ServerName.valueOf("localhost,1,1"), createServerLoadProto());
058    String slToString = sl.toString();
059    assertNotNull(sl.obtainServerLoadPB());
060    assertTrue(slToString.contains("numberOfStores=13"));
061    assertTrue(slToString.contains("numberOfStorefiles=114"));
062    assertTrue(slToString.contains("storefileUncompressedSizeMB=129"));
063    assertTrue(slToString.contains("storefileSizeMB=820"));
064    assertTrue(slToString.contains("rootIndexSizeKB=504"));
065    assertTrue(slToString.contains("coprocessors=[]"));
066    assertTrue(slToString.contains("filteredReadRequestsCount=300"));
067  }
068
069  @Test
070  public void testRegionLoadWrapAroundAggregation() {
071    ServerLoad sl = new ServerLoad(ServerName.valueOf("localhost,1,1"), createServerLoadProto());
072    assertNotNull(sl.obtainServerLoadPB());
073    long totalCount = ((long) Integer.MAX_VALUE) * 2;
074    assertEquals(totalCount, sl.getReadRequestsCount());
075    assertEquals(totalCount, sl.getWriteRequestsCount());
076  }
077
078  private ClusterStatusProtos.ServerLoad createServerLoadProto() {
079    HBaseProtos.RegionSpecifier rSpecOne = HBaseProtos.RegionSpecifier.newBuilder()
080      .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME)
081      .setValue(ByteString.copyFromUtf8("ASDFGQWERT")).build();
082    HBaseProtos.RegionSpecifier rSpecTwo = HBaseProtos.RegionSpecifier.newBuilder()
083      .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.ENCODED_REGION_NAME)
084      .setValue(ByteString.copyFromUtf8("QWERTYUIOP")).build();
085
086    ClusterStatusProtos.RegionLoad rlOne =
087      ClusterStatusProtos.RegionLoad.newBuilder().setRegionSpecifier(rSpecOne).setStores(10)
088        .setStorefiles(101).setStoreUncompressedSizeMB(106).setStorefileSizeMB(520)
089        .setFilteredReadRequestsCount(100).setStorefileIndexSizeKB(42).setRootIndexSizeKB(201)
090        .setReadRequestsCount(Integer.MAX_VALUE).setWriteRequestsCount(Integer.MAX_VALUE).build();
091    ClusterStatusProtos.RegionLoad rlTwo =
092      ClusterStatusProtos.RegionLoad.newBuilder().setRegionSpecifier(rSpecTwo).setStores(3)
093        .setStorefiles(13).setStoreUncompressedSizeMB(23).setStorefileSizeMB(300)
094        .setFilteredReadRequestsCount(200).setStorefileIndexSizeKB(40).setRootIndexSizeKB(303)
095        .setReadRequestsCount(Integer.MAX_VALUE).setWriteRequestsCount(Integer.MAX_VALUE).build();
096
097    ClusterStatusProtos.ServerLoad sl =
098      ClusterStatusProtos.ServerLoad.newBuilder().addRegionLoads(rlOne).
099        addRegionLoads(rlTwo).build();
100    return sl;
101  }
102
103}