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; 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.HBaseTestingUtility; 027import org.apache.hadoop.hbase.HConstants; 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.ipc.PriorityFunction; 032import org.apache.hadoop.hbase.security.User; 033import org.apache.hadoop.hbase.testclassification.MediumTests; 034import org.apache.hadoop.hbase.testclassification.RegionServerTests; 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.hbase.thirdparty.com.google.protobuf.ByteString; 043import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; 044 045import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Get; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetRequest; 047import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 048import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier; 049import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType; 050import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader; 051 052/** 053 * Tests that verify certain RPCs get a higher QoS. 054 */ 055@Category({RegionServerTests.class, MediumTests.class}) 056public class TestPriorityRpc { 057 058 @ClassRule 059 public static final HBaseClassTestRule CLASS_RULE = 060 HBaseClassTestRule.forClass(TestPriorityRpc.class); 061 062 private Configuration conf; 063 private HRegionServer regionServer = null; 064 private PriorityFunction priority = null; 065 066 @Before 067 public void setup() { 068 conf = HBaseConfiguration.create(); 069 conf.setBoolean("hbase.testing.nocluster", true); // No need to do ZK 070 final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf); 071 TEST_UTIL.getDataTestDir(this.getClass().getName()); 072 regionServer = HRegionServer.constructRegionServer(HRegionServer.class, conf); 073 priority = regionServer.rpcServices.getPriority(); 074 } 075 076 @Test 077 public void testQosFunctionForMeta() throws IOException { 078 priority = regionServer.rpcServices.getPriority(); 079 RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); 080 //create a rpc request that has references to hbase:meta region and also 081 //uses one of the known argument classes (known argument classes are 082 //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) 083 headerBuilder.setMethodName("foo"); 084 085 GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); 086 RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); 087 regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); 088 ByteString name = UnsafeByteOperations.unsafeWrap( 089 RegionInfoBuilder.FIRST_META_REGIONINFO.getRegionName()); 090 regionSpecifierBuilder.setValue(name); 091 RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); 092 getRequestBuilder.setRegion(regionSpecifier); 093 Get.Builder getBuilder = Get.newBuilder(); 094 getBuilder.setRow(UnsafeByteOperations.unsafeWrap(Bytes.toBytes("somerow"))); 095 getRequestBuilder.setGet(getBuilder.build()); 096 GetRequest getRequest = getRequestBuilder.build(); 097 RequestHeader header = headerBuilder.build(); 098 HRegion mockRegion = Mockito.mock(HRegion.class); 099 HRegionServer mockRS = Mockito.mock(HRegionServer.class); 100 RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); 101 Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); 102 RegionInfo mockRegionInfo = Mockito.mock(RegionInfo.class); 103 Mockito.when(mockRpc.getRegion(Mockito.any())).thenReturn(mockRegion); 104 Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); 105 Mockito.when(mockRegionInfo.getTable()) 106 .thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable()); 107 // Presume type. 108 ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); 109 assertEquals( 110 HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, getRequest, createSomeUser())); 111 } 112 113 @Test 114 public void testQosFunctionWithoutKnownArgument() throws IOException { 115 //The request is not using any of the 116 //known argument classes (it uses one random request class) 117 //(known argument classes are listed in 118 //HRegionServer.QosFunctionImpl.knownArgumentClasses) 119 RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); 120 headerBuilder.setMethodName("foo"); 121 RequestHeader header = headerBuilder.build(); 122 PriorityFunction qosFunc = regionServer.rpcServices.getPriority(); 123 assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, null, createSomeUser())); 124 } 125 126 @Test 127 public void testQosFunctionForScanMethod() throws IOException { 128 RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); 129 headerBuilder.setMethodName("Scan"); 130 RequestHeader header = headerBuilder.build(); 131 132 //build an empty scan request 133 ScanRequest.Builder scanBuilder = ScanRequest.newBuilder(); 134 ScanRequest scanRequest = scanBuilder.build(); 135 HRegion mockRegion = Mockito.mock(HRegion.class); 136 HRegionServer mockRS = Mockito.mock(HRegionServer.class); 137 RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); 138 Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); 139 RegionInfo mockRegionInfo = Mockito.mock(RegionInfo.class); 140 Mockito.when(mockRpc.getRegion(Mockito.any())).thenReturn(mockRegion); 141 Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); 142 // make isSystemTable return false 143 Mockito.when(mockRegionInfo.getTable()) 144 .thenReturn(TableName.valueOf("testQosFunctionForScanMethod")); 145 // Presume type. 146 ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); 147 final int qos = priority.getPriority(header, scanRequest, createSomeUser()); 148 assertEquals(Integer.toString(qos), qos, HConstants.NORMAL_QOS); 149 150 //build a scan request with scannerID 151 scanBuilder = ScanRequest.newBuilder(); 152 scanBuilder.setScannerId(12345); 153 scanRequest = scanBuilder.build(); 154 //mock out a high priority type handling and see the QoS returned 155 RegionScanner mockRegionScanner = Mockito.mock(RegionScanner.class); 156 Mockito.when(mockRpc.getScanner(12345)).thenReturn(mockRegionScanner); 157 Mockito.when(mockRegionScanner.getRegionInfo()).thenReturn(mockRegionInfo); 158 Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); 159 Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); 160 Mockito.when(mockRegionInfo.getTable()) 161 .thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable()); 162 163 // Presume type. 164 ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); 165 166 assertEquals( 167 HConstants.SYSTEMTABLE_QOS, 168 priority.getPriority(header, scanRequest, createSomeUser())); 169 170 //the same as above but with non-meta region 171 // make isSystemTable return false 172 Mockito.when(mockRegionInfo.getTable()) 173 .thenReturn(TableName.valueOf("testQosFunctionForScanMethod")); 174 assertEquals( 175 HConstants.NORMAL_QOS, 176 priority.getPriority(header, scanRequest, createSomeUser())); 177 } 178 179 private User createSomeUser() { 180 return User.createUserForTesting(conf, "someuser", new String[] { "somegroup" }); 181 } 182}