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