View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Helper utility to build protocol buffer responses,
63   * or retrieve data from protocol buffer responses.
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  // Start utilities for Client
73  
74    /**
75     * Get the results from a protocol buffer MultiResponse
76     *
77     * @param request the protocol buffer MultiResponse to convert
78     * @param cells Cells to go with the passed in <code>proto</code>.  Can be null.
79     * @return the results that were in the MultiResponse (a Result or an Exception).
80     * @throws IOException
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, roe.getIndex(), ProtobufUtil.toException(roe.getException()));
121         } else if (roe.hasResult()) {
122           results.add(regionName, roe.getIndex(), ProtobufUtil.toResult(roe.getResult(), cells));
123         } else if (roe.hasServiceResult()) {
124           results.add(regionName, roe.getIndex(), roe.getServiceResult());
125         } else {
126           // no result & no exception. Unexpected.
127           throw new IllegalStateException("No result & no exception roe=" + roe +
128               " for region " + actions.getRegion());
129         }
130       }
131     }
132 
133     return results;
134   }
135 
136   /**
137    * Wrap a throwable to an action result.
138    *
139    * @param t
140    * @return an action result builder
141    */
142   public static ResultOrException.Builder buildActionResult(final Throwable t) {
143     ResultOrException.Builder builder = ResultOrException.newBuilder();
144     if (t != null) builder.setException(buildException(t));
145     return builder;
146   }
147 
148   /**
149    * Wrap a throwable to an action result.
150    *
151    * @param r
152    * @return an action result builder
153    */
154   public static ResultOrException.Builder buildActionResult(final ClientProtos.Result r) {
155     ResultOrException.Builder builder = ResultOrException.newBuilder();
156     if (r != null) builder.setResult(r);
157     return builder;
158   }
159 
160   /**
161    * @param t
162    * @return NameValuePair of the exception name to stringified version os exception.
163    */
164   public static NameBytesPair buildException(final Throwable t) {
165     NameBytesPair.Builder parameterBuilder = NameBytesPair.newBuilder();
166     parameterBuilder.setName(t.getClass().getName());
167     parameterBuilder.setValue(
168       ByteString.copyFromUtf8(StringUtils.stringifyException(t)));
169     return parameterBuilder.build();
170   }
171 
172   /**
173    * Converts the permissions list into a protocol buffer GetUserPermissionsResponse
174    */
175   public static GetUserPermissionsResponse buildGetUserPermissionsResponse(
176       final List<UserPermission> permissions) {
177     GetUserPermissionsResponse.Builder builder = GetUserPermissionsResponse.newBuilder();
178     for (UserPermission perm : permissions) {
179       builder.addUserPermission(ProtobufUtil.toUserPermission(perm));
180     }
181     return builder.build();
182   }
183 
184 // End utilities for Client
185 // Start utilities for Admin
186 
187   /**
188    * Get the list of regions to flush from a RollLogWriterResponse
189    *
190    * @param proto the RollLogWriterResponse
191    * @return the the list of regions to flush
192    */
193   public static byte[][] getRegions(final RollWALWriterResponse proto) {
194     if (proto == null || proto.getRegionToFlushCount() == 0) return null;
195     List<byte[]> regions = new ArrayList<byte[]>();
196     for (ByteString region: proto.getRegionToFlushList()) {
197       regions.add(region.toByteArray());
198     }
199     return (byte[][])regions.toArray();
200   }
201 
202   /**
203    * Get the list of region info from a GetOnlineRegionResponse
204    *
205    * @param proto the GetOnlineRegionResponse
206    * @return the list of region info
207    */
208   public static List<HRegionInfo> getRegionInfos(final GetOnlineRegionResponse proto) {
209     if (proto == null || proto.getRegionInfoCount() == 0) return null;
210     return ProtobufUtil.getRegionInfos(proto);
211   }
212 
213   /**
214    * Get the region opening state from a OpenRegionResponse
215    *
216    * @param proto the OpenRegionResponse
217    * @return the region opening state
218    */
219   public static RegionOpeningState getRegionOpeningState
220       (final OpenRegionResponse proto) {
221     if (proto == null || proto.getOpeningStateCount() != 1) return null;
222     return RegionOpeningState.valueOf(
223       proto.getOpeningState(0).name());
224   }
225 
226   /**
227    * Get a list of region opening state from a OpenRegionResponse
228    * 
229    * @param proto the OpenRegionResponse
230    * @return the list of region opening state
231    */
232   public static List<RegionOpeningState> getRegionOpeningStateList(
233       final OpenRegionResponse proto) {
234     if (proto == null) return null;
235     List<RegionOpeningState> regionOpeningStates = new ArrayList<RegionOpeningState>();
236     for (int i = 0; i < proto.getOpeningStateCount(); i++) {
237       regionOpeningStates.add(RegionOpeningState.valueOf(
238           proto.getOpeningState(i).name()));
239     }
240     return regionOpeningStates;
241   }
242 
243   /**
244    * Check if the region is closed from a CloseRegionResponse
245    *
246    * @param proto the CloseRegionResponse
247    * @return the region close state
248    */
249   public static boolean isClosed
250       (final CloseRegionResponse proto) {
251     if (proto == null || !proto.hasClosed()) return false;
252     return proto.getClosed();
253   }
254 
255   /**
256    * A utility to build a GetServerInfoResponse.
257    *
258    * @param serverName
259    * @param webuiPort
260    * @return the response
261    */
262   public static GetServerInfoResponse buildGetServerInfoResponse(
263       final ServerName serverName, final int webuiPort) {
264     GetServerInfoResponse.Builder builder = GetServerInfoResponse.newBuilder();
265     ServerInfo.Builder serverInfoBuilder = ServerInfo.newBuilder();
266     serverInfoBuilder.setServerName(ProtobufUtil.toServerName(serverName));
267     if (webuiPort >= 0) {
268       serverInfoBuilder.setWebuiPort(webuiPort);
269     }
270     builder.setServerInfo(serverInfoBuilder.build());
271     return builder.build();
272   }
273 
274   /**
275    * A utility to build a GetOnlineRegionResponse.
276    *
277    * @param regions
278    * @return the response
279    */
280   public static GetOnlineRegionResponse buildGetOnlineRegionResponse(
281       final List<HRegionInfo> regions) {
282     GetOnlineRegionResponse.Builder builder = GetOnlineRegionResponse.newBuilder();
283     for (HRegionInfo region: regions) {
284       builder.addRegionInfo(HRegionInfo.convert(region));
285     }
286     return builder.build();
287   }
288 
289   /**
290    * Creates a response for the catalog scan request
291    * @return A RunCatalogScanResponse
292    */
293   public static RunCatalogScanResponse buildRunCatalogScanResponse(int numCleaned) {
294     return RunCatalogScanResponse.newBuilder().setScanResult(numCleaned).build();
295   }
296 
297   /**
298    * Creates a response for the catalog scan request
299    * @return A EnableCatalogJanitorResponse
300    */
301   public static EnableCatalogJanitorResponse buildEnableCatalogJanitorResponse(boolean prevValue) {
302     return EnableCatalogJanitorResponse.newBuilder().setPrevValue(prevValue).build();
303   }
304 
305 // End utilities for Admin
306 
307   /**
308    * Creates a response for the last flushed sequence Id request
309    * @return A GetLastFlushedSequenceIdResponse
310    */
311   public static GetLastFlushedSequenceIdResponse buildGetLastFlushedSequenceIdResponse(
312       long seqId) {
313     return GetLastFlushedSequenceIdResponse.newBuilder().setLastFlushedSequenceId(seqId).build();
314   }
315 
316   /**
317    * Stores an exception encountered during RPC invocation so it can be passed back
318    * through to the client.
319    * @param controller the controller instance provided by the client when calling the service
320    * @param ioe the exception encountered
321    */
322   public static void setControllerException(RpcController controller, IOException ioe) {
323     if (controller != null) {
324       if (controller instanceof ServerRpcController) {
325         ((ServerRpcController)controller).setFailedOn(ioe);
326       } else {
327         controller.setFailed(StringUtils.stringifyException(ioe));
328       }
329     }
330   }
331 
332   /**
333    * Create Results from the cells using the cells meta data. 
334    * @param cellScanner
335    * @param response
336    * @return results
337    */
338   public static Result[] getResults(CellScanner cellScanner, ScanResponse response)
339       throws IOException {
340     if (response == null) return null;
341     // If cellscanner, then the number of Results to return is the count of elements in the
342     // cellsPerResult list.  Otherwise, it is how many results are embedded inside the response.
343     int noOfResults = cellScanner != null?
344       response.getCellsPerResultCount(): response.getResultsCount();
345     Result[] results = new Result[noOfResults];
346     for (int i = 0; i < noOfResults; i++) {
347       if (cellScanner != null) {
348         // Cells are out in cellblocks.  Group them up again as Results.  How many to read at a
349         // time will be found in getCellsLength -- length here is how many Cells in the i'th Result
350         int noOfCells = response.getCellsPerResult(i);
351         List<Cell> cells = new ArrayList<Cell>(noOfCells);
352         for (int j = 0; j < noOfCells; j++) {
353           try {
354             if (cellScanner.advance() == false) {
355               // We are not able to retrieve the exact number of cells which ResultCellMeta says us.
356               // We have to scan for the same results again. Throwing DNRIOE as a client retry on the
357               // same scanner will result in OutOfOrderScannerNextException
358               String msg = "Results sent from server=" + noOfResults + ". But only got " + i
359                 + " results completely at client. Resetting the scanner to scan again.";
360               LOG.error(msg);
361               throw new DoNotRetryIOException(msg);
362             }
363           } catch (IOException ioe) {
364             // We are getting IOE while retrieving the cells for Results.
365             // We have to scan for the same results again. Throwing DNRIOE as a client retry on the
366             // same scanner will result in OutOfOrderScannerNextException
367             LOG.error("Exception while reading cells from result."
368               + "Resetting the scanner to scan again.", ioe);
369             throw new DoNotRetryIOException("Resetting the scanner.", ioe);
370           }
371           cells.add(cellScanner.current());
372         }
373         results[i] = Result.create(cells);
374       } else {
375         // Result is pure pb.
376         results[i] = ProtobufUtil.toResult(response.getResults(i));
377       }
378     }
379     return results;
380   }
381 }