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