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.HConstants;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.NotServingRegionException;
30  import org.apache.hadoop.hbase.ipc.PriorityFunction;
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.ReportRegionStateTransitionRequest;
34  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionStateTransition;
35  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CompactRegionRequest;
37  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetRegionInfoRequest;
39  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetStoreFileRequest;
40  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.SplitRegionRequest;
41  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetRequest;
42  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateRequest;
43  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
44  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
45  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.RequestHeader;
46  import org.apache.hadoop.hbase.regionserver.HRegionServer.QosPriority;
47  
48  import com.google.common.annotations.VisibleForTesting;
49  import com.google.protobuf.Message;
50  import com.google.protobuf.TextFormat;
51  
52  
53  /**
54   * Reads special method annotations and table names to figure a priority for use by QoS facility in
55   * ipc; e.g: rpcs to hbase:meta get priority.
56   */
57  // TODO: Remove.  This is doing way too much work just to figure a priority.  Do as Elliott
58  // suggests and just have the client specify a priority.
59  
60  //The logic for figuring out high priority RPCs is as follows:
61  //1. if the method is annotated with a QosPriority of QOS_HIGH,
62  //   that is honored
63  //2. parse out the protobuf message and see if the request is for meta
64  //   region, and if so, treat it as a high priority RPC
65  //Some optimizations for (2) are done here -
66  //Clients send the argument classname as part of making the RPC. The server
67  //decides whether to deserialize the proto argument message based on the
68  //pre-established set of argument classes (knownArgumentClasses below).
69  //This prevents the server from having to deserialize all proto argument
70  //messages prematurely.
71  //All the argument classes declare a 'getRegion' method that returns a
72  //RegionSpecifier object. Methods can be invoked on the returned object
73  //to figure out whether it is a meta region or not.
74  @InterfaceAudience.Private
75  class AnnotationReadingPriorityFunction implements PriorityFunction {
76    public static final Log LOG =
77      LogFactory.getLog(AnnotationReadingPriorityFunction.class.getName());
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 HRegionServer hRegionServer;
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   AnnotationReadingPriorityFunction(final HRegionServer hrs) {
102     this.hRegionServer = hrs;
103     Map<String, Integer> qosMap = new HashMap<String, Integer>();
104     for (Method m : HRegionServer.class.getMethods()) {
105       QosPriority p = m.getAnnotation(QosPriority.class);
106       if (p != null) {
107         // Since we protobuf'd, and then subsequently, when we went with pb style, method names
108         // are capitalized.  This meant that this brittle compare of method names gotten by
109         // reflection no longer matched the method names coming in over pb.  TODO: Get rid of this
110         // check.  For now, workaround is to capitalize the names we got from reflection so they
111         // have chance of matching the pb ones.
112         String capitalizedMethodName = capitalize(m.getName());
113         qosMap.put(capitalizedMethodName, p.priority());
114       }
115     }
116     this.annotatedQos = qosMap;
117     if (methodMap.get("getRegion") == null) {
118       methodMap.put("hasRegion", new HashMap<Class<? extends Message>, Method>());
119       methodMap.put("getRegion", new HashMap<Class<? extends Message>, Method>());
120     }
121     for (Class<? extends Message> cls : knownArgumentClasses) {
122       argumentToClassMap.put(cls.getName(), cls);
123       try {
124         methodMap.get("hasRegion").put(cls, cls.getDeclaredMethod("hasRegion"));
125         methodMap.get("getRegion").put(cls, cls.getDeclaredMethod("getRegion"));
126       } catch (Exception e) {
127         throw new RuntimeException(e);
128       }
129     }
130   }
131 
132   private String capitalize(final String s) {
133     StringBuilder strBuilder = new StringBuilder(s);
134     strBuilder.setCharAt(0, Character.toUpperCase(strBuilder.charAt(0)));
135     return strBuilder.toString();
136   }
137 
138   public boolean isMetaRegion(byte[] regionName) {
139     HRegion region;
140     try {
141       region = hRegionServer.getRegion(regionName);
142     } catch (NotServingRegionException ignored) {
143       return false;
144     }
145     return region.getRegionInfo().isMetaTable();
146   }
147 
148   @Override
149   public int getPriority(RequestHeader header, Message param) {
150     String methodName = header.getMethodName();
151     Integer priorityByAnnotation = annotatedQos.get(methodName);
152     if (priorityByAnnotation != null) {
153       return priorityByAnnotation;
154     }
155     if (param == null) {
156       return HConstants.NORMAL_QOS;
157     }
158     // Trust the client-set priorities if set
159     if (header.hasPriority()) {
160       return header.getPriority();
161     }
162     String cls = param.getClass().getName();
163     Class<? extends Message> rpcArgClass = argumentToClassMap.get(cls);
164     RegionSpecifier regionSpecifier = null;
165     //check whether the request has reference to meta region or now.
166     try {
167       // Check if the param has a region specifier; the pb methods are hasRegion and getRegion if
168       // hasRegion returns true.  Not all listed methods have region specifier each time.  For
169       // example, the ScanRequest has it on setup but thereafter relies on the scannerid rather than
170       // send the region over every time.
171       Method hasRegion = methodMap.get("hasRegion").get(rpcArgClass);
172       if (hasRegion != null && (Boolean)hasRegion.invoke(param, (Object[])null)) {
173         Method getRegion = methodMap.get("getRegion").get(rpcArgClass);
174         regionSpecifier = (RegionSpecifier)getRegion.invoke(param, (Object[])null);
175         HRegion region = hRegionServer.getRegion(regionSpecifier);
176         if (region.getRegionInfo().isMetaTable()) {
177           if (LOG.isTraceEnabled()) {
178             LOG.trace("High priority because region=" + region.getRegionNameAsString());
179           }
180           return HConstants.HIGH_QOS;
181         }
182       }
183     } catch (Exception ex) {
184       // Not good throwing an exception out of here, a runtime anyways.  Let the query go into the
185       // server and have it throw the exception if still an issue.  Just mark it normal priority.
186       if (LOG.isTraceEnabled()) LOG.trace("Marking normal priority after getting exception=" + ex);
187       return HConstants.NORMAL_QOS;
188     }
189 
190     if (param instanceof ScanRequest) { // scanner methods...
191       ScanRequest request = (ScanRequest)param;
192       if (!request.hasScannerId()) {
193         return HConstants.NORMAL_QOS;
194       }
195       RegionScanner scanner = hRegionServer.getScanner(request.getScannerId());
196       if (scanner != null && scanner.getRegionInfo().isMetaRegion()) {
197         if (LOG.isTraceEnabled()) {
198           // Scanner requests are small in size so TextFormat version should not overwhelm log.
199           LOG.trace("High priority scanner request " + TextFormat.shortDebugString(request));
200         }
201         return HConstants.HIGH_QOS;
202       }
203     }
204 
205     // If meta is moving then all the rest of report the report state transitions will be
206     // blocked. We shouldn't be in the same queue.
207     if (param instanceof ReportRegionStateTransitionRequest) { // Regions are moving
208       ReportRegionStateTransitionRequest tRequest = (ReportRegionStateTransitionRequest) param;
209       for (RegionStateTransition transition : tRequest.getTransitionList()) {
210         if (transition.getRegionInfoList() != null) {
211           for (HBaseProtos.RegionInfo info : transition.getRegionInfoList()) {
212             TableName tn = ProtobufUtil.toTableName(info.getTableName());
213             if (tn.isSystemTable()) {
214               return HConstants.HIGH_QOS;
215             }
216           }
217         }
218       }
219     }
220     return HConstants.NORMAL_QOS;
221   }
222 
223   @VisibleForTesting
224   void setRegionServer(final HRegionServer hrs) {
225     this.hRegionServer = hrs;
226   }
227 }