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.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
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.ipc.PriorityFunction;
033import org.apache.hadoop.hbase.security.User;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.testclassification.RegionServerTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.mockito.Mockito;
042
043import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;
044import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
045
046import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.Get;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetRequest;
048import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest;
049import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;
051import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader;
052
053/**
054 * Tests that verify certain RPCs get a higher QoS.
055 */
056@Category({RegionServerTests.class, MediumTests.class})
057public class TestPriorityRpc {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061      HBaseClassTestRule.forClass(TestPriorityRpc.class);
062
063  private HRegionServer regionServer = null;
064  private PriorityFunction priority = null;
065
066  @Before
067  public void setup() {
068    Configuration 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(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, getRequest,
110      User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"})));
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,
124      User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"})));
125  }
126
127  @Test
128  public void testQosFunctionForScanMethod() throws IOException {
129    RequestHeader.Builder headerBuilder = RequestHeader.newBuilder();
130    headerBuilder.setMethodName("Scan");
131    RequestHeader header = headerBuilder.build();
132
133    //build an empty scan request
134    ScanRequest.Builder scanBuilder = ScanRequest.newBuilder();
135    ScanRequest scanRequest = scanBuilder.build();
136    HRegion mockRegion = Mockito.mock(HRegion.class);
137    HRegionServer mockRS = Mockito.mock(HRegionServer.class);
138    RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class);
139    Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc);
140    RegionInfo mockRegionInfo = Mockito.mock(RegionInfo.class);
141    Mockito.when(mockRpc.getRegion(Mockito.any())).thenReturn(mockRegion);
142    Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo);
143    // make isSystemTable return false
144    Mockito.when(mockRegionInfo.getTable()).thenReturn(TableName.valueOf("testQosFunctionForScanMethod"));
145    // Presume type.
146    ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS);
147    int qos = priority.getPriority(header, scanRequest,
148      User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"}));
149    assertTrue ("" + qos, qos == HConstants.NORMAL_QOS);
150
151    //build a scan request with scannerID
152    scanBuilder = ScanRequest.newBuilder();
153    scanBuilder.setScannerId(12345);
154    scanRequest = scanBuilder.build();
155    //mock out a high priority type handling and see the QoS returned
156    RegionScanner mockRegionScanner = Mockito.mock(RegionScanner.class);
157    Mockito.when(mockRpc.getScanner(12345)).thenReturn(mockRegionScanner);
158    Mockito.when(mockRegionScanner.getRegionInfo()).thenReturn(mockRegionInfo);
159    Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion);
160    Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo);
161    Mockito.when(mockRegionInfo.getTable()).thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable());
162
163    // Presume type.
164    ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS);
165
166    assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, scanRequest,
167      User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"})));
168
169    //the same as above but with non-meta region
170    // make isSystemTable return false
171    Mockito.when(mockRegionInfo.getTable()).thenReturn(TableName.valueOf("testQosFunctionForScanMethod"));
172    assertEquals(HConstants.NORMAL_QOS, priority.getPriority(header, scanRequest,
173      User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"})));
174  }
175}