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.coprocessor;
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.hbase.Cell;
27  import org.apache.hadoop.hbase.CellUtil;
28  import org.apache.hadoop.hbase.Coprocessor;
29  import org.apache.hadoop.hbase.CoprocessorEnvironment;
30  import org.apache.hadoop.hbase.DoNotRetryIOException;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.client.Scan;
33  import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationWithErrorsProtos;
34  import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationWithErrorsProtos.ColumnAggregationWithErrorsSumRequest;
35  import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationWithErrorsProtos.ColumnAggregationWithErrorsSumResponse;
36  import org.apache.hadoop.hbase.protobuf.ResponseConverter;
37  import org.apache.hadoop.hbase.regionserver.HRegion;
38  import org.apache.hadoop.hbase.regionserver.InternalScanner;
39  import org.apache.hadoop.hbase.util.Bytes;
40  
41  import com.google.protobuf.RpcCallback;
42  import com.google.protobuf.RpcController;
43  import com.google.protobuf.Service;
44  
45  /**
46   * Test coprocessor endpoint that always throws a {@link DoNotRetryIOException} for requests on
47   * the last region in the table.  This allows tests to ensure correct error handling of
48   * coprocessor endpoints throwing exceptions.
49   */
50  public class ColumnAggregationEndpointWithErrors
51      extends
52      ColumnAggregationWithErrorsProtos.ColumnAggregationServiceWithErrors
53  implements Coprocessor, CoprocessorService  {
54    static final Log LOG = LogFactory.getLog(ColumnAggregationEndpointWithErrors.class);
55    private RegionCoprocessorEnvironment env = null;
56    @Override
57    public Service getService() {
58      return this;
59    }
60  
61    @Override
62    public void start(CoprocessorEnvironment env) throws IOException {
63      if (env instanceof RegionCoprocessorEnvironment) {
64        this.env = (RegionCoprocessorEnvironment)env;
65        return;
66      }
67      throw new CoprocessorException("Must be loaded on a table region!");
68    }
69  
70    @Override
71    public void stop(CoprocessorEnvironment env) throws IOException {
72      // Nothing to do.
73    }
74  
75    @Override
76    public void sum(RpcController controller, ColumnAggregationWithErrorsSumRequest request, 
77        RpcCallback<ColumnAggregationWithErrorsSumResponse> done) {
78      // aggregate at each region
79      Scan scan = new Scan();
80      // Family is required in pb. Qualifier is not.
81      byte[] family = request.getFamily().toByteArray();
82      byte[] qualifier = request.hasQualifier() ? request.getQualifier().toByteArray() : null;
83      if (request.hasQualifier()) {
84        scan.addColumn(family, qualifier);
85      } else {
86        scan.addFamily(family);
87      }
88      int sumResult = 0;
89      InternalScanner scanner = null;
90      try {
91        HRegion region = this.env.getRegion();
92        // throw an exception for requests to the last region in the table, to test error handling
93        if (Bytes.equals(region.getEndKey(), HConstants.EMPTY_END_ROW)) {
94          throw new DoNotRetryIOException("An expected exception");
95        }
96        scanner = region.getScanner(scan);
97        List<Cell> curVals = new ArrayList<Cell>();
98        boolean hasMore = false;
99        do {
100         curVals.clear();
101         hasMore = scanner.next(curVals);
102         for (Cell kv : curVals) {
103           if (CellUtil.matchingQualifier(kv, qualifier)) {
104             sumResult += Bytes.toInt(kv.getValueArray(), kv.getValueOffset());
105           }
106         }
107       } while (hasMore);
108     } catch (IOException e) {
109       ResponseConverter.setControllerException(controller, e);
110       // Set result to -1 to indicate error.
111       sumResult = -1;
112       LOG.info("Setting sum result to -1 to indicate error", e);
113     } finally {
114       if (scanner != null) {
115         try {
116           scanner.close();
117         } catch (IOException e) {
118           ResponseConverter.setControllerException(controller, e);
119           sumResult = -1;
120           LOG.info("Setting sum result to -1 to indicate error", e);
121         }
122       }
123     }
124     done.run(ColumnAggregationWithErrorsSumResponse.newBuilder().setSum(sumResult).build());
125   }
126 }