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
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 class AnnotationReadingPriorityFunction implements PriorityFunction {
72 public 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 private 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 AnnotationReadingPriorityFunction(final RSRpcServices rpcServices) {
111 this(rpcServices, rpcServices.getClass());
112 }
113
114
115
116
117
118
119
120
121
122
123 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) {
174 String methodName = header.getMethodName();
175 Integer priorityByAnnotation = annotatedQos.get(methodName);
176 if (priorityByAnnotation != null) {
177 return priorityByAnnotation;
178 }
179 if (param == null) {
180 return HConstants.NORMAL_QOS;
181 }
182 if (methodName.equalsIgnoreCase("multi") && param instanceof MultiRequest) {
183
184
185 return header.hasPriority()? header.getPriority(): HConstants.NORMAL_QOS;
186 }
187 String cls = param.getClass().getName();
188 Class<? extends Message> rpcArgClass = argumentToClassMap.get(cls);
189 RegionSpecifier regionSpecifier = null;
190
191 try {
192
193
194
195
196 Method hasRegion = methodMap.get("hasRegion").get(rpcArgClass);
197 if (hasRegion != null && (Boolean)hasRegion.invoke(param, (Object[])null)) {
198 Method getRegion = methodMap.get("getRegion").get(rpcArgClass);
199 regionSpecifier = (RegionSpecifier)getRegion.invoke(param, (Object[])null);
200 Region region = rpcServices.getRegion(regionSpecifier);
201 if (region.getRegionInfo().isSystemTable()) {
202 if (LOG.isTraceEnabled()) {
203 LOG.trace("High priority because region=" +
204 region.getRegionInfo().getRegionNameAsString());
205 }
206 return HConstants.SYSTEMTABLE_QOS;
207 }
208 }
209 } catch (Exception ex) {
210
211
212 if (LOG.isTraceEnabled()) LOG.trace("Marking normal priority after getting exception=" + ex);
213 return HConstants.NORMAL_QOS;
214 }
215
216 if (methodName.equalsIgnoreCase("scan")) {
217 ScanRequest request = (ScanRequest)param;
218 if (!request.hasScannerId()) {
219 return HConstants.NORMAL_QOS;
220 }
221 RegionScanner scanner = rpcServices.getScanner(request.getScannerId());
222 if (scanner != null && scanner.getRegionInfo().isSystemTable()) {
223 if (LOG.isTraceEnabled()) {
224
225 LOG.trace("High priority scanner request " + TextFormat.shortDebugString(request));
226 }
227 return HConstants.SYSTEMTABLE_QOS;
228 }
229 }
230 return HConstants.NORMAL_QOS;
231 }
232
233
234
235
236
237
238
239
240 @Override
241 public long getDeadline(RequestHeader header, Message param) {
242 String methodName = header.getMethodName();
243 if (methodName.equalsIgnoreCase("scan")) {
244 ScanRequest request = (ScanRequest)param;
245 if (!request.hasScannerId()) {
246 return 0;
247 }
248
249
250
251
252 long vtime = rpcServices.getScannerVirtualTime(request.getScannerId());
253 return Math.round(Math.sqrt(vtime * scanVirtualTimeWeight));
254 }
255 return 0;
256 }
257
258 @VisibleForTesting
259 void setRegionServer(final HRegionServer hrs) {
260 this.rpcServices = hrs.getRSRpcServices();
261 }
262 }