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, 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           // no result & no exception. Unexpected.
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    * Wrap a throwable to an action result.
140    *
141    * @param t
142    * @return an action result builder
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    * Wrap a throwable to an action result.
152    *
153    * @param r
154    * @return an action result builder
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    * @param t
164    * @return NameValuePair of the exception name to stringified version os exception.
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    * Converts the permissions list into a protocol buffer GetUserPermissionsResponse
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 // End utilities for Client
187 // Start utilities for Admin
188 
189   /**
190    * Get the list of regions to flush from a RollLogWriterResponse
191    *
192    * @param proto the RollLogWriterResponse
193    * @return the the list of regions to flush
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    * Get the list of region info from a GetOnlineRegionResponse
206    *
207    * @param proto the GetOnlineRegionResponse
208    * @return the list of region info
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    * Get the region opening state from a OpenRegionResponse
217    *
218    * @param proto the OpenRegionResponse
219    * @return the region opening state
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    * Get a list of region opening state from a OpenRegionResponse
230    * 
231    * @param proto the OpenRegionResponse
232    * @return the list of region opening state
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    * Check if the region is closed from a CloseRegionResponse
247    *
248    * @param proto the CloseRegionResponse
249    * @return the region close state
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    * A utility to build a GetServerInfoResponse.
259    *
260    * @param serverName
261    * @param webuiPort
262    * @return the response
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    * A utility to build a GetOnlineRegionResponse.
278    *
279    * @param regions
280    * @return the response
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    * Creates a response for the catalog scan request
293    * @return A RunCatalogScanResponse
294    */
295   public static RunCatalogScanResponse buildRunCatalogScanResponse(int numCleaned) {
296     return RunCatalogScanResponse.newBuilder().setScanResult(numCleaned).build();
297   }
298 
299   /**
300    * Creates a response for the catalog scan request
301    * @return A EnableCatalogJanitorResponse
302    */
303   public static EnableCatalogJanitorResponse buildEnableCatalogJanitorResponse(boolean prevValue) {
304     return EnableCatalogJanitorResponse.newBuilder().setPrevValue(prevValue).build();
305   }
306 
307 // End utilities for Admin
308 
309   /**
310    * Creates a response for the last flushed sequence Id request
311    * @return A GetLastFlushedSequenceIdResponse
312    */
313   public static GetLastFlushedSequenceIdResponse buildGetLastFlushedSequenceIdResponse(
314       long seqId) {
315     return GetLastFlushedSequenceIdResponse.newBuilder().setLastFlushedSequenceId(seqId).build();
316   }
317 
318   /**
319    * Stores an exception encountered during RPC invocation so it can be passed back
320    * through to the client.
321    * @param controller the controller instance provided by the client when calling the service
322    * @param ioe the exception encountered
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    * Create Results from the cells using the cells meta data. 
336    * @param cellScanner
337    * @param response
338    * @return results
339    */
340   public static Result[] getResults(CellScanner cellScanner, ScanResponse response)
341       throws IOException {
342     if (response == null) return null;
343     // If cellscanner, then the number of Results to return is the count of elements in the
344     // cellsPerResult list.  Otherwise, it is how many results are embedded inside the response.
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         // Cells are out in cellblocks.  Group them up again as Results.  How many to read at a
351         // time will be found in getCellsLength -- length here is how many Cells in the i'th Result
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               // We are not able to retrieve the exact number of cells which ResultCellMeta says us.
358               // We have to scan for the same results again. Throwing DNRIOE as a client retry on the
359               // same scanner will result in OutOfOrderScannerNextException
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             // We are getting IOE while retrieving the cells for Results.
367             // We have to scan for the same results again. Throwing DNRIOE as a client retry on the
368             // same scanner will result in OutOfOrderScannerNextException
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         // Result is pure pb.
378         results[i] = ProtobufUtil.toResult(response.getResults(i));
379       }
380     }
381     return results;
382   }
383 }