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