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.master; 019 020import static org.mockito.Mockito.when; 021 022import java.io.IOException; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseConfiguration; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.HRegionInfo; 028import org.apache.hadoop.hbase.QosTestHelper; 029import org.apache.hadoop.hbase.ServerName; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.regionserver.AnnotationReadingPriorityFunction; 032import org.apache.hadoop.hbase.regionserver.RSRpcServices; 033import org.apache.hadoop.hbase.testclassification.MasterTests; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.junit.Before; 037import org.junit.ClassRule; 038import org.junit.Test; 039import org.junit.experimental.categories.Category; 040import org.mockito.Mockito; 041 042import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 043import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos; 044import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos; 045 046@Category({MasterTests.class, SmallTests.class}) 047public class TestMasterQosFunction extends QosTestHelper { 048 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestMasterQosFunction.class); 052 053 private Configuration conf; 054 private RSRpcServices rpcServices; 055 private AnnotationReadingPriorityFunction qosFunction; 056 057 058 @Before 059 public void setUp() { 060 conf = HBaseConfiguration.create(); 061 rpcServices = Mockito.mock(MasterRpcServices.class); 062 when(rpcServices.getConfiguration()).thenReturn(conf); 063 qosFunction = new MasterAnnotationReadingPriorityFunction(rpcServices, MasterRpcServices.class); 064 } 065 066 @Test 067 public void testRegionInTransition() throws IOException { 068 // Check ReportRegionInTransition 069 HBaseProtos.RegionInfo meta_ri = HRegionInfo.convert(HRegionInfo.FIRST_META_REGIONINFO); 070 HBaseProtos.RegionInfo normal_ri = HRegionInfo.convert( 071 new HRegionInfo(TableName.valueOf("test:table"), 072 Bytes.toBytes("a"), Bytes.toBytes("b"), false)); 073 074 075 RegionServerStatusProtos.RegionStateTransition metaTransition = RegionServerStatusProtos 076 .RegionStateTransition.newBuilder() 077 .addRegionInfo(meta_ri) 078 .setTransitionCode(RegionServerStatusProtos.RegionStateTransition.TransitionCode.CLOSED) 079 .build(); 080 081 RegionServerStatusProtos.RegionStateTransition normalTransition = RegionServerStatusProtos 082 .RegionStateTransition.newBuilder() 083 .addRegionInfo(normal_ri) 084 .setTransitionCode(RegionServerStatusProtos.RegionStateTransition.TransitionCode.CLOSED) 085 .build(); 086 087 RegionServerStatusProtos.ReportRegionStateTransitionRequest metaTransitionRequest = 088 RegionServerStatusProtos.ReportRegionStateTransitionRequest.newBuilder() 089 .setServer(ProtobufUtil.toServerName(ServerName.valueOf("locahost:60020", 100))) 090 .addTransition(normalTransition) 091 .addTransition(metaTransition).build(); 092 093 RegionServerStatusProtos.ReportRegionStateTransitionRequest normalTransitionRequest = 094 RegionServerStatusProtos.ReportRegionStateTransitionRequest.newBuilder() 095 .setServer(ProtobufUtil.toServerName(ServerName.valueOf("locahost:60020", 100))) 096 .addTransition(normalTransition).build(); 097 098 final String reportFuncName = "ReportRegionStateTransition"; 099 checkMethod(conf, reportFuncName, HConstants.META_QOS, qosFunction, 100 metaTransitionRequest); 101 checkMethod(conf, reportFuncName, HConstants.HIGH_QOS, qosFunction, normalTransitionRequest); 102 } 103 104 @Test 105 public void testAnnotations() { 106 checkMethod(conf, "GetLastFlushedSequenceId", HConstants.ADMIN_QOS, qosFunction); 107 checkMethod(conf, "CompactRegion", HConstants.ADMIN_QOS, qosFunction); 108 checkMethod(conf, "GetLastFlushedSequenceId", HConstants.ADMIN_QOS, qosFunction); 109 checkMethod(conf, "GetRegionInfo", HConstants.ADMIN_QOS, qosFunction); 110 } 111}