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  
23  import static org.mockito.Mockito.when;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HConstants;
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      HRegionServer hrs = Mockito.mock(HRegionServer.class);
55      AnnotationReadingPriorityFunction qosFunction = new AnnotationReadingPriorityFunction(hrs);
56  
57      // Set method name in pb style with the method name capitalized.
58      checkMethod("ReplicateWALEntry", HConstants.REPLICATION_QOS, qosFunction);
59      // Set method name in pb style with the method name capitalized.
60      checkMethod("OpenRegion", HConstants.HIGH_QOS, qosFunction);
61      // Check multi works.
62      checkMethod("Multi", HConstants.NORMAL_QOS, qosFunction, MultiRequest.getDefaultInstance());
63  
64    }
65  
66    @Test
67    public void testRegionInTransition() throws IOException {
68      Configuration conf = HBaseConfiguration.create();
69      HRegionServer rs = Mockito.mock(HRegionServer.class);
70      when(rs.getConfiguration()).thenReturn(conf);
71  
72      AnnotationReadingPriorityFunction qosFunction = new AnnotationReadingPriorityFunction(rs);
73  
74      // Check ReportRegionInTransition
75      HBaseProtos.RegionInfo meta_ri = HRegionInfo.convert(HRegionInfo.FIRST_META_REGIONINFO);
76      HBaseProtos.RegionInfo normal_ri = HRegionInfo.convert(
77          new HRegionInfo(TableName.valueOf("test:table"),
78              Bytes.toBytes("a"), Bytes.toBytes("b"), false));
79  
80  
81      RegionServerStatusProtos.RegionStateTransition metaTransition = RegionServerStatusProtos
82          .RegionStateTransition.newBuilder()
83          .addRegionInfo(meta_ri)
84          .setTransitionCode(RegionServerStatusProtos.RegionStateTransition.TransitionCode.CLOSED)
85          .build();
86  
87      RegionServerStatusProtos.RegionStateTransition normalTransition = RegionServerStatusProtos
88          .RegionStateTransition.newBuilder()
89          .addRegionInfo(normal_ri)
90          .setTransitionCode(RegionServerStatusProtos.RegionStateTransition.TransitionCode.CLOSED)
91          .build();
92  
93      RegionServerStatusProtos.ReportRegionStateTransitionRequest metaTransitionRequest =
94          RegionServerStatusProtos.ReportRegionStateTransitionRequest.newBuilder()
95              .setServer(ProtobufUtil.toServerName(ServerName.valueOf("locahost:60020", 100)))
96              .addTransition(normalTransition)
97              .addTransition(metaTransition).build();
98  
99      RegionServerStatusProtos.ReportRegionStateTransitionRequest normalTransitionRequest =
100         RegionServerStatusProtos.ReportRegionStateTransitionRequest.newBuilder()
101             .setServer(ProtobufUtil.toServerName(ServerName.valueOf("locahost:60020", 100)))
102             .addTransition(normalTransition).build();
103 
104     final String reportFuncName = "ReportRegionStateTransition";
105     checkMethod(reportFuncName, HConstants.HIGH_QOS, qosFunction, metaTransitionRequest);
106     checkMethod(reportFuncName, HConstants.NORMAL_QOS, qosFunction, normalTransitionRequest);
107   }
108 
109   private void checkMethod(final String methodName, final int expected,
110       final AnnotationReadingPriorityFunction qosf) {
111     checkMethod(methodName, expected, qosf, null);
112   }
113 
114   private void checkMethod(final String methodName, final int expected,
115       final AnnotationReadingPriorityFunction qosf, final Message param) {
116     RequestHeader.Builder builder = RequestHeader.newBuilder();
117     builder.setMethodName(methodName);
118     assertEquals(methodName, expected, qosf.getPriority(builder.build(), param));
119   }
120 }