View Javadoc

1   /**
2   * Licensed to the Apache Software Foundation (ASF) under one
3   * or more contributor license agreements.  See the NOTICE file
4   * distributed with this work for additional information
5   * regarding copyright ownership.  The ASF licenses this file
6   * to you under the Apache License, Version 2.0 (the
7   * "License"); you may not use this file except in compliance
8   * with the License.  You may obtain a copy of the License at
9   *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.mockito.Mockito.when;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.security.Superusers;
28  import org.apache.hadoop.hbase.HRegionInfo;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
33  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.RequestHeader;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  import org.mockito.Mockito;
41  
42  import com.google.protobuf.Message;
43  
44  import java.io.IOException;
45  
46  /**
47   * Basic test that qos function is sort of working; i.e. a change in method naming style
48   * over in pb doesn't break it.
49   */
50  @Category(SmallTests.class)
51  public class TestQosFunction {
52    @Test
53    public void testPriority() {
54      Configuration conf = HBaseConfiguration.create();
55      RSRpcServices rpcServices = Mockito.mock(RSRpcServices.class);
56      when(rpcServices.getConfiguration()).thenReturn(conf);
57  
58      AnnotationReadingPriorityFunction qosFunction =
59        new AnnotationReadingPriorityFunction(rpcServices, RSRpcServices.class);
60  
61      // Set method name in pb style with the method name capitalized.
62      checkMethod("ReplicateWALEntry", HConstants.REPLICATION_QOS, qosFunction);
63      // Set method name in pb style with the method name capitalized.
64      checkMethod("OpenRegion", HConstants.ADMIN_QOS, qosFunction);
65      // Check multi works.
66      checkMethod("Multi", HConstants.NORMAL_QOS, qosFunction, MultiRequest.getDefaultInstance());
67  
68    }
69  
70    @Test
71    public void testRegionInTransition() throws IOException {
72      Configuration conf = HBaseConfiguration.create();
73      Superusers.initialize(conf);
74      RSRpcServices rpcServices = Mockito.mock(RSRpcServices.class);
75      when(rpcServices.getConfiguration()).thenReturn(conf);
76  
77      AnnotationReadingPriorityFunction qosFunction =
78          new AnnotationReadingPriorityFunction(rpcServices, RSRpcServices.class);
79  
80      // Check ReportRegionInTransition
81      HBaseProtos.RegionInfo meta_ri = HRegionInfo.convert(HRegionInfo.FIRST_META_REGIONINFO);
82      HBaseProtos.RegionInfo normal_ri = HRegionInfo.convert(
83          new HRegionInfo(TableName.valueOf("test:table"),
84              Bytes.toBytes("a"), Bytes.toBytes("b"), false));
85  
86  
87      RegionServerStatusProtos.RegionStateTransition metaTransition = RegionServerStatusProtos
88          .RegionStateTransition.newBuilder()
89          .addRegionInfo(meta_ri)
90          .setTransitionCode(RegionServerStatusProtos.RegionStateTransition.TransitionCode.CLOSED)
91          .build();
92  
93      RegionServerStatusProtos.RegionStateTransition normalTransition = RegionServerStatusProtos
94          .RegionStateTransition.newBuilder()
95          .addRegionInfo(normal_ri)
96          .setTransitionCode(RegionServerStatusProtos.RegionStateTransition.TransitionCode.CLOSED)
97          .build();
98  
99      RegionServerStatusProtos.ReportRegionStateTransitionRequest metaTransitionRequest =
100         RegionServerStatusProtos.ReportRegionStateTransitionRequest.newBuilder()
101             .setServer(ProtobufUtil.toServerName(ServerName.valueOf("locahost:60020", 100)))
102             .addTransition(normalTransition)
103             .addTransition(metaTransition).build();
104 
105     RegionServerStatusProtos.ReportRegionStateTransitionRequest normalTransitionRequest =
106         RegionServerStatusProtos.ReportRegionStateTransitionRequest.newBuilder()
107             .setServer(ProtobufUtil.toServerName(ServerName.valueOf("locahost:60020", 100)))
108             .addTransition(normalTransition).build();
109 
110     final String reportFuncName = "ReportRegionStateTransition";
111     checkMethod(reportFuncName, HConstants.SYSTEMTABLE_QOS, qosFunction, metaTransitionRequest);
112     checkMethod(reportFuncName, HConstants.NORMAL_QOS, qosFunction, normalTransitionRequest);
113   }
114 
115   private void checkMethod(final String methodName, final int expected,
116       final AnnotationReadingPriorityFunction qosf) {
117     checkMethod(methodName, expected, qosf, null);
118   }
119 
120   private void checkMethod(final String methodName, final int expected,
121       final AnnotationReadingPriorityFunction qosf, final Message param) {
122     RequestHeader.Builder builder = RequestHeader.newBuilder();
123     builder.setMethodName(methodName);
124     assertEquals(methodName, expected, qosf.getPriority(builder.build(), param));
125   }
126 }