1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.protobuf;
19
20 import javax.annotation.Nullable;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.CellScanner;
31 import org.apache.hadoop.hbase.DoNotRetryIOException;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.ServerName;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.client.Result;
36 import org.apache.hadoop.hbase.ipc.ServerRpcController;
37 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.GetUserPermissionsResponse;
38 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionResponse;
39 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetOnlineRegionResponse;
40 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoResponse;
41 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.OpenRegionResponse;
42 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.ServerInfo;
43 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
44 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
45 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiResponse;
46 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionAction;
47 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionActionResult;
48 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ResultOrException;
49 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
50 import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos.RegionStoreSequenceIds;
51 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
52 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameBytesPair;
53 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameInt64Pair;
54 import org.apache.hadoop.hbase.protobuf.generated.MapReduceProtos.ScanMetrics;
55 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.EnableCatalogJanitorResponse;
56 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.RunCatalogScanResponse;
57 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.GetLastFlushedSequenceIdResponse;
58 import org.apache.hadoop.hbase.regionserver.RegionOpeningState;
59 import org.apache.hadoop.hbase.security.access.UserPermission;
60 import org.apache.hadoop.util.StringUtils;
61
62 import com.google.protobuf.ByteString;
63 import com.google.protobuf.RpcController;
64
65
66
67
68
69 @InterfaceAudience.Private
70 public final class ResponseConverter {
71 private static final Log LOG = LogFactory.getLog(ResponseConverter.class);
72
73 private ResponseConverter() {
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public static org.apache.hadoop.hbase.client.MultiResponse getResults(final MultiRequest request,
87 final MultiResponse response, final CellScanner cells)
88 throws IOException {
89 int requestRegionActionCount = request.getRegionActionCount();
90 int responseRegionActionResultCount = response.getRegionActionResultCount();
91 if (requestRegionActionCount != responseRegionActionResultCount) {
92 throw new IllegalStateException("Request mutation count=" + responseRegionActionResultCount +
93 " does not match response mutation result count=" + responseRegionActionResultCount);
94 }
95
96 org.apache.hadoop.hbase.client.MultiResponse results =
97 new org.apache.hadoop.hbase.client.MultiResponse();
98
99 for (int i = 0; i < responseRegionActionResultCount; i++) {
100 RegionAction actions = request.getRegionAction(i);
101 RegionActionResult actionResult = response.getRegionActionResult(i);
102 HBaseProtos.RegionSpecifier rs = actions.getRegion();
103 if (rs.hasType() &&
104 (rs.getType() != HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME)){
105 throw new IllegalArgumentException(
106 "We support only encoded types for protobuf multi response.");
107 }
108 byte[] regionName = rs.getValue().toByteArray();
109
110 if (actionResult.hasException()) {
111 Throwable regionException = ProtobufUtil.toException(actionResult.getException());
112 results.addException(regionName, regionException);
113 continue;
114 }
115
116 if (actions.getActionCount() != actionResult.getResultOrExceptionCount()) {
117 throw new IllegalStateException("actions.getActionCount=" + actions.getActionCount() +
118 ", actionResult.getResultOrExceptionCount=" +
119 actionResult.getResultOrExceptionCount() + " for region " + actions.getRegion());
120 }
121
122 for (ResultOrException roe : actionResult.getResultOrExceptionList()) {
123 Object responseValue;
124 if (roe.hasException()) {
125 responseValue = ProtobufUtil.toException(roe.getException());
126 } else if (roe.hasResult()) {
127 responseValue = ProtobufUtil.toResult(roe.getResult(), cells);
128
129 if (roe.hasLoadStats()) {
130 ((Result) responseValue).addResults(roe.getLoadStats());
131 }
132 } else if (roe.hasServiceResult()) {
133 responseValue = roe.getServiceResult();
134 } else {
135
136 throw new IllegalStateException("No result & no exception roe=" + roe +
137 " for region " + actions.getRegion());
138 }
139 results.add(regionName, roe.getIndex(), responseValue);
140 }
141 }
142
143 return results;
144 }
145
146
147
148
149
150
151
152 public static ResultOrException.Builder buildActionResult(final Throwable t) {
153 ResultOrException.Builder builder = ResultOrException.newBuilder();
154 if (t != null) builder.setException(buildException(t));
155 return builder;
156 }
157
158
159
160
161
162
163
164 public static ResultOrException.Builder buildActionResult(final ClientProtos.Result r,
165 ClientProtos.RegionLoadStats stats) {
166 ResultOrException.Builder builder = ResultOrException.newBuilder();
167 if (r != null) builder.setResult(r);
168 if(stats != null) builder.setLoadStats(stats);
169 return builder;
170 }
171
172
173
174
175
176 public static NameBytesPair buildException(final Throwable t) {
177 NameBytesPair.Builder parameterBuilder = NameBytesPair.newBuilder();
178 parameterBuilder.setName(t.getClass().getName());
179 parameterBuilder.setValue(
180 ByteString.copyFromUtf8(StringUtils.stringifyException(t)));
181 return parameterBuilder.build();
182 }
183
184
185
186
187 public static GetUserPermissionsResponse buildGetUserPermissionsResponse(
188 final List<UserPermission> permissions) {
189 GetUserPermissionsResponse.Builder builder = GetUserPermissionsResponse.newBuilder();
190 for (UserPermission perm : permissions) {
191 builder.addUserPermission(ProtobufUtil.toUserPermission(perm));
192 }
193 return builder.build();
194 }
195
196
197
198
199
200
201
202
203
204
205 public static List<HRegionInfo> getRegionInfos(final GetOnlineRegionResponse proto) {
206 if (proto == null || proto.getRegionInfoCount() == 0) return null;
207 return ProtobufUtil.getRegionInfos(proto);
208 }
209
210
211
212
213
214
215
216 public static RegionOpeningState getRegionOpeningState
217 (final OpenRegionResponse proto) {
218 if (proto == null || proto.getOpeningStateCount() != 1) return null;
219 return RegionOpeningState.valueOf(
220 proto.getOpeningState(0).name());
221 }
222
223
224
225
226
227
228
229 public static List<RegionOpeningState> getRegionOpeningStateList(
230 final OpenRegionResponse proto) {
231 if (proto == null) return null;
232 List<RegionOpeningState> regionOpeningStates = new ArrayList<RegionOpeningState>();
233 for (int i = 0; i < proto.getOpeningStateCount(); i++) {
234 regionOpeningStates.add(RegionOpeningState.valueOf(
235 proto.getOpeningState(i).name()));
236 }
237 return regionOpeningStates;
238 }
239
240
241
242
243
244
245
246 public static boolean isClosed
247 (final CloseRegionResponse proto) {
248 if (proto == null || !proto.hasClosed()) return false;
249 return proto.getClosed();
250 }
251
252
253
254
255
256
257
258
259 public static GetServerInfoResponse buildGetServerInfoResponse(
260 final ServerName serverName, final int webuiPort) {
261 GetServerInfoResponse.Builder builder = GetServerInfoResponse.newBuilder();
262 ServerInfo.Builder serverInfoBuilder = ServerInfo.newBuilder();
263 serverInfoBuilder.setServerName(ProtobufUtil.toServerName(serverName));
264 if (webuiPort >= 0) {
265 serverInfoBuilder.setWebuiPort(webuiPort);
266 }
267 builder.setServerInfo(serverInfoBuilder.build());
268 return builder.build();
269 }
270
271
272
273
274
275
276
277 public static GetOnlineRegionResponse buildGetOnlineRegionResponse(
278 final List<HRegionInfo> regions) {
279 GetOnlineRegionResponse.Builder builder = GetOnlineRegionResponse.newBuilder();
280 for (HRegionInfo region: regions) {
281 builder.addRegionInfo(HRegionInfo.convert(region));
282 }
283 return builder.build();
284 }
285
286
287
288
289
290 public static RunCatalogScanResponse buildRunCatalogScanResponse(int numCleaned) {
291 return RunCatalogScanResponse.newBuilder().setScanResult(numCleaned).build();
292 }
293
294
295
296
297
298 public static EnableCatalogJanitorResponse buildEnableCatalogJanitorResponse(boolean prevValue) {
299 return EnableCatalogJanitorResponse.newBuilder().setPrevValue(prevValue).build();
300 }
301
302
303
304
305
306
307
308 public static GetLastFlushedSequenceIdResponse buildGetLastFlushedSequenceIdResponse(
309 RegionStoreSequenceIds ids) {
310 return GetLastFlushedSequenceIdResponse.newBuilder()
311 .setLastFlushedSequenceId(ids.getLastFlushedSequenceId())
312 .addAllStoreLastFlushedSequenceId(ids.getStoreSequenceIdList()).build();
313 }
314
315
316
317
318
319
320
321 public static void setControllerException(RpcController controller, IOException ioe) {
322 if (controller != null) {
323 if (controller instanceof ServerRpcController) {
324 ((ServerRpcController)controller).setFailedOn(ioe);
325 } else {
326 controller.setFailed(StringUtils.stringifyException(ioe));
327 }
328 }
329 }
330
331
332
333
334
335
336
337 @Nullable
338 public static IOException getControllerException(RpcController controller) throws IOException {
339 if (controller != null && controller.failed()) {
340 if (controller instanceof ServerRpcController) {
341 return ((ServerRpcController)controller).getFailedOn();
342 } else {
343 return new DoNotRetryIOException(controller.errorText());
344 }
345 }
346 return null;
347 }
348
349
350
351
352
353
354
355
356 public static Result[] getResults(CellScanner cellScanner, ScanResponse response)
357 throws IOException {
358 if (response == null) return null;
359
360
361 int noOfResults = cellScanner != null?
362 response.getCellsPerResultCount(): response.getResultsCount();
363 Result[] results = new Result[noOfResults];
364 for (int i = 0; i < noOfResults; i++) {
365 if (cellScanner != null) {
366
367
368 int noOfCells = response.getCellsPerResult(i);
369 boolean isPartial =
370 response.getPartialFlagPerResultCount() > i ?
371 response.getPartialFlagPerResult(i) : false;
372 List<Cell> cells = new ArrayList<Cell>(noOfCells);
373 for (int j = 0; j < noOfCells; j++) {
374 try {
375 if (cellScanner.advance() == false) {
376
377
378
379 String msg = "Results sent from server=" + noOfResults + ". But only got " + i
380 + " results completely at client. Resetting the scanner to scan again.";
381 LOG.error(msg);
382 throw new DoNotRetryIOException(msg);
383 }
384 } catch (IOException ioe) {
385
386
387
388 LOG.error("Exception while reading cells from result."
389 + "Resetting the scanner to scan again.", ioe);
390 throw new DoNotRetryIOException("Resetting the scanner.", ioe);
391 }
392 cells.add(cellScanner.current());
393 }
394 results[i] = Result.create(cells, null, response.getStale(), isPartial);
395 } else {
396
397 results[i] = ProtobufUtil.toResult(response.getResults(i));
398 }
399 }
400 return results;
401 }
402
403 public static Map<String, Long> getScanMetrics(ScanResponse response) {
404 Map<String, Long> metricMap = new HashMap<String, Long>();
405 if (response == null || !response.hasScanMetrics() || response.getScanMetrics() == null) {
406 return metricMap;
407 }
408
409 ScanMetrics metrics = response.getScanMetrics();
410 int numberOfMetrics = metrics.getMetricsCount();
411 for (int i = 0; i < numberOfMetrics; i++) {
412 NameInt64Pair metricPair = metrics.getMetrics(i);
413 if (metricPair != null) {
414 String name = metricPair.getName();
415 Long value = metricPair.getValue();
416 if (name != null && value != null) {
417 metricMap.put(name, value);
418 }
419 }
420 }
421
422 return metricMap;
423 }
424 }