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.Matchers.any;
024import static org.mockito.Matchers.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
047 * of Region space use to the 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)
061        .setStartKey(Bytes.toBytes("a"))
062        .setEndKey(Bytes.toBytes("b"))
063        .build();
064    RegionInfo hri2 = RegionInfoBuilder.newBuilder(tn)
065        .setStartKey(Bytes.toBytes("b"))
066        .setEndKey(Bytes.toBytes("c"))
067        .build();
068    RegionInfo hri3 = RegionInfoBuilder.newBuilder(tn)
069        .setStartKey(Bytes.toBytes("c"))
070        .setEndKey(Bytes.toBytes("d"))
071        .build();
072    RegionSizeStore store = RegionSizeStoreFactory.getInstance().createStore();
073    store.put(hri1, 1024L * 1024L);
074    store.put(hri2, 1024L * 1024L * 8L);
075    store.put(hri3, 1024L * 1024L * 32L);
076
077    // Call the real method to convert the map into a protobuf
078    HRegionServer rs = mock(HRegionServer.class);
079    doCallRealMethod().when(rs).buildRegionSpaceUseReportRequest(any(RegionSizeStore.class));
080    doCallRealMethod().when(rs).convertRegionSize(any(), anyLong());
081
082    RegionSpaceUseReportRequest requests = rs.buildRegionSpaceUseReportRequest(store);
083    assertEquals(store.size(), requests.getSpaceUseCount());
084    for (RegionSpaceUse spaceUse : requests.getSpaceUseList()) {
085      RegionInfo hri = ProtobufUtil.toRegionInfo(spaceUse.getRegionInfo());
086      RegionSize expectedSize = store.remove(hri);
087      assertNotNull("Could not find size for HRI: " + hri, expectedSize);
088      assertEquals(expectedSize.getSize(), spaceUse.getRegionSize());
089    }
090    assertTrue("Should not have any space use entries left: " + store, store.isEmpty());
091  }
092}