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