View Javadoc

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