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 edu.umd.cs.findbugs.annotations.Nullable;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.CellScanner;
30 import org.apache.hadoop.hbase.DoNotRetryIOException;
31 import org.apache.hadoop.hbase.HRegionInfo;
32 import org.apache.hadoop.hbase.ServerName;
33 import org.apache.hadoop.hbase.client.Result;
34 import org.apache.hadoop.hbase.ipc.ServerRpcController;
35 import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos.GetUserPermissionsResponse;
36 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionResponse;
37 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetOnlineRegionResponse;
38 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoResponse;
39 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.OpenRegionResponse;
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.ClusterStatusProtos.RegionStoreSequenceIds;
49 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
50 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameBytesPair;
51 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.EnableCatalogJanitorResponse;
52 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.RunCatalogScanResponse;
53 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.GetLastFlushedSequenceIdResponse;
54 import org.apache.hadoop.hbase.regionserver.RegionOpeningState;
55 import org.apache.hadoop.hbase.security.access.UserPermission;
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 Object responseValue;
120 if (roe.hasException()) {
121 responseValue = ProtobufUtil.toException(roe.getException());
122 } else if (roe.hasResult()) {
123 responseValue = ProtobufUtil.toResult(roe.getResult(), cells);
124
125 if (roe.hasLoadStats()) {
126 ((Result) responseValue).addResults(roe.getLoadStats());
127 }
128 } else if (roe.hasServiceResult()) {
129 responseValue = roe.getServiceResult();
130 } else {
131
132 throw new IllegalStateException("No result & no exception roe=" + roe +
133 " for region " + actions.getRegion());
134 }
135 results.add(regionName, roe.getIndex(), responseValue);
136 }
137 }
138
139 return results;
140 }
141
142
143
144
145
146
147
148 public static ResultOrException.Builder buildActionResult(final Throwable t) {
149 ResultOrException.Builder builder = ResultOrException.newBuilder();
150 if (t != null) builder.setException(buildException(t));
151 return builder;
152 }
153
154
155
156
157
158
159
160 public static ResultOrException.Builder buildActionResult(final ClientProtos.Result r,
161 ClientProtos.RegionLoadStats stats) {
162 ResultOrException.Builder builder = ResultOrException.newBuilder();
163 if (r != null) builder.setResult(r);
164 if(stats != null) builder.setLoadStats(stats);
165 return builder;
166 }
167
168
169
170
171
172 public static NameBytesPair buildException(final Throwable t) {
173 NameBytesPair.Builder parameterBuilder = NameBytesPair.newBuilder();
174 parameterBuilder.setName(t.getClass().getName());
175 parameterBuilder.setValue(
176 ByteString.copyFromUtf8(StringUtils.stringifyException(t)));
177 return parameterBuilder.build();
178 }
179
180
181
182
183 public static GetUserPermissionsResponse buildGetUserPermissionsResponse(
184 final List<UserPermission> permissions) {
185 GetUserPermissionsResponse.Builder builder = GetUserPermissionsResponse.newBuilder();
186 for (UserPermission perm : permissions) {
187 builder.addUserPermission(ProtobufUtil.toUserPermission(perm));
188 }
189 return builder.build();
190 }
191
192
193
194
195
196
197
198
199
200
201 public static List<HRegionInfo> getRegionInfos(final GetOnlineRegionResponse proto) {
202 if (proto == null || proto.getRegionInfoCount() == 0) return null;
203 return ProtobufUtil.getRegionInfos(proto);
204 }
205
206
207
208
209
210
211
212 public static RegionOpeningState getRegionOpeningState
213 (final OpenRegionResponse proto) {
214 if (proto == null || proto.getOpeningStateCount() != 1) return null;
215 return RegionOpeningState.valueOf(
216 proto.getOpeningState(0).name());
217 }
218
219
220
221
222
223
224
225 public static List<RegionOpeningState> getRegionOpeningStateList(
226 final OpenRegionResponse proto) {
227 if (proto == null) return null;
228 List<RegionOpeningState> regionOpeningStates = new ArrayList<RegionOpeningState>();
229 for (int i = 0; i < proto.getOpeningStateCount(); i++) {
230 regionOpeningStates.add(RegionOpeningState.valueOf(
231 proto.getOpeningState(i).name()));
232 }
233 return regionOpeningStates;
234 }
235
236
237
238
239
240
241
242 public static boolean isClosed
243 (final CloseRegionResponse proto) {
244 if (proto == null || !proto.hasClosed()) return false;
245 return proto.getClosed();
246 }
247
248
249
250
251
252
253
254
255 public static GetServerInfoResponse buildGetServerInfoResponse(
256 final ServerName serverName, final int webuiPort) {
257 GetServerInfoResponse.Builder builder = GetServerInfoResponse.newBuilder();
258 ServerInfo.Builder serverInfoBuilder = ServerInfo.newBuilder();
259 serverInfoBuilder.setServerName(ProtobufUtil.toServerName(serverName));
260 if (webuiPort >= 0) {
261 serverInfoBuilder.setWebuiPort(webuiPort);
262 }
263 builder.setServerInfo(serverInfoBuilder.build());
264 return builder.build();
265 }
266
267
268
269
270
271
272
273 public static GetOnlineRegionResponse buildGetOnlineRegionResponse(
274 final List<HRegionInfo> regions) {
275 GetOnlineRegionResponse.Builder builder = GetOnlineRegionResponse.newBuilder();
276 for (HRegionInfo region: regions) {
277 builder.addRegionInfo(HRegionInfo.convert(region));
278 }
279 return builder.build();
280 }
281
282
283
284
285
286 public static RunCatalogScanResponse buildRunCatalogScanResponse(int numCleaned) {
287 return RunCatalogScanResponse.newBuilder().setScanResult(numCleaned).build();
288 }
289
290
291
292
293
294 public static EnableCatalogJanitorResponse buildEnableCatalogJanitorResponse(boolean prevValue) {
295 return EnableCatalogJanitorResponse.newBuilder().setPrevValue(prevValue).build();
296 }
297
298
299
300
301
302
303
304 public static GetLastFlushedSequenceIdResponse buildGetLastFlushedSequenceIdResponse(
305 RegionStoreSequenceIds ids) {
306 return GetLastFlushedSequenceIdResponse.newBuilder()
307 .setLastFlushedSequenceId(ids.getLastFlushedSequenceId())
308 .addAllStoreLastFlushedSequenceId(ids.getStoreSequenceIdList()).build();
309 }
310
311
312
313
314
315
316
317 public static void setControllerException(RpcController controller, IOException ioe) {
318 if (controller != null) {
319 if (controller instanceof ServerRpcController) {
320 ((ServerRpcController)controller).setFailedOn(ioe);
321 } else {
322 controller.setFailed(StringUtils.stringifyException(ioe));
323 }
324 }
325 }
326
327
328
329
330
331
332
333 public static Result[] getResults(CellScanner cellScanner, ScanResponse response)
334 throws IOException {
335 if (response == null) return null;
336
337
338 int noOfResults = cellScanner != null?
339 response.getCellsPerResultCount(): response.getResultsCount();
340 Result[] results = new Result[noOfResults];
341 for (int i = 0; i < noOfResults; i++) {
342 if (cellScanner != null) {
343
344
345 int noOfCells = response.getCellsPerResult(i);
346 boolean isPartial =
347 response.getPartialFlagPerResultCount() > i ?
348 response.getPartialFlagPerResult(i) : false;
349 List<Cell> cells = new ArrayList<Cell>(noOfCells);
350 for (int j = 0; j < noOfCells; j++) {
351 try {
352 if (cellScanner.advance() == false) {
353
354
355
356 String msg = "Results sent from server=" + noOfResults + ". But only got " + i
357 + " results completely at client. Resetting the scanner to scan again.";
358 LOG.error(msg);
359 throw new DoNotRetryIOException(msg);
360 }
361 } catch (IOException ioe) {
362
363
364
365 LOG.error("Exception while reading cells from result."
366 + "Resetting the scanner to scan again.", ioe);
367 throw new DoNotRetryIOException("Resetting the scanner.", ioe);
368 }
369 cells.add(cellScanner.current());
370 }
371 results[i] = Result.create(cells, null, response.getStale(), isPartial);
372 } else {
373
374 results[i] = ProtobufUtil.toResult(response.getResults(i));
375 }
376 }
377 return results;
378 }
379 }