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.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.hadoop.hbase.client.RegionInfoBuilder;
029import org.apache.hadoop.hbase.ipc.PriorityFunction;
030import org.apache.hadoop.hbase.security.User;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.mockito.Mockito;
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, MediumTests.class })
055public class TestPriorityRpc {
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059    HBaseClassTestRule.forClass(TestPriorityRpc.class);
060
061  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
062
063  private static HRegionServer RS = null;
064  private static PriorityFunction PRIORITY = null;
065
066  @BeforeClass
067  public static void setUp() throws Exception {
068    UTIL.startMiniCluster(1);
069    RS = UTIL.getHBaseCluster().getRegionServer(0);
070    PRIORITY = RS.rpcServices.getPriority();
071  }
072
073  @AfterClass
074  public static void tearDown() throws IOException {
075    UTIL.shutdownMiniCluster();
076  }
077
078  @Test
079  public void testQosFunctionForMeta() throws IOException {
080    PRIORITY = RS.rpcServices.getPriority();
081    RequestHeader.Builder headerBuilder = RequestHeader.newBuilder();
082    // create a rpc request that has references to hbase:meta region and also
083    // uses one of the known argument classes (known argument classes are
084    // listed in HRegionServer.QosFunctionImpl.knownArgumentClasses)
085    headerBuilder.setMethodName("foo");
086
087    GetRequest.Builder getRequestBuilder = GetRequest.newBuilder();
088    RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder();
089    regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME);
090    ByteString name =
091      UnsafeByteOperations.unsafeWrap(RegionInfoBuilder.FIRST_META_REGIONINFO.getRegionName());
092    regionSpecifierBuilder.setValue(name);
093    RegionSpecifier regionSpecifier = regionSpecifierBuilder.build();
094    getRequestBuilder.setRegion(regionSpecifier);
095    Get.Builder getBuilder = Get.newBuilder();
096    getBuilder.setRow(UnsafeByteOperations.unsafeWrap(Bytes.toBytes("somerow")));
097    getRequestBuilder.setGet(getBuilder.build());
098    GetRequest getRequest = getRequestBuilder.build();
099    RequestHeader header = headerBuilder.build();
100    HRegion mockRegion = Mockito.mock(HRegion.class);
101    HRegionServer mockRS = Mockito.mock(HRegionServer.class);
102    RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class);
103    Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc);
104    RegionInfo mockRegionInfo = Mockito.mock(RegionInfo.class);
105    Mockito.when(mockRpc.getRegion(Mockito.any())).thenReturn(mockRegion);
106    Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo);
107    Mockito.when(mockRegionInfo.getTable())
108      .thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable());
109    // Presume type.
110    ((AnnotationReadingPriorityFunction) PRIORITY).setRegionServer(mockRS);
111    assertEquals(HConstants.SYSTEMTABLE_QOS,
112      PRIORITY.getPriority(header, getRequest, createSomeUser()));
113  }
114
115  @Test
116  public void testQosFunctionWithoutKnownArgument() throws IOException {
117    // The request is not using any of the
118    // known argument classes (it uses one random request class)
119    // (known argument classes are listed in
120    // HRegionServer.QosFunctionImpl.knownArgumentClasses)
121    RequestHeader.Builder headerBuilder = RequestHeader.newBuilder();
122    headerBuilder.setMethodName("foo");
123    RequestHeader header = headerBuilder.build();
124    PriorityFunction qosFunc = RS.rpcServices.getPriority();
125    assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, null, createSomeUser()));
126  }
127
128  @Test
129  public void testQosFunctionForScanMethod() throws IOException {
130    RequestHeader.Builder headerBuilder = RequestHeader.newBuilder();
131    headerBuilder.setMethodName("Scan");
132    RequestHeader header = headerBuilder.build();
133
134    // build an empty scan request
135    ScanRequest.Builder scanBuilder = ScanRequest.newBuilder();
136    ScanRequest scanRequest = scanBuilder.build();
137    HRegion mockRegion = Mockito.mock(HRegion.class);
138    HRegionServer mockRS = Mockito.mock(HRegionServer.class);
139    RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class);
140    Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc);
141    RegionInfo mockRegionInfo = Mockito.mock(RegionInfo.class);
142    Mockito.when(mockRpc.getRegion(Mockito.any())).thenReturn(mockRegion);
143    Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo);
144    // make isSystemTable return false
145    Mockito.when(mockRegionInfo.getTable())
146      .thenReturn(TableName.valueOf("testQosFunctionForScanMethod"));
147    // Presume type.
148    ((AnnotationReadingPriorityFunction) PRIORITY).setRegionServer(mockRS);
149    final int qos = PRIORITY.getPriority(header, scanRequest, createSomeUser());
150    assertEquals(Integer.toString(qos), qos, HConstants.NORMAL_QOS);
151
152    // build a scan request with scannerID
153    scanBuilder = ScanRequest.newBuilder();
154    scanBuilder.setScannerId(12345);
155    scanRequest = scanBuilder.build();
156    // mock out a high priority type handling and see the QoS returned
157    RegionScanner mockRegionScanner = Mockito.mock(RegionScanner.class);
158    Mockito.when(mockRpc.getScanner(12345)).thenReturn(mockRegionScanner);
159    Mockito.when(mockRegionScanner.getRegionInfo()).thenReturn(mockRegionInfo);
160    Mockito.when(mockRpc.getRegion((RegionSpecifier) Mockito.any())).thenReturn(mockRegion);
161    Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo);
162    Mockito.when(mockRegionInfo.getTable())
163      .thenReturn(RegionInfoBuilder.FIRST_META_REGIONINFO.getTable());
164
165    // Presume type.
166    ((AnnotationReadingPriorityFunction) PRIORITY).setRegionServer(mockRS);
167
168    assertEquals(HConstants.SYSTEMTABLE_QOS,
169      PRIORITY.getPriority(header, scanRequest, createSomeUser()));
170
171    // the same as above but with non-meta region
172    // make isSystemTable return false
173    Mockito.when(mockRegionInfo.getTable())
174      .thenReturn(TableName.valueOf("testQosFunctionForScanMethod"));
175    assertEquals(HConstants.NORMAL_QOS,
176      PRIORITY.getPriority(header, scanRequest, createSomeUser()));
177  }
178
179  private static User createSomeUser() {
180    return User.createUserForTesting(UTIL.getConfiguration(), "someuser",
181      new String[] { "somegroup" });
182  }
183}