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.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import edu.umd.cs.findbugs.annotations.Nullable;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.CellScanner;
32  import org.apache.hadoop.hbase.DoNotRetryIOException;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.ServerName;
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.RegionAction;
46  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionActionResult;
47  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ResultOrException;
48  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
49  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiResponse;
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   * Helper utility to build protocol buffer responses,
67   * or retrieve data from protocol buffer responses.
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  // Start utilities for Client
77  
78    /**
79     * Get the results from a protocol buffer MultiResponse
80     *
81     * @param request the protocol buffer MultiResponse to convert
82     * @param cells Cells to go with the passed in <code>proto</code>.  Can be null.
83     * @return the results that were in the MultiResponse (a Result or an Exception).
84     * @throws IOException
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           // add the load stats, if we got any
129           if (roe.hasLoadStats()) {
130             ((Result) responseValue).addResults(roe.getLoadStats());
131           }
132         } else if (roe.hasServiceResult()) {
133           responseValue = roe.getServiceResult();
134         } else {
135           // no result & no exception. Unexpected.
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    * Wrap a throwable to an action result.
148    *
149    * @param t
150    * @return an action result builder
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    * Wrap a throwable to an action result.
160    *
161    * @param r
162    * @return an action result builder
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    * @param t
174    * @return NameValuePair of the exception name to stringified version os exception.
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    * Converts the permissions list into a protocol buffer GetUserPermissionsResponse
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 // End utilities for Client
197 // Start utilities for Admin
198 
199   /**
200    * Get the list of region info from a GetOnlineRegionResponse
201    *
202    * @param proto the GetOnlineRegionResponse
203    * @return the list of region info
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    * Get the region opening state from a OpenRegionResponse
212    *
213    * @param proto the OpenRegionResponse
214    * @return the region opening state
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    * Get a list of region opening state from a OpenRegionResponse
225    * 
226    * @param proto the OpenRegionResponse
227    * @return the list of region opening state
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    * Check if the region is closed from a CloseRegionResponse
242    *
243    * @param proto the CloseRegionResponse
244    * @return the region close state
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    * A utility to build a GetServerInfoResponse.
254    *
255    * @param serverName
256    * @param webuiPort
257    * @return the response
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    * A utility to build a GetOnlineRegionResponse.
273    *
274    * @param regions
275    * @return the response
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    * Creates a response for the catalog scan request
288    * @return A RunCatalogScanResponse
289    */
290   public static RunCatalogScanResponse buildRunCatalogScanResponse(int numCleaned) {
291     return RunCatalogScanResponse.newBuilder().setScanResult(numCleaned).build();
292   }
293 
294   /**
295    * Creates a response for the catalog scan request
296    * @return A EnableCatalogJanitorResponse
297    */
298   public static EnableCatalogJanitorResponse buildEnableCatalogJanitorResponse(boolean prevValue) {
299     return EnableCatalogJanitorResponse.newBuilder().setPrevValue(prevValue).build();
300   }
301 
302 // End utilities for Admin
303 
304   /**
305    * Creates a response for the last flushed sequence Id request
306    * @return A GetLastFlushedSequenceIdResponse
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    * Stores an exception encountered during RPC invocation so it can be passed back
317    * through to the client.
318    * @param controller the controller instance provided by the client when calling the service
319    * @param ioe the exception encountered
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    * Retreivies exception stored during RPC invocation.
333    * @param controller the controller instance provided by the client when calling the service
334    * @return exception if any, or null; Will return DoNotRetryIOException for string represented
335    * failure causes in controller.
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    * Create Results from the cells using the cells meta data. 
352    * @param cellScanner
353    * @param response
354    * @return results
355    */
356   public static Result[] getResults(CellScanner cellScanner, ScanResponse response)
357       throws IOException {
358     if (response == null) return null;
359     // If cellscanner, then the number of Results to return is the count of elements in the
360     // cellsPerResult list.  Otherwise, it is how many results are embedded inside the response.
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         // Cells are out in cellblocks.  Group them up again as Results.  How many to read at a
367         // time will be found in getCellsLength -- length here is how many Cells in the i'th Result
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               // We are not able to retrieve the exact number of cells which ResultCellMeta says us.
377               // We have to scan for the same results again. Throwing DNRIOE as a client retry on the
378               // same scanner will result in OutOfOrderScannerNextException
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             // We are getting IOE while retrieving the cells for Results.
386             // We have to scan for the same results again. Throwing DNRIOE as a client retry on the
387             // same scanner will result in OutOfOrderScannerNextException
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         // Result is pure pb.
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 }