1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.User;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @InterfaceAudience.Private
71 public class AnnotationReadingPriorityFunction implements PriorityFunction {
72 private static final Log LOG =
73 LogFactory.getLog(AnnotationReadingPriorityFunction.class.getName());
74
75
76 public static final String SCAN_VTIME_WEIGHT_CONF_KEY = "hbase.ipc.server.scan.vtime.weight";
77
78 protected final Map<String, Integer> annotatedQos;
79
80
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
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
104
105
106
107
108
109
110 public AnnotationReadingPriorityFunction(final RSRpcServices rpcServices) {
111 this(rpcServices, rpcServices.getClass());
112 }
113
114
115
116
117
118
119
120
121
122
123 public AnnotationReadingPriorityFunction(final RSRpcServices rpcServices,
124 Class<? extends RSRpcServices> clz) {
125 Map<String,Integer> qosMap = new HashMap<String,Integer>();
126 for (Method m : clz.getMethods()) {
127 QosPriority p = m.getAnnotation(QosPriority.class);
128 if (p != null) {
129
130
131
132
133
134 String capitalizedMethodName = capitalize(m.getName());
135 qosMap.put(capitalizedMethodName, p.priority());
136 }
137 }
138 this.rpcServices = rpcServices;
139 this.annotatedQos = qosMap;
140 if (methodMap.get("getRegion") == null) {
141 methodMap.put("hasRegion", new HashMap<Class<? extends Message>, Method>());
142 methodMap.put("getRegion", new HashMap<Class<? extends Message>, Method>());
143 }
144 for (Class<? extends Message> cls : knownArgumentClasses) {
145 argumentToClassMap.put(cls.getName(), cls);
146 try {
147 methodMap.get("hasRegion").put(cls, cls.getDeclaredMethod("hasRegion"));
148 methodMap.get("getRegion").put(cls, cls.getDeclaredMethod("getRegion"));
149 } catch (Exception e) {
150 throw new RuntimeException(e);
151 }
152 }
153
154 Configuration conf = rpcServices.getConfiguration();
155 scanVirtualTimeWeight = conf.getFloat(SCAN_VTIME_WEIGHT_CONF_KEY, 1.0f);
156 }
157
158 private String capitalize(final String s) {
159 StringBuilder strBuilder = new StringBuilder(s);
160 strBuilder.setCharAt(0, Character.toUpperCase(strBuilder.charAt(0)));
161 return strBuilder.toString();
162 }
163
164
165
166
167
168
169
170
171
172 @Override
173 public int getPriority(RequestHeader header, Message param, User user) {
174 int priorityByAnnotation = getAnnotatedPriority(header);
175
176 if (priorityByAnnotation >= 0) {
177 return priorityByAnnotation;
178 }
179 return getBasePriority(header, param);
180 }
181
182
183
184
185
186
187
188 protected int getAnnotatedPriority(RequestHeader header) {
189 String methodName = header.getMethodName();
190 Integer priorityByAnnotation = annotatedQos.get(methodName);
191 if (priorityByAnnotation != null) {
192 return priorityByAnnotation;
193 }
194 return -1;
195 }
196
197
198
199
200
201
202 protected int getBasePriority(RequestHeader header, Message param) {
203 if (param == null) {
204 return HConstants.NORMAL_QOS;
205 }
206 if (param instanceof MultiRequest) {
207
208
209 return header.hasPriority()? header.getPriority(): HConstants.NORMAL_QOS;
210 }
211
212 String cls = param.getClass().getName();
213 Class<? extends Message> rpcArgClass = argumentToClassMap.get(cls);
214 RegionSpecifier regionSpecifier = null;
215
216 try {
217
218
219
220
221 Method hasRegion = methodMap.get("hasRegion").get(rpcArgClass);
222 if (hasRegion != null && (Boolean)hasRegion.invoke(param, (Object[])null)) {
223 Method getRegion = methodMap.get("getRegion").get(rpcArgClass);
224 regionSpecifier = (RegionSpecifier)getRegion.invoke(param, (Object[])null);
225 Region region = rpcServices.getRegion(regionSpecifier);
226 if (region.getRegionInfo().isSystemTable()) {
227 if (LOG.isTraceEnabled()) {
228 LOG.trace("High priority because region=" +
229 region.getRegionInfo().getRegionNameAsString());
230 }
231 return HConstants.SYSTEMTABLE_QOS;
232 }
233 }
234 } catch (Exception ex) {
235
236
237 if (LOG.isTraceEnabled()) LOG.trace("Marking normal priority after getting exception=" + ex);
238 return HConstants.NORMAL_QOS;
239 }
240
241 if (param instanceof ScanRequest) {
242 ScanRequest request = (ScanRequest)param;
243 if (!request.hasScannerId()) {
244 return HConstants.NORMAL_QOS;
245 }
246 RegionScanner scanner = rpcServices.getScanner(request.getScannerId());
247 if (scanner != null && scanner.getRegionInfo().isSystemTable()) {
248 if (LOG.isTraceEnabled()) {
249
250 LOG.trace("High priority scanner request " + TextFormat.shortDebugString(request));
251 }
252 return HConstants.SYSTEMTABLE_QOS;
253 }
254 }
255
256 return HConstants.NORMAL_QOS;
257 }
258
259
260
261
262
263
264
265
266 @Override
267 public long getDeadline(RequestHeader header, Message param) {
268 if (param instanceof ScanRequest) {
269 ScanRequest request = (ScanRequest)param;
270 if (!request.hasScannerId()) {
271 return 0;
272 }
273
274
275
276
277 long vtime = rpcServices.getScannerVirtualTime(request.getScannerId());
278 return Math.round(Math.sqrt(vtime * scanVirtualTimeWeight));
279 }
280 return 0;
281 }
282
283 @VisibleForTesting
284 void setRegionServer(final HRegionServer hrs) {
285 this.rpcServices = hrs.getRSRpcServices();
286 }
287 }