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  package org.apache.hadoop.hbase.regionserver;
19  
20  import java.lang.reflect.Method;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.ipc.PriorityFunction;
30  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionRequest;
31  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CompactRegionRequest;
32  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionRequest;
33  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetRegionInfoRequest;
34  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetStoreFileRequest;
35  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.SplitRegionRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetRequest;
37  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateRequest;
39  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
40  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
41  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.RequestHeader;
42  import org.apache.hadoop.hbase.regionserver.RSRpcServices.QosPriority;
43  
44  import com.google.common.annotations.VisibleForTesting;
45  import com.google.protobuf.Message;
46  import com.google.protobuf.TextFormat;
47  
48  
49  /**
50   * Reads special method annotations and table names to figure a priority for use by QoS facility in
51   * ipc; e.g: rpcs to hbase:meta get priority.
52   */
53  // TODO: Remove.  This is doing way too much work just to figure a priority.  Do as Elliott
54  // suggests and just have the client specify a priority.
55  
56  //The logic for figuring out high priority RPCs is as follows:
57  //1. if the method is annotated with a QosPriority of QOS_HIGH,
58  //   that is honored
59  //2. parse out the protobuf message and see if the request is for meta
60  //   region, and if so, treat it as a high priority RPC
61  //Some optimizations for (2) are done here -
62  //Clients send the argument classname as part of making the RPC. The server
63  //decides whether to deserialize the proto argument message based on the
64  //pre-established set of argument classes (knownArgumentClasses below).
65  //This prevents the server from having to deserialize all proto argument
66  //messages prematurely.
67  //All the argument classes declare a 'getRegion' method that returns a
68  //RegionSpecifier object. Methods can be invoked on the returned object
69  //to figure out whether it is a meta region or not.
70  @InterfaceAudience.Private
71  class AnnotationReadingPriorityFunction implements PriorityFunction {
72    public static final Log LOG =
73      LogFactory.getLog(AnnotationReadingPriorityFunction.class.getName());
74  
75    /** Used to control the scan delay, currently sqrt(numNextCall * weight) */
76    public static final String SCAN_VTIME_WEIGHT_CONF_KEY = "hbase.ipc.server.scan.vtime.weight";
77  
78    private final Map<String, Integer> annotatedQos;
79    //We need to mock the regionserver instance for some unit tests (set via
80    //setRegionServer method.
81    private RSRpcServices rpcServices;
82    @SuppressWarnings("unchecked")
83    private final Class<? extends Message>[] knownArgumentClasses = new Class[]{
84        GetRegionInfoRequest.class,
85        GetStoreFileRequest.class,
86        CloseRegionRequest.class,
87        FlushRegionRequest.class,
88        SplitRegionRequest.class,
89        CompactRegionRequest.class,
90        GetRequest.class,
91        MutateRequest.class,
92        ScanRequest.class
93    };
94  
95    // Some caches for helping performance
96    private final Map<String, Class<? extends Message>> argumentToClassMap =
97      new HashMap<String, Class<? extends Message>>();
98    private final Map<String, Map<Class<? extends Message>, Method>> methodMap =
99      new HashMap<String, Map<Class<? extends Message>, Method>>();
100 
101   private final float scanVirtualTimeWeight;
102 
103   AnnotationReadingPriorityFunction(final RSRpcServices rpcServices) {
104     Map<String, Integer> qosMap = new HashMap<String, Integer>();
105     for (Method m : RSRpcServices.class.getMethods()) {
106       QosPriority p = m.getAnnotation(QosPriority.class);
107       if (p != null) {
108         // Since we protobuf'd, and then subsequently, when we went with pb style, method names
109         // are capitalized.  This meant that this brittle compare of method names gotten by
110         // reflection no longer matched the method names coming in over pb.  TODO: Get rid of this
111         // check.  For now, workaround is to capitalize the names we got from reflection so they
112         // have chance of matching the pb ones.
113         String capitalizedMethodName = capitalize(m.getName());
114         qosMap.put(capitalizedMethodName, p.priority());
115       }
116     }
117     this.rpcServices = rpcServices;
118     this.annotatedQos = qosMap;
119     if (methodMap.get("getRegion") == null) {
120       methodMap.put("hasRegion", new HashMap<Class<? extends Message>, Method>());
121       methodMap.put("getRegion", new HashMap<Class<? extends Message>, Method>());
122     }
123     for (Class<? extends Message> cls : knownArgumentClasses) {
124       argumentToClassMap.put(cls.getName(), cls);
125       try {
126         methodMap.get("hasRegion").put(cls, cls.getDeclaredMethod("hasRegion"));
127         methodMap.get("getRegion").put(cls, cls.getDeclaredMethod("getRegion"));
128       } catch (Exception e) {
129         throw new RuntimeException(e);
130       }
131     }
132 
133     Configuration conf = rpcServices.getConfiguration();
134     scanVirtualTimeWeight = conf.getFloat(SCAN_VTIME_WEIGHT_CONF_KEY, 1.0f);
135   }
136 
137   private String capitalize(final String s) {
138     StringBuilder strBuilder = new StringBuilder(s);
139     strBuilder.setCharAt(0, Character.toUpperCase(strBuilder.charAt(0)));
140     return strBuilder.toString();
141   }
142 
143   /**
144    * Returns a 'priority' based on the request type.
145    *
146    * Currently the returned priority is used for queue selection.
147    * See the SimpleRpcScheduler as example. It maintains a queue per 'priory type'
148    * HIGH_QOS (meta requests), REPLICATION_QOS (replication requests),
149    * NORMAL_QOS (user requests).
150    */
151   @Override
152   public int getPriority(RequestHeader header, Message param) {
153     String methodName = header.getMethodName();
154     Integer priorityByAnnotation = annotatedQos.get(methodName);
155     if (priorityByAnnotation != null) {
156       return priorityByAnnotation;
157     }
158     if (param == null) {
159       return HConstants.NORMAL_QOS;
160     }
161     if (methodName.equalsIgnoreCase("multi") && param instanceof MultiRequest) {
162       // The multi call has its priority set in the header.  All calls should work this way but
163       // only this one has been converted so far.  No priority == NORMAL_QOS.
164       return header.hasPriority()? header.getPriority(): HConstants.NORMAL_QOS;
165     }
166     String cls = param.getClass().getName();
167     Class<? extends Message> rpcArgClass = argumentToClassMap.get(cls);
168     RegionSpecifier regionSpecifier = null;
169     //check whether the request has reference to meta region or now.
170     try {
171       // Check if the param has a region specifier; the pb methods are hasRegion and getRegion if
172       // hasRegion returns true.  Not all listed methods have region specifier each time.  For
173       // example, the ScanRequest has it on setup but thereafter relies on the scannerid rather than
174       // send the region over every time.
175       Method hasRegion = methodMap.get("hasRegion").get(rpcArgClass);
176       if (hasRegion != null && (Boolean)hasRegion.invoke(param, (Object[])null)) {
177         Method getRegion = methodMap.get("getRegion").get(rpcArgClass);
178         regionSpecifier = (RegionSpecifier)getRegion.invoke(param, (Object[])null);
179         HRegion region = rpcServices.getRegion(regionSpecifier);
180         if (region.getRegionInfo().isSystemTable()) {
181           if (LOG.isTraceEnabled()) {
182             LOG.trace("High priority because region=" + region.getRegionNameAsString());
183           }
184           return HConstants.SYSTEMTABLE_QOS;
185         }
186       }
187     } catch (Exception ex) {
188       // Not good throwing an exception out of here, a runtime anyways.  Let the query go into the
189       // server and have it throw the exception if still an issue.  Just mark it normal priority.
190       if (LOG.isTraceEnabled()) LOG.trace("Marking normal priority after getting exception=" + ex);
191       return HConstants.NORMAL_QOS;
192     }
193 
194     if (methodName.equalsIgnoreCase("scan")) { // scanner methods...
195       ScanRequest request = (ScanRequest)param;
196       if (!request.hasScannerId()) {
197         return HConstants.NORMAL_QOS;
198       }
199       RegionScanner scanner = rpcServices.getScanner(request.getScannerId());
200       if (scanner != null && scanner.getRegionInfo().isSystemTable()) {
201         if (LOG.isTraceEnabled()) {
202           // Scanner requests are small in size so TextFormat version should not overwhelm log.
203           LOG.trace("High priority scanner request " + TextFormat.shortDebugString(request));
204         }
205         return HConstants.SYSTEMTABLE_QOS;
206       }
207     }
208     return HConstants.NORMAL_QOS;
209   }
210 
211   /**
212    * Based on the request content, returns the deadline of the request.
213    *
214    * @param header
215    * @param param
216    * @return Deadline of this request. 0 now, otherwise msec of 'delay'
217    */
218   @Override
219   public long getDeadline(RequestHeader header, Message param) {
220     String methodName = header.getMethodName();
221     if (methodName.equalsIgnoreCase("scan")) {
222       ScanRequest request = (ScanRequest)param;
223       if (!request.hasScannerId()) {
224         return 0;
225       }
226 
227       // get the 'virtual time' of the scanner, and applies sqrt() to get a
228       // nice curve for the delay. More a scanner is used the less priority it gets.
229       // The weight is used to have more control on the delay.
230       long vtime = rpcServices.getScannerVirtualTime(request.getScannerId());
231       return Math.round(Math.sqrt(vtime * scanVirtualTimeWeight));
232     }
233     return 0;
234   }
235 
236   @VisibleForTesting
237   void setRegionServer(final HRegionServer hrs) {
238     this.rpcServices = hrs.getRSRpcServices();
239   }
240 }