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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023import static org.mockito.ArgumentMatchers.any;
024import static org.mockito.ArgumentMatchers.anyLong;
025import static org.mockito.Mockito.doCallRealMethod;
026import static org.mockito.Mockito.mock;
027
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.client.RegionInfoBuilder;
032import org.apache.hadoop.hbase.quotas.RegionSize;
033import org.apache.hadoop.hbase.quotas.RegionSizeStore;
034import org.apache.hadoop.hbase.quotas.RegionSizeStoreFactory;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
042import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionSpaceUse;
043import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionSpaceUseReportRequest;
044
045/**
046 * Test class for isolated (non-cluster) tests surrounding the report of Region space use to the
047 * Master by RegionServers.
048 */
049@Category(SmallTests.class)
050public class TestRegionServerRegionSpaceUseReport {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestRegionServerRegionSpaceUseReport.class);
055
056  @Test
057  public void testConversion() {
058    TableName tn = TableName.valueOf("table1");
059
060    RegionInfo hri1 = RegionInfoBuilder.newBuilder(tn).setStartKey(Bytes.toBytes("a"))
061      .setEndKey(Bytes.toBytes("b")).build();
062    RegionInfo hri2 = RegionInfoBuilder.newBuilder(tn).setStartKey(Bytes.toBytes("b"))
063      .setEndKey(Bytes.toBytes("c")).build();
064    RegionInfo hri3 = RegionInfoBuilder.newBuilder(tn).setStartKey(Bytes.toBytes("c"))
065      .setEndKey(Bytes.toBytes("d")).build();
066    RegionSizeStore store = RegionSizeStoreFactory.getInstance().createStore();
067    store.put(hri1, 1024L * 1024L);
068    store.put(hri2, 1024L * 1024L * 8L);
069    store.put(hri3, 1024L * 1024L * 32L);
070
071    // Call the real method to convert the map into a protobuf
072    HRegionServer rs = mock(HRegionServer.class);
073    doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any(RegionSizeStore.class));
074    doCallRealMethod().when(rs).convertRegionSize(any(), anyLong());
075
076    RegionSpaceUseReportRequest requests = rs.buildRegionSpaceUseReportRequest(store);
077    assertEquals(store.size(), requests.getSpaceUseCount());
078    for (RegionSpaceUse spaceUse : requests.getSpaceUseList()) {
079      RegionInfo hri = ProtobufUtil.toRegionInfo(spaceUse.getRegionInfo());
080      RegionSize expectedSize = store.remove(hri);
081      assertNotNull("Could not find size for HRI: " + hri, expectedSize);
082      assertEquals(expectedSize.getSize(), spaceUse.getRegionSize());
083    }
084    assertTrue("Should not have any space use entries left: " + store, store.isEmpty());
085  }
086}