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.exceptions.NotServingRegionException;
28  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionRequest;
29  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CompactRegionRequest;
30  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionRequest;
31  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetRegionInfoRequest;
32  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetStoreFileRequest;
33  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.SplitRegionRequest;
34  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetRequest;
35  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateRequest;
37  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
39  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.RequestHeader;
40  import org.apache.hadoop.hbase.regionserver.HRegionServer.QosPriority;
41  import org.apache.hadoop.hbase.util.Pair;
42  
43  import com.google.common.annotations.VisibleForTesting;
44  import com.google.common.base.Function;
45  import com.google.protobuf.Message;
46  import com.google.protobuf.TextFormat;
47  
48  
49  /**
50   * A guava function that will return a priority for use by QoS facility in regionserver; e.g.
51   * rpcs to .META. and -ROOT-, etc., 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  class QosFunction implements Function<Pair<RequestHeader, Message>, Integer> {
71    public static final Log LOG = LogFactory.getLog(QosFunction.class.getName());
72    private final Map<String, Integer> annotatedQos;
73    //We need to mock the regionserver instance for some unit tests (set via
74    //setRegionServer method.
75    private HRegionServer hRegionServer;
76    @SuppressWarnings("unchecked")
77    private final Class<? extends Message>[] knownArgumentClasses = new Class[]{
78        GetRegionInfoRequest.class,
79        GetStoreFileRequest.class,
80        CloseRegionRequest.class,
81        FlushRegionRequest.class,
82        SplitRegionRequest.class,
83        CompactRegionRequest.class,
84        GetRequest.class,
85        MutateRequest.class,
86        ScanRequest.class,
87        MultiRequest.class
88    };
89  
90    // Some caches for helping performance
91    private final Map<String, Class<? extends Message>> argumentToClassMap =
92      new HashMap<String, Class<? extends Message>>();
93    private final Map<String, Map<Class<? extends Message>, Method>> methodMap =
94      new HashMap<String, Map<Class<? extends Message>, Method>>();
95  
96    QosFunction(final HRegionServer hrs) {
97      this.hRegionServer = hrs;
98      Map<String, Integer> qosMap = new HashMap<String, Integer>();
99      for (Method m : HRegionServer.class.getMethods()) {
100       QosPriority p = m.getAnnotation(QosPriority.class);
101       if (p != null) {
102         qosMap.put(m.getName(), p.priority());
103       }
104     }
105     this.annotatedQos = qosMap;
106 
107     if (methodMap.get("getRegion") == null) {
108       methodMap.put("hasRegion", new HashMap<Class<? extends Message>, Method>());
109       methodMap.put("getRegion", new HashMap<Class<? extends Message>, Method>());
110     }
111     for (Class<? extends Message> cls : knownArgumentClasses) {
112       argumentToClassMap.put(cls.getName(), cls);
113       try {
114         methodMap.get("hasRegion").put(cls, cls.getDeclaredMethod("hasRegion"));
115         methodMap.get("getRegion").put(cls, cls.getDeclaredMethod("getRegion"));
116       } catch (Exception e) {
117         throw new RuntimeException(e);
118       }
119     }
120   }
121 
122   public boolean isMetaRegion(byte[] regionName) {
123     HRegion region;
124     try {
125       region = hRegionServer.getRegion(regionName);
126     } catch (NotServingRegionException ignored) {
127       return false;
128     }
129     return region.getRegionInfo().isMetaTable();
130   }
131 
132   @Override
133   public Integer apply(Pair<RequestHeader, Message> headerAndParam) {
134     RequestHeader header = headerAndParam.getFirst();
135     String methodName = header.getMethodName();
136     Integer priorityByAnnotation = annotatedQos.get(header.getMethodName());
137     if (priorityByAnnotation != null) {
138       return priorityByAnnotation;
139     }
140 
141     Message param = headerAndParam.getSecond();
142     if (param == null) {
143       return HConstants.NORMAL_QOS;
144     }
145     String cls = param.getClass().getName();
146     Class<? extends Message> rpcArgClass = argumentToClassMap.get(cls);
147     RegionSpecifier regionSpecifier = null;
148     //check whether the request has reference to meta region or now.
149     try {
150       // Check if the param has a region specifier; the pb methods are hasRegion and getRegion if
151       // hasRegion returns true.  Not all listed methods have region specifier each time.  For
152       // example, the ScanRequest has it on setup but thereafter relies on the scannerid rather than
153       // send the region over every time.
154       Method hasRegion = methodMap.get("hasRegion").get(rpcArgClass);
155       if (hasRegion != null && (Boolean)hasRegion.invoke(param, (Object[])null)) {
156         Method getRegion = methodMap.get("getRegion").get(rpcArgClass);
157         regionSpecifier = (RegionSpecifier)getRegion.invoke(param, (Object[])null);
158         HRegion region = hRegionServer.getRegion(regionSpecifier);
159         if (region.getRegionInfo().isMetaTable()) {
160           if (LOG.isTraceEnabled()) {
161             LOG.trace("High priority: " + TextFormat.shortDebugString(param));
162           }
163           return HConstants.HIGH_QOS;
164         }
165       }
166     } catch (Exception ex) {
167       // Not good throwing an exception out of here, a runtime anyways.  Let the query go into the
168       // server and have it throw the exception if still an issue.  Just mark it normal priority.
169       if (LOG.isDebugEnabled()) LOG.debug("Marking normal priority after getting exception=" + ex);
170       return HConstants.NORMAL_QOS;
171     }
172 
173     if (methodName.equals("scan")) { // scanner methods...
174       ScanRequest request = (ScanRequest)param;
175       if (!request.hasScannerId()) {
176         return HConstants.NORMAL_QOS;
177       }
178       RegionScanner scanner = hRegionServer.getScanner(request.getScannerId());
179       if (scanner != null && scanner.getRegionInfo().isMetaRegion()) {
180         if (LOG.isTraceEnabled()) {
181           LOG.trace("High priority scanner request: " + TextFormat.shortDebugString(request));
182         }
183         return HConstants.HIGH_QOS;
184       }
185     }
186     if (LOG.isTraceEnabled()) {
187       LOG.trace("Low priority: " + TextFormat.shortDebugString(param));
188     }
189     return HConstants.NORMAL_QOS;
190   }
191 
192   @VisibleForTesting
193   void setRegionServer(final HRegionServer hrs) {
194     this.hRegionServer = hrs;
195   }
196 }