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.mockito.ArgumentMatchers.any; 022import static org.mockito.Mockito.mock; 023import static org.mockito.Mockito.when; 024 025import java.io.IOException; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseConfiguration; 028import org.apache.hadoop.hbase.HConstants; 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.security.User; 033import org.apache.hadoop.hbase.testclassification.RegionServerTests; 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.hbase.thirdparty.com.google.protobuf.ByteString; 040import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; 041 042import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Get; 043import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetRequest; 044import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 045import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType; 047import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader; 048 049/** 050 * Tests that verify certain RPCs get a higher QoS. 051 */ 052@Tag(RegionServerTests.TAG) 053@Tag(SmallTests.TAG) 054public class TestPriorityRpc { 055 056 private static Configuration CONF = HBaseConfiguration.create(); 057 058 @Test 059 public void testQosFunctionForMeta() throws IOException { 060 RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); 061 // create a rpc request that has references to hbase:meta region and also 062 // uses one of the known argument classes (known argument classes are 063 // listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) 064 headerBuilder.setMethodName("foo"); 065 066 GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); 067 RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); 068 regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); 069 ByteString name = 070 UnsafeByteOperations.unsafeWrap(RegionInfoBuilder.FIRST_META_REGIONINFO.getRegionName()); 071 regionSpecifierBuilder.setValue(name); 072 RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); 073 getRequestBuilder.setRegion(regionSpecifier); 074 Get.Builder getBuilder = Get.newBuilder(); 075 getBuilder.setRow(UnsafeByteOperations.unsafeWrap(Bytes.toBytes("somerow"))); 076 getRequestBuilder.setGet(getBuilder.build()); 077 GetRequest getRequest = getRequestBuilder.build(); 078 RequestHeader header = headerBuilder.build(); 079 HRegion mockRegion = mock(HRegion.class); 080 RSRpcServices mockRpc = mock(RSRpcServices.class); 081 when(mockRpc.getConfiguration()).thenReturn(CONF); 082 RegionInfo mockRegionInfo = mock(RegionInfo.class); 083 when(mockRpc.getRegion(any())).thenReturn(mockRegion); 084 when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); 085 when(mockRegionInfo.getTable()).thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable()); 086 087 RSAnnotationReadingPriorityFunction qosFunc = new RSAnnotationReadingPriorityFunction(mockRpc); 088 assertEquals(HConstants.SYSTEMTABLE_QOS, 089 qosFunc.getPriority(header, getRequest, createSomeUser())); 090 } 091 092 @Test 093 public void testQosFunctionWithoutKnownArgument() throws IOException { 094 // The request is not using any of the 095 // known argument classes (it uses one random request class) 096 // (known argument classes are listed in 097 // HRegionServer.QosFunctionImpl.knownArgumentClasses) 098 RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); 099 headerBuilder.setMethodName("foo"); 100 RequestHeader header = headerBuilder.build(); 101 RSRpcServices mockRpc = mock(RSRpcServices.class); 102 when(mockRpc.getConfiguration()).thenReturn(CONF); 103 104 RSAnnotationReadingPriorityFunction qosFunc = new RSAnnotationReadingPriorityFunction(mockRpc); 105 assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, null, createSomeUser())); 106 } 107 108 @Test 109 public void testQosFunctionForScanMethod() throws IOException { 110 RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); 111 headerBuilder.setMethodName("Scan"); 112 RequestHeader header = headerBuilder.build(); 113 114 // build an empty scan request 115 ScanRequest.Builder scanBuilder = ScanRequest.newBuilder(); 116 ScanRequest scanRequest = scanBuilder.build(); 117 HRegion mockRegion = mock(HRegion.class); 118 RSRpcServices mockRpc = mock(RSRpcServices.class); 119 when(mockRpc.getConfiguration()).thenReturn(CONF); 120 RegionInfo mockRegionInfo = mock(RegionInfo.class); 121 when(mockRpc.getRegion(any())).thenReturn(mockRegion); 122 when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); 123 // make isSystemTable return false 124 when(mockRegionInfo.getTable()).thenReturn(TableName.valueOf("testQosFunctionForScanMethod")); 125 126 RSAnnotationReadingPriorityFunction qosFunc = new RSAnnotationReadingPriorityFunction(mockRpc); 127 final int qos = qosFunc.getPriority(header, scanRequest, createSomeUser()); 128 assertEquals(HConstants.NORMAL_QOS, qos, Integer.toString(qos)); 129 130 // build a scan request with scannerID 131 scanBuilder = ScanRequest.newBuilder(); 132 scanBuilder.setScannerId(12345); 133 scanRequest = scanBuilder.build(); 134 // mock out a high priority type handling and see the QoS returned 135 RegionScanner mockRegionScanner = mock(RegionScanner.class); 136 when(mockRpc.getScanner(12345)).thenReturn(mockRegionScanner); 137 when(mockRegionScanner.getRegionInfo()).thenReturn(mockRegionInfo); 138 when(mockRpc.getRegion((RegionSpecifier) any())).thenReturn(mockRegion); 139 when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); 140 when(mockRegionInfo.getTable()).thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable()); 141 142 assertEquals(HConstants.SYSTEMTABLE_QOS, 143 qosFunc.getPriority(header, scanRequest, createSomeUser())); 144 145 // the same as above but with non-meta region 146 // make isSystemTable return false 147 when(mockRegionInfo.getTable()).thenReturn(TableName.valueOf("testQosFunctionForScanMethod")); 148 assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, scanRequest, createSomeUser())); 149 } 150 151 private static User createSomeUser() { 152 return User.createUserForTesting(CONF, "someuser", new String[] { "somegroup" }); 153 } 154}