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  
19  package org.apache.hadoop.hbase.thrift;
20  
21  import org.apache.hadoop.hbase.CallQueueTooBigException;
22  import org.apache.hadoop.hbase.Cell;
23  import org.apache.hadoop.hbase.DoNotRetryIOException;
24  import org.apache.hadoop.hbase.MultiActionResultTooLarge;
25  import org.apache.hadoop.hbase.NotServingRegionException;
26  import org.apache.hadoop.hbase.RegionTooBusyException;
27  import org.apache.hadoop.hbase.UnknownScannerException;
28  import org.apache.hadoop.hbase.client.Get;
29  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
30  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
31  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
32  import org.apache.hadoop.hbase.exceptions.FailedSanityCheckException;
33  import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException;
34  import org.apache.hadoop.hbase.exceptions.RegionMovedException;
35  import org.apache.hadoop.hbase.exceptions.ScannerResetException;
36  import org.apache.hadoop.hbase.metrics.ExceptionTrackingSource;
37  import org.apache.hadoop.hbase.util.Bytes;
38  
39  import java.io.IOException;
40  import java.util.List;
41  
42  /**
43   * Simple test coprocessor for injecting exceptions on Get requests.
44   */
45  public class ErrorThrowingGetObserver extends BaseRegionObserver {
46    public static final String SHOULD_ERROR_ATTRIBUTE = "error";
47  
48    @Override
49    public void preGetOp(ObserverContext<RegionCoprocessorEnvironment> e,
50                         Get get, List<Cell> results) throws IOException {
51      byte[] errorType = get.getAttribute(SHOULD_ERROR_ATTRIBUTE);
52      if (errorType != null) {
53        ErrorType type = ErrorType.valueOf(Bytes.toString(errorType));
54        switch (type) {
55          case CALL_QUEUE_TOO_BIG:
56            throw new CallQueueTooBigException("Failing for test");
57          case MULTI_ACTION_RESULT_TOO_LARGE:
58            throw new MultiActionResultTooLarge("Failing for test");
59          case FAILED_SANITY_CHECK:
60            throw new FailedSanityCheckException("Failing for test");
61          case NOT_SERVING_REGION:
62            throw new NotServingRegionException("Failing for test");
63          case REGION_MOVED:
64            throw new RegionMovedException(
65                e.getEnvironment().getRegionServerServices().getServerName(), 1);
66          case SCANNER_RESET:
67            throw new ScannerResetException("Failing for test");
68          case UNKNOWN_SCANNER:
69            throw new UnknownScannerException("Failing for test");
70          case REGION_TOO_BUSY:
71            throw new RegionTooBusyException("Failing for test");
72          case OUT_OF_ORDER_SCANNER_NEXT:
73            throw new OutOfOrderScannerNextException("Failing for test");
74          default:
75            throw new DoNotRetryIOException("Failing for test");
76        }
77      }
78      super.preGetOp(e, get, results);
79    }
80  
81    public enum ErrorType {
82      CALL_QUEUE_TOO_BIG(ExceptionTrackingSource.EXCEPTIONS_CALL_QUEUE_TOO_BIG),
83      MULTI_ACTION_RESULT_TOO_LARGE(ExceptionTrackingSource.EXCEPTIONS_MULTI_TOO_LARGE_NAME),
84      FAILED_SANITY_CHECK(ExceptionTrackingSource.EXCEPTIONS_SANITY_NAME),
85      NOT_SERVING_REGION(ExceptionTrackingSource.EXCEPTIONS_NSRE_NAME),
86      REGION_MOVED(ExceptionTrackingSource.EXCEPTIONS_MOVED_NAME),
87      SCANNER_RESET(ExceptionTrackingSource.EXCEPTIONS_SCANNER_RESET_NAME),
88      UNKNOWN_SCANNER(ExceptionTrackingSource.EXCEPTIONS_UNKNOWN_NAME),
89      REGION_TOO_BUSY(ExceptionTrackingSource.EXCEPTIONS_BUSY_NAME),
90      OUT_OF_ORDER_SCANNER_NEXT(ExceptionTrackingSource.EXCEPTIONS_OOO_NAME);
91  
92      private final String metricName;
93  
94      ErrorType(String metricName) {
95        this.metricName = metricName;
96      }
97  
98      public String getMetricName() {
99        return metricName;
100     }
101   }
102 }